-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
97 lines (79 loc) · 3.04 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Main entry file
const Discord = require('discord.js'); // Import Libraries
const auth = require('./auth.json');
import parser from './utils/ParseMessage' // Import parser
import { logger } from './src/utils' // Import logger
import { rolex } from './src/bot' // RoleX main logic
// Create Discord Client
export const client = new Discord.Client();
// Called when the bot starts up.
client.on('ready', async () => {
await client.user.setActivity('Your Mom', { type: 'WATCHING' });
logger.info(`Logging in as ${ client.user.tag }`); // output name of of the bot to the console
//client.user.setUsername("RoleX"); // Set the bot username on startup
});
// Called when new users join server (guild)
client.on('guildMemberAdd', member => {
// Send the message to a designated channel on a server:
// const channel = member.guild.channels.find('name', 'welcomes');
const channel = member.guild.channels.find( val => val.name === 'welcomes' );
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send the message, mentioning the member
channel.send(`Welcome to the server, ${ member }!\nCheck out <#430981000907194370> and don't forget to <#430970251174215690> to us. :smile:\nAlso you can type \`.help\` for a list of commands you can use.`);
});
// Called whenever a user posts a message
client.on('message', async msg => {
// Check if the message was from a bot
if (msg.author.bot) return;
/* Can filter chat here */
if (!msg.content.startsWith('.')) return;
// Setup Bot with current Message
rolex.setMessage(msg);
// Get the text of the message
let message = msg.content;
let cmd = parser.getCommandArgs(message);
// Record the command in the log, else return early since we don't need to do anything
if (cmd) {
if (cmd.args)
logger.info(`#${ msg.channel.name } | ${ msg.author.username }: ${ cmd.command } - ${ cmd.args ? cmd.args : '' }`);
else
logger.info(`#${ msg.channel.name } | ${ msg.author.username }: ${ cmd.command }`);
} else {
return;
}
// Sanitize the input
let command = cmd.command.replace( /[|&;$%@"<>(),]/g, '' );
let args = cmd.args.replace( /[|&;$%"<>(),]/g, '' );
// React to different commands
switch (command) {
case 'shout':
rolex.say(args);
break;
default:
try {
if (args)
rolex[command](args);
else
rolex[command]();
} catch (e) {
logger.warn(`Invalid Command: ${ command }`);
logger.error(e.message);
}
break;
}
});
export const start = () => {
( async () => {
try {
// Attempt to Login to Discord
await client.login(auth.token);
logger.info('Logged in.');
rolex.setClient(client);
} catch (e) {
logger.error(e);
}
})().then( () => {
console.log('RoleX started.');
});
};