Major updates to core

This commit is contained in:
Logan Cusano
2024-02-10 15:10:35 -05:00
parent d563021866
commit 9c46792959
22 changed files with 977 additions and 156 deletions

View File

@@ -0,0 +1,25 @@
import { SlashCommandBuilder } from 'discord.js';
// Exporting data property
export const data = new SlashCommandBuilder()
.setName('join')
.setDescription('Replies with your input!');
// Exporting other properties
export const example = "/join";
export const deferInitialReply = false;
// Exporting execute function
export async function execute(nodeIo, interaction) {
try {
const sockets = await nodeIo.allSockets();
console.log("All open sockets: ",sockets);
console.log("Open Socket: ", sockets.values(), nodeIo.sockets.sockets.get(sockets[0]))
//await interaction.reply(`**Online Sockets: '${sockets}'**`);
await interaction.reply('**Pong.**');
//await interaction.channel.send('**Pong.**');
} catch (err) {
console.error(err);
// await interaction.reply(err.toString());
}
}

View File

@@ -1,29 +1,32 @@
import { Client, GatewayIntentBits, Collection } from 'discord.js';
import { registerActiveCommands, unregisterAllCommands } from './modules/registerCommands.mjs'
import { join, dirname } from 'path';
import { readdirSync } from 'fs';
import { fileURLToPath } from 'url';
import { Collection } from 'discord.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import dotenv from 'dotenv';
dotenv.config()
/**
* Add the enabled commands to the bot to be used by users in discord
* (commands that end in '.mjs' will be enabled, to disable just remove the extension or replace with '.mjs.disabled')
* @param {any} serverClient
* @param {any} nodeIo
* @param {any} _commandsPath="./commands"
* @returns {any}
*/
export function addEnabledCommands(serverClient, nodeIo, _commandsPath = "../commands") {
export const addEnabledCommands = async (serverClient, _commandsPath = "./commands") => {
// Setup commands for the Discord bot
serverClient.commands = new Collection();
const commandsPath = join(__dirname, _commandsPath);
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.mjs'));
for (const file of commandFiles) {
const filePath = join(commandsPath, file);
const filePath = await join(commandsPath, file);
console.log(`Adding enabled command: ${filePath}`);
import(`file://${filePath}`).then(command => {
await import(`file://${filePath}`).then(command => {
if (command.data instanceof Promise) {
command.data.then(async (builder) => {
command.data = builder;
@@ -40,17 +43,19 @@ export function addEnabledCommands(serverClient, nodeIo, _commandsPath = "../com
}
})
}
// Register the commands currently in use by the bot
await registerActiveCommands(serverClient);
}
/**
* Add the enabled event listeners to the bot
* (events that end in '.mjs' will be enabled, to disable just remove the extension or replace with '.mjs.disabled')
* @param {any} serverClient
* @param {any} nodeIo
* @param {any} _eventsPath="./events"
* @returns {any}
*/
export function addEnabledEventListeners(serverClient, nodeIo, _eventsPath = "../events") {
export function addEnabledEventListeners(serverClient, _eventsPath = "./events") {
const eventsPath = join(__dirname, _eventsPath);
const eventFiles = readdirSync(eventsPath).filter(file => file.endsWith('.mjs'));
@@ -60,10 +65,28 @@ export function addEnabledEventListeners(serverClient, nodeIo, _eventsPath = "..
import(`file://${filePath}`).then(event => {
console.log("Adding event: ", event);
if (event.once) {
serverClient.once(event.name, (...args) => event.execute(nodeIo, ...args));
serverClient.once(event.name, (...args) => event.execute(serverClient.nodeIo, ...args));
} else {
serverClient.on(event.name, (...args) => event.execute(nodeIo, ...args));
serverClient.on(event.name, (...args) => event.execute(serverClient.nodeIo, ...args));
}
})
}
}
}
// The discord client
export const serverClient = new Client({ intents: [GatewayIntentBits.Guilds] });
// Run when the bot is ready
serverClient.on('ready', async () => {
console.log(`Logged in as ${serverClient.user.tag}!`);
// Add and register commands
await addEnabledCommands(serverClient);
// Config the discord bot with events
await addEnabledEventListeners(serverClient);
});
// Startup the discord bot
console.log(`Logging into discord with ID: ${process.env.DISCORD_TOKEN}`);
serverClient.login(process.env.DISCORD_TOKEN);

View File

@@ -0,0 +1,83 @@
import { REST, Routes } from 'discord.js';
import dotenv from 'dotenv';
dotenv.config()
const discordToken = process.env.DISCORD_TOKEN;
export const registerActiveCommands = async (serverClient) => {
const guildIDs = serverClient.guilds.cache;
const clientId = serverClient.user.id;
const commands = await serverClient.commands.map(command => command = command.data.toJSON());
// Construct and prepare an instance of the REST module
const rest = new REST({ version: '10' }).setToken(discordToken);
// and deploy your commands!
guildIDs.forEach(guild => {
console.log("Deploying commands for: ", guild.id);
console.log("Commands", commands);
(async () => {
try {
console.log(`Started refreshing application (/) commands for guild ID: ${guild.id}.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(clientId, guild.id),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands for guild ID: ${guild.id}.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.log("ERROR Deploying commands: ", error, "Body from error: ", commands);
}
})()
})
};
/**
* Remove all commands for a given bot in a given guild
*
* @param {any} serverClient The discord bot client
*/
export const unregisterAllCommands = async (serverClient) => {
const guildIDs = serverClient.guilds.cache;
const clientId = serverClient.user.id;
commands = [];
const rest = new REST({ version: '10' }).setToken(discordToken);
guildIDs.forEach(guild => {
console.log("Removing commands for: ", clientId, guild.id);
(async () => {
try {
console.log(`Started removal of ${commands.length} application (/) commands for guild ID: ${guild.id}.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(clientId, guild.id),
{ body: commands },
);
console.log(`Successfully removed ${data.length} application (/) commands for guild ID: ${guild.id}.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.log("ERROR removing commands: ", error, "Body from error: ", commands);
}
})()
})
}
/**
* This named wrapper will remove all commands and then re-add the commands back, effectively refreshing them
* @param {any} serverClient The discord bot client object
* @returns {any}
*/
export const refreshActiveCommandsWrapper = async (serverClient) => {
// Remove all commands
console.log("Removing/Unregistering all commands from all connected servers/guilds");
await unregisterAllCommands(serverClient);
// Deploy the active commands
console.log("Adding commands to all connected servers/guilds");
await registerActiveCommands(serverClient);
return;
}