diff --git a/src/utils/database/memberMod.ts b/src/utils/database/memberMod.ts new file mode 100644 index 0000000..0c59e54 --- /dev/null +++ b/src/utils/database/memberMod.ts @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +/* + Animal Rights Advocates Discord Bot + Copyright (C) 2024 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 { Snowflake } from 'discord.js'; +import { countWarnings } from '#utils/database/moderation/warnings'; +import { countRestrictions } from '#utils/database/moderation/restriction'; +import { container } from '@sapphire/framework'; + +/** + * Checks if the user has + * @param userId Discord Snowflake of the user to check + * @return Boolean true if no prior moderation action + */ +export async function noModHistory(userId: Snowflake) { + const warnCount = await countWarnings(userId); + const restrictCount = await countRestrictions(userId); + + return warnCount === 0 && restrictCount === 0; +} + +/** + * Checks if the user has previously had a role given or taken away by a moderator. + * @param userId Discord Snowflake of the user to check + * @param roleId Snowflake of the role being checked for the user + * @return Boolean true if the user has had a moderator give/remove the specified role + */ +export async function userPreviouslyHadRole( + userId: Snowflake, + roleId: Snowflake, +) { + const count = await container.database.roleLog.count({ + where: { + userId, + roleId, + }, + }); + + return count !== 0; +} diff --git a/src/utils/database/moderation/restriction.ts b/src/utils/database/moderation/restriction.ts index e8852e5..2296095 100644 --- a/src/utils/database/moderation/restriction.ts +++ b/src/utils/database/moderation/restriction.ts @@ -114,6 +114,19 @@ export async function getSection(userId: Snowflake) { return restriction.section; } +/** + * Returns the amount of restrictions a user has. + * @param userId Discord Snowflake of the user to check + * @return number The amount of restrictions the user has + */ +export async function countRestrictions(userId: Snowflake) { + return container.database.restrict.count({ + where: { + userId, + }, + }); +} + // This is only for restrictions created with the old bot export async function unRestrictLegacy( userId: Snowflake, diff --git a/src/utils/database/moderation/warnings.ts b/src/utils/database/moderation/warnings.ts index e36d4fa..481c911 100644 --- a/src/utils/database/moderation/warnings.ts +++ b/src/utils/database/moderation/warnings.ts @@ -61,3 +61,16 @@ export async function deleteWarning(warningId: number) { }, }); } + +/** + * Returns the amount of warnings a user has. + * @param userId Discord Snowflake of the user to check + * @return number The amount of warnings the user has + */ +export async function countWarnings(userId: Snowflake) { + return container.database.warning.count({ + where: { + userId, + }, + }); +}