Add basic moderation tools
This commit is contained in:
parent
ad17e07f35
commit
b0200446b2
|
@ -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!",
|
||||
|
|
53
src/commands/confessban.ts
Normal file
53
src/commands/confessban.ts
Normal 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
|
||||
});
|
||||
}
|
49
src/commands/confesspardon.ts
Normal file
49
src/commands/confesspardon.ts
Normal 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
|
||||
});
|
||||
}
|
|
@ -16,12 +16,16 @@
|
|||
* 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 setup from "./setup";
|
||||
import * as confess from "./confess";
|
||||
|
||||
export const commands = {
|
||||
confess,
|
||||
confessban,
|
||||
confesspardon,
|
||||
ping,
|
||||
setup,
|
||||
confess,
|
||||
};
|
||||
|
|
|
@ -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!)) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
export interface Confession {
|
||||
id: string;
|
||||
messageId: string;
|
||||
author: string;
|
||||
authorId: string;
|
||||
content: string;
|
||||
|
|
Loading…
Reference in a new issue