Skip to content

Commit

Permalink
feat: command permissions v2
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Jun 19, 2022
1 parent 810a253 commit 28021d2
Show file tree
Hide file tree
Showing 40 changed files with 538 additions and 184 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/node_modules
/commands/dev/eval.js
eval.js
database.sqlite3
/backup
.env
Expand Down
5 changes: 4 additions & 1 deletion commands/bot/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ module.exports = {
const nog = '<:nog:676105350306594819>';
const gas = '<:gas:896370532751147028>';
const aytchSoftware = '<:AytchSoftware:720949593696894996>';

await client.application.fetch();

message.channel.send({
embeds: [{
title: 'Links',
fields: [
{
name: `Want to remove ${nog} in your server?`,
value: `${gas} Invite the bot [here](${client.generateInvite(client.config.invite)})`
value: `${gas} Invite the bot [here](${client.generateInvite(client.application.installParams ?? client.config.invite)})`
},
{
name: 'Want to support the bot?',
Expand Down
2 changes: 1 addition & 1 deletion commands/fun/huh.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
embeds: [{
title: 'huh',
image: {
url: 'https://cdn.discordapp.com/attachments/896351890609143808/896352500062498846/huh.png'
url: 'https://cdn.discordapp.com/emojis/805862278766395483.png?size=4096&quality=lossless'
},
color: client.config.color
}]
Expand Down
2 changes: 1 addition & 1 deletion commands/fun/tank.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
embeds: [{
title: 'THE ULTIMATE G DESTROYER',
image: {
url: 'https://cdn.discordapp.com/attachments/896351395484164116/896351420461240390/tank.png'
url: 'https://cdn.discordapp.com/attachments/713675042143076356/988129506151776346/tank.png'
},
color: client.config.color
}]
Expand Down
2 changes: 1 addition & 1 deletion commands/fun/toilet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
embeds: [{
title: 'polish toilet',
image: {
url: 'https://cdn.discordapp.com/attachments/896351890609143808/896351917872136222/htoilet.gif'
url: 'https://c.tenor.com/4vgPhxKQw_MAAAAS/polish-toilet.gif'
},
color: client.config.color
}]
Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"guildLogs": "754681399499423764",
"globalLogs": "805472059790589974",
"testServer": "764591810374664193",
"support": "https://discord.gg/z2QsKF7ZJ6",
"gasServer": "https://discord.gg/qs9QDW5M5J",
"docs": "https://h-projects.github.io/docs/bots/gas",
Expand Down
33 changes: 14 additions & 19 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ module.exports = {
name: 'interactionCreate',
once: false,
async execute(interaction, client) {
if (!interaction.inCachedGuild()) return;
if (interaction.inRawGuild()) {
return interaction.reply({
content: 'The bot wasn\'t invited correctly, please invite it with the correct scopes',
ephemeral: true
});
}

if (interaction.isApplicationCommand()) {
const name = interaction.commandName;
const command = client.interactions.commands.get(name) ?? client.interactions.commands.find(c => c.contextMenu === name);
const command = client.interactions.commands.get(name) ?? client.interactions.commands.find(c => c.contextData?.name === name);

if (!command) return;

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

if (!interaction.guild.me.permissions.has(command.botPermissions ?? 0n)) {
if (interaction.guild && !interaction.guild.me.permissions.has(command.botPermissions ?? 0n)) {
return interaction.reply({
embeds: [{
title: 'Missinq Permissions',
Expand All @@ -28,25 +26,22 @@ module.exports = {
});
}

if (!client.config.developers.includes(interaction.user.id) && command.category === 'dev') {
return;
}

return command.execute(client, interaction);
}

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

if (!component || interaction.author !== interaction.user.id) {
return interaction.deferUpdate();
}

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

if (!interaction.guild.me.permissions.has(component.botPermissions ?? 0n)) {
if (interaction.guild && !interaction.guild.me.permissions.has(component.botPermissions ?? 0n)) {
return interaction.reply({
embeds: [{
title: 'Missinq Permissions',
Expand Down
2 changes: 1 addition & 1 deletion events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
return message.channel.send({
embeds: [{
title: 'Prefix',
description: `My prefix is \`${message.prefix}\``,
description: `My prefix is \`/\` but you can also use \`${message.prefix}\``,
color: client.config.color
}]
});
Expand Down
32 changes: 31 additions & 1 deletion events/ready.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
const { Routes } = require('discord-api-types/v10');

module.exports = {
name: 'ready',
once: true,
async execute() {
async execute(client) {
const commands = [];
const devCommands = [];

if (process.argv[2] === 'no-deploy') return console.log('Ready!');
client.interactions.commands.forEach(command => {
command.category === 'dev' ?
devCommands.push(command.data.toJSON()) :
commands.push(command.data.toJSON());

if (command.contextData) {
command.category === 'dev' ?
devCommands.push(command.contextData.toJSON()) :
commands.push(command.contextData.toJSON());
}
});

await client.restModule.put(Routes.applicationCommands(client.user.id), {
body: commands
});

console.log(`Deployed ${commands.length} global commands`);

await client.restModule.put(Routes.applicationGuildCommands(client.user.id, client.config.testServer), {
body: devCommands
});

console.log(`Deployed ${devCommands.length} dev commands`);

console.log('Ready!');
}
};
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require('dotenv/config');
const { readdirSync } = require('fs');
const { Client } = require('discord.js');
const { REST } = require('@discordjs/rest');
const Database = require('better-sqlite3');
const { Client } = require('discord.js');
const { readdirSync } = require('fs');
const { DJSPoster } = require('topgg-autoposter');

const client = new Client({
intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MESSAGE_REACTIONS', 'GUILD_MEMBERS'],
partials: ['MESSAGE', 'REACTION', 'USER', 'GUILD_MEMBER'],
Expand All @@ -20,8 +22,9 @@ if (process.env.NODE_ENV !== 'development') {
new DJSPoster(process.env.TOPGG_TOKEN, client);
}

client.db = new Database('database.sqlite3', { fileMustExist: true });
client.config = require('./config.json');
client.db = new Database('database.sqlite3', { fileMustExist: true });
client.restModule = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);

const loaders = readdirSync('./loaders').filter(file => file.endsWith('.js'));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const changelog = require('../../changelog.json');
const changelog = require('../../../changelog.json');
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
name: 'chanqeloq',
async execute(client, interaction) {
interaction.reply({
embeds: [{
Expand All @@ -14,5 +14,9 @@ module.exports = {
fields: changelog.features
}]
});
}
},

data: new SlashCommandBuilder()
.setName('chanqeloq')
.setDescription('Check the latest version')
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
name: 'credits',
async execute(client, interaction) {
const developers = (await Promise.all(client.config.developers.map(async id => (await client.users.fetch(id)).tag))).join('\n');
const specialThanksUsers = (await Promise.all(client.config.specialThanksUsers.map(async id => (await client.users.fetch(id)).tag))).join('\n');
Expand All @@ -13,5 +14,9 @@ module.exports = {
color: client.config.color
}]
});
}
},

data: new SlashCommandBuilder()
.setName('credits')
.setDescription('People who helped in the development')
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { SlashCommandBuilder } = require('@discordjs/builders');
const { PermissionFlagsBits } = require('discord-api-types/v10');

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 Expand Up @@ -69,5 +70,28 @@ module.exports = {
]
}]
});
}
},

data: new SlashCommandBuilder()
.setName('detector')
.setDescription('Manaqe the detection level')
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
.setDMPermission(false)
.addStringOption(option => option
.setName('level')
.setDescription('The new detection level')
.setRequired(false)
.addChoices(
{
name: 'Low',
value: 'low'
}, {
name: 'Medium',
value: 'medium'
}, {
name: 'Hiqh',
value: 'hiqh'
}
)
)
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { SlashCommandBuilder, ContextMenuCommandBuilder } = require('@discordjs/builders');
const { PermissionFlagsBits, ApplicationCommandType } = require('discord-api-types/v10');

module.exports = {
name: 'g-spy',
contextMenu: 'Mark as G Spy',
permissions: ['MANAGE_ROLES'],
botPermissions: ['MANAGE_ROLES'],
async execute(client, interaction) {
const member = interaction.options.getMember('user');
Expand Down Expand Up @@ -50,6 +50,21 @@ module.exports = {
}]
}]
});
}
},

data: new SlashCommandBuilder()
.setName('g-spy')
.setDescription('Mark a user as a g-spy')
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
.setDMPermission(false)
.addUserOption(option => option
.setName('user')
.setDescription('User to mark as g-spy')
),

contextData: new ContextMenuCommandBuilder()
.setName('Mark as G Spy')
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
.setDMPermission(false)
.setType(ApplicationCommandType.User)
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { SlashCommandBuilder } = require('@discordjs/builders');
const { stripIndents } = require('common-tags');
const { version } = require('discord.js');
const { version: botVersion } = require('../../package.json');
const { version: botVersion } = require('../../../package.json');

module.exports = {
name: 'info',
async execute(client, interaction) {
const developers = (await Promise.all(client.config.developers.map(async id => (await client.users.fetch(id)).tag))).join('\n');
interaction.reply({
Expand Down Expand Up @@ -39,5 +39,9 @@ module.exports = {
}
}]
});
}
},

data: new SlashCommandBuilder()
.setName('info')
.setDescription('Display information about the bot')
};
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
name: 'links',
async execute(client, interaction) {
const nog = '<:nog:676105350306594819>';
const gas = '<:gas:896370532751147028>';
const aytchSoftware = '<:AytchSoftware:720949593696894996>';

await client.application.fetch();

interaction.reply({
embeds: [{
title: 'Links',
fields: [
{
name: `Want to remove ${nog} in your server?`,
value: `${gas} Invite the bot [here](${client.generateInvite(client.config.invite)})`
value: `${gas} Invite the bot [here](${client.generateInvite(client.application.installParams ?? client.config.invite)})`
},
{
name: 'Want to support the bot?',
Expand All @@ -28,5 +32,9 @@ module.exports = {
color: client.config.color
}]
});
}
},

data: new SlashCommandBuilder()
.setName('links')
.setDescription('Useful bot links')
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { SlashCommandBuilder } = require('@discordjs/builders');
const { ChannelType, PermissionFlagsBits } = require('discord-api-types/v10');

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 Expand Up @@ -44,5 +45,17 @@ module.exports = {
}]
}]
});
}
},

data: new SlashCommandBuilder()
.setName('loqs')
.setDescription('Manaqe the loqs channel')
.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels)
.setDMPermission(false)
.addChannelOption(option => option
.setName('channel')
.setDescription('Set loqs channel')
.addChannelTypes(ChannelType.GuildText, ChannelType.GuildNews)
.setRequired(false)
)
};
Loading

0 comments on commit 28021d2

Please sign in to comment.