forked from akargl/Foxxobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (111 loc) · 3.77 KB
/
index.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
const config = require("./config");
const { Client, Collection } = require("discord.js");
const fs = require("fs");
const prettyMilliseconds = require("pretty-ms");
const client = new Client();
client.commands = new Collection();
client.commandAliases = new Collection();
fs.readdirSync("./commands")
.filter(f => f.endsWith(".js"))
.forEach((f) => {
const command = require(`./commands/${f}`);
client.commands.set(command.name, command);
if (command.aliases) {
command.aliases.forEach((alias) => client.commandAliases.set(alias, command.name));
}
if (command.onLoad) {
command.onLoad(client);
}
});
const cooldowns = new Collection();
client.on("error", console.error);
client.once("ready", () => {
console.log("Ready to rock!");
});
client.login(config.DISCORD_TOKEN);
client.on("message", message => {
if (!message.content.startsWith(config.PREFIX) || message.author.bot) {
return;
}
const args = message.content.slice(config.PREFIX.length).split(/ +/);
let commandName = args.shift().toLowerCase();
commandName = client.commandAliases.get(commandName) || commandName;
if (!client.commands.has(commandName)) {
return;
}
const command = client.commands.get(commandName);
if (command.guildOnly && message.channel.type !== "text") {
return message.reply("Command not possible in a non-text channel");
}
if (command.args && !args.length) {
let reply = "You didn't provide required arguments.";
if (command.usage) {
reply += `Usage: '${config.PREFIX}${command.name} ${command.usage}'`;
}
return message.channel.send(reply);
}
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (!timestamps.has(message.author.id)) {
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
} else {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the '${command.name}' command.`);
}
}
try {
command.execute(message, args);
} catch (error) {
console.error(error);
return message.reply("There was an error executing that command!");
}
});
const botUptimes = new Collection();
client.on("presenceUpdate", (oldMember, newMember) => {
if (!oldMember) {
return;
}
const settings = JSON.parse(fs.readFileSync("settings.json", "utf8"));
const id = oldMember.id;
if (settings.monitoredBots.hasOwnProperty(id)) {
let botChannel = newMember.guild.defaultChannel;
if (settings.defaultChannels.hasOwnProperty(newMember.guild.id)) {
botChannel = client.channels.get(settings.defaultChannels[newMember.guild.id]);
}
if (!botChannel) {
console.error("couldn't find default channel");
return;
}
const now = Date.now();
const lastStateChange = botUptimes.get(id);
let duration;
if (lastStateChange) {
duration = prettyMilliseconds(now - lastStateChange, { compact: true });
}
if (oldMember.presence.status === "offline" && newMember.presence.status !== "offline") {
let msg = `Yay, ${newMember.displayName} is back online!`;
if (duration) {
msg += ` Downtime was ${duration}.`;
}
botChannel.send(msg);
botUptimes.set(id, now);
} else if (oldMember.presence.status !== "offline" && newMember.presence.status === "offline") {
let msg = `OnO, ${newMember.displayName} is gone`;
if (settings.monitoredBots[id].pingOwner) {
msg += ` <@${settings.monitoredBots[id].ownerId}>.`;
}
if (duration) {
msg += ` Uptime was ${duration}.`;
}
botChannel.send(msg);
botUptimes.set(id, now);
}
}
});