fix: make /move edit the channel name and permission overwrites

This commit is contained in:
Isaac 2023-02-10 14:47:13 +00:00
parent 3b50135cc0
commit a953308532
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33

View File

@ -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 newCategory = await client.prisma.category.findUnique({ where: { id: interaction.options.getInteger('category', true) } });
const discordCategory = await interaction.guild.channels.fetch(newCategory.discordCategory); const discordCategory = await interaction.guild.channels.fetch(newCategory.discordCategory);
const getMessage = client.i18n.getLocale(ticket.guild.locale); const getMessage = client.i18n.getLocale(ticket.guild.locale);
@ -80,6 +81,7 @@ module.exports = class MoveSlashCommand extends SlashCommand {
ephemeral: true, ephemeral: true,
}); });
} else { } else {
// don't reassign `ticket`, the previous value is used below
await client.prisma.ticket.update({ await client.prisma.ticket.update({
data: { category: { connect: { id: newCategory.id } } }, data: { category: { connect: { id: newCategory.id } } },
where: { id: ticket.id }, where: { id: ticket.id },
@ -97,10 +99,43 @@ module.exports = class MoveSlashCommand extends SlashCommand {
$newCategory[ticket.createdById] ||= 0; $newCategory[ticket.createdById] ||= 0;
$newCategory[ticket.createdById]++; $newCategory[ticket.createdById]++;
await interaction.channel.setParent(discordCategory, { // 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, 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}`, reason: `Moved by ${interaction.user.tag}`,
}); });
}
await interaction.editReply({ await interaction.editReply({
embeds: [ embeds: [