From ff5882730d4092f6e20ca12071473c7350bd278e Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Mon, 12 Sep 2022 15:01:26 +0800 Subject: [PATCH] feat: add tag mentions and deletions --- src/_reupload.ts | 6 ++++++ src/commands/tags.ts | 6 +++++- src/index.ts | 28 +++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/_reupload.ts b/src/_reupload.ts index 4d7fba7..a352fcd 100644 --- a/src/_reupload.ts +++ b/src/_reupload.ts @@ -27,6 +27,12 @@ export const reuploadCommands = async () => { .setDescription('The tag name') .setRequired(true) .addChoices(...tags.map((b) => ({ name: b.name, value: b.name }))) + ) + .addUserOption((option) => + option + .setName('user') + .setDescription('The user to mention') + .setRequired(false) ), new SlashCommandBuilder() .setName('modrinth') diff --git a/src/commands/tags.ts b/src/commands/tags.ts index 98304e2..9ba8b16 100644 --- a/src/commands/tags.ts +++ b/src/commands/tags.ts @@ -10,6 +10,8 @@ export const tagsCommand = async ( ) => { const tags = await getTags(); const tagName = i.options.getString('name', true); + const mention = i.options.getUser('user', false); + const tag = tags.find( (tag) => tag.name === tagName || tag.aliases?.includes(tagName) ); @@ -23,7 +25,9 @@ export const tagsCommand = async ( } await i.reply({ - content: tag.text ? `**${tag.name}**\n\n` + tag.text : tag.text, + content: + (mention ? `<@${mention.id}> ` : '') + + (tag.text ? `**${tag.name}**\n\n` + tag.text : ''), embeds: tag.embed ? [ new EmbedBuilder(tag.embed).setFooter({ diff --git a/src/index.ts b/src/index.ts index b3565c2..1421396 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,10 @@ -import { Client, GatewayIntentBits, Partials, OAuth2Scopes } from 'discord.js'; +import { + Client, + GatewayIntentBits, + Partials, + OAuth2Scopes, + InteractionType, +} from 'discord.js'; import { reuploadCommands } from './_reupload'; import * as BuildConfig from './constants'; @@ -125,6 +131,26 @@ client.on('interactionCreate', async (interaction) => { } }); +client.on('messageReactionAdd', async (reaction, user) => { + if (reaction.partial) { + try { + await reaction.fetch(); + } catch (error) { + console.error('Something went wrong when fetching the message:', error); + return; + } + } + + if ( + reaction.message.interaction && + reaction.message.interaction?.type === InteractionType.ApplicationCommand && + reaction.message.interaction?.user === user && + reaction.emoji.name === '❌' + ) { + await reaction.message.delete(); + } +}); + reuploadCommands() .then(() => { client.login(process.env.DISCORD_TOKEN);