-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
188 lines (156 loc) · 5.33 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* ====================NOTE====================
* This code was created by LostAndDead,
* please don't claim this as your own work
* https://github.com/LostAndDead
* ============================================
*/
/* --- Done, No Further Changes Needed --- */
//Imports!
const { Client, Collection, Intents } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');
const yaml = require('js-yaml');
const MCSS = require('./utils/MCSS API');
//Init the client with discord.js
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES]
});
//Load the config file
let Config = null;
try {
let fileContents = fs.readFileSync('./config.yml', 'utf8');
Config = yaml.load(fileContents);
}
catch (e) {
console.log(e);
}
//Create a collection of commands and commandData
const commands = new Collection();
//Command data is a list of JSON objects that need to be sent to register the slash commands
const commandsData = [];
//We load all the command files and save the command and the commandData
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
//Load the file
const command = require(`./commands/${file}`);
//Add to command collection
commands.set(command.info.name, command);
//Load the commandData
const commandData = require(`./commands/${file}`);
var json = commandData.info.toJSON()
//Merge it with the extra data and save it
json = Object.assign({}, json, commandData.extraJSON)
commandsData.push(json);
}
//Load the rest API for registering slash commands, auto deals with rate limits
const rest = new REST({
version: '9'
}).setToken(Config.Token);
//Send all the slash commandData to the discord API to register the commands for the guild only
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(Config.ClientID, Config.GuildID), {
body: commandsData
},
);
console.log('Successfully reloaded application (/) commands.');
}
catch (error) {
console.error(error);
}
})();
//set the default status
setTimeout(() => {
const status = commands.get('status');
status.init(Config, client);
}, 5000)
//All loaded and ready
client.once('ready', async () => {
console.log('Ready!');
var commands = await client.guilds.cache.get(Config.GuildID).commands.fetch()
await Promise.all(commands.map(async (command) => {
if (command.name == "perms") {
var command = await client.guilds.cache.get(Config.GuildID).commands.fetch(command.id);
const permissions = [{
id: Config.AdminID,
type: 'ROLE',
permission: true,
}, ];
await command.permissions.add({
permissions
});
}
}))
console.log("Connecting to MCSS...");
MCSS.init(Config.MCSS.username, Config.MCSS.password, Config.MCSS.endpoint, Config.MCSS.port, Config.MCSS.secure);
});
//Listen for commands coming from chat and context menus
client.on('interactionCreate', async interaction => {
if (!(interaction.isCommand() || interaction.isContextMenu())) return;
if (!interaction.inGuild()) {
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
})
return;
}
//Find the commands we want to run
const command = commands.get(interaction.commandName);
if (!command) return;
try {
//Run the command
await command.run(interaction, Config, client);
}
catch (error) {
console.error(error);
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
});
}
});
//Listen for button clicks
client.on('interactionCreate', async interaction => {
if (!(interaction.isButton() || interaction.isSelectMenu())) return;
if (!interaction.inGuild()) {
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
})
return;
}
//Find the command the button originates from, this is using a custom routing system based on the button ID
const command = commands.get(interaction.customId.split('.')[0]);
if (!command) return;
try {
//Run the button data
await command.callButton(interaction, Config, client);
}
catch (error) {
console.error(error);
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
});
}
});
//Listen for autocomplete
client.on('interactionCreate', async interaction => {
if (!interaction.isAutocomplete()) return;
//Find the commands we want to auto complete for
const command = commands.get(interaction.commandName);
if (!command) return;
try {
//Run the autocomplete resolver
await command.autocomplete(interaction, Config, client);
}
catch (error) {
console.error(error);
}
});
//All Good!
client.login(Config.Token);