Init Commit

Server
- Working init discord bot
    - Modular events and commands
    - Has access to the socket server
- Working init socket server
    - Need to work on getting access to discord bot
- Working init web server

Currently working on breaking out the init of the socket server

Client
- Working init socket client

Currently working on the discord bot to join voice channels
This commit is contained in:
Logan Cusano
2024-01-08 00:12:47 -05:00
parent 86dd058d2a
commit 4a0b1004d2
16 changed files with 2599 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
import { SlashCommandBuilder } from 'discord.js';
// Exporting data property
export const data = new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with your input!');
// Exporting other properties
export const example = "/ping";
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);
//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

@@ -0,0 +1,28 @@
import { Events } from 'discord.js';
export const name = Events.InteractionCreate;
export async function execute(nodeIo, interaction) {
const command = interaction.client.commands.get(interaction.commandName);
console.log("Interaction created for command: ", command);
// Execute autocomplete if the user is checking autocomplete
/*
if (interaction.isAutocomplete()) {
console.log("Running autocomplete for command: ", command.data.name);
return await command.autocomplete(interaction);
}
*/
// Check if the interaction is a command
if (!interaction.isChatInputCommand()) return;
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
console.log(`${interaction.member.user} is running '${interaction.commandName}'`);
command.execute(nodeIo, interaction);
}

View File

@@ -0,0 +1,80 @@
import { REST, Routes } from 'discord.js';
import dotenv from 'dotenv';
dotenv.config()
//const clientId = process.env.clientId;
//const guildId = process.env.guildId;
const fs = require('node:fs');
const path = require('node:path');
var commands = [];
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.resolve(__dirname, '../commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
export function deploy (clientId, guildIDs) {
console.log("Deploying commands for: ", guildIDs);
if (!Array.isArray(guildIDs)) guildIDs = [guildIDs];
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const command = require(`${path.resolve(commandsPath, file)}`);
console.log('Deploying Command: ', command);
commands.push(command.data.toJSON());
}
// Construct and prepare an instance of the REST module
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
// and deploy your commands!
for (const guildId of guildIDs) {
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands for guild ID: ${guildId}.`);
// 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, guildId),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands for guild ID: ${guildId}.`);
} 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 {*} clientId The client ID of the bot to remove commands from
* @param {*} guildId The ID of the guild to remove the bot commands from
*/
export function removeAll (clientId, guildId) {
if (!Array.isArray(guildId)) guildIDs = [guildId];
console.log("Removing commands for: ", clientId, guildIDs);
commands = [];
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
for (const guildId of guildIDs) {
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands for guild ID: ${guildId}.`);
// 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, guildId),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands for guild ID: ${guildId}.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.log("ERROR Deploying commands: ", error, "Body from error: ", commands);
}
})()
}
}

View File

@@ -0,0 +1,69 @@
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);
/**
* 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") {
// 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);
console.log(`Adding enabled command: ${filePath}`);
import(`file://${filePath}`).then(command => {
if (command.data instanceof Promise) {
command.data.then(async (builder) => {
command.data = builder;
console.log("Importing command: ", command.data.name, command);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
serverClient.commands.set(command.data.name, command);
});
} else {
console.log("Importing command: ", command.data.name, command);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
serverClient.commands.set(command.data.name, command);
}
})
}
}
/**
* 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") {
const eventsPath = join(__dirname, _eventsPath);
const eventFiles = readdirSync(eventsPath).filter(file => file.endsWith('.mjs'));
for (const file of eventFiles) {
const filePath = join(eventsPath, file);
console.log(`Adding enabled event listener: ${filePath}`);
import(`file://${filePath}`).then(event => {
console.log("Adding event: ", event);
if (event.once) {
serverClient.once(event.name, (...args) => event.execute(nodeIo, ...args));
} else {
serverClient.on(event.name, (...args) => event.execute(nodeIo, ...args));
}
})
}
}