From e99f42b0b004d041e45d8bf34bde15e96a03a02f Mon Sep 17 00:00:00 2001 From: Tarrgon <61888458+Tarrgon@users.noreply.github.com> Date: Thu, 29 May 2025 14:59:06 -0400 Subject: [PATCH] Add auto ban feature --- src/index.ts | 3 +- .../RedisClient.ts} | 53 ++++++++++++++++--- src/types/e621-types.ts | 15 +++++- src/utils/index.ts | 1 - 4 files changed, 63 insertions(+), 9 deletions(-) rename src/{utils/e621-ticket-listener.ts => shared/RedisClient.ts} (75%) diff --git a/src/index.ts b/src/index.ts index e8ed1e6..0f7815e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,11 +2,12 @@ import 'source-map-support/register'; import { Client as DiscordClient, GatewayIntentBits, Guild, MessageFlags, Partials } from 'discord.js'; import { config } from './config'; import { Handler } from './types'; -import { initIfNecessary, loadHandlersFrom, openRedisClient, refreshCommands } from './utils'; +import { initIfNecessary, loadHandlersFrom, refreshCommands } from './utils'; import { initializeDiscordJoiner } from './discord-joiner'; import { Database } from './shared/Database'; import { handleAuditLogCreate, handleBulkMessageDelete, handleGuildCreate, handleMemberJoin, handleMessageCreate, handleMessageDelete, handleMessageUpdate, handleThreadCreate, handleVoiceStateUpdate } from './events'; import { ticketCooldownMap } from './shared/ticket-cooldown'; +import { openRedisClient } from './shared/RedisClient'; let ready = false; diff --git a/src/utils/e621-ticket-listener.ts b/src/shared/RedisClient.ts similarity index 75% rename from src/utils/e621-ticket-listener.ts rename to src/shared/RedisClient.ts index bbeac3b..ab8dbed 100644 --- a/src/utils/e621-ticket-listener.ts +++ b/src/shared/RedisClient.ts @@ -1,10 +1,10 @@ import { createClient } from '@redis/client'; -import { Database } from '../shared/Database'; +import { Database } from './Database'; import { config } from '../config'; import { APIEmbedField, Client, EmbedAuthorOptions, EmbedBuilder, SendableChannels, TextBasedChannel } from 'discord.js'; -import { humanizeCapitalization } from './string-utils'; -import { Ticket, TicketPhrase, TicketUpdate } from '../types'; -import { shouldAlert } from './ticket-utils'; +import { humanizeCapitalization } from '../utils/string-utils'; +import { BanUpdate, Ticket, TicketPhrase, TicketUpdate } from '../types'; +import { shouldAlert } from '../utils/ticket-utils'; const MAX_DESCRIPTION_LENGTH = 500; @@ -21,10 +21,51 @@ export async function openRedisClient(url: string, discClient: Client) { console.log('Connected to redis database'); - await client.subscribe('ticket_updates', updateHandler); + await client.subscribe(['ticket_updates', 'ban_updates'], updateHandler); } -async function updateHandler(update: string, channel: string) { +function updateHandler(data: string, channel: string) { + switch (channel) { + case 'ticket_updates': + return ticketUpdateHandler(data); + case 'ban_updates': + return banUpdateHandler(data); + } +} + +async function banUpdateHandler(update: string) { + const data: BanUpdate = JSON.parse(update); + + if (data.action == 'create') { + banDiscordAccounts(data); + } else if (data.action == 'delete') { + unbanDiscordAccounts(data); + } +} + +async function banDiscordAccounts(data: BanUpdate) { + const guild = await discordClient.guilds.fetch(config.DISCORD_GUILD_ID!); + + const discordIds = await Database.getDiscordIds(data.ban.user_id); + + for (const id of discordIds) { + await guild.bans.create(id, { + reason: data.ban.reason + }); + } +} + +async function unbanDiscordAccounts(data: BanUpdate) { + const guild = await discordClient.guilds.fetch(config.DISCORD_GUILD_ID!); + + const discordIds = await Database.getDiscordIds(data.ban.user_id); + + for (const id of discordIds) { + await guild.bans.remove(id); + } +} + +async function ticketUpdateHandler(update: string) { const data: TicketUpdate = JSON.parse(update); if (data.action == 'create') { diff --git a/src/types/e621-types.ts b/src/types/e621-types.ts index c6b7dbe..a76f25e 100644 --- a/src/types/e621-types.ts +++ b/src/types/e621-types.ts @@ -119,10 +119,23 @@ export type Ticket = { target?: string status: 'pending' | 'partial' | 'approved' category: 'blip' | 'comment' | 'dmail' | 'forum' | 'pool' | 'post' | 'set' | 'user' | 'wiki' - reason: 'string' + reason: string }; export type TicketUpdate = { action: 'claim' | 'create' | 'unclaim' | 'update' ticket: Ticket +}; + +export type Ban = { + id: number + user_id: number + banner_id: number + expires_at: string + reason: string +}; + +export type BanUpdate = { + action: 'create' | 'update' | 'delete' + ban: Ban }; \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts index 29b1a0d..531cf25 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,7 +2,6 @@ export * from './array-utils'; export * from './audit-log-utils'; export * from './channel-utils'; export * from './commands'; -export * from './e621-ticket-listener'; export * from './e621-utils'; export * from './message-logger'; export * from './message-utils';