Skip to content

Commit

Permalink
fix: Moved /network delete to /hub leave
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed May 24, 2023
1 parent cac6501 commit c2002ad
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 47 deletions.
40 changes: 38 additions & 2 deletions src/Commands/Main/hub.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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: {
Expand All @@ -238,7 +256,6 @@ export default {

}
else if (subcommand === 'manage' || subcommand === 'networks') {
const focusedValue = interaction.options.getFocused();
hubChoices = await getDb().hubs.findMany({
where: {
name: {
Expand All @@ -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;
},
Expand Down
20 changes: 1 addition & 19 deletions src/Commands/Main/network.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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.')
Expand Down Expand Up @@ -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();
Expand Down
44 changes: 18 additions & 26 deletions src/Scripts/network/displaySettings.ts
Original file line number Diff line number Diff line change
@@ -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())
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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)] });

});
Expand Down Expand Up @@ -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 },
},
});

Expand Down

0 comments on commit c2002ad

Please sign in to comment.