From 038538d73a3473d1de3eac99e744a1d3ab9f02fc Mon Sep 17 00:00:00 2001 From: Dawid Jaworski Date: Fri, 10 May 2024 17:23:40 +0200 Subject: [PATCH 1/3] Add user command to create thread in support channel // These changes need to be checked before merging --- commands/mod/settings/help.js | 33 ++++++++++++ commands/standard/redirecthelp.js | 90 +++++++++++++++++++++++++++++++ models/ServerSettings.js | 6 ++- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 commands/mod/settings/help.js create mode 100644 commands/standard/redirecthelp.js diff --git a/commands/mod/settings/help.js b/commands/mod/settings/help.js new file mode 100644 index 0000000..b6ad1aa --- /dev/null +++ b/commands/mod/settings/help.js @@ -0,0 +1,33 @@ +import { SlashCommandSubcommandBuilder } from 'discord.js'; +import { errorEmbed, successEmbed } from '../../../utils/embeds.js'; +import ServerSettings from '../../../models/ServerSettings.js'; + +export default { + ...new SlashCommandSubcommandBuilder() + .setName('help_category') + .setDescription('Pozwala na ustawienie kategorii pomocy') + .addChannelOption(string => + string.setName('category') + .setDescription('Wybierz kategorię pomocy') + .addChannelTypes(4) + .setRequired(true), + ) + .toJSON(), + async execute(interaction) { + const category = interaction.options.getChannel('help_category'); + + let serverSettings = await ServerSettings.findOne({ server_id: interaction.guild?.id }); + + if (!serverSettings) { + serverSettings = new ServerSettings({ server_id: interaction.guild?.id, help_category: category.id }); + } else { + serverSettings.help_category = category.id; + } + + await serverSettings.save((e) => { + if (!e) return interaction.reply({ embeds: [successEmbed('Pomyślnie zmodyfikowano kategorię z kanałami pomocy.')] }).catch(console.error); + return interaction.reply({ embeds: [errorEmbed('Wystąpił nieznany błąd podczas modyfikacji kategorii z kanałami pomocy')] }).catch(console.error); + }); + + }, +}; diff --git a/commands/standard/redirecthelp.js b/commands/standard/redirecthelp.js new file mode 100644 index 0000000..d6c1991 --- /dev/null +++ b/commands/standard/redirecthelp.js @@ -0,0 +1,90 @@ +import { + ActionRowBuilder, + ContextMenuCommandBuilder, + StringSelectMenuBuilder, +} from 'discord.js'; +import { ChannelType } from 'discord-api-types/v9'; +import ServerSettings from '../../models/ServerSettings.js'; + +export default { + ...new ContextMenuCommandBuilder() + .setName('Utwórz wątek na kanałach pomocy') + .setType(3) + .setDMPermission(false) + .toJSON(), + async execute(interaction) { + const message = interaction.options?.get('message')?.message; + + if (interaction.channel.isThread()) { + return interaction.reply({ + content: 'Ta komenda może być użyta tylko na kanale', + ephemeral: true, + }); + } + + if (message.author.id === interaction.user.id) { + return interaction.reply({ + content: 'Nie możesz utworzyć prywatnego wątku dla siebie', + ephemeral: true, + }); + } + + if (message.author.bot) { + return interaction.reply({ + content: 'Nie możesz utworzyć prywatnego wątku dla bota', + ephemeral: true, + }); + } + + const settings = await ServerSettings.find({ server_id: interaction.guild.id }).exec(); + + const supportChannels = interaction.guild.channels.cache.get(settings.help_category).parent.children.filter(channel => channel.type === ChannelType.GuildForum); + + if (!supportChannels.size) { + return interaction.reply({ + content: 'Nie znaleziono kanałów pomocy', + ephemeral: true, + }); + } + + const channelActionRow = new ActionRowBuilder() + .addComponents( + new StringSelectMenuBuilder() + .setCustomId('channel') + .setPlaceholder('Wybierz kanał') + .addOptions( + supportChannels.map(channel => ({ + label: channel.name, + value: channel.id, + })), + ), + ); + + const reply = await interaction.reply({ + content: 'Wybierz kanał na który chcesz przekierować osobę', + components: [channelActionRow], + ephemeral: true, + }); + + const colletor = reply.createMessageComponentCollector({ time: 60000 }); + + colletor.on('collect', async (int) => { + const channel = int.guild.channels.cache.get(int.values[0]); + + const thread = await channel.threads.create({ + name: `${message.author.username}`, + autoArchiveDuration: 1440, + reason: `${interaction.user.username} przekierował ${message.author.username} na kanał ${channel.name}`, + }); + + await thread.members.add(message.author.id); + await thread.members.add(interaction.user.id); + + await interaction.reply({ + content: `Pomyślnie utworzono wątek dla ${message.author.username} na kanale ${channel.name}`, + ephemeral: true, + }); + }); + + }, +}; diff --git a/models/ServerSettings.js b/models/ServerSettings.js index 691e72b..6cad352 100644 --- a/models/ServerSettings.js +++ b/models/ServerSettings.js @@ -7,6 +7,10 @@ const ServerSettingsSchema = Schema({ type: String, require: true, }, + help_category: { + type: String, + require: true, + }, anty_invite_enabled: { type: Boolean, require: true, @@ -31,4 +35,4 @@ const ServerSettingsSchema = Schema({ const ServerSettings = model('ServerSettings', ServerSettingsSchema); -export default ServerSettings; \ No newline at end of file +export default ServerSettings; From c900b4c4829b2be3fa8f080f62097e24744dedf5 Mon Sep 17 00:00:00 2001 From: Dawid Jaworski Date: Sat, 11 May 2024 13:21:55 +0200 Subject: [PATCH 2/3] Remove typo, add 'end' event on collector to remove message after time --- commands/standard/redirecthelp.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/commands/standard/redirecthelp.js b/commands/standard/redirecthelp.js index d6c1991..3e260d1 100644 --- a/commands/standard/redirecthelp.js +++ b/commands/standard/redirecthelp.js @@ -66,9 +66,9 @@ export default { ephemeral: true, }); - const colletor = reply.createMessageComponentCollector({ time: 60000 }); + const collector = reply.createMessageComponentCollector({ time: 60000 }); - colletor.on('collect', async (int) => { + collector.on('collect', async (int) => { const channel = int.guild.channels.cache.get(int.values[0]); const thread = await channel.threads.create({ @@ -86,5 +86,12 @@ export default { }); }); + collector.on('end', () => { + reply.edit({ + content: 'Czas na wybór kanału minął', + components: [], + }); + }); + }, }; From eff692fbe54fa7a56941a300d3ab6a7501c0eb25 Mon Sep 17 00:00:00 2001 From: Dawid Jaworski Date: Sat, 11 May 2024 13:25:23 +0200 Subject: [PATCH 3/3] Use defaultAutoArchiveDuration instead of hardcoded time --- commands/standard/redirecthelp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/standard/redirecthelp.js b/commands/standard/redirecthelp.js index 3e260d1..83cda9b 100644 --- a/commands/standard/redirecthelp.js +++ b/commands/standard/redirecthelp.js @@ -73,7 +73,7 @@ export default { const thread = await channel.threads.create({ name: `${message.author.username}`, - autoArchiveDuration: 1440, + autoArchiveDuration: channel.defaultAutoArchiveDuration, reason: `${interaction.user.username} przekierował ${message.author.username} na kanał ${channel.name}`, });