From ed78fba214554dc7a54c8af4990a7019c0187c94 Mon Sep 17 00:00:00 2001 From: JonatanMGit Date: Thu, 12 Oct 2023 00:55:43 +0200 Subject: [PATCH] feat: Allow hiding the /say command author For moderation reasons this is still logged and viewable for admins --- bot/src/commands/author.ts | 92 +++++++++++++++++++++++++++++++++++--- bot/src/commands/say.ts | 18 +++++--- bot/src/db.ts | 8 +++- bot/src/index.ts | 1 + 4 files changed, 105 insertions(+), 14 deletions(-) diff --git a/bot/src/commands/author.ts b/bot/src/commands/author.ts index f0d0df1..9e4ede4 100644 --- a/bot/src/commands/author.ts +++ b/bot/src/commands/author.ts @@ -1,6 +1,8 @@ -import { ContextMenuCommandBuilder, ApplicationCommandType, MessageContextMenuCommandInteraction } from "discord.js"; +import { ContextMenuCommandBuilder, ApplicationCommandType, MessageContextMenuCommandInteraction, ButtonBuilder, ButtonStyle, ActionRowBuilder, ButtonInteraction, } from "discord.js"; import { getSaid } from "../db"; import client from ".."; +import { IGNORED_IDs } from ".."; + module.exports = { @@ -14,17 +16,93 @@ module.exports = { return; } + getSaid(interaction.targetMessage).then((said) => { - if (said) { + if (said && !said.dataValues.hidden) { interaction.targetMessage.reply("Author: <@" + said.dataValues.user_id + ">"); interaction.deferReply({ ephemeral: true }); interaction.deleteReply(); - } - else { - interaction.targetMessage.reply("No author found! It could be the real Mr Sweet!"); + } else if (said && said.dataValues.hidden) { + + // if user is in allowed ids ask with a button if they want to reveal the author + // if they click yes reveal the author + // if they click delete the prompt + if (IGNORED_IDs.includes(interaction.user.id)) { + interaction.deferReply({ ephemeral: true }); + + const button = new ButtonBuilder() + .setCustomId("reveal") + .setLabel("Reveal") + .setStyle(ButtonStyle.Success); + const button2 = new ButtonBuilder() + .setCustomId("ignore") + .setLabel("Ignore") + .setStyle(ButtonStyle.Danger); + + const row = new ActionRowBuilder() + .addComponents(button, button2); + + + // send dm to user with the message + + const member = interaction.guild.members.cache.get(interaction.user.id); + + if (member) { + const message = member.send({ + content: `Are you sure you want to reveal the author of this message?`, + //@ts-ignore + components: [row], + }); + + const collectorFilter = (i: ButtonInteraction) => { + return (i.user.id === interaction.user.id && (i.customId === "reveal" || i.customId === "ignore")); + }; + const collector = message.then((message) => { + return message.createMessageComponentCollector({ + filter: collectorFilter, + time: 60000, + }); + } + ); + + collector.then((collector) => { + //console.log("collector created"); + collector.on('collect', (i: ButtonInteraction) => { + //console.log("button clicked"); + if (i.customId === "reveal") { + interaction.targetMessage.reply("Author: <@" + said.dataValues.user_id + ">"); + i.update({ content: "You chose to reveal the author!", components: [] }); + } + if (i.customId === "ignore") { + i.update({ content: "You chose to not reveal the author!", components: [] }); + } + }); + + collector.on('end', (collected, reason) => { + if (reason === 'time') { + console.log("collector timed out"); + message.then((message) => { + message.delete(); + }); + } + interaction.deleteReply(); + }); + }); + } + } else { + const member = interaction.guild.members.cache.get(interaction.user.id); + if (member) { + member.send("https://miarecki.eu/img/BLEHHHHH_Cat.jpg"); + } + interaction.deferReply({ ephemeral: true }); + interaction.deleteReply(); + } + } else { + interaction.targetMessage.reply("This was sent by me!"); interaction.deferReply({ ephemeral: true }); interaction.deleteReply(); } - }); + } + ); } -}; +} diff --git a/bot/src/commands/say.ts b/bot/src/commands/say.ts index 6caad4a..c9aa456 100644 --- a/bot/src/commands/say.ts +++ b/bot/src/commands/say.ts @@ -1,9 +1,7 @@ import { SlashCommandBuilder } from '@discordjs/builders'; import { ChatInputCommandInteraction, MessageCreateOptions } from 'discord.js'; import { saveSaid } from '../db'; - - -const IGNORED_IDs = process.env.IGNORED_IDs?.split(',') || []; +import { IGNORED_IDs } from ".."; module.exports = { global: true, @@ -22,6 +20,10 @@ module.exports = { .addStringOption(option => option.setName('reply') .setDescription('The message id to reply to') + .setRequired(false)) + .addBooleanOption(option => + option.setName('hidden') + .setDescription('Whether you should be publicly credited for the message using the author command') .setRequired(false)), async execute(interaction: ChatInputCommandInteraction) { const input = interaction.options.getString('input'); @@ -31,6 +33,13 @@ module.exports = { // defer reply to allow for file upload if it takes longer than 3 seconds await interaction.deferReply({ ephemeral: true }); + let ishidden = interaction.options.getBoolean('hidden') + // invert the hidden selection if the user is authorized (IGNORED_IDs) + if (IGNORED_IDs.includes(interaction.user.id)) { + ishidden = !ishidden; + } + + // check if message begins with Author: abd then deny it if (input && input.includes('Author:')) { interaction.editReply("Acces denied as you are trying to impersonate someone!"); @@ -73,8 +82,7 @@ module.exports = { const sentMessage = await interaction.channel.send({ ...message, allowedMentions: { parse: [] } }); // save the message to the database - if (!IGNORED_IDs.includes(interaction.user.id)) - await saveSaid(sentMessage, interaction.user); + await saveSaid(sentMessage, interaction.user, ishidden); // finish the interaction await interaction.deleteReply(); diff --git a/bot/src/db.ts b/bot/src/db.ts index 2ec2e47..8f2e140 100644 --- a/bot/src/db.ts +++ b/bot/src/db.ts @@ -97,17 +97,21 @@ export class said extends Model { @Column user_id: string; + + @Column + hidden: boolean; } // register models sequelize.addModels([User, Guild, Settings, Messages, said]); // said functions -export const saveSaid = async (message: Message, user: DiscordUser) => { +export const saveSaid = async (message: Message, user: DiscordUser, hidden: boolean = false) => { sequelize.sync(); const saidMessage = new said({ message_id: message.id, - user_id: user.id + user_id: user.id, + hidden: hidden }); saidMessage.save() .catch(err => { diff --git a/bot/src/index.ts b/bot/src/index.ts index 447d992..8b06a71 100644 --- a/bot/src/index.ts +++ b/bot/src/index.ts @@ -3,6 +3,7 @@ require('dotenv').config(); import { prepareGlobalCommands, loadCommands, registerCommands } from './commandUtils'; import handleEvents from './eventHandler'; +export const IGNORED_IDs = process.env.IGNORED_IDs?.split(',') || []; export interface CustomClient extends Client { commands: Collection }