Skip to content

Commit

Permalink
feat: add /move command
Browse files Browse the repository at this point in the history
  • Loading branch information
eartharoid committed Oct 31, 2022
1 parent d10965d commit 9f18958
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 16 deletions.
23 changes: 22 additions & 1 deletion src/autocomplete/category.js
Expand Up @@ -8,5 +8,26 @@ module.exports = class CategoryCompleter extends Autocompleter {
});
}

async run(value, comamnd, interaction) { }
/**
* @param {string} value
* @param {*} command
* @param {import("discord.js").AutocompleteInteraction} interaction
*/
async run(value, command, interaction) {
/** @type {import("client")} */
const client = this.client;

let categories = await client.prisma.category.findMany({ where: { guildId: interaction.guild.id } });
const ticket = await client.prisma.ticket.findUnique({ where: { id: interaction.channel.id } });
if (ticket) categories = categories.filter(category => ticket.categoryId !== category.id);
const options = value ? categories.filter(category => category.name.match(new RegExp(value, 'i'))) : categories;
await interaction.respond(
options
.slice(0, 25)
.map(category => ({
name: category.name,
value: category.id,
})),
);
}
};
6 changes: 0 additions & 6 deletions src/commands/slash/close.js
Expand Up @@ -17,12 +17,6 @@ module.exports = class CloseSlashCommand extends SlashCommand {
required: false,
type: ApplicationCommandOptionType.String,
},
{
autocomplete: true,
name: 'ticket',
required: false,
type: ApplicationCommandOptionType.String,
},
].map(option => {
option.descriptionLocalizations = client.i18n.getAllMessages(`commands.slash.${name}.options.${option.name}.description`);
option.description = option.descriptionLocalizations['en-GB'];
Expand Down
89 changes: 87 additions & 2 deletions src/commands/slash/move.js
@@ -1,5 +1,6 @@
const { SlashCommand } = require('@eartharoid/dbf');
const { ApplicationCommandOptionType } = require('discord.js');
const ExtendedEmbedBuilder = require('../../lib/embed');

module.exports = class MoveSlashCommand extends SlashCommand {
constructor(client, options) {
Expand Down Expand Up @@ -27,8 +28,92 @@ module.exports = class MoveSlashCommand extends SlashCommand {
});
}

/**
* @param {import("discord.js").ChatInputCommandInteraction} interaction
*/
async run(interaction) {
// TODO: check discordCategory max but not category max (ignore)
// TODO: update cached count for both categories and category-members (from and to)
/** @type {import("client")} */
const client = this.client;

await interaction.deferReply({ ephemeral: false });

const ticket = await client.prisma.ticket.findUnique({
include: {
category: true,
guild: true,
},
where: { id: interaction.channel.id },
});

if (!ticket) {
const { locale } = await client.prisma.guild.findUnique({ where: { id: interaction.guild.id } });
const getMessage = client.i18n.getLocale(locale);
return await interaction.editReply({
embeds: [
new ExtendedEmbedBuilder({
iconURL: interaction.guild.iconURL(),
text: ticket.guild.footer,
})
.setColor(ticket.guild.errorColour)
.setTitle(getMessage('misc.not_ticket.title'))
.setDescription(getMessage('misc.not_ticket.description')),
],
ephemeral: 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 getMessage = client.i18n.getLocale(ticket.guild.locale);

if (discordCategory.children.cache.size === 50) {
return await interaction.editReply({
embeds: [
new ExtendedEmbedBuilder({
iconURL: interaction.guild.iconURL(),
text: ticket.guild.footer,
})
.setColor(ticket.guild.errorColour)
.setTitle(getMessage('misc.category_full.title'))
.setDescription(getMessage('misc.category_full.description')),
],
ephemeral: true,
});
} else {
await client.prisma.ticket.update({
data: { category: { connect: { id: newCategory.id } } },
where: { id: ticket.id },
});

const $oldCategory = client.tickets.$.categories[ticket.categoryId];
const $newCategory = client.tickets.$.categories[newCategory.id];

$oldCategory.total--;
$oldCategory[ticket.createdById]--;

if (!$newCategory.total) $newCategory.total = 0;
$newCategory.total++;

if (!$newCategory[ticket.createdById]) $newCategory[ticket.createdById] = 0;
$newCategory[ticket.createdById]++;

await interaction.channel.setParent(discordCategory, {
lockPermissions: false,
reason: `Moved by ${interaction.user.tag}`,
});

await interaction.editReply({
embeds: [
new ExtendedEmbedBuilder()
.setColor(ticket.guild.primaryColour)
.setDescription(getMessage('commands.slash.move.moved', {
by: interaction.user.toString(),
from: ticket.category.name,
to: newCategory.name,
})),
],
});

}
}
};
19 changes: 15 additions & 4 deletions src/commands/slash/transfer.js
Expand Up @@ -44,7 +44,7 @@ module.exports = class TransferSlashCommand extends SlashCommand {

let ticket = await client.prisma.ticket.findUnique({ where: { id: interaction.channel.id } });
const from = ticket.createdById;

console.log(1)
ticket = await client.prisma.ticket.update({
data: {
createdBy: {
Expand All @@ -58,19 +58,30 @@ module.exports = class TransferSlashCommand extends SlashCommand {
where: { id: interaction.channel.id },
});

await interaction.channel.setTopic(`${member.toString()}${ticket.topic?.length > 0 ? ` | ${decrypt(ticket.topic)}` : ''}`);

await interaction.editReply({
embeds: [
new EmbedBuilder()
.setColor(ticket.guild.primaryColour)
.setDescription(client.i18n.getMessage(ticket.guild.locale, 'commands.slash.transfer.transferred', {
.setDescription(client.i18n.getMessage(ticket.guild.locale, `commands.slash.transfer.transferred${interaction.member.id !== from ? '_from' : ''}`, {
from: `<@${from}>`,
to: member.toString(),
user: interaction.user.toString(),
})),

],
});

await interaction.channel.setTopic(`${member.toString()}${ticket.topic?.length > 0 ? ` | ${decrypt(ticket.topic)}` : ''}`);

await interaction.channel.permissionOverwrites.edit(
member,
{
AttachFiles: true,
EmbedLinks: true,
ReadMessageHistory: true,
SendMessages: true,
ViewChannel: true,
},
);
}
};
5 changes: 4 additions & 1 deletion src/i18n/en-GB.yml
Expand Up @@ -118,6 +118,7 @@ commands:
title: Help
move:
description: Move a ticket to another category
moved: 🗃️ {by} has moved this ticket from **{from}** to **{to}**.
name: move
options:
category:
Expand Down Expand Up @@ -285,7 +286,9 @@ misc:
description: Please wait {time} before creating another ticket in this category.
title: ❌ Please wait
error:
description: Sorry, an unexpected error occurred.
description: |
Sorry, an unexpected error occurred.
Please give this information to an administrator.
fields:
code: Error code
identifier: Identifier
Expand Down
4 changes: 2 additions & 2 deletions src/lib/tickets/manager.js
Expand Up @@ -804,7 +804,7 @@ module.exports = class TicketManager {
/**
* @param {import("discord.js").ChatInputCommandInteraction|import("discord.js").ButtonInteraction} interaction
*/
async preClose(interaction) {
async requestClose(interaction) {
const ticket = await this.client.prisma.ticket.findUnique({
include: {
category: true,
Expand All @@ -821,7 +821,7 @@ module.exports = class TicketManager {
* @param {boolean} skip
* @param {string} reason
*/
async close(ticketId, skip, reason) {
async final(ticketId, skip, reason) {
// TODO: update cache/cat count
// TODO: update cache/member count
// TODO: set messageCount on ticket
Expand Down

0 comments on commit 9f18958

Please sign in to comment.