Add auto ban feature
This commit is contained in:
+2
-1
@@ -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;
|
||||
|
||||
|
||||
@@ -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') {
|
||||
+14
-1
@@ -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
|
||||
};
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user