-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclient.js
More file actions
119 lines (100 loc) · 4.33 KB
/
client.js
File metadata and controls
119 lines (100 loc) · 4.33 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const Discord = require('discord.js');
const util = require('apex-util');
const { isAdmin } = require('./botUtils.js');
// If production server, set default debug mode to production setting
if (process.env.NODE_ENV === 'production' && !process.env.DEBUG_MODE) process.env.DEBUG_MODE = 0;
const client = new Discord.Client();
let toggle = false;
let interval = 0;
// Pre-load controllers
const controllers = require('./controllers')();
// Alert when ready
client.on('ready', () => {
util.log('Bot Online and Ready and willing to go!', 0);
});
// Listen for messages
client.on('message', (message) => {
// Create a ToS message for new members
const newMessage = message;
newMessage.content = message.type === 'GUILD_MEMBER_JOIN' ? '!terms' : newMessage.content;
// Check for ! prefix on message to ensure it is a command
if (newMessage.content.charAt(0) === '!') {
util.log('Command message received', newMessage.content, 0);
// Build basic help string
let helpString = 'v1.4.0 Discovered Commands:\n\n\t**<> - Required Item\t\t[] - Optional Item**';
// Process message against every controller
Object.keys(controllers).forEach((key) => {
// Instantiate the controller
const controllerInstance = new controllers[key](newMessage);
util.log('Controller instance', controllerInstance, 5);
// Runs commands after constructor is called
controllerInstance.run();
// Loop through commands if help command and add to string
if (newMessage.content.toLowerCase() === '!help') {
Object.keys(controllerInstance.commands).forEach((commandKey) => {
const commandInstance = controllerInstance.commands[commandKey];
// Check if command should be shown in help menu
if (commandInstance.showWithHelp) {
if (commandInstance.adminOnly && isAdmin(newMessage.member)) {
helpString += `\n\n \`${commandInstance.example}\` **- Admin Only** \n\t ${commandInstance.description}`;
} else if (commandInstance.adminOnly && !isAdmin(newMessage.member)) {
helpString += '';
} else {
helpString += `\n\n \`${commandInstance.example}\` \n\t ${commandInstance.description}`;
}
}
});
}
});
// If help command called, display string
if (newMessage.content.toLowerCase() === '!help') {
newMessage.reply(helpString);
}
}
// Listens for the $loop command and repeats a message every X seconds
if (newMessage.content.startsWith("!loop")) {
// Grab the message string and dissect it for the time, in seconds, and string to be repeated
let result = newMessage.content.substr(newMessage.content.indexOf(" ") + 1);
let result2 = result.substr(result.indexOf(" ") + 1);
result = result.split(" ");
result = Number(result[0]);
util.log(result);
util.log(result2);
// Check if the pre-existing toggle is true or false
// If the toggle is true, then set the toggle to false and turn off the loop
if (toggle !== false) {
clearInterval(interval);
newMessage.channel.send("Loop is off");
toggle = false;
} else if (toggle !== true) {
// If the toggle is false, set it to true and check that the criteria for the command
// is correct.
if ((typeof result == 'number') && (result >= 5)) {
if (result2.length > 0) {
interval = setInterval(function () {
newMessage.channel.send(result2)
}, (result * 1000));
newMessage.channel.send("Loop is now on");
toggle = true;
} else {
newMessage.channel.send("please enter a message");
}
} else {
newMessage.channel.send("please input a time in seconds and make it greater than or equal to 5");
}
}
}
});
// Listen out for new users to the discord
client.on('guildMemberAdd', (member) => {
// Add new welcome message to general
const mainChannel = member.guild.channels.find('name', 'general');
mainChannel.send(`Hi, ${member}. It's nice to meet you!`);
});
// Listen out for members that leave the discord
client.on('guildMemberRemove', (member) => {
// Add a message for someone leaving the discord
const mainChannel = member.guild.channels.find('name', 'general');
mainChannel.send(`${member} has left... it was nice knowing you! ;-;`);
});
client.login(process.env.TOKEN);