From 81a011802a57c94a23aa648f4c70a99d858f45ed Mon Sep 17 00:00:00 2001 From: Rafael Oliveira <56204853+RafDevX@users.noreply.github.com> Date: Sat, 18 Sep 2021 15:43:45 +0100 Subject: [PATCH] Say logs (#70) --- src/modules/misc.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/modules/misc.ts b/src/modules/misc.ts index d3764b4..473ef84 100644 --- a/src/modules/misc.ts +++ b/src/modules/misc.ts @@ -8,6 +8,7 @@ import * as Builders from "@discordjs/builders"; import { CommandDescriptor } from "../bot.d"; import { CommandPermission } from "../bot"; import * as utils from "./utils"; +import { SlashCommandBuilder } from "@discordjs/builders"; export function provideCommands(): CommandDescriptor[] { const say = new Builders.SlashCommandBuilder() @@ -33,6 +34,15 @@ export function provideCommands(): CommandDescriptor[] { ) .setRequired(false) ); + const whoSaid = new SlashCommandBuilder() + .setName("who-said") + .setDescription("Shows who ordered the bot to say something"); + whoSaid.addStringOption( + new Builders.SlashCommandStringOption() + .setName("message-id") + .setDescription("Message ID in question") + .setRequired(true) + ); return [ { builder: new Builders.SlashCommandBuilder() @@ -45,6 +55,10 @@ export function provideCommands(): CommandDescriptor[] { builder: say, handler: handleSayCommand, }, + { + builder: whoSaid, + handler: handleWhoSaidCommand, + }, ]; } @@ -92,6 +106,8 @@ export async function handleAboutCommand( }); } +const sayLogs: Discord.Collection = new Discord.Collection(); + export async function handleSayCommand( interaction: Discord.CommandInteraction ): Promise { @@ -103,10 +119,21 @@ export async function handleSayCommand( interaction.options.getBoolean("allow-mentions", false) ?? false; if (channel && channel.isText()) { - await channel.send({ + const msg = await channel.send({ content: message.replace(/\\n/g, "\n"), allowedMentions: allowMentions ? undefined : { parse: [] }, }); + const uid = interaction.member?.user.id; + if (uid) { + sayLogs.set(msg.id, uid); + console.log( + `User ${ + interaction.member?.user.username + } said «${message}» (w/${ + allowMentions ? "" : "o" + } mentions)` + ); + } await interaction.editReply("✅ Successfully sent message."); return; } @@ -115,3 +142,23 @@ export async function handleSayCommand( await interaction.editReply("❌ Something went wrong."); } } + +export async function handleWhoSaidCommand( + interaction: Discord.CommandInteraction +): Promise { + try { + const messageId = interaction.options.getString("message-id", true); + const split = messageId.split("-"); + const who = sayLogs.get(split[split.length - 1]); + + if (who) { + await interaction.editReply(`<@${who}> said it!`); + } else { + await interaction.editReply("I don't know who said it..."); + } + } catch (e) { + await interaction.editReply( + "❌ Something went wrong, maybe wrong message ID?" + ); + } +}