Skip to content
This repository was archived by the owner on Sep 18, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions commands/deploy.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
exports.run = async (client, message, args, level) => { // eslint-disable-line no-unused-vars
// Filter the slash commands to find guild only ones.
const guildCmds = client.container.slashcmds.filter(c => c.guildOnly).map(c => c.commandData);

// Now we filter out global commands by inverting the filter.
const globalCmds = client.container.slashcmds.filter(c => !c.guildOnly).map(c => c.commandData);
// We'll partition the slash commands based on the guildOnly boolean.
// Separating them into the correct objects defined in the array below.
const [globalCmds, guildCmds] = client.container.slashcmds.partition(c => !c.conf.guildOnly);

// Give the user a notification the commands are deploying.
await message.channel.send("Deploying commands!");

// We'll use set but please keep in mind that `set` is overkill for a singular command.
// Set the guild commands like
await client.guilds.cache.get(message.guild.id)?.commands.set(guildCmds);
await client.guilds.cache.get(message.guild.id)?.commands.set(guildCmds.map(c => c.commandData));

// Then set the global commands like
await client.application?.commands.set(globalCmds).catch(e => console.log(e));
await client.application?.commands.set(globalCmds.map(c => c.commandData)).catch(e => console.log(e));

// Reply to the user that the commands have been deployed.
await message.channel.send("All commands deployed!");
Expand Down
6 changes: 3 additions & 3 deletions commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ exports.run = (client, message, args, level) => {
} else {
// Show individual command's help.
let command = args[0];
if (container.commands.has(command)) {
command = container.commands.get(command);
if (container.commands.has(command) || container.commands.has(container.aliases.get(command))) {
command = container.commands.get(command) ?? container.commands.get(container.aliases.get(command));
if (level < container.levelCache[command.conf.permLevel]) return;
message.channel.send(codeBlock("asciidoc", `= ${command.help.name} = \n${command.help.description}\nusage:: ${command.help.usage}\naliases:: ${command.conf.aliases.join(", ")}`));
}
} else return message.channel.send("No command with that name, or alias exists.");
}};

exports.conf = {
Expand Down
20 changes: 16 additions & 4 deletions config.js.example
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ const config = {
name: "Server Owner",
// Simple check, if the guild owner id matches the message author's ID, then it will return true.
// Otherwise it will return false.
check: (message) => message.channel.type === "text" ? (message.guild.ownerID === message.author.id ? true : false) : false
check: (message) => {
const serverOwner = message.author ?? message.user;
return message.guild?.ownerId === serverOwner.id;
}
},

// Bot Support is a special in between level that has the equivalent of server owner access
Expand All @@ -85,13 +88,19 @@ const config = {
name: "Bot Support",
// The check is by reading if an ID is part of this array. Yes, this means you need to
// change this and reboot the bot to add a support user. Make it better yourself!
check: (message) => config.support.includes(message.author.id)
check: (message) => {
const botSupport = message.author ?? message.user;
return config.support.includes(botSupport.id);
}
},

// Bot Admin has some limited access like rebooting the bot or reloading commands.
{ level: 9,
name: "Bot Admin",
check: (message) => config.admins.includes(message.author.id)
check: (message) => {
const botAdmin = message.author ?? message.user;
return config.admins.includes(botAdmin.id);
}
},

// This is the bot owner, this should be the highest permission level available.
Expand All @@ -101,7 +110,10 @@ const config = {
{ level: 10,
name: "Bot Owner",
// Another simple check, compares the message author id to a list of owners found in the bot application.
check: (message) => message.author.id === process.env.OWNER
check: (message) => {
const owner = message.author ?? message.user;
return owner.id === process.env.OWNER;
}
}
]
};
Expand Down
31 changes: 29 additions & 2 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
const logger = require("../modules/Logger.js");
const { getSettings, permlevel } = require("../modules/functions.js");
const config = require("../config.js");

module.exports = async (client, interaction) => {
// If it's not a command, stop.
if (!interaction.isCommand()) return;

// Grab the settings for this server from Enmap.
// If there is no guild, get default conf (DMs)
const settings = interaction.settings = getSettings(interaction.guild);

// Get the user or member's permission level from the elevation
const level = permlevel(interaction);

// Grab the command data from the client.container.slashcmds Collection
const cmd = client.container.slashcmds.get(interaction.commandName);

// If that command doesn't exist, silently exit and do nothing
if (!cmd) return;
// Run the command

// Since the permission system from Discord is rather limited in regarding to
// Slash Commands, we'll just utilise our permission checker.
if (level < client.container.levelCache[cmd.conf.permLevel]) {
// Due to the nature of interactions we **must** respond to them otherwise
// they will error out because we didn't respond to them.
return await interaction.reply({
content: `This command can only be used by ${cmd.conf.permLevel}'s only`,
// This will basically set the ephemeral response to either announce
// to everyone, or just the command executioner. But we **HAVE** to
// respond.
ephemeral: settings.systemNotice !== "true"
});
}

// If everything checks out, run the command
try {
await cmd.run(client, interaction);
logger.log(`${interaction.user.id} ran slash command ${interaction.commandName}`, "cmd");
logger.log(`${config.permLevels.find(l => l.level === level).name} ${interaction.user.id} ran slash command ${interaction.commandName}`, "cmd");

} catch (e) {
console.error(e);
Expand Down
1 change: 1 addition & 0 deletions events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ This command requires level ${container.levelCache[cmd.conf.permLevel]} (${cmd.c
await cmd.run(client, message, args, level);
logger.log(`${config.permLevels.find(l => l.level === level).name} ${message.author.id} ran command ${cmd.help.name}`, "cmd");
} catch (e) {
console.error(e);
message.channel.send({ content: `There was a problem with your request.\n\`\`\`${e.message}\`\`\`` })
.catch(e => console.error("An error occurred replying on an error", e));
}
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions slash/leave.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { Permissions } = require('discord.js');
const { Permissions } = require("discord.js");

exports.run = async (client, interaction) => { // eslint-disable-line no-unused-vars
await interaction.deferReply();
if(!interaction.guild.me.permissions.has(Permissions.FLAGS.KICK_MEMBERS))
if (!interaction.guild.me.permissions.has(Permissions.FLAGS.KICK_MEMBERS))
return await interaction.editReply("I do not have permission to kick members in this server.");
await interaction.member.send("You requested to leave the server, if you change your mind you can rejoin at a later date.");
await interaction.member.kick(`${interaction.member.displayName} wanted to leave.`);
Expand All @@ -16,5 +16,9 @@ exports.commandData = {
defaultPermission: true,
};

// Set this to false if you want it to be global.
exports.guildOnly = true;
// Set guildOnly to true if you want it to be available on guilds only.
// Otherwise false is global.
exports.conf = {
permLevel: "User",
guildOnly: true
};
8 changes: 6 additions & 2 deletions slash/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ exports.commandData = {
defaultPermission: true,
};

// Set this to false if you want it to be global.
exports.guildOnly = false;
// Set guildOnly to true if you want it to be available on guilds only.
// Otherwise false is global.
exports.conf = {
permLevel: "User",
guildOnly: false
};
8 changes: 6 additions & 2 deletions slash/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ exports.commandData = {
defaultPermission: true,
};

// Set this to false if you want it to be global.
exports.guildOnly = false;
// Set guildOnly to true if you want it to be available on guilds only.
// Otherwise false is global.
exports.conf = {
permLevel: "User",
guildOnly: false
};
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1892,9 +1892,9 @@
"readable-stream" "^3.1.1"

"tar@^6.1.0":
"integrity" "sha512-oaWyu5dQbHaYcyZCTfyPpC+VmI62/OM2RTUYavTk1MDr1cwW5Boi3baeYQKiZbY2uSQJGr+iMOzb/JFxLrft+g=="
"resolved" "https://registry.npmjs.org/tar/-/tar-6.1.6.tgz"
"version" "6.1.6"
"integrity" "sha512-an/KZQzQUkZCkuoAA64hM92X0Urb6VpRhAFllDzz44U2mcD5scmT3zBc4VgVpkugF580+DQn8eAFSyoQt0tznA=="
"resolved" "https://registry.npmjs.org/tar/-/tar-6.1.11.tgz"
"version" "6.1.11"
dependencies:
"chownr" "^2.0.0"
"fs-minipass" "^2.0.0"
Expand Down