Files
drb-server/discordBot/commands/leave.mjs
Logan Cusano 117cbea67f
All checks were successful
release-tag / release-image (push) Successful in 1m52s
Lint JavaScript/Node.js / lint-js (push) Successful in 11s
DRB Tests / drb_mocha_tests (push) Successful in 29s
Linting
2024-08-11 15:57:46 -04:00

82 lines
2.4 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,
});
}
}