Skip to content

Commit

Permalink
Merge pull request #21 from Wiibleyde/develop
Browse files Browse the repository at this point in the history
feat: implement permission utility for command execution and refactor…
  • Loading branch information
Wiibleyde authored Oct 3, 2024
2 parents 98dbe0a + de2d88f commit e326054
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/commands/config/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions src/commands/config/rename.ts
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions src/commands/fun/quiz/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions src/utils/permissionTester.ts
Original file line number Diff line number Diff line change
@@ -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<CacheType>|ModalSubmitInteraction, permissionsToTests: bigint[], exact: boolean): Promise<boolean> {
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))
}
}
}

0 comments on commit e326054

Please sign in to comment.