Use roles and owner IDs to restrict commands. This will make it so only the server owner and admins can use commands. Works if the bot is in multiple servers as well.
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let canUseAdminCommand = false;
let guildInfo = await lib.discord.guilds['@0.1.0'].retrieve({
guild_id: `${context.params.event.guild_id}`
});
let roles = await lib.discord.guilds['@0.1.0'].roles.list({
guild_id: `${context.params.event.guild_id}`
});
let userRoles = roles.filter((role) => context.params.event.member.roles.includes(role.id));
for (let i = 0; i < userRoles.length; i++) {
let _role = userRoles[i];
if (_role.permission_names.includes('ADMINISTRATOR')) {
canUseAdminCommands = true;
break;
}
}
if (guildInfo.owner_id === context.params.event.author.id) {
canUseAdminCommands = true;
}
if (canUseAdminCommands) {
// All admin commands should go here!
if (context.params.event.content.startsWith('!admin')) {
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `π You can use admin commands!`
});
}
} else {
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `π‘ You are not allowed to use admin commands!`
});
}