From c2002ada552d94bd6cf195278c72db96b1d794a1 Mon Sep 17 00:00:00 2001 From: dev-737 <73829355+dev-737@users.noreply.github.com> Date: Wed, 24 May 2023 19:10:17 +0530 Subject: [PATCH] fix: Moved `/network delete` to `/hub leave` --- src/Commands/Main/hub.ts | 40 +++++++++++++++++++++-- src/Commands/Main/network.ts | 20 +----------- src/Scripts/network/displaySettings.ts | 44 +++++++++++--------------- 3 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/Commands/Main/hub.ts b/src/Commands/Main/hub.ts index af9d5eb9..c6cfe605 100644 --- a/src/Commands/Main/hub.ts +++ b/src/Commands/Main/hub.ts @@ -1,5 +1,6 @@ import { SlashCommandBuilder, ChatInputCommandInteraction, ChannelType, AutocompleteInteraction } from 'discord.js'; import { getDb } from '../../Utils/functions/utils'; +import reset from '../../Scripts/network/reset'; export default { data: new SlashCommandBuilder() @@ -53,6 +54,18 @@ export default { .setRequired(false), ), ) + .addSubcommand(subcommand => + subcommand + .setName('leave') + .setDescription('🚪 Leave a hub.') + .addStringOption(stringOption => + stringOption + .setName('hub') + .setDescription('The name of the hub') + .setAutocomplete(true) + .setRequired(true), + ), + ) .addSubcommand((subcommand) => subcommand .setName('create') @@ -219,14 +232,19 @@ export default { const subcommand = interaction.options.getSubcommand(); const subcommandGroup = interaction.options.getSubcommandGroup(); + if (subcommand === 'leave') { + const channelId = interaction.options.getString('hub', true); + return reset.execute(interaction, channelId); + } + require(`../../Scripts/hub/${subcommandGroup || subcommand}`).execute(interaction); }, async autocomplete(interaction: AutocompleteInteraction) { const subcommand = interaction.options.getSubcommand(); + const focusedValue = interaction.options.getFocused(); let hubChoices; if (subcommand === 'browse' || subcommand === 'delete') { - const focusedValue = interaction.options.getFocused(); hubChoices = await getDb().hubs.findMany({ where: { @@ -238,7 +256,6 @@ export default { } else if (subcommand === 'manage' || subcommand === 'networks') { - const focusedValue = interaction.options.getFocused(); hubChoices = await getDb().hubs.findMany({ where: { name: { @@ -254,6 +271,25 @@ export default { }); } + + else if (subcommand === 'leave') { + const networks = await getDb().connectedList.findMany({ + where: { serverId: interaction.guild?.id }, + select: { channelId: true, hub: true }, + take: 25, + }); + + const filteredNets = networks + .filter((network) => network.hub?.name.toLowerCase().includes(focusedValue.toLowerCase())) + .map(async (network) => { + const channel = await interaction.guild?.channels.fetch(network.channelId).catch(() => null); + return { name: `${network.hub?.name} | #${channel?.name || network.channelId}`, value: network.channelId }; + }); + + + return await interaction.respond(await Promise.all(filteredNets)); + } + const filtered = hubChoices?.map((hub) => ({ name: hub.name, value: hub.name })); filtered ? await interaction.respond(filtered) : null; }, diff --git a/src/Commands/Main/network.ts b/src/Commands/Main/network.ts index 57b79096..b9a07e3e 100644 --- a/src/Commands/Main/network.ts +++ b/src/Commands/Main/network.ts @@ -1,6 +1,5 @@ import { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction, AutocompleteInteraction } from 'discord.js'; import { getDb } from '../../Utils/functions/utils'; -import reset from '../../Scripts/network/reset'; import displaySettings from '../../Scripts/network/displaySettings'; export default { @@ -9,17 +8,6 @@ export default { .setDescription('Manage network connections for this server.') .setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels) .setDMPermission(false) - .addSubcommand((subcommand) => subcommand - .setName('delete') - .setDescription('Delete a network connection from this server.') - .addStringOption((stringOption) => - stringOption - .setName('network') - .setDescription('Select the network you wish to delete. Use the channel ID if you don\'t see the network name.') - .setRequired(true) - .setAutocomplete(true), - ), - ) .addSubcommand((subcommand) => subcommand .setName('manage') .setDescription('Manage a network connection for this server.') @@ -50,13 +38,7 @@ export default { return; } - - if (interaction.options.getSubcommand() === 'delete') { - reset.execute(interaction); - } - else { - displaySettings.execute(interaction, interaction.options.getString('network', true)); - } + displaySettings.execute(interaction, interaction.options.getString('network', true)); }, async autocomplete(interaction: AutocompleteInteraction) { const focusedValue = interaction.options.getFocused(); diff --git a/src/Scripts/network/displaySettings.ts b/src/Scripts/network/displaySettings.ts index 18a067b3..4b7a5bba 100644 --- a/src/Scripts/network/displaySettings.ts +++ b/src/Scripts/network/displaySettings.ts @@ -1,35 +1,32 @@ -import { ChatInputCommandInteraction, ButtonBuilder, ActionRowBuilder, ButtonStyle, GuildTextBasedChannel, EmbedBuilder, ChannelType, ComponentType, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, Interaction, ChannelSelectMenuBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, TextChannel } from 'discord.js'; +import { ChatInputCommandInteraction, ButtonBuilder, ActionRowBuilder, ButtonStyle, GuildTextBasedChannel, EmbedBuilder, ChannelType, ComponentType, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, Interaction, ChannelSelectMenuBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, TextChannel, ButtonInteraction, AnySelectMenuInteraction } from 'discord.js'; import { reconnect, disconnect } from '../../Structures/network'; import { colors, getDb } from '../../Utils/functions/utils'; import logger from '../../Utils/logger'; import { captureException } from '@sentry/node'; -// function to make it easier to edit embeds with updated data -async function setupEmbed(interaction: ChatInputCommandInteraction, channelId: string) { - const emoji = interaction.client.emotes; +function yesOrNo(option: unknown, yesEmoji: string, noEmoji: string) { + return option ? yesEmoji : noEmoji; +} +// function to make it easier to edit embeds with updated data +async function setupEmbed(interaction: Interaction, channelId: string) { const networkData = await getDb().connectedList.findFirst({ where: { channelId } }); - const channel = interaction.guild?.channels.cache.get(`${networkData?.channelId}`); - // enabled/disabled emojis - const connected = networkData?.connected ? emoji.normal.yes : emoji.normal.no; - const profanity = networkData?.profFilter ? emoji.normal.enabled : emoji.normal.disabled; - const webhook = networkData?.webhook ? emoji.normal.enabled : emoji.normal.disabled; - const compact = networkData?.compact ? emoji.normal.enabled : emoji.normal.disabled; + const { yes, no, enabled, disabled } = interaction.client.emotes.normal; const invite = networkData?.invite ? `Code: [\`${networkData.invite}\`](https://discord.gg/${networkData.invite})` : 'Not Set.'; return new EmbedBuilder() .setTitle('Edit Settings') - .setDescription(`Showing network settings for ${channel || 'None'}.`) + .setDescription(`Showing network settings for <#${channelId}>.`) .addFields([ - { name: 'Channel', value: `${channel || `${emoji.normal.no} Error.`}`, inline: true }, - { name: 'Connected', value: connected, inline: true }, + { name: 'Channel', value: `<#${channelId}>`, inline: true }, { name: 'Invite', value: invite, inline: true }, - { name: 'Compact', value: compact, inline: true }, - { name: 'Profanity Filter', value: profanity, inline: true }, - { name: 'Webhook', value: webhook, inline: true }, + { name: 'Connected', value: yesOrNo(networkData?.connected, yes, no), inline: true }, + { name: 'Compact', value: yesOrNo(networkData?.compact, enabled, disabled), inline: true }, + { name: 'Webhook', value: yesOrNo(networkData?.webhook, enabled, disabled), inline: true }, + { name: 'Profanity Filter', value: yesOrNo(networkData?.profFilter, enabled, disabled), inline: true }, ]) .setColor(colors('chatbot')) .setThumbnail(interaction.guild?.iconURL() || interaction.client.user.avatarURL()) @@ -38,7 +35,7 @@ async function setupEmbed(interaction: ChatInputCommandInteraction, channelId: s } export = { - async execute(interaction: ChatInputCommandInteraction, channelId: string) { + async execute(interaction: ChatInputCommandInteraction | ButtonInteraction | AnySelectMenuInteraction, channelId: string) { if (!interaction.deferred && !interaction.replied) await interaction.deferReply(); const db = getDb(); @@ -149,7 +146,7 @@ export = { .setEmoji(emoji.icons.disconnect); await component.reply({ content: 'Channel has been reconnected!', ephemeral: true }); - component.message.edit({ + interaction.editReply({ components: [customizeMenu, setupActionButtons], }); break; @@ -167,7 +164,7 @@ export = { logger.info(`${interaction.guild?.name} (${interaction.guildId}) has disconnected from the network.`); await component.reply({ content: 'Disconnected!', ephemeral: true }); - component.message.edit({ + interaction.editReply({ components: [customizeMenu, setupActionButtons], }); break; @@ -176,7 +173,7 @@ export = { break; } component.replied || component.deferred - ? component.message.edit({ embeds: [await setupEmbed(interaction, channelId)] }) + ? interaction.editReply({ embeds: [await setupEmbed(interaction, channelId)] }) : component.update({ embeds: [await setupEmbed(interaction, channelId)] }); }); @@ -257,16 +254,11 @@ export = { }); } - await db.connectedList.update({ - where: { channelId: updConnection.channelId }, - data: { channelId: selected?.values[0] }, - }); - await db.connectedList.update({ where: { channelId: updConnection.channelId }, data: { channelId: channel?.id, - webhook: webhook ? { id: webhook.id, token: `${webhook.token}`, url: webhook.url } : null, + webhook: webhook ? { id: webhook.id, token: `${webhook.token}`, url: webhook.url } : { unset: true }, }, });