From a95330853203b1b035bd9cb66aaa48b3efa11b9f Mon Sep 17 00:00:00 2001 From: Isaac Date: Fri, 10 Feb 2023 14:47:13 +0000 Subject: [PATCH] fix: make `/move` edit the channel name and permission overwrites --- src/commands/slash/move.js | 43 ++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/commands/slash/move.js b/src/commands/slash/move.js index b5437a2..4083ff6 100644 --- a/src/commands/slash/move.js +++ b/src/commands/slash/move.js @@ -62,6 +62,7 @@ module.exports = class MoveSlashCommand extends SlashCommand { }); } + const creator = await interaction.guild.members.fetch(ticket.createdById); const newCategory = await client.prisma.category.findUnique({ where: { id: interaction.options.getInteger('category', true) } }); const discordCategory = await interaction.guild.channels.fetch(newCategory.discordCategory); const getMessage = client.i18n.getLocale(ticket.guild.locale); @@ -80,6 +81,7 @@ module.exports = class MoveSlashCommand extends SlashCommand { ephemeral: true, }); } else { + // don't reassign `ticket`, the previous value is used below await client.prisma.ticket.update({ data: { category: { connect: { id: newCategory.id } } }, where: { id: ticket.id }, @@ -97,10 +99,43 @@ module.exports = class MoveSlashCommand extends SlashCommand { $newCategory[ticket.createdById] ||= 0; $newCategory[ticket.createdById]++; - await interaction.channel.setParent(discordCategory, { - lockPermissions: false, - reason: `Moved by ${interaction.user.tag}`, - }); + // these 3 could be done separately, + // but using `setParent`, `setName` etc instead of a single `edit` call increases the number of API requests + if ( + newCategory.staffRoles !== ticket.category.staffRoles || + newCategory.channelName !== ticket.category.channelName || + newCategory.discordCategory !== ticket.category.discordCategory + ) { + const allow = ['ViewChannel', 'ReadMessageHistory', 'SendMessages', 'EmbedLinks', 'AttachFiles']; + const channelName = newCategory.channelName + .replace(/{+\s?(user)?name\s?}+/gi, creator.user.username) + .replace(/{+\s?(nick|display)(name)?\s?}+/gi, creator.displayName) + .replace(/{+\s?num(ber)?\s?}+/gi, ticket.number === 1488 ? '1487b' : ticket.number); + await interaction.channel.edit({ + lockPermissions: false, + name: channelName, + parent: discordCategory, + permissionOverwrites: [ + { + deny: ['ViewChannel'], + id: interaction.guild.roles.everyone, + }, + { + allow, + id: this.client.user.id, + }, + { + allow, + id: creator.id, + }, + ...newCategory.staffRoles.map(id => ({ + allow, + id, + })), + ], + reason: `Moved by ${interaction.user.tag}`, + }); + } await interaction.editReply({ embeds: [