Skip to content

Commit

Permalink
fix(permissions): switch to PermissionsBitField enums
Browse files Browse the repository at this point in the history
Fixes #1345
  • Loading branch information
eritislami committed Jan 24, 2023
1 parent ebc3a07 commit c702b09
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
10 changes: 7 additions & 3 deletions commands/play.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { DiscordGatewayAdapterCreator, joinVoiceChannel } from "@discordjs/voice";
import { Message } from "discord.js";
import { Message, PermissionsBitField } from "discord.js";
import { bot } from "../index";
import { MusicQueue } from "../structs/MusicQueue";
import { Song } from "../structs/Song";
import { i18n } from "../utils/i18n";
import { playlistPattern } from "../utils/patterns";

export default {
name: "play",
cooldown: 3,
aliases: ["p"],
description: i18n.__("play.description"),
permissions: ["CONNECT", "SPEAK", "ADD_REACTIONS", "MANAGE_MESSAGES"],
permissions: [
PermissionsBitField.Flags.Connect,
PermissionsBitField.Flags.Speak,
PermissionsBitField.Flags.AddReactions,
PermissionsBitField.Flags.ManageMessages
],
async execute(message: Message, args: string[]) {
const { channel } = message.member!.voice;

Expand Down
9 changes: 7 additions & 2 deletions commands/playlist.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DiscordGatewayAdapterCreator, joinVoiceChannel } from "@discordjs/voice";
import { Message, EmbedBuilder } from "discord.js";
import { Message, EmbedBuilder, PermissionsBitField } from "discord.js";
import { bot } from "../index";
import { MusicQueue } from "../structs/MusicQueue";
import { Playlist } from "../structs/Playlist";
Expand All @@ -11,7 +11,12 @@ export default {
cooldown: 5,
aliases: ["pl"],
description: i18n.__("playlist.description"),
permissions: ["CONNECT", "SPEAK", "ADD_REACTIONS", "MANAGE_MESSAGES"],
permissions: [
PermissionsBitField.Flags.Connect,
PermissionsBitField.Flags.Speak,
PermissionsBitField.Flags.AddReactions,
PermissionsBitField.Flags.ManageMessages
],
async execute(message: Message, args: any[]) {
const { channel } = message.member!.voice;

Expand Down
4 changes: 2 additions & 2 deletions commands/queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, EmbedBuilder, MessageReaction, User } from "discord.js";
import { Message, EmbedBuilder, MessageReaction, User, PermissionsBitField } from "discord.js";
import { bot } from "../index";
import { Song } from "../structs/Song";
import { i18n } from "../utils/i18n";
Expand All @@ -8,7 +8,7 @@ export default {
cooldown: 5,
aliases: ["q"],
description: i18n.__("queue.description"),
permissions: ["MANAGE_MESSAGES", "ADD_REACTIONS"],
permissions: [PermissionsBitField.Flags.AddReactions, PermissionsBitField.Flags.ManageMessages],
async execute(message: Message) {
const queue = bot.queues.get(message.guild!.id);
if (!queue || !queue.songs.length) return message.reply(i18n.__("queue.errorNotQueue"));
Expand Down
4 changes: 2 additions & 2 deletions structs/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ActivityType, Client, Collection, Snowflake } from "discord.js";
import { readdirSync } from "fs";
import { join } from "path";
import { Command } from "../interfaces/Command";
import { checkPermissions } from "../utils/checkPermissions";
import { checkPermissions, PermissionResult } from "../utils/checkPermissions";
import { config } from "../utils/config";
import { i18n } from "../utils/i18n";
import { MissingPermissionsException } from "../utils/MissingPermissionsException";
Expand Down Expand Up @@ -80,7 +80,7 @@ export class Bot {
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);

try {
const permissionsCheck: any = await checkPermissions(command, message);
const permissionsCheck: PermissionResult = await checkPermissions(command, message);

if (permissionsCheck.result) {
command.execute(message, args);
Expand Down
12 changes: 4 additions & 8 deletions utils/checkPermissions.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { Message, PermissionResolvable } from "discord.js";
import { Command } from "../interfaces/Command";

interface PermissionResult {
export interface PermissionResult {
result: boolean;
missing?: string[];
missing: string[];
}

export async function checkPermissions(command: Command, message: Message): Promise<PermissionResult> {
const member = await message.guild!.members.fetch({ user: message.client.user!.id });
const requiredPermissions = command.permissions as PermissionResolvable[];

if (!command.permissions) return { result: true };
if (!command.permissions) return { result: true, missing: [] };

const missing = member.permissions.missing(requiredPermissions);

if (missing.length) {
return { result: false, missing: missing };
}

return { result: true };
return { result: !Boolean(missing.length), missing };
}

0 comments on commit c702b09

Please sign in to comment.