Skip to content

Commit 8f6cf68

Browse files
committed
Full ES6 Re-Factor
- We are using an experimental feature of ES6, import assertions. This allows us to import JSON files without causing issues, while sticking to the standardized ES6 module structure. - IA will most likely become stable in the future, if not we would most likely convert JSON's to JS exportable objects, or start storing that information in other ways.
1 parent cab13c3 commit 8f6cf68

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+329
-731
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0",
44
"description": "",
55
"main": "src/index.js",
6+
"type": "module",
67
"scripts": {
78
"test": "echo \"Error: no test specified\" && exit 1"
89
},

src/commandEvents/interactionCreate/checkCommands.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//Handler for slash commands
22

3-
const { devs, modServer } = require('../../../config.json');
4-
const getLocalCommands = require('../../commandUtils/getLocalCommands');
3+
import { devs, modServer } from '../../../config.json' assert { type: 'json' };
4+
import getLocalCommands from '../../commandUtils/getLocalCommands';
55

6-
module.exports = async (client, interaction) => {
6+
const checkCommands = async (client, interaction) => {
77
if (!interaction.isChatInputCommand()) return;
88

99
const localCommands = getLocalCommands();
@@ -64,4 +64,6 @@ module.exports = async (client, interaction) => {
6464
} catch (error) {
6565
console.log(`There was an error running this command: ${error}`);
6666
}
67-
};
67+
};
68+
69+
export default checkCommands;

src/commandEvents/ready/consoleLog.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
module.exports = (client) => {
1+
const consoleLog = (client) => {
22
console.log(`${client.user.tag} is online.`);
3-
};
3+
};
4+
export default consoleLog;

src/commandEvents/ready/registerCommands.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
// Initial runthrough of all slash commands, reading through and initializing them
22

3-
const { modServer } = require('../../../config.json');
4-
const areCommandsDifferent = require('../../commandUtils/areCommandsDifferent');
5-
const getApplicationCommands = require('../../commandUtils/getApplicationCommands');
6-
const getLocalCommands = require('../../commandUtils/getLocalCommands');
3+
import config from '../../../config.json' assert { type: 'json' };
4+
import areCommandsDifferent from '../../commandUtils/areCommandsDifferent.js';
5+
import getApplicationCommands from '../../commandUtils/getApplicationCommands.js';
6+
import getLocalCommands from '../../commandUtils/getLocalCommands.js';
77

8-
module.exports = async (client) => {
8+
const registerCommands = async (client) => {
99
try {
10-
const localCommands = getLocalCommands();
11-
const applicationCommands = await getApplicationCommands(
12-
client,
13-
modServer
14-
);
10+
const localCommands = await getLocalCommands();
11+
const applicationCommands = await getApplicationCommands(client, config.modServer);
1512

1613
for (const localCommand of localCommands) {
1714
const { name, description, options } = localCommand;
@@ -51,10 +48,13 @@ module.exports = async (client) => {
5148

5249
console.log(`👍 Registered command "${name}."`);
5350
}
51+
console.log(`👍 Command ${name} already registered.`)
5452
}
5553
} catch (error) {
5654
console.log(`There was an error: ${error}`);
5755
}
5856

5957
console.log('👍 Registered all commands.');
60-
};
58+
};
59+
60+
export default registerCommands;
+53-51
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,57 @@
1-
module.exports = (existingCommand, localCommand) => {
2-
const areChoicesDifferent = (existingChoices, localChoices) => {
3-
for (const localChoice of localChoices) {
4-
const existingChoice = existingChoices?.find(
5-
(choice) => choice.name === localChoice.name
6-
);
7-
8-
if (!existingChoice) {
9-
return true;
10-
}
11-
12-
if (localChoice.value !== existingChoice.value) {
13-
return true;
14-
}
15-
}
16-
return false;
17-
};
18-
19-
const areOptionsDifferent = (existingOptions, localOptions) => {
20-
for (const localOption of localOptions) {
21-
const existingOption = existingOptions?.find(
22-
(option) => option.name === localOption.name
23-
);
24-
25-
if (!existingOption) {
26-
return true;
27-
}
28-
29-
if (
30-
localOption.description !== existingOption.description ||
31-
localOption.type !== existingOption.type ||
32-
(localOption.required || false) !== existingOption.required ||
33-
(localOption.choices?.length || 0) !==
34-
(existingOption.choices?.length || 0) ||
35-
areChoicesDifferent(
36-
localOption.choices || [],
37-
existingOption.choices || []
38-
)
39-
) {
40-
return true;
41-
}
42-
}
43-
return false;
44-
};
45-
1+
const areChoicesDifferent = (existingChoices, localChoices) => {
2+
for (const localChoice of localChoices) {
3+
const existingChoice = existingChoices?.find(
4+
(choice) => choice.name === localChoice.name
5+
);
6+
7+
if (!existingChoice) {
8+
return true;
9+
}
10+
11+
if (localChoice.value !== existingChoice.value) {
12+
return true;
13+
}
14+
}
15+
return false;
16+
};
17+
18+
const areOptionsDifferent = (existingOptions, localOptions) => {
19+
for (const localOption of localOptions) {
20+
const existingOption = existingOptions?.find(
21+
(option) => option.name === localOption.name
22+
);
23+
24+
if (!existingOption) {
25+
return true;
26+
}
27+
4628
if (
47-
existingCommand.description !== localCommand.description ||
48-
existingCommand.options?.length !== (localCommand.options?.length || 0) ||
49-
areOptionsDifferent(existingCommand.options, localCommand.options || [])
29+
localOption.description !== existingOption.description ||
30+
localOption.type !== existingOption.type ||
31+
(localOption.required || false) !== existingOption.required ||
32+
(localOption.choices?.length || 0) !==
33+
(existingOption.choices?.length || 0) ||
34+
areChoicesDifferent(
35+
localOption.choices || [],
36+
existingOption.choices || []
37+
)
5038
) {
5139
return true;
5240
}
53-
54-
return false;
55-
};
41+
}
42+
return false;
43+
};
44+
45+
const areCommandsDifferent = (existingCommand, localCommand) => {
46+
if (
47+
existingCommand.description !== localCommand.description ||
48+
existingCommand.options?.length !== (localCommand.options?.length || 0) ||
49+
areOptionsDifferent(existingCommand.options, localCommand.options || [])
50+
) {
51+
return true;
52+
}
53+
54+
return false;
55+
};
56+
57+
export default areCommandsDifferent;

src/commandUtils/getAllFiles.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const fs = require('fs');
2-
const path = require('path');
1+
import fs from 'fs';
2+
import path from 'path';
33

4-
module.exports = (directory, foldersOnly = false) => {
4+
const getFilesInDirectory = (directory, foldersOnly = false) => {
55
let fileNames = [];
66

77
const files = fs.readdirSync(directory, { withFileTypes: true });
@@ -21,4 +21,6 @@ module.exports = (directory, foldersOnly = false) => {
2121
}
2222

2323
return fileNames;
24-
};
24+
};
25+
26+
export default getFilesInDirectory;
+12-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
module.exports = async (client, guildId) => {
2-
let applicationCommands;
3-
4-
if (guildId) {
1+
const fetchApplicationCommands = async (client, guildId) => {
2+
let applicationCommands;
3+
4+
if (guildId) {
55
const guild = await client.guilds.fetch(guildId);
66
applicationCommands = guild.commands;
7-
} else {
7+
} else {
88
applicationCommands = await client.application.commands;
9-
}
10-
11-
await applicationCommands.fetch();
12-
return applicationCommands;
13-
};
9+
}
10+
11+
await applicationCommands.fetch();
12+
return applicationCommands;
13+
};
14+
15+
export default fetchApplicationCommands;

src/commandUtils/getLocalCommands.js

+24-21
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
const path = require('path');
2-
const getAllFiles = require('./getAllFiles');
1+
import path from 'path';
2+
import getAllFiles from '../commandUtils/getAllFiles.js';
3+
import { fileURLToPath, pathToFileURL } from 'url';
34

4-
module.exports = (exceptions = []) => {
5-
let localCommands = [];
5+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
66

7-
const commandCategories = getAllFiles(
8-
path.join(__dirname, '..', 'commands'),
9-
true
10-
);
11-
12-
for (const commandCategory of commandCategories) {
13-
const commandFiles = getAllFiles(commandCategory);
7+
const getLocalCommands = async (exceptions = []) => {
8+
let localCommands = [];
149

15-
for (const commandFile of commandFiles) {
16-
const commandObject = require(commandFile);
17-
18-
if (exceptions.includes(commandObject.name)) {
19-
continue;
20-
}
10+
const commandCategories = getAllFiles(path.join(__dirname, '..', 'commands'), true);
11+
12+
for (const commandCategory of commandCategories) {
13+
const commandFiles = getAllFiles(commandCategory);
14+
15+
for (const commandFile of commandFiles) {
16+
const commandFileURL = pathToFileURL(commandFile).href;
17+
const commandObject = await import(commandFileURL).then(module => module.default);
18+
19+
if (exceptions.includes(commandObject.name)) {
20+
continue;
21+
}
2122

22-
localCommands.push(commandObject);
23+
localCommands.push(commandObject);
24+
}
2325
}
24-
}
2526

26-
return localCommands;
27-
};
27+
return localCommands;
28+
};
29+
30+
export default getLocalCommands;

src/commands/test/getForumTags.js src/commands/dev/getForumTags.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
// Command to get the forum tag IDs for the parent forums
2+
/*---- Dev Only ----*/
13

2-
3-
module.exports = {
4+
export default {
45
name: 'getforumtags',
56
description: 'Gets the forum tag IDs for the parent forums',
67
devOnly: true,
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
// Front facing UX for the ticket system. It is a button menu that allows users to select a ticket type and open a ticket.
22
/*---- Dev Only ----*/
33

4-
const {
5-
PermissionFlagsBits,
6-
ButtonBuilder,
7-
EmbedBuilder,
8-
ActionRowBuilder,
9-
MessageEmbed,
10-
} = require("discord.js");
11-
const ticketSchema = require("../../schemas/ticket");
12-
const emojis = require("../../emojis.json");
4+
import { ButtonBuilder, EmbedBuilder, ActionRowBuilder } from "discord.js";
5+
import emojis from "../../emojis.json" assert { type: "json" };
136

14-
module.exports = {
7+
export default {
158
name: 'ticketlistener',
169
description: 'Adds the ticket listener to the channel',
1710
devOnly: true,
1811
callback: async (client, interaction) => {
1912
await interaction.deferReply();
20-
2113
const channel = interaction.channel;
2214
const ticketEmbed = new EmbedBuilder()
2315
.setColor([108,0,18])
@@ -40,37 +32,6 @@ module.exports = {
4032
.setFooter({text: "© Sanctuary Development Team - 2023"})
4133
;
4234

43-
// No longer using a dropdown menu, but keeping this code for future reference
44-
/*
45-
const menu = new StringSelectMenuBuilder()
46-
.setCustomId("Select")
47-
.setMaxValues(1)
48-
.setPlaceholder("Select a topic.")
49-
.addOptions(
50-
new StringSelectMenuOptionBuilder()
51-
.setLabel("Report a User")
52-
.setDescription("Do you need to report a user?")
53-
.setValue("Report a User")
54-
.setEmoji("<:icon_report:1140779824793788486>"),
55-
new StringSelectMenuOptionBuilder()
56-
.setLabel("Technical Support")
57-
.setDescription("Are you having technical difficulties?")
58-
.setValue("Technical Support")
59-
.setEmoji("<:icon_tech2:1140800254141268018>"),
60-
new StringSelectMenuOptionBuilder()
61-
.setLabel("VIP Applications")
62-
.setDescription("Apply for VIP / Content Creator status.")
63-
.setValue("VIP Applications")
64-
.setEmoji("<:icon_vip2:1140799537942900799>"),
65-
new StringSelectMenuOptionBuilder()
66-
.setLabel("General Support")
67-
.setDescription("Have some general questions?")
68-
.setValue("General Support")
69-
.setEmoji("<:icon_general2:1140799531496263700>"),
70-
);
71-
const oldRow = new ActionRowBuilder().addComponents(menu);
72-
*/
73-
7435
// Create Buttons
7536
const report_button = new ButtonBuilder()
7637
.setCustomId('report_button')
@@ -98,7 +59,7 @@ module.exports = {
9859
.setEmoji(`${emojis.generalButtonEmoji}`)
9960
.setStyle('Success');
10061

101-
// Create Button Row
62+
// Create Button Rows
10263
const row1 = new ActionRowBuilder()
10364
.addComponents(report_button, staff_report_button);
10465
const row2 = new ActionRowBuilder()
@@ -108,7 +69,6 @@ module.exports = {
10869

10970

11071
await interaction.deleteReply(); // Delete command for cleanliness
111-
//await channel.send({ files: [{attachment: './resources/support.png', name: 'support.png'}] });
11272
await channel.send({ embeds: [ticketEmbed], files: [{attachment: './resources/support.png', name: 'support.png'}], components: [row1, row2, row3] });
11373
}
11474
}

src/commands/misc/ping.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
name: 'ping',
33
description: 'Replies with the bot ping!',
44

0 commit comments

Comments
 (0)