From b0200446b2f490a467fe46f2b5ba9ce4b384a70f Mon Sep 17 00:00:00 2001 From: powermaker450 Date: Sat, 12 Oct 2024 15:26:28 -0400 Subject: [PATCH] Add basic moderation tools --- src/commands/confess.ts | 12 +++- src/commands/confessban.ts | 53 +++++++++++++++++ src/commands/confesspardon.ts | 49 ++++++++++++++++ src/commands/index.ts | 8 ++- src/commands/setup.ts | 4 +- src/storeman/client.ts | 107 ++++++++++++++++++++++++++++++---- src/storeman/types.ts | 1 + 7 files changed, 219 insertions(+), 15 deletions(-) create mode 100644 src/commands/confessban.ts create mode 100644 src/commands/confesspardon.ts diff --git a/src/commands/confess.ts b/src/commands/confess.ts index e643c50..50c7138 100644 --- a/src/commands/confess.ts +++ b/src/commands/confess.ts @@ -28,7 +28,7 @@ import { StoreMan } from "../storeman"; export const data = new SlashCommandBuilder() .setName("confess") .setDescription("Send a confession") - .addStringOption((option) => + .addStringOption(option => option .setName("message") .setRequired(true) @@ -36,6 +36,14 @@ export const data = new SlashCommandBuilder() ); export async function execute(interaction: CommandInteraction) { + + if (dt.isBanned(interaction.guild?.id!, interaction.user.id)) { + return interaction.reply({ + content: "You are banned from confessions in this server!", + ephemeral: true + }); + } + if (!dt.getGuildInfo(interaction.guild?.id!)) { return interaction.reply({ content: @@ -64,7 +72,7 @@ export async function execute(interaction: CommandInteraction) { `# Confession ${messageId}:\n### Author: ${interaction.user.displayName}\n### Author ID: ${interaction.user.id}\n${interaction.options.getString("message")}`, ); - dt.addConfession(message, messageId, interaction.user.displayName); + dt.addConfession(message, messageId, interaction.user.displayName, interaction.user.id); return interaction.reply({ content: "Confession sent!", diff --git a/src/commands/confessban.ts b/src/commands/confessban.ts new file mode 100644 index 0000000..c3ca3b0 --- /dev/null +++ b/src/commands/confessban.ts @@ -0,0 +1,53 @@ +/* + * Confoss: Anonymous confessions for Discord, free as in freedom and price! + * Copyright (C) 2024 powermaker450 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . +*/ + +import { CommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js"; +import { dt } from "../main"; +import Logger from "../utils/Logger"; + +export const data = new SlashCommandBuilder() + .setName("confessban") + .setDescription("Ban a user from submitting confessions.") + .addStringOption(option => + option + .setName("id") + .setDescription("The confession ID to ban") + .setRequired(true) + ) + .setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers); + +export async function execute(interaction: CommandInteraction) { + // @ts-ignore + Logger.log(interaction.options.getString("id")); + + const result = dt.addBan( + interaction.guild?.id!, + // @ts-ignore + interaction.options.getString("id") + ); + + return result + ? interaction.reply({ + content: "User was banned.", + ephemeral: true + }) + : interaction.reply({ + content: "No confession with that ID was found.", + ephemeral: true + }); +} diff --git a/src/commands/confesspardon.ts b/src/commands/confesspardon.ts new file mode 100644 index 0000000..202125f --- /dev/null +++ b/src/commands/confesspardon.ts @@ -0,0 +1,49 @@ +/* + * Confoss: Anonymous confessions for Discord, free as in freedom and price! + * Copyright (C) 2024 powermaker450 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . +*/ + +import { CommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js"; +import { dt } from "../main"; + +export const data = new SlashCommandBuilder() + .setName("confesspardon") + .setDescription("Unbans a user from confessions") + .addStringOption(option => + option + .setName("id") + .setDescription("The confession ID to unban") + .setRequired(true) + ) + .setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages) + +export function execute(interaction: CommandInteraction) { + const result = dt.removeBan( + interaction.guild?.id!, + // @ts-ignore + interaction.options.getString("id") + ); + + return result + ? interaction.reply({ + content: "User was unbanned.", + ephemeral: true + }) + : interaction.reply({ + content: "No confession with that ID was found.", + ephemeral: true + }); +} diff --git a/src/commands/index.ts b/src/commands/index.ts index 6e0cc1c..b620607 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -16,12 +16,16 @@ * along with this program. If not, see . */ +import * as confess from "./confess"; +import * as confessban from "./confessban"; +import * as confesspardon from "./confesspardon"; import * as ping from "./ping"; import * as setup from "./setup"; -import * as confess from "./confess"; export const commands = { + confess, + confessban, + confesspardon, ping, setup, - confess, }; diff --git a/src/commands/setup.ts b/src/commands/setup.ts index ff03763..548ac01 100644 --- a/src/commands/setup.ts +++ b/src/commands/setup.ts @@ -22,6 +22,7 @@ import { ChannelType, CommandInteraction, ComponentType, + PermissionFlagsBits, SlashCommandBuilder, } from "discord.js"; import { dt } from "../main"; @@ -29,7 +30,8 @@ import Logger from "../utils/Logger"; export const data = new SlashCommandBuilder() .setName("setup") - .setDescription("Setup the bot."); + .setDescription("Setup the bot.") + .setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild); export async function execute(interaction: CommandInteraction) { if (dt.checkSetup(interaction.guild?.id!)) { diff --git a/src/storeman/client.ts b/src/storeman/client.ts index ec60eb8..f126703 100644 --- a/src/storeman/client.ts +++ b/src/storeman/client.ts @@ -37,11 +37,13 @@ export class StoreMan { message: Message, id: string, author: string, + authorId: string, ): Confession { return { id: id, + messageId: message.id, author: author, - authorId: message.author.id, + authorId: authorId, content: message.content.replace(/(# Confession .{4}:\n)/, ""), }; } @@ -113,14 +115,20 @@ export class StoreMan { return null; } - public addConfession(message: Message, id: string, author: string): void { + // Attempts to add a confession. Returns true if the confession is sent, false if otherwise. + public addConfession(message: Message, id: string, author: string, authorId: string): boolean { const guildId = message.guild?.id; for (const guild of this.data) { if (guild.id === guildId) { - guild.confessions.push(StoreMan.toConfession(message, id, author)); + // If the author's user ID is in the ban list, don't let them post a confession. + if (this.isBanned(guildId, author)) { + return false; + } + + guild.confessions.push(StoreMan.toConfession(message, id, author, authorId)); this.saveFile(); - return; + return true; } } @@ -129,12 +137,91 @@ export class StoreMan { ); } - public delConfesssion(interaction: CommandInteraction): void { - const guildId = interaction.guild?.id; - const userId = interaction.user.id; + public getConfession(guildId: string, confessionId: string): Confession | null { + for (const guild of this.data) { + if (guild.id === guildId) { + for (const confession of guild.confessions) { + if (confession.id === confessionId) { + return confession; + } + } + } + } - // Check if the user is allowed to delete that confesssion - // If so, delete it, else let them know that they can't delete that confesssion, - // or it wasn't found. + return null; + } + + // Attempts to delete a confession. If it is sucessfully deleted, returns true, else false. + public delConfesssion({ guild, user }: CommandInteraction, confessionId: string): boolean { + const guildId = guild?.id; + const userId = user.id; + + for (const guild of this.data) { + if (guild.id === guildId) { + for (const confession of guild.confessions) { + if (confession.authorId === userId) { + guild.confessions = guild.confessions.filter(confession => { + return confession.id !== confessionId; + }); + + return true; + } + } + } + } + + return false; + } + + // Check if a certain user is banned within a guild. + public isBanned(guildId: string, userId: string): boolean { + for (const guild of this.data) { + if (guild.id === guildId) { + for (const ban of guild.settings.bans) { + if (ban === userId) { + return true; + } + } + } + } + + return false; + } + + // Attempts to ban a user from confessions. + public addBan(guildId: string, confessionId: string): boolean { + const confession = this.getConfession(guildId, confessionId); + + for (const guild of this.data) { + if (guild.id === guildId) { + if (confession) { + // Only add the user to the ban list if they aren't banned already + !this.isBanned(guildId, confession.authorId) && guild.settings.bans.push(confession.authorId!); + + this.saveFile(); + return true; + } + } + } + + return false; + } + + // Attempts to pardon a user from a ban. If sucessfully completed, returns true, false if otherwise. + public removeBan(guildId: string, confessionId: string): boolean { + for (const guild of this.data) { + if (guild.id === guildId) { + if (this.getConfession(guildId, confessionId)) { + guild.settings.bans = guild.settings.bans.filter(ban => { + return ban !== this.getConfession(guildId, confessionId)?.authorId!; + }); + + this.saveFile(); + return true; + } + } + } + + return false; } } diff --git a/src/storeman/types.ts b/src/storeman/types.ts index 5dbef22..00d0fc4 100644 --- a/src/storeman/types.ts +++ b/src/storeman/types.ts @@ -18,6 +18,7 @@ export interface Confession { id: string; + messageId: string; author: string; authorId: string; content: string;