Skip to content

Commit

Permalink
feat: use single client config import
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirasaki committed May 23, 2023
1 parent e7c712c commit 5edb477
Showing 4 changed files with 25 additions and 16 deletions.
20 changes: 10 additions & 10 deletions src/handlers/permissions.js
Original file line number Diff line number Diff line change
@@ -5,13 +5,12 @@
* @module Handler/Permissions
*/

const { existsSync } = require('fs');
const { PermissionsBitField } = require('discord.js');

// NOTE:
// This can't use clientConfig from Util module because
// it would create a circular dependency
const config = require(existsSync('../../config.js') ? '../../config' : '../../config.example');
// Instead, provide it as a argument

/**
* The `discord.js` GuildMember object
@@ -107,23 +106,23 @@ const permConfig = [
{
name: 'Moderator',
level: 1,
hasLevel: (member, channel) => hasChannelPerms(
hasLevel: (config, member, channel) => hasChannelPerms(
member.id, channel, [ 'KickMembers', 'BanMembers' ]
) === true
},

{
name: 'Administrator',
level: 2,
hasLevel: (member, channel) => hasChannelPerms(member.id, channel, [ 'Administrator' ]) === true
hasLevel: (config, member, channel) => hasChannelPerms(member.id, channel, [ 'Administrator' ]) === true
},

{
name: 'Server Owner',
level: 3,
hasLevel: (member, channel) => {
hasLevel: (config, member, channel) => {
// Shorthand
// hasLevel: (member, channel) => (channel.guild?.ownerId === member.user?.id)
// hasLevel: (config, member, channel) => (channel.guild?.ownerId === member.user?.id)
// COULD result in (undefined === undefined)
if (channel.guild && channel.guild.ownerId) {
return (channel.guild.ownerId === member.id);
@@ -135,13 +134,13 @@ const permConfig = [
{
name: 'Developer',
level: 4,
hasLevel: (member) => config.permissions.developers.includes(member.id)
hasLevel: (config, member) => config.permissions.developers.includes(member.id)
},

{
name: 'Bot Owner',
level: 5,
hasLevel: (member) => config.permissions.ownerId === member.id
hasLevel: (config, member) => config.permissions.ownerId === member.id
}
];

@@ -171,13 +170,14 @@ const sortedPermConfig = permConfig.sort((a, b) => {
/**
* Resolves someone's permission level
* @method getPermissionLevel
* @param {module:Client~ClientConfiguration} config Our client configuration object
* @param {external:DiscordGuildMember} member The Discord API member object
* @param {external:DiscordGuildChannel} channel The Discord API channel object
* @returns {number} The member's permission level
*/
const getPermissionLevel = (member, channel) => {
const getPermissionLevel = (config, member, channel) => {
for (const currLvl of sortedPermConfig) {
if (currLvl.hasLevel(member, channel)) {
if (currLvl.hasLevel(config, member, channel)) {
return currLvl.level;
}
}
6 changes: 4 additions & 2 deletions src/listeners/interaction/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,9 @@ const { InteractionType } = require('discord.js');
const { checkCommandCanExecute,
throttleCommand } = require('../../handlers/commands');
const { getPermissionLevel } = require('../../handlers/permissions');
const { titleCase, getRuntime } = require('../../util');
const {
titleCase, getRuntime, clientConfig
} = require('../../util');

// Destructure from origin file because it's
// used in multiple functions
@@ -173,7 +175,7 @@ module.exports = (client, interaction) => {
if (checkInteractionAvailability(interaction) === false) return;

// Setting the permLevel on the member object before we do anything else
const permLevel = getPermissionLevel(member, channel);
const permLevel = getPermissionLevel(clientConfig, member, channel);

interaction.member.permLevel = permLevel;

13 changes: 10 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -11,10 +11,13 @@

// Importing from libraries
const { OAuth2Scopes, PermissionFlagsBits } = require('discord.js');
const { readdirSync, statSync } = require('fs');
const {
readdirSync, statSync, existsSync
} = require('fs');
const moment = require('moment');
const path = require('path');
const colors = require('./config/colors.json');
const logger = require('@mirasaki/logger');
const colors = require('../config/colors.json');

// Import our constants
const {
@@ -30,7 +33,11 @@ const { validPermValues } = require('./handlers/permissions');

// Resolve client configuration
const modeArg = process.argv.find((arg) => arg.startsWith('mode='));
const configFilePath = modeArg && modeArg.endsWith('test') ? '../config.example.js' : '../config.js';
const configFilePath = modeArg && modeArg.endsWith('test') ? '../config/config.example.js' : '../config/config.js';
if (!existsSync(configFilePath.replace(/\.\.\//g, ''))) {
logger.syserr(`Configuration file at "${ configFilePath.replace(/\.\.\//g, '') }" doesn't exists, please refer to documentation, exiting...`);
process.exit(0);
}
const clientConfig = require(configFilePath);

/**
2 changes: 1 addition & 1 deletion tutorials/permissions.md
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

```javascript
// Getting someone's internal permission level
const targetPermLevel = getPermissionLevel(interaction.member, interaction.channel);
const targetPermLevel = getPermissionLevel(clientConfig, interaction.member, interaction.channel);

// Resolving the permLevel
console.log(targetPermLevel, getPermLevelName(targetPermLevel));

0 comments on commit 5edb477

Please sign in to comment.