diff --git a/src/database/models/Guild.ts b/src/database/models/Guild.ts index e505759a..18b31dff 100644 --- a/src/database/models/Guild.ts +++ b/src/database/models/Guild.ts @@ -6,6 +6,7 @@ export interface DBGuild extends WithId { caseNumber: number; lockdown: boolean; enabledChannels: Array; + roles: { muted?: string }; youtubeChannels: { blocklisted: Array }; protectionActivatedAt: number; images: { guidelines: Binary | null }; @@ -20,6 +21,8 @@ export class Guild { enabledChannels: Array; + roles: { muted?: string }; + youtubeChannels: { blocklisted: Array }; protectionActivatedAt: number; @@ -31,6 +34,9 @@ export class Guild { this.caseNumber = 1; this.lockdown = false; this.enabledChannels = []; + this.roles = { + muted: undefined, + }; this.youtubeChannels = { blocklisted: [], }; diff --git a/src/listeners/guildMemberAdd.ts b/src/listeners/guildMemberAdd.ts index 8f3e7c01..5e48912c 100644 --- a/src/listeners/guildMemberAdd.ts +++ b/src/listeners/guildMemberAdd.ts @@ -6,11 +6,42 @@ import { boldify, getDisplayTag } from "../utility/StringUtil.js"; import { Constants } from "../utility/Constants.js"; import TryVal from "../utility/TryVal.js"; import { genericLog } from "../services/ModerationService.js"; +import { getDBGuild, getDBUser } from "../utility/DatabaseUtil.js"; +import db from "../database/index.js"; export class GuildMemberAddListener extends Listener { public async run(member: GuildMember) { await ProtectionService.checkJoins(member.guild); + const dbUser = await getDBUser(member.id, member.guild.id); + const dbGuild = await getDBGuild(member.guild.id); + if (dbGuild == null) { + return; + } + + if (dbUser?.leftRoles != null) { + const promises = dbUser.leftRoles.map(async (roleId) => { + const role = await TryVal(member.guild.roles.fetch(roleId)); + if (role != null && role.id !== dbGuild.roles.muted) { + return member.roles.add(role.id); + } + return null; + }); + + await Promise.all(promises); + } + + if ( + dbGuild.roles.muted != null && + (await db.muteRepo?.anyMute(member.id, member.guild.id)) + ) { + const role = await TryVal(member.guild.roles.fetch(dbGuild.roles.muted)); + + if (role != null) { + await member.roles.add(role); + } + } + await genericLog( member.guild, member, diff --git a/src/listeners/guildMemberRemove.ts b/src/listeners/guildMemberRemove.ts index aeb5bb27..cb0d3ac8 100644 --- a/src/listeners/guildMemberRemove.ts +++ b/src/listeners/guildMemberRemove.ts @@ -2,9 +2,18 @@ import { GuildMember } from "discord.js"; import { Listener } from "@sapphire/framework"; import { Constants } from "../utility/Constants.js"; import { genericLog } from "../services/ModerationService.js"; +import db from "../database/index.js"; export class GuildMemberRemoveListener extends Listener { public async run(member: GuildMember) { + await db.userRepo?.upsertUser(member.id, member.guild.id, { + $set: { + leftRoles: [...member.roles.cache.keys()].filter( + (roleId) => roleId !== member.guild.id, + ), + }, + }); // Map to role ID and filter out the @everyone role. + await genericLog( member.guild, member,