Skip to content

Commit

Permalink
feat: Allow hiding the /say command author
Browse files Browse the repository at this point in the history
For moderation reasons this is still logged and viewable for admins
  • Loading branch information
JonatanMGit committed Oct 11, 2023
1 parent 056af69 commit ed78fba
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 14 deletions.
92 changes: 85 additions & 7 deletions bot/src/commands/author.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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();
}
});
}
);
}
};
}
18 changes: 13 additions & 5 deletions bot/src/commands/say.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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');
Expand All @@ -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!");
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions bot/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
1 change: 1 addition & 0 deletions bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>
}
Expand Down

0 comments on commit ed78fba

Please sign in to comment.