-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinskubot.js
155 lines (124 loc) · 5.52 KB
/
finskubot.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* FinskuBot - Another stupid Discord/Twitch bot.
* Author: Markus (github/markspl)
*/
const fs = require("fs");
const Discord = require("discord.js");
const Client = new Discord.Client();
const { version } = require("./package.json");
// Use environment variables if there is. Otherwise use ./config.json
const { discord_options } = require("./config.json");
const BOT_TOKEN = process.env.BOT_TOKEN || discord_options.bot_token;
const BOT_PREFIX = process.env.BOT_PREFIX || discord_options.bot_prefix;
const BOT_HOMECHANNEL = process.env.BOT_HOMECHANNEL || discord_options.bot_homechannel;
process.title = "FinskuBot";
// Strings
const MESSAGE_DM = "I don't serve you personally!\nTry again but on this time write the command \ninto the guild channel where I'm serving my master(s).";
const MESSAGE_MENTION = "Hi there, I'm FinskuBot!\nUse **!finskubot** to see the command list! *bzzzzzz*";
const MESSAGE_NOROLE = "You don't have permission to use that command!\nReason: `role missing`";
// Bot launch
Client.once("ready", async () => {
console.time("# Loading");
Client.load();
// Activity text on the user list (left panel)
Client.user.setActivity(`${BOT_PREFIX}\finskubot | ${version}`);
console.log(`# FinskuBot version: \x1b[36m${version} \x1b[0m`);
console.log(`# Logged in as \x1b[36m${Client.user.tag}\x1b[0m`);
console.log(`# Serving in \x1b[36m${Client.guilds.array().length} \x1b[0mserver(s)`);
console.timeEnd("# Loading");
console.log("\n# I'm ready!\n");
// Report on specific Discord text channel bot is running
Client.channels.get(BOT_HOMECHANNEL).send(`**I'm up!** *Use me!* (͡° ͜ʖ ͡°)\n[${new Date().toUTCString()}]`);
});
// Load commands from ./commands folder
Client.load = (command) => {
const commandList = fs.readdirSync(__dirname + "/commands/");
// Check if commands are already loaded.
if (command) {
// Reload commands
if (commandList.indexOf(command + ".js") >= 0) {
delete require.cache[require.resolve(__dirname + "/commands/" + command)];
Client.commands[command] = require(__dirname + "/commands/" + command);
}
} else {
// Load commands
Client.commands = [];
for (let i = 0; i < commandList.length; i++) {
const item = commandList[i];
if (item.match(/\.js$/)) {
delete require.cache[require.resolve(__dirname + "/commands/" + item)];
Client.commands[item.slice(0, -3)] = require(__dirname + "/commands/" + item);
}
}
console.log(`\n# Commands loaded: \x1b[36m"${commandList.length}\x1b[0m`);
}
};
// Join a new guild
Client.on("guildCreate", async (guild) => {
console.info(`# Joined Guild "${Client.guilds.get(guild).name}"`).catch(console.error.stack);;
});
// Disconnect from a guild
Client.on("disconnect", event => {
console.log("# Disconnected : \n" + event.reason);
});
// Error occurred
Client.on("error", (err) => {
console.log("\n##########\n## [ERROR]\n##########\n\n" + err.stack + "\n\n##########");
});
/**
* Message handler
*/
Client.on("message", message => {
if (message.channel.type === "dm" && message.author.id !== Client.user.id) {
// Return message if private message is sent
message.channel.send(MESSAGE_DM);
} else if (message.channel.type === "text") {
// Text channels
if (message.isMentioned(Client.user)) {
// Bot is pinged with '@'
message.reply(MESSAGE_MENTION);
} else {
// Checking if the message starts with the prefix set in config file
if (message.content.startsWith(BOT_PREFIX)) {
let args = message.content.split(" ");
const command = args[0].slice(BOT_PREFIX.length);
const guildMember = message.author.id;
args.splice(0, 1);
// Used a command which is found from folder './commands'
if (command in Client.commands) {
// Check if the command is made for specific server(s)
if (Client.commands[command].server == message.guild.id || Client.commands[command].server.length === 0) {
// ... for specific user(s)
if (Client.commands[command].user.includes(message.author.id) || Client.commands[command].user.length === 0) {
// ... for specific role(s)
// Get all roles which user has
const allUserRoles = [ ...message.guild.members.get(message.author.id).roles.keys() ];
// Check if at least one role match
if(Client.commands[command].role.some(r => allUserRoles.includes(r)) || Client.commands[command].role.length === 0) {
// Execute command
try {
// Message logging
console.log(`[${new Date().toLocaleString()}][${message.guild.name} (${message.guild.id})] [${message.author.username} (${message.author.id})] - ${message.content}`);
Client.commands[command].execute(Client, message, args, guildMember);
} catch (e) {
console.log("\n##########\n## [ERROR]\n##########\n\n" + e.stack);
}
} else {
// Inform command user they don't have correct role(s) to use the command
console.log(MESSAGE_NOROLE);
message.delete();
message.channel.send(MESSAGE_NOROLE).then(m => m.delete(30000));
console.log(`[*${new Date().toLocaleString()}*][**${message.guild.id}**] [${message.author.username} (*${message.author.id}*)] tried to use command ${command} [role missing]`);
}
}
}
}
}
}
}
});
/**
* Bot boot
*/
console.log(`# Launghing - (${new Date().toLocaleString()}).\n\n# Using prefix "\x1b[36m${BOT_PREFIX}\x1b[0m"`);
Client.login(BOT_TOKEN).catch(console.error);