diff --git a/src/listeners/rolesJoinServer.ts b/src/listeners/rolesJoinServer.ts index 13060bd..13f14c0 100644 --- a/src/listeners/rolesJoinServer.ts +++ b/src/listeners/rolesJoinServer.ts @@ -19,10 +19,11 @@ import { Listener } from '@sapphire/framework'; import type { GuildMember, Snowflake } from 'discord.js'; -import { fetchRoles } from '#utils/database/dbExistingUser'; -import IDs from '#utils/ids'; +import { fetchRoles, getLeaveRoles } from '#utils/database/dbExistingUser'; import { blockTime } from '#utils/database/verification'; import { checkActive, getSection } from '#utils/database/restriction'; +import blockedRoles from '#utils/blockedRoles'; +import IDs from '#utils/ids'; export class RolesJoinServerListener extends Listener { public constructor(context: Listener.Context, options: Listener.Options) { @@ -40,8 +41,14 @@ export class RolesJoinServerListener extends Listener { const section = await getSection(member.id); roles.push(IDs.roles.restrictions.restricted[section - 1]); } else { + const logRoles = await getLeaveRoles(member.id); + // Add roles if not restricted - roles = await fetchRoles(member.id); + if (logRoles === null) { + roles = await fetchRoles(member.id); + } else { + roles = logRoles.roles.filter(this.blockedRole); + } } // Check if the user has a verification block @@ -55,4 +62,8 @@ export class RolesJoinServerListener extends Listener { await member.roles.add(roles); } } + + private blockedRole(role: Snowflake) { + return !blockedRoles.includes(role); + } } diff --git a/src/utils/blockedRoles.ts b/src/utils/blockedRoles.ts new file mode 100644 index 0000000..c4d8a0e --- /dev/null +++ b/src/utils/blockedRoles.ts @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +/* + Animal Rights Advocates Discord Bot + Copyright (C) 2023 Anthony Berg + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +import IDs from '#utils/ids'; + +const blockedRoles = [ + IDs.roles.staff.verifierCoordinator, + IDs.roles.staff.mentorCoordinator, + IDs.roles.staff.moderator, + IDs.roles.staff.trialModerator, + IDs.roles.staff.verifier, + IDs.roles.staff.trialVerifier, + IDs.roles.staff.mentor, + IDs.roles.stageHost, +]; + +export default blockedRoles; diff --git a/src/utils/database/dbExistingUser.ts b/src/utils/database/dbExistingUser.ts index 6cb8401..10c1d6f 100644 --- a/src/utils/database/dbExistingUser.ts +++ b/src/utils/database/dbExistingUser.ts @@ -201,3 +201,19 @@ export async function logLeaving(member: GuildMember) { }, }); } + +export async function getLeaveRoles(userId: Snowflake) { + const roles = container.database.leaveLog.findFirst({ + where: { + userId, + }, + orderBy: { + id: 'desc', + }, + select: { + roles: true, + }, + }); + + return roles; +}