Separate user and confession bans

This commit is contained in:
powermaker450 2024-10-21 12:37:14 -04:00
parent 4564cfb6a2
commit e24b163a34
3 changed files with 102 additions and 21 deletions

View file

@ -18,11 +18,16 @@
import { import {
ChatInputCommandInteraction, ChatInputCommandInteraction,
heading,
HeadingLevel,
inlineCode,
italic,
PermissionFlagsBits, PermissionFlagsBits,
SlashCommandBuilder SlashCommandBuilder
} from "discord.js"; } from "discord.js";
import { dt } from "../main"; import { dt } from "../main";
import Logger from "../utils/Logger"; import Logger from "../utils/Logger";
import { BanReason } from "../storeman";
const logger = new Logger("(/) confessban"); const logger = new Logger("(/) confessban");
@ -33,7 +38,7 @@ export const data = new SlashCommandBuilder()
.addSubcommand(ban => .addSubcommand(ban =>
ban ban
.setName("ban") .setName("ban")
.setDescription("Ban a user from confessions") .setDescription("Ban an ID from confessions")
.addStringOption(option => .addStringOption(option =>
option option
.setName("id") .setName("id")
@ -43,6 +48,14 @@ export const data = new SlashCommandBuilder()
.setRequired(true) .setRequired(true)
) )
) )
.addSubcommand(banuser =>
banuser
.setName("banuser")
.setDescription("Ban a user from confessions")
.addUserOption(user =>
user.setName("user").setDescription("The user to ban").setRequired(true)
)
)
.addSubcommand(list => .addSubcommand(list =>
list.setName("list").setDescription("Show the list of banned users") list.setName("list").setDescription("Show the list of banned users")
) )
@ -78,7 +91,7 @@ export async function execute(interaction: ChatInputCommandInteraction) {
} }
} }
const result = dt.addBan(guildId, confessionId); const result = dt.addBanById(guildId, confessionId);
try { try {
return result return result
@ -93,34 +106,72 @@ export async function execute(interaction: ChatInputCommandInteraction) {
} catch (err) { } catch (err) {
logger.error("A ban interaction error occured:", err); logger.error("A ban interaction error occured:", err);
} }
// /confessmod banuser <user>
} else if (interaction.options.getSubcommand() === "banuser") {
const { id: userId } = interaction.options.getUser("user")!;
const result = dt.addBanByUser(guildId, userId);
try {
return result
? interaction.reply({
content: "User was banned.",
ephemeral: true
})
: interaction.reply({
content: "How did we get here? (An error occured.)}",
ephemeral: true
});
} catch (err) {
logger.error("A banuser interaction error occured:", err);
}
// /confessmod list // /confessmod list
} else if (interaction.options.getSubcommand() === "list") { } else if (interaction.options.getSubcommand() === "list") {
const bannedMembers = dt.getBans(guildId); const bannedMembers = dt.getBans(guildId);
let content = bannedMembers.length const determineContent = () => {
? "Banned Members:\n" if (!bannedMembers.length) {
: "There are no banned members."; return "There are no bans.";
}
// For each member, add them to the message content. let userHead = heading("Users:", HeadingLevel.Two);
// It will end up looking something like this: let userCount = false;
//
// Banned Members: let idHead = "\n" + heading("Confessions:", HeadingLevel.Two);
// let idCount = false;
// @user1 | a1b2 for (const member of bannedMembers) {
// @user2 | c3d4
// @user3 | e5f6 if (member.method === BanReason.ByUser) {
// userHead += "\n" + `<@${member.user}>`;
for (const member of bannedMembers) { userCount = true;
content += `\n<@${member.user}> | \`${member.confessionId}\``; } else if (member.method === BanReason.ById) {
const confession = dt.getConfession(guildId, member.confessionId!)!;
idHead += `\nConfession ${inlineCode(member.confessionId!)}: ${italic(confession.content)}`;
idCount = true;
}
}
// If there are users and confessions use both headers, otherwise use whichever is populated
if (userCount && idCount) {
return userHead + idHead;
} else {
return userCount
? userHead
: idHead;
}
} }
try { try {
return interaction.reply({ return interaction.reply({
content: content, content: determineContent(),
ephemeral: true ephemeral: true
}); });
} catch (err) { } catch (err) {
logger.error("A banlist interaction error occured:", err); logger.error("A banlist interaction error occured:", err);
return interaction.reply({
content: "A server-side error occurred when getting the ban list.",
ephemeral: true
});
} }
// /confessmod pardon <id> // /confessmod pardon <id>
} else if (interaction.options.getSubcommand() === "pardon") { } else if (interaction.options.getSubcommand() === "pardon") {

View file

@ -18,7 +18,13 @@
import fs from "fs"; import fs from "fs";
import crypto from "crypto"; import crypto from "crypto";
import { Confession, ConfessionBan, GuildData, GuildSettings } from "./types"; import {
BanReason,
Confession,
ConfessionBan,
GuildData,
GuildSettings
} from "./types";
import { DATA_DIR } from "./config"; import { DATA_DIR } from "./config";
import { CommandInteraction, Message } from "discord.js"; import { CommandInteraction, Message } from "discord.js";
import Logger from "../utils/Logger"; import Logger from "../utils/Logger";
@ -254,7 +260,7 @@ export class StoreMan {
} }
// Attempts to ban a user from confessions. // Attempts to ban a user from confessions.
public addBan(guildId: string, confessionId: string): boolean { public addBanById(guildId: string, confessionId: string): boolean {
const confession = this.getConfession(guildId, confessionId); const confession = this.getConfession(guildId, confessionId);
for (const guild of this.data) { for (const guild of this.data) {
@ -264,7 +270,8 @@ export class StoreMan {
!this.isBannedByUser(guildId, confession.authorId) && !this.isBannedByUser(guildId, confession.authorId) &&
guild.settings.bans.push({ guild.settings.bans.push({
user: confession.authorId, user: confession.authorId,
confessionId: confessionId confessionId: confessionId,
method: BanReason.ById
}); });
this.saveFile(); this.saveFile();
@ -276,6 +283,23 @@ export class StoreMan {
return false; return false;
} }
public addBanByUser(guildId: string, userId: string): boolean {
for (const guild of this.data) {
if (guild.id === guildId) {
// Only add the user to the ban list if they aren't banned already
!this.isBannedByUser(guildId, userId) && guild.settings.bans.push({
user: userId,
method: BanReason.ByUser
});
this.saveFile();
return true;
}
}
return false;
}
// Attempts to pardon a user from a ban. If sucessfully completed, returns true, false if otherwise. // Attempts to pardon a user from a ban. If sucessfully completed, returns true, false if otherwise.
public removeBan(guildId: string, confessionId: string): boolean { public removeBan(guildId: string, confessionId: string): boolean {
for (const guild of this.data) { for (const guild of this.data) {

View file

@ -25,9 +25,15 @@ export interface Confession {
attachment?: string; attachment?: string;
} }
export enum BanReason {
ById,
ByUser
}
export interface ConfessionBan { export interface ConfessionBan {
user: string; user: string;
confessionId: string; confessionId?: string;
method: BanReason;
} }
export interface GuildSettings { export interface GuildSettings {