-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
76 lines (61 loc) Β· 2.56 KB
/
commands.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
const { MessageEmbed } = require('discord.js');
const { readInFile, writeFile, SETTINGS_FILE_PATH } = require('./file_reader.js');
const { remind, sendLeaderboardMessage } = require('./reminder.js');
const { SlashCommandBuilder } = require('@discordjs/builders');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
// Public
async function attemptInteractionEvaluation(interaction) {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
var succeeded = false;
if (commandName == 'setchannel') {
setChannel(interaction.channel);
succeeded = true;
} else if (commandName == 'testreminder') {
testReminder(interaction.client);
succeeded = true;
} else if (commandName == 'leaderboard') {
showLeaderboard(interaction.channel);
succeeded = true;
} else if (commandName == 'ping') {
await interaction.reply({ content: 'PONG!' });
}
if (succeeded) {
await interaction.reply({ content: 'Success!', ephemeral: true });
}
};
function registerCommands(client, authToken, clientId) {
for (const [guildKey, guild] of client.guilds.cache) {
const commands = [
new SlashCommandBuilder().setName('setchannel').setDescription('Set the current channel for the reminder to send a message in.'),
new SlashCommandBuilder().setName('testreminder').setDescription('Run a test version of the scheduled reminder.'),
new SlashCommandBuilder().setName('leaderboard').setDescription('Show the current leaderboard.'),
new SlashCommandBuilder().setName('ping').setDescription('Duh.'),
]
.map(command => command.toJSON());
const rest = new REST({ version: '9' }).setToken(authToken);
rest.put(Routes.applicationGuildCommands(clientId, guildKey), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error);
}
}
exports.attemptInteractionEvaluation = attemptInteractionEvaluation;
exports.registerCommands = registerCommands;
// Private
function setChannel(channel) {
readInFile(SETTINGS_FILE_PATH, data => {
var settings = JSON.parse(data);
settings.channelToSendTo = channel.id;
writeFile(SETTINGS_FILE_PATH, JSON.stringify(settings, null, '\t'), succeeded => {
if (succeeded) {
}
});
});
}
function testReminder(client) {
remind(client);
}
function showLeaderboard(channel) {
sendLeaderboardMessage(channel);
}