-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCommandHandler.js
140 lines (119 loc) · 5.32 KB
/
CommandHandler.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
const Eris = require('eris');
const config = require('../configs/config.json');
const secret = require('../configs/secret.json');
const { readFileSync, writeFileSync } = require('fs');
const topics = require('./assets/topics.json');
const server = require('./Models/Server');
require('dotenv').config()
const COMMAND_COOLDOWN = 600000;
const Constants = Eris.Constants;
class CommandHandler {
constructor() {
const bot = new Eris(process.env.TOKEN, {
intents: [
'guilds',
'guildBans',
'guildEmojis',
'guildIntegrations',
'guildWebhooks',
'guildInvites',
'guildVoiceStates',
'guildMessages',
'guildMessageReactions',
'guildMessageTyping',
'directMessages',
'directMessageReactions',
'directMessageTyping',
'guildPresences',
'guildMembers'
]
});
bot.on("ready", async () => { // When the bot is ready
console.log("Slash command handler ready!"); // Log "Ready!"
//Note: You should use guild commands to test, as they update instantly. Global commands can take up to an hour to update.
const commands = await bot.getCommands();
if(!commands.length) {
bot.createCommand({
name: "topic",
description: "Topic",
type: Constants.ApplicationCommandTypes.CHAT_INPUT //Not required for Chat input type, but recommended
}); //Create a chat input command
}
let atlaID = "370708369951948800";
bot.bulkEditGuildCommands(atlaID, [{
name: "Report Message to Server Staff",
type: 3
}])
});
bot.on("error", (err) => {
console.error(err); // or your preferred logger
});
function handleCooldown(timestamp) {
const timeLeft = Date.now() - timestamp;
if (timeLeft <= COMMAND_COOLDOWN) {
let time = Math.ceil((600000 - timeLeft) / 100) / 10
let minutes = Math.floor(time / 60);
let seconds = Math.ceil(time - minutes * 60);
if (minutes === 0) {
return `${seconds} sec`;
} else {
return `${minutes} minutes ${seconds} seconds`;
}
} else return false;
}
function getColor(color) {
let colors = {
red: 15747399,
yellow: 16439902,
green: 4437377,
blue: 9031664,
darkblue: 26544,
spotify: 1947988
}
return colors[color];
}
bot.on("interactionCreate", async (interaction) => {
if(interaction instanceof Eris.CommandInteraction) {
if(interaction.data.name === "Report Message to Server Staff" && interaction.data.type === 3) {
let c = await this.bot.getChannel('761932330028892194');
return this.sendMessage(c, {
embed: {
color: this.utils.getColor('red'),
description: `<@${interaction.member.id}> has reported a message [BETA TESTING]`,
timestamp: new Date(),
}
})}
switch(interaction.data.name) {
case "topic":
server.findById('370708369951948800', (err, doc) => {
let timeRemaining = handleCooldown(doc.data.topicTimestamps.normal);
if (timeRemaining !== false) {
return interaction.createMessage({
embed: {
color: getColor('red'),
description: `This command has already been used recently!\nTry again in **${timeRemaining}**!`
}
});
}
let topic = Math.floor(Math.random() * topics.length);
if (doc.data.ignoredTopics.length === topics.length) {
doc.data.ignoredTopics = [];
};
while (doc.data.ignoredTopics.includes(topic)) {
topic = Math.floor(Math.random() * topics.length);
};
doc.data.ignoredTopics.push(topic);
return interaction.createMessage({
embed: {
color: getColor('blue'),
description: topics[topic],
}
}).then(doc.data.topicTimestamps.normal = interaction.createdAt, doc.save());
});
}
}
});
bot.connect(); // Get the bot to connect to Discord
}
}
module.exports = CommandHandler;