Improved joining and leaving
All checks were successful
DRB Tests / drb_mocha_tests (push) Successful in 1m5s
release-tag / release-image (push) Successful in 2m6s

- Added wrappers
- Improved readability of command code
This commit is contained in:
Logan Cusano
2024-07-14 19:26:17 -04:00
parent f29459aadb
commit b51300d878
3 changed files with 225 additions and 164 deletions

View File

@@ -1,8 +1,9 @@
import { DebugBuilder } from "../../modules/debugger.mjs";
const log = new DebugBuilder("server", "discordBot.command.leave");
import { SlashCommandBuilder } from 'discord.js';
import { requestBotLeaveServer, getSocketIdByNuid } from '../../modules/socketServerWrappers.mjs';
import { checkOnlineBotsInGuild } from '../modules/wrappers.mjs'
import { checkOnlineBotsInGuild } from '../modules/wrappers.mjs';
const log = new DebugBuilder("server", "discordBot.command.leave");
// Exporting data property
export const data = new SlashCommandBuilder()
@@ -12,7 +13,8 @@ export const data = new SlashCommandBuilder()
system.setName('bot')
.setDescription('The bot you would like to disconnect')
.setRequired(true)
.setAutocomplete(true));;
.setAutocomplete(true)
);
// Exporting other properties
export const example = "/leave *{Bot Name}*";
@@ -25,15 +27,22 @@ export const deferInitialReply = true;
*/
export async function autocomplete(nodeIo, interaction) {
const focusedValue = interaction.options.getFocused();
const choices = (await checkOnlineBotsInGuild(nodeIo, interaction.guild.id));
const choices = await checkOnlineBotsInGuild(nodeIo, interaction.guild.id);
log.DEBUG(choices);
const filtered = choices.filter(choice => choice.name.startsWith(focusedValue)).map(choice => choice = {name: choice.name, value: choice.nuid});
const filtered = choices
.filter(choice => choice.name.startsWith(focusedValue))
.map(choice => ({ name: choice.name, value: choice.nuid }));
log.DEBUG(focusedValue, choices, filtered);
await interaction.respond(filtered);
try{
await interaction.respond(filtered);
}
catch (e) {
log.WARN("Autocomplete interaction failure", e);
}
}
/**
@@ -43,16 +52,19 @@ export async function autocomplete(nodeIo, interaction) {
*/
export async function execute(nodeIo, interaction) {
try {
// Get the requested bot
const selectedNode = interaction.options.getString('bot');
const socket = await getSocketIdByNuid(nodeIo, selectedNode);
log.DEBUG("All open sockets:", socket, 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.reply(`**Online Sockets: '${sockets}'**`);
await interaction.editReply(`Ok <@${interaction.member.id}>, the bot is leaving shortly`);
//await interaction.channel.send('**Pong.**');
await interaction.editReply(`Ok <@${interaction.member.id}>, the bot is leaving shortly.`);
} catch (err) {
console.error(err);
// await interaction.reply(err.toString());
log.ERROR("Failed to disconnect bot:", err);
await interaction.editReply({ content: `An error occurred: ${err.message}`, ephemeral: true });
}
}
}