diff --git a/.env.example b/.env.example index 4180e08..744bddc 100644 --- a/.env.example +++ b/.env.example @@ -6,4 +6,6 @@ DISCORD_TOKEN= LOG_DIR= API_TOKEN= WEBHOOK_QUERY_SECRET= -WEBHOOK_URL= \ No newline at end of file +WEBHOOK_URL= +QUEUE_BLACKLIST_ROLE_ID= +TOURNEY_BLACKLIST_ROLE_ID= \ No newline at end of file diff --git a/src/command-handlers/moderation/createBan.ts b/src/command-handlers/moderation/createBan.ts index 3d18dce..c1cfbae 100644 --- a/src/command-handlers/moderation/createBan.ts +++ b/src/command-handlers/moderation/createBan.ts @@ -6,6 +6,7 @@ import { logModerationEvent } from '../../utils/logModerationEvent' import { sendDm } from '../../utils/sendDm' import { resolveModerationTarget } from './resolveModerationTarget' import { getGuild } from '../../client' +import { env } from 'env' const DAY_IN_MS = 24 * 60 * 60 * 1000 @@ -120,10 +121,12 @@ export async function createBan({ // add blacklisted roles for visibility + tourney blacklisting const member = await guild.members.fetch(userId).catch(() => null) + console.log('Member found for role add:', !!member) if (member) { await Promise.all([ - member.roles.add('1354296037094854788'), - member.roles.add('1344793211146600530'), + member.roles.add(env.QUEUE_BLACKLIST_ROLE_ID), + member.roles.add(env.TOURNEY_BLACKLIST_ROLE_ID), + ]) } diff --git a/src/command-handlers/moderation/removeBan.ts b/src/command-handlers/moderation/removeBan.ts index 0b40c0d..1ea8d0b 100644 --- a/src/command-handlers/moderation/removeBan.ts +++ b/src/command-handlers/moderation/removeBan.ts @@ -5,6 +5,7 @@ import { pool } from '../../db' import { createEmbedType, logStrike } from '../../utils/logCommandUse' import { logModerationEvent } from '../../utils/logModerationEvent' import { sendDm } from '../../utils/sendDm' +import { env } from 'env' export class RemoveBanError extends Error { code: 'NOT_FOUND' @@ -58,8 +59,8 @@ export async function removeBan({ userId, blame, reason }: RemoveBanParams) { const member = await guild.members.fetch(userId).catch(() => null) if (member) { await Promise.all([ - member.roles.remove('1354296037094854788'), - member.roles.remove('1344793211146600530'), + member.roles.remove(env.QUEUE_BLACKLIST_ROLE_ID), + member.roles.remove(env.TOURNEY_BLACKLIST_ROLE_ID), ]) } diff --git a/src/env.ts b/src/env.ts index ddc1146..217764e 100644 --- a/src/env.ts +++ b/src/env.ts @@ -25,10 +25,13 @@ const envSchema = z.object({ z.string().default(path.join(process.cwd(), 'fonts')), ), GUILD_ID: z.string().trim().min(1, 'GUILD_ID is required'), + QUEUE_BLACKLIST_ROLE_ID: z.string().trim().min(1, 'QUEUE_BLACKLIST_ROLE_ID is required'), + TOURNEY_BLACKLIST_ROLE_ID: z.string().trim().min(1, 'TOURNEY_BLACKLIST_ROLE_ID is required'), LOG_DIR: z.preprocess( emptyStringToUndefined, z.string().default(path.join(process.cwd(), 'logs')), ), + NODE_ENV: z.preprocess( emptyStringToUndefined, z.enum(['development', 'production', 'test']).default('production'), diff --git a/src/utils/automaticUnbans.ts b/src/utils/automaticUnbans.ts index f7e8499..368e093 100644 --- a/src/utils/automaticUnbans.ts +++ b/src/utils/automaticUnbans.ts @@ -6,6 +6,7 @@ import { logModerationEvent } from './logModerationEvent' import { sendDm } from './sendDm' import { createEmbedType, logStrike } from './logCommandUse' import { getGuild } from '../client' +import { env } from 'env' export async function automaticUnban(ban: Bans) { // remove ban @@ -14,7 +15,17 @@ export async function automaticUnban(ban: Bans) { await sendDm(userId, moderationMessages.banLiftedDm({ expired: true })) const guild = await getGuild() - const username = (await guild.members.fetch(userId))?.displayName ?? userId + // Added member fetch to remove blacklisted roles when unbanning a user whose ban has expired. + const member = await guild.members.fetch(userId).catch(() => null) + const username = member?.displayName ?? userId + + if (member) { + await Promise.all([ + member.roles.remove(env.QUEUE_BLACKLIST_ROLE_ID), + member.roles.remove(env.TOURNEY_BLACKLIST_ROLE_ID), + ]) +} + // log ban removal const embedType = createEmbedType( @@ -44,6 +55,7 @@ export async function automaticUnban(ban: Bans) { }) } + // check all bans for timeout. todo: replace with an api call from external service that is running a cronjob export async function checkBans() { const res = await pool.query('SELECT * FROM "bans"') @@ -62,3 +74,4 @@ export async function checkBans() { await automaticUnban(expiredBan) } } +