diff --git a/commands/debug.js b/commands/debug.js index dbafd73..744b790 100644 --- a/commands/debug.js +++ b/commands/debug.js @@ -63,8 +63,8 @@ module.exports = { socket.emit('botping', {message:'hello there'}); socket.on('botpong', (data) => { if (debugConsole) client.log('Debug: Socket responded') - return interaction.send({ content: `Debug: Socket responded` }) socket.disconnect(); + return interaction.send({ content: `Debug: Socket responded` }) }); } else if (command == "apicheck") { diff --git a/commands/updateLicense.js b/commands/updateLicense.js index f216bc8..a8d42ed 100644 --- a/commands/updateLicense.js +++ b/commands/updateLicense.js @@ -104,10 +104,14 @@ module.exports = { ) } - interaction.send({ embeds: [nameResult], components: [row] }); - - client.on("interactionCreate", ButtonInteraction => { - const socket = io.connect(client.config.socket); + return interaction.send({ embeds: [nameResult], components: [row] }); + }); + }, + }, + Interactions: { + license: { + run: async (client, ButtonInteraction, { GuildDB }) => { + const socket = io.connect(client.config.socket); let queryString = ButtonInteraction.customId; let query = { @@ -124,12 +128,11 @@ module.exports = { } socket.emit("update_drivers_license_status", query); socket.on("bot_updated_drivers_license_status", (res) => { - if (!res.success) ButtonInteraction.update({ content: 'Failed to update license.', embeds: [], components: [] }); socket.disconnect(); + if (!res.success) return ButtonInteraction.update({ content: 'Failed to update license.', embeds: [], components: [] }); }); return ButtonInteraction.update({ content: 'Successfully updated license.', embeds: [], components: [] }); - }); - }); - }, - }, + } + } + } } \ No newline at end of file diff --git a/commands/updateStatus.js b/commands/updateStatus.js index 29e624a..8b6bed5 100644 --- a/commands/updateStatus.js +++ b/commands/updateStatus.js @@ -66,9 +66,10 @@ module.exports = { const socket = io.connect(client.config.socket); socket.emit('bot_update_status', req); socket.on('bot_updated_status', (res) => { - interaction.send({ content: `Succesfully updated <@${interaction.member.user.id}>'s status to \`${args[0].value}\`` }); socket.disconnect(); - }); + return interaction.send({ content: `Succesfully updated <@${interaction.member.user.id}>'s status to \`${args[0].value}\`` }); + }); + return; }, }, } \ No newline at end of file diff --git a/config/config.js b/config/config.js index c96460c..fadfa63 100644 --- a/config/config.js +++ b/config/config.js @@ -2,7 +2,7 @@ require('dotenv').config() module.exports = { Dev: "PRODUCTION", - Version: "3.1.3", + Version: "3.1.4", Admins: ["362791661274660874"], // Admins of the bot DefaultPrefix: "?", socket: "https://www.linespolice-cad.com/", diff --git a/events/interactionCreate.js b/events/interactionCreate.js new file mode 100644 index 0000000..8a81e6e --- /dev/null +++ b/events/interactionCreate.js @@ -0,0 +1,13 @@ +module.exports = async (client, interaction) => { + if (interaction.isCommand()) return; + + /* + This file hanldes all button interactions from any command + */ + + let GuildDB = await client.GetGuild(interaction.guildId); + const interactionName = interaction.customId.split('-')[0]; + let interactionHandler = client.interactionHandlers.get(interactionName); + + interactionHandler.run(client, interaction, GuildDB); +} \ No newline at end of file diff --git a/structures/LinesPoliceCadBot.js b/structures/LinesPoliceCadBot.js index 4f0254d..c3d0467 100644 --- a/structures/LinesPoliceCadBot.js +++ b/structures/LinesPoliceCadBot.js @@ -13,6 +13,7 @@ class LinesPoliceCadBot extends Client { this.config = config; this.commands = new Collection(); + this.interactionHandlers = new Collection(); this.logger = new Logger(path.join(__dirname, "..", "logs/Logs.log")); if (this.config.Token === "") @@ -23,7 +24,7 @@ class LinesPoliceCadBot extends Client { this.db; this.dbo; this.connectMongo(this.config.mongoURI, this.config.dbo); - this.LoadCommands(); + this.LoadCommandsAndInteractionHandlers(); this.LoadEvents(); this.Ready = false; @@ -121,7 +122,7 @@ class LinesPoliceCadBot extends Client { exists(n) {return null != n && undefined != n && "" != n} - LoadCommands() { + LoadCommandsAndInteractionHandlers() { let CommandsDir = path.join(__dirname, '..', 'commands'); fs.readdir(CommandsDir, (err, files) => { if (err) this.log(err); @@ -135,6 +136,11 @@ class LinesPoliceCadBot extends Client { ", Reason: File doesn't had run/name/desciption" ); this.commands.set(file.split(".")[0].toLowerCase(), cmd); + if (this.exists(cmd.Interactions)) { + for (let [interaction, handler] of Object.entries(cmd.Interactions)) { + this.interactionHandlers.set(interaction, handler); + } + } this.log("Command Loaded: " + file.split(".")[0]); }); });