From 59c6ab353768d422d76b33b6f71f8f7472f0712f Mon Sep 17 00:00:00 2001 From: Isaac Date: Tue, 18 Oct 2022 16:39:03 +0100 Subject: [PATCH] Make `/tag` and auto tag replies work (very cool) --- src/autocomplete/tag.js | 25 +++++++++++++++++++++- src/commands/slash/tag.js | 28 +++++++++++++++++++++++-- src/listeners/client/messageCreate.js | 30 ++++++++++++++++++++------- 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/autocomplete/tag.js b/src/autocomplete/tag.js index 126bd78..b75a76d 100644 --- a/src/autocomplete/tag.js +++ b/src/autocomplete/tag.js @@ -8,5 +8,28 @@ module.exports = class TagCompleter 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; + + const tags = await client.prisma.tag.findMany({ where: { guildId: interaction.guild.id } }); + const options = value ? tags.filter(tag => + tag.name.match(new RegExp(value, 'i')) || + tag.content.match(new RegExp(value, 'i')) || + tag.regex?.match(new RegExp(value, 'i')), + ) : tags; + await interaction.respond( + options + .slice(0, 25) + .map(tag => ({ + name: tag.name, + value: tag.id, + })), + ); + } }; \ No newline at end of file diff --git a/src/commands/slash/tag.js b/src/commands/slash/tag.js index b6f0a05..dd4b655 100644 --- a/src/commands/slash/tag.js +++ b/src/commands/slash/tag.js @@ -1,5 +1,6 @@ const { SlashCommand } = require('@eartharoid/dbf'); const { ApplicationCommandOptionType } = require('discord.js'); +const ExtendedEmbedBuilder = require('../../lib/embed'); module.exports = class TagSlashCommand extends SlashCommand { constructor(client, options) { @@ -14,7 +15,7 @@ module.exports = class TagSlashCommand extends SlashCommand { autocomplete: true, name: 'tag', required: true, - type: ApplicationCommandOptionType.String, + type: ApplicationCommandOptionType.Integer, }, { name: 'for', @@ -48,5 +49,28 @@ module.exports = class TagSlashCommand extends SlashCommand { }); } - async run(interaction) { } + /** + * @param {import("discord.js").ChatInputCommandInteraction} interaction + */ + async run(interaction) { + /** @type {import("client")} */ + const client = this.client; + + const user = interaction.options.getUser('for', false); + await interaction.deferReply({ ephemeral: !user }); + const tag = await client.prisma.tag.findUnique({ + include: { guild: true }, + where: { id: interaction.options.getInteger('tag', true) }, + }); + + await interaction.editReply({ + allowedMentions: { users: user ? [user.id]: [] }, + content: user?.toString(), + embeds: [ + new ExtendedEmbedBuilder() + .setColor(tag.guild.primaryColour) + .setDescription(tag.content), + ], + }); + } }; \ No newline at end of file diff --git a/src/listeners/client/messageCreate.js b/src/listeners/client/messageCreate.js index 00aa22a..842def2 100644 --- a/src/listeners/client/messageCreate.js +++ b/src/listeners/client/messageCreate.js @@ -179,13 +179,11 @@ module.exports = class extends Listener { }); } } else { - let ticket = await client.prisma.ticket.findUnique({ - include: { guild: true }, - where: { id: message.channel.id }, - }); + const settings = await client.prisma.guild.findUnique({ where: { id:message.guild.id } }); + let ticket = await client.prisma.ticket.findUnique({ where: { id: message.channel.id } }); if (ticket) { - if (ticket.guild.archive) { + if (settings.archive) { try { await client.tickets.archiver.saveMessage(ticket.id, message); } catch (error) { @@ -218,8 +216,26 @@ module.exports = class extends Listener { // TODO: if (!message.author.bot) staff status alert, working hours alerts } - // TODO: if (!message.author.bot) auto tag - + if (!message.author.bot) { + const enabled = + (settings.autoTag === 'all') || + (settings.autoTag === 'ticket' && ticket) || + (settings.autoTag === '!ticket' && !ticket) || + (settings.autoTag.includes(message.channel.id)); + if (enabled) { + const tags = await client.prisma.tag.findMany({ where: { guildId: message.guild.id } }); + const tag = tags.find(tag => message.content.match(new RegExp(tag.regex, 'mi'))); + if (tag) { + await message.reply({ + embeds: [ + new EmbedBuilder() + .setColor(settings.primaryColour) + .setDescription(tag.content), + ], + }); + } + } + } } } };