Skip to content

Commit

Permalink
feat: permissions check
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Sep 25, 2021
1 parent 1fe4a04 commit 7eb48d6
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 16 deletions.
3 changes: 1 addition & 2 deletions commands/bot/detector.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module.exports = {
name: 'detector',
description: 'Manaqe the detection level',
permissions: 'MANAGE_MESSAGES',

permissions: ['MANAGE_MESSAGES'],
async execute(client, message) {
const level = client.db.prepare('SELECT level FROM guilds WHERE id = ?').get(message.guild.id)?.level ?? 1;
const levelNames = { 0: 'Low', 1: 'Medium', 2: 'Hiqh' };
Expand Down
3 changes: 1 addition & 2 deletions commands/bot/g-spy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module.exports = {
name: 'g-spy',
description: 'Mark an user as a g-spy',
permissions: ['MANAGE_MESSAGES', 'MANAGE_ROLES'],

permissions: ['MANAGE_ROLES'],
async execute(client, message) {
const userId = /\d+/u.exec(message.content)?.toString();
const member = userId ? await message.guild.members.fetch(userId).catch(() => null) : null;
Expand Down
1 change: 0 additions & 1 deletion commands/bot/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module.exports = {
name: 'help',
description: 'Views all commands in the bot',
hidden: true,

async execute(client, message) {
const buttons = [
{
Expand Down
1 change: 1 addition & 0 deletions commands/bot/logs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
name: 'loqs',
description: 'Manaqe the loqs channel',
permissions: ['MANAGE_CHANNELS'],
async execute(client, message, args) {
const database = client.db.prepare('SELECT logs FROM guilds WHERE id = ?').get(message.guildId);
const statement = database ? 'UPDATE guilds SET logs = @logs WHERE id = @id' : 'INSERT INTO guilds (id, logs) VALUES (@id, @logs)';
Expand Down
1 change: 0 additions & 1 deletion commands/bot/ping.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = {
name: 'pinq',
description: 'Pinq of G.A.S Bot',

async execute(client, message) {
message.channel.send({
embeds: [{
Expand Down
2 changes: 1 addition & 1 deletion commands/bot/prefix.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
name: 'prefix',
description: 'Manaqe the bot\'s prefix',

permissions: ['MANAGE_MESSAGES'],
async execute(client, message, args) {
const database = client.db.prepare('SELECT prefix FROM guilds WHERE id = ?').get(message.guildId);
const statement = database ? 'UPDATE guilds SET prefix = @prefix WHERE id = @id' : 'INSERT INTO guilds (id, prefix) VALUES (@id, @prefix)';
Expand Down
22 changes: 18 additions & 4 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,35 @@ module.exports = {
const name = interaction.commandName;
const command = client.interactions.commands.get(name) ?? client.interactions.commands.find(cmd => name === cmd.contextMenu);

if (!command) return;
if (!command || !interaction.inGuild()) return;

if (!interaction.member.permissions.has(command.permissions ?? 0)) {
return interaction.reply({
content: `You need the \`${command.permissions}\` permission to use this command`,
ephemeral: true
});
}

command.execute(client, interaction);
break;


case 'MESSAGE_COMPONENT':

[interaction.name, interaction.value, interaction.author] = interaction.customId.split(':');
const component = client.interactions.components.get(interaction.name);

if (!client.interactions.components.has(interaction.name) || interaction.author !== interaction.user.id) {
if (!component || interaction.author !== interaction.user.id) {
return interaction.deferUpdate();
}

client.interactions.components.get(interaction.name).execute(client, interaction);
if (!interaction.member.permissions.has(component.permissions ?? 0)) {
return interaction.reply({
content: `You need the \`${command.permissions}\` permission to use this component`,
ephemeral: true
});
}

component.execute(client, interaction);
break;
}
}
Expand Down
5 changes: 5 additions & 0 deletions events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ module.exports = {


if (!client.commands.has(command) || !message.content.startsWith(message.prefix)) return;

if (!message.member.permissions.has(command.permissions ?? 0)) {
return message.channel.send(`You need the \`${command.permissions}\` permission to use this command`);
}

client.commands.get(command).execute(client, message, args);
}
};
2 changes: 1 addition & 1 deletion interactions/commands/detector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
name: 'detector',

permissions: ['MANAGE_MESSAGES'],
async execute(client, interaction) {
const input = interaction.options.getString('level');
const database = client.db.prepare('SELECT level FROM guilds WHERE id = ?').get(interaction.guildId);
Expand Down
1 change: 1 addition & 0 deletions interactions/commands/g-spy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
name: 'g-spy',
contextMenu: 'Mark As G Spy',
permissions: ['MANAGE_ROLES'],
async execute(client, interaction) {
const member = interaction.options.getMember('user');

Expand Down
2 changes: 1 addition & 1 deletion interactions/commands/logs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
name: 'loqs',

permissions: ['MANAGE_CHANNELS'],
async execute(client, interaction) {
const database = client.db.prepare('SELECT logs FROM guilds WHERE id = ?').get(interaction.guildId);
const statement = database ? 'UPDATE guilds SET logs = @logs WHERE id = @id' : 'INSERT INTO guilds (id, logs) VALUES (@id, @logs)';
Expand Down
1 change: 0 additions & 1 deletion interactions/commands/ping.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module.exports = {
name: 'pinq',

async execute(client, interaction) {
interaction.reply({
embeds: [{
Expand Down
2 changes: 1 addition & 1 deletion interactions/components/detector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
name: 'detector',

permissions: ['MANAGE_MESSAGES'],
async execute(client, interaction) {
const database = client.db.prepare('SELECT level FROM guilds WHERE id = ?').get(interaction.guildId);
const statement = database ? 'UPDATE guilds SET level = @level WHERE id = @id' : 'INSERT INTO guilds (id, level) VALUES (@id, @level)';
Expand Down
2 changes: 1 addition & 1 deletion interactions/components/g-spy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
name: 'g-spy',

permissions: ['MANAGE_ROLES'],
async execute(client, interaction) {
const member = await interaction.guild.members.fetch(interaction.value).catch(() => null);
const role = interaction.guild.roles.cache.find(r => r.name === 'g-spy');
Expand Down

0 comments on commit 7eb48d6

Please sign in to comment.