-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
80 lines (72 loc) · 2.36 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const { Client, GatewayIntentBits } = require('discord.js');
const winston = require('winston');
const config = require('./config.json');
const axios = require('axios')
// Configure logger settings
const logger = winston.createLogger({
level: 'debug',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new winston.transports.File({ filename: 'bot.log', level: 'info', maxsize: '10000', maxFiles: '5',
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp(),
winston.format.simple()
),
}),
new winston.transports.Console( { colorize: true, timestamp: true })
]
});
// Initialize Discord Bot
var bot = new Client( { intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent ] } );
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ' + bot.user.tag);
});
bot.on('messageCreate', message => {
try {
// don't respond to ourselves, to web hooks, or to bots
if (message.author == bot.user) { return; }
if (bot.discriminator == '0000') { return; }
if (message.author.bot === true) { return; }
var nickname = null;
if (message.member) {
nickname = message.member.nickname;
}
logger.info(`Handling message on ${message.channel.name} from ${message.author.username} (${nickname})`);
post = message.cleanContent;
if ((message.attachments || []).size > 0) {
post = message.attachments.reduce((accumulate, attachment) =>
`${accumulate}%r${attachment.url}`,`${post}%r- Attachments -`)
}
else {
post = message.cleanContent;
}
axios.post(config.url + '/webhook', {
cmd: 'discord',
token: config.api_token,
message: post,
channel: message.channel.name,
user: message.author.username,
userid: message.author.discriminator,
nickname: nickname
})
.catch(function (error) {
logger.info(error);
});
} catch (error) {
logger.error('Unexpected error: ' + error);
}
});
bot.on('error', function (err) {
logger.error('Unexpected error: ' + err);
});
bot.on('uncaughtException', function (err) {
logger.error('Caught exception: ' + err);
throw err;
});
bot.login(config.bot_token);