Add basic moderation tools

This commit is contained in:
powermaker450 2024-10-12 15:26:28 -04:00
parent ad17e07f35
commit b0200446b2
7 changed files with 219 additions and 15 deletions

View file

@ -28,7 +28,7 @@ import { StoreMan } from "../storeman";
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName("confess") .setName("confess")
.setDescription("Send a confession") .setDescription("Send a confession")
.addStringOption((option) => .addStringOption(option =>
option option
.setName("message") .setName("message")
.setRequired(true) .setRequired(true)
@ -36,6 +36,14 @@ export const data = new SlashCommandBuilder()
); );
export async function execute(interaction: CommandInteraction) { 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!)) { if (!dt.getGuildInfo(interaction.guild?.id!)) {
return interaction.reply({ return interaction.reply({
content: 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")}`, `# 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({ return interaction.reply({
content: "Confession sent!", content: "Confession sent!",

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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
});
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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
});
}

View file

@ -16,12 +16,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import * as confess from "./confess";
import * as confessban from "./confessban";
import * as confesspardon from "./confesspardon";
import * as ping from "./ping"; import * as ping from "./ping";
import * as setup from "./setup"; import * as setup from "./setup";
import * as confess from "./confess";
export const commands = { export const commands = {
confess,
confessban,
confesspardon,
ping, ping,
setup, setup,
confess,
}; };

View file

@ -22,6 +22,7 @@ import {
ChannelType, ChannelType,
CommandInteraction, CommandInteraction,
ComponentType, ComponentType,
PermissionFlagsBits,
SlashCommandBuilder, SlashCommandBuilder,
} from "discord.js"; } from "discord.js";
import { dt } from "../main"; import { dt } from "../main";
@ -29,7 +30,8 @@ import Logger from "../utils/Logger";
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName("setup") .setName("setup")
.setDescription("Setup the bot."); .setDescription("Setup the bot.")
.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild);
export async function execute(interaction: CommandInteraction) { export async function execute(interaction: CommandInteraction) {
if (dt.checkSetup(interaction.guild?.id!)) { if (dt.checkSetup(interaction.guild?.id!)) {

View file

@ -37,11 +37,13 @@ export class StoreMan {
message: Message, message: Message,
id: string, id: string,
author: string, author: string,
authorId: string,
): Confession { ): Confession {
return { return {
id: id, id: id,
messageId: message.id,
author: author, author: author,
authorId: message.author.id, authorId: authorId,
content: message.content.replace(/(# Confession .{4}:\n)/, ""), content: message.content.replace(/(# Confession .{4}:\n)/, ""),
}; };
} }
@ -113,14 +115,20 @@ export class StoreMan {
return null; 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; const guildId = message.guild?.id;
for (const guild of this.data) { for (const guild of this.data) {
if (guild.id === guildId) { 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(); this.saveFile();
return; return true;
} }
} }
@ -129,12 +137,91 @@ export class StoreMan {
); );
} }
public delConfesssion(interaction: CommandInteraction): void { public getConfession(guildId: string, confessionId: string): Confession | null {
const guildId = interaction.guild?.id; for (const guild of this.data) {
const userId = interaction.user.id; 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 return null;
// If so, delete it, else let them know that they can't delete that confesssion, }
// or it wasn't found.
// 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;
} }
} }

View file

@ -18,6 +18,7 @@
export interface Confession { export interface Confession {
id: string; id: string;
messageId: string;
author: string; author: string;
authorId: string; authorId: string;
content: string; content: string;