Skip to content

Commit

Permalink
Add leave role tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
FozzieHi committed Aug 31, 2024
1 parent e8b95c2 commit 9ebb62c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/database/models/Guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface DBGuild extends WithId<Document> {
caseNumber: number;
lockdown: boolean;
enabledChannels: Array<Snowflake>;
roles: { muted?: string };
youtubeChannels: { blocklisted: Array<string> };
protectionActivatedAt: number;
images: { guidelines: Binary | null };
Expand All @@ -20,6 +21,8 @@ export class Guild {

enabledChannels: Array<Snowflake>;

roles: { muted?: string };

youtubeChannels: { blocklisted: Array<string> };

protectionActivatedAt: number;
Expand All @@ -31,6 +34,9 @@ export class Guild {
this.caseNumber = 1;
this.lockdown = false;
this.enabledChannels = [];
this.roles = {
muted: undefined,
};
this.youtubeChannels = {
blocklisted: [],
};
Expand Down
31 changes: 31 additions & 0 deletions src/listeners/guildMemberAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions src/listeners/guildMemberRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9ebb62c

Please sign in to comment.