Skip to content

Commit

Permalink
added: permission system in event interactionCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
devjhoan committed Apr 10, 2022
1 parent f14292c commit a2d0fc7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
2 changes: 0 additions & 2 deletions commands/general/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { CommandInteraction, MessageEmbed, MessageActionRow, MessageButton } = require("discord.js");
const { havePerms } = require("../../controllers/ticketChecks");
const dataGuild = require("../../models/dataGuild");

module.exports = {
Expand All @@ -13,7 +12,6 @@ module.exports = {
* @param {String[]} args
*/
run: async (client, interaction, args) => {
if (!(await havePerms(interaction))) return;
let transcript_channel, staff_role, staff_mention;
await interaction.reply({embeds: [
new MessageEmbed()
Expand Down
49 changes: 47 additions & 2 deletions controllers/ticketChecks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { CommandInteraction } = require("discord.js");
const { CommandInteraction, MessageEmbed } = require("discord.js");
const dataGuild = require("../models/dataGuild");
const dataTicket = require("../models/dataTicket");

/**
Expand Down Expand Up @@ -33,6 +34,50 @@ async function isTicket(interaction) {
}
}

/**
*
* @param {CommandInteraction} interaction
* @returns {Promise<{Boolean}>}
*/
async function havePerms(interaction) {
const guildData = await dataGuild.findOne({
guildID: interaction.guild.id
});
if (!guildData) {
interaction.reply({embeds: [
new MessageEmbed()
.setTitle("Ticket System \❌")
.setDescription(interaction.client.languages.__("errors.server_without_tickets"))
.setFooter({text: "Ticket System by: Jhoan#6969", iconURL: interaction.client.user.displayAvatarURL()})
.setColor("RED")
]});
return false;
}
if (!guildData.staffRole && !interaction.member.permissions.has("ADMINISTRATOR")) {
interaction.reply({embeds: [
new MessageEmbed()
.setTitle("Ticket System \❌")
.setDescription(interaction.client.languages.__("errors.no_staff_role"))
.setFooter({text: "Ticket System by: Jhoan#6969", iconURL: interaction.client.user.displayAvatarURL()})
.setColor("RED")
]});
return false;
}

if (!interaction.member.roles.cache.has(guildData.staffRole) && !interaction.member.permissions.has("ADMINISTRATOR")) {
interaction.reply({embeds: [
new MessageEmbed()
.setTitle("Ticket System \❌")
.setDescription(interaction.client.languages.__("errors.no_permission"))
.setFooter({text: "Ticket System by: Jhoan#6969", iconURL: interaction.client.user.displayAvatarURL()})
.setColor("RED")
]});
return false;
}
return true;
}

module.exports = {
isTicket
isTicket,
havePerms
}
2 changes: 2 additions & 0 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { error } = require("../controllers/logger");
const { havePerms } = require("../controllers/ticketChecks");
const client = require("../index");

client.on("interactionCreate", async (interaction) => {
Expand All @@ -24,6 +25,7 @@ client.on("interactionCreate", async (interaction) => {
}

interaction.member = interaction.guild.members.cache.get(interaction.user.id);
if (!(await havePerms(interaction))) return;
command.run(client, interaction, args);
}
});
4 changes: 3 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@
"ticket_already_claimed": "This ticket is already claimed by someone else.",
"transcript_channel_not_found": "The transcript channel was not found.",
"ticket_already_open": "This ticket is already open.",
"ticket_not_closed": "This ticket is not closed."
"ticket_not_closed": "This ticket is not closed.",
"no_staff_role": "This server has no set up a staff role.\nPlease use `config` command to setup the staff role.",
"no_permission": "You don't have permission to use this command."
}
}
4 changes: 3 additions & 1 deletion locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@
"ticket_already_claimed": "El ticket ya está reclamado por otro usuario.",
"transcript_channel_not_found": "No se encontró el canal de transcripción.",
"ticket_already_open": "El ticket ya está abierto.",
"ticket_not_closed": "El ticket no está cerrado."
"ticket_not_closed": "El ticket no está cerrado.",
"no_staff_role": "Este servidor no tiene configurado un rol de personal.\nUtilice el comando `config` para configurar el rol de personal.",
"no_permission": "Usted no tiene permiso para ejecutar este comando."
}
}

0 comments on commit a2d0fc7

Please sign in to comment.