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 { Client as DiscordClient, GatewayIntentBits, Guild, MessageFlags, Partials } from 'discord.js';
|
||||||
import { config } from './config';
|
import { config } from './config';
|
||||||
import { Handler } from './types';
|
import { Handler } from './types';
|
||||||
import { initIfNecessary, loadHandlersFrom, openRedisClient, refreshCommands } from './utils';
|
import { initIfNecessary, loadHandlersFrom, refreshCommands } from './utils';
|
||||||
import { initializeDiscordJoiner } from './discord-joiner';
|
import { initializeDiscordJoiner } from './discord-joiner';
|
||||||
import { Database } from './shared/Database';
|
import { Database } from './shared/Database';
|
||||||
import { handleAuditLogCreate, handleBulkMessageDelete, handleGuildCreate, handleMemberJoin, handleMessageCreate, handleMessageDelete, handleMessageUpdate, handleThreadCreate, handleVoiceStateUpdate } from './events';
|
import { handleAuditLogCreate, handleBulkMessageDelete, handleGuildCreate, handleMemberJoin, handleMessageCreate, handleMessageDelete, handleMessageUpdate, handleThreadCreate, handleVoiceStateUpdate } from './events';
|
||||||
import { ticketCooldownMap } from './shared/ticket-cooldown';
|
import { ticketCooldownMap } from './shared/ticket-cooldown';
|
||||||
|
import { openRedisClient } from './shared/RedisClient';
|
||||||
|
|
||||||
let ready = false;
|
let ready = false;
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { createClient } from '@redis/client';
|
import { createClient } from '@redis/client';
|
||||||
import { Database } from '../shared/Database';
|
import { Database } from './Database';
|
||||||
import { config } from '../config';
|
import { config } from '../config';
|
||||||
import { APIEmbedField, Client, EmbedAuthorOptions, EmbedBuilder, SendableChannels, TextBasedChannel } from 'discord.js';
|
import { APIEmbedField, Client, EmbedAuthorOptions, EmbedBuilder, SendableChannels, TextBasedChannel } from 'discord.js';
|
||||||
import { humanizeCapitalization } from './string-utils';
|
import { humanizeCapitalization } from '../utils/string-utils';
|
||||||
import { Ticket, TicketPhrase, TicketUpdate } from '../types';
|
import { BanUpdate, Ticket, TicketPhrase, TicketUpdate } from '../types';
|
||||||
import { shouldAlert } from './ticket-utils';
|
import { shouldAlert } from '../utils/ticket-utils';
|
||||||
|
|
||||||
const MAX_DESCRIPTION_LENGTH = 500;
|
const MAX_DESCRIPTION_LENGTH = 500;
|
||||||
|
|
||||||
@@ -21,10 +21,51 @@ export async function openRedisClient(url: string, discClient: Client) {
|
|||||||
|
|
||||||
console.log('Connected to redis database');
|
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);
|
const data: TicketUpdate = JSON.parse(update);
|
||||||
|
|
||||||
if (data.action == 'create') {
|
if (data.action == 'create') {
|
||||||
+14
-1
@@ -119,10 +119,23 @@ export type Ticket = {
|
|||||||
target?: string
|
target?: string
|
||||||
status: 'pending' | 'partial' | 'approved'
|
status: 'pending' | 'partial' | 'approved'
|
||||||
category: 'blip' | 'comment' | 'dmail' | 'forum' | 'pool' | 'post' | 'set' | 'user' | 'wiki'
|
category: 'blip' | 'comment' | 'dmail' | 'forum' | 'pool' | 'post' | 'set' | 'user' | 'wiki'
|
||||||
reason: 'string'
|
reason: string
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TicketUpdate = {
|
export type TicketUpdate = {
|
||||||
action: 'claim' | 'create' | 'unclaim' | 'update'
|
action: 'claim' | 'create' | 'unclaim' | 'update'
|
||||||
ticket: Ticket
|
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 './audit-log-utils';
|
||||||
export * from './channel-utils';
|
export * from './channel-utils';
|
||||||
export * from './commands';
|
export * from './commands';
|
||||||
export * from './e621-ticket-listener';
|
|
||||||
export * from './e621-utils';
|
export * from './e621-utils';
|
||||||
export * from './message-logger';
|
export * from './message-logger';
|
||||||
export * from './message-utils';
|
export * from './message-utils';
|
||||||
|
|||||||
Reference in New Issue
Block a user