Files
drb-server/discordBot/commands/leave.mjs
Logan Cusano b51300d878
All checks were successful
DRB Tests / drb_mocha_tests (push) Successful in 1m5s
release-tag / release-image (push) Successful in 2m6s
Improved joining and leaving
- Added wrappers
- Improved readability of command code
2024-07-14 19:26:17 -04:00

71 lines
2.3 KiB
JavaScript

import { DebugBuilder } from "../../modules/debugger.mjs";
import { SlashCommandBuilder } from 'discord.js';
import { requestBotLeaveServer, getSocketIdByNuid } from '../../modules/socketServerWrappers.mjs';
import { checkOnlineBotsInGuild } from '../modules/wrappers.mjs';
const log = new DebugBuilder("server", "discordBot.command.leave");
// Exporting data property
export const data = new SlashCommandBuilder()
.setName('leave')
.setDescription('Disconnect a bot from the server')
.addStringOption(system =>
system.setName('bot')
.setDescription('The bot you would like to disconnect')
.setRequired(true)
.setAutocomplete(true)
);
// Exporting other properties
export const example = "/leave *{Bot Name}*";
export const deferInitialReply = true;
/**
* Function to give the user auto-reply suggestions
* @param {any} nodeIo The nodeIO server for manipulation of sockets
* @param {any} interaction The interaction object
*/
export async function autocomplete(nodeIo, interaction) {
const focusedValue = interaction.options.getFocused();
const choices = await checkOnlineBotsInGuild(nodeIo, interaction.guild.id);
log.DEBUG(choices);
const filtered = choices
.filter(choice => choice.name.startsWith(focusedValue))
.map(choice => ({ name: choice.name, value: choice.nuid }));
log.DEBUG(focusedValue, choices, filtered);
try{
await interaction.respond(filtered);
}
catch (e) {
log.WARN("Autocomplete interaction failure", e);
}
}
/**
* The function to run when the command is called by a discord user
* @param {any} nodeIo The nodeIO server for manipulation of sockets
* @param {any} interaction The interaction object
*/
export async function execute(nodeIo, interaction) {
try {
const selectedNode = interaction.options.getString('bot');
const socket = await getSocketIdByNuid(nodeIo, selectedNode);
if (!socket) {
await interaction.editReply({ content: `Bot '${selectedNode}' not found or not connected.`, ephemeral: true });
return;
}
await requestBotLeaveServer(socket, interaction.guild.id);
await interaction.editReply(`Ok <@${interaction.member.id}>, the bot is leaving shortly.`);
} catch (err) {
log.ERROR("Failed to disconnect bot:", err);
await interaction.editReply({ content: `An error occurred: ${err.message}`, ephemeral: true });
}
}