diff --git a/src/commands/config/channels.ts b/src/commands/config/channels.ts index ded6dee..2833038 100644 --- a/src/commands/config/channels.ts +++ b/src/commands/config/channels.ts @@ -3,6 +3,7 @@ import { prisma } from "@/utils/database" import { errorEmbed, successEmbed } from "@/utils/embeds" import { config } from "@/config" import { backSpace } from "@/utils/textUtils" +import { PermissionUtils } from "@/utils/permissionTester" export const data = new SlashCommandBuilder() .setName("channels") @@ -47,7 +48,7 @@ export const data = new SlashCommandBuilder() export async function execute(interaction: CommandInteraction) { await interaction.deferReply({ ephemeral: true, fetchReply: true }) const user = interaction.guild?.members.cache.get(interaction.client.user.id) - if (!(user?.permissions.has(PermissionFlagsBits.ManageChannels) || user?.permissions.has(PermissionFlagsBits.Administrator)) && config.OWNER_ID !== interaction.user.id) { + if (!await PermissionUtils.hasPermission(interaction, [PermissionFlagsBits.ManageChannels], false)) { await interaction.editReply({ embeds: [errorEmbed(interaction, new Error("Vous n'avez pas la permission de changer la configuration."))] }) return } diff --git a/src/commands/config/rename.ts b/src/commands/config/rename.ts index e8d7093..2b60185 100644 --- a/src/commands/config/rename.ts +++ b/src/commands/config/rename.ts @@ -1,6 +1,7 @@ import { CommandInteraction, SlashCommandBuilder, PermissionFlagsBits, InteractionContextType } from "discord.js" import { errorEmbed, successEmbed } from "@/utils/embeds" -import { config } from "@/config" +import { PermissionUtils } from "@/utils/permissionTester" +import { logger } from "@/utils/logger" export const data = new SlashCommandBuilder() .setName("rename") @@ -18,8 +19,8 @@ export const data = new SlashCommandBuilder() export async function execute(interaction: CommandInteraction) { await interaction.deferReply({ ephemeral: true, fetchReply: true }) - const user = interaction.guild?.members.cache.get(interaction.client.user.id) - if (!(user?.permissions.has(PermissionFlagsBits.ChangeNickname) || user?.permissions.has(PermissionFlagsBits.Administrator)) && config.OWNER_ID !== interaction.user.id) { + logger.debug("Checking if user has permission to change bot's name") + if (!await PermissionUtils.hasPermission(interaction, [PermissionFlagsBits.ManageChannels], false)) { await interaction.editReply({ embeds: [errorEmbed(interaction, new Error("Vous n'avez pas la permission de changer le nom du bot"))] }) return } diff --git a/src/commands/fun/quiz/leaderboard.ts b/src/commands/fun/quiz/leaderboard.ts index 1e8f273..a03a5d6 100644 --- a/src/commands/fun/quiz/leaderboard.ts +++ b/src/commands/fun/quiz/leaderboard.ts @@ -15,6 +15,7 @@ export async function execute(interaction: CommandInteraction) { } }) + // Sort the users by the ratio of good answers (and the most good answers) users.sort((a, b) => { const ratioA = a.quizGoodAnswers / (a.quizGoodAnswers + a.quizBadAnswers) const ratioB = b.quizGoodAnswers / (b.quizGoodAnswers + b.quizBadAnswers) diff --git a/src/index.ts b/src/index.ts index 20bf2a9..4eb90d3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import { CronJob } from 'cron' import { prisma } from "./utils/database" import { initAi, generateWithGoogle } from "./utils/intelligence" import { maintenance } from "./commands/dev/maintenance" +import { PermissionUtils } from "./utils/permissionTester" export const client = new Client({ intents: [ @@ -51,8 +52,7 @@ client.on(Events.InteractionCreate, async (interaction) => { if (interaction.isCommand()) { try { if(maintenance) { - const user = await interaction.guild?.members.fetch(interaction.user.id) - if ((!user?.permissions.has(PermissionFlagsBits.Administrator) && config.OWNER_ID !== interaction.user.id) && maintenance) { + if(!await PermissionUtils.hasPermission(interaction, [], false)) { await interaction.reply({ embeds: [errorEmbed(interaction, new Error("Le bot est en maintenance, veuillez réessayer plus tard."))], ephemeral: true }) return } diff --git a/src/utils/permissionTester.ts b/src/utils/permissionTester.ts new file mode 100644 index 0000000..5a773c8 --- /dev/null +++ b/src/utils/permissionTester.ts @@ -0,0 +1,26 @@ +import { CommandInteraction, ButtonInteraction, ModalSubmitInteraction, CacheType, PermissionFlagsBits } from 'discord.js'; +import { config } from '@/config'; +import { logger } from './logger'; + + +export namespace PermissionUtils { + /** + * Check if the user has the required permissions (PermissionsFlagsBits.Administrator bypasses all permissions) + * @param interaction The interaction to check + * @param permissionsToTests The permissions to check + * @param exact If the user needs all the permissions or just one + * @returns If the user has the required permissions + * */ + export async function hasPermission(interaction: CommandInteraction|ButtonInteraction|ModalSubmitInteraction, permissionsToTests: bigint[], exact: boolean): Promise { + if(config.OWNER_ID == interaction.user.id) return true + const member = await interaction.guild?.members.fetch(interaction.user.id) + if(!member) return false + if(member.permissions.has(PermissionFlagsBits.Administrator)) return true + if(permissionsToTests.length == 0) return false + if(exact) { + return permissionsToTests.every(permission => member.permissions.has(permission)) + } else { + return permissionsToTests.some(permission => member.permissions.has(permission)) + } + } +} \ No newline at end of file