From df698fe812f74cd3e9da1fb5b8571ec6cf653b31 Mon Sep 17 00:00:00 2001 From: xhyrom Date: Fri, 25 Jun 2021 17:33:11 +0200 Subject: [PATCH 01/10] .send() --- src/structures/DMChannel.js | 1 + src/structures/NewsChannel.js | 1 + src/structures/TextChannel.js | 1 + 3 files changed, 3 insertions(+) diff --git a/src/structures/DMChannel.js b/src/structures/DMChannel.js index 9f993988b..65647df28 100644 --- a/src/structures/DMChannel.js +++ b/src/structures/DMChannel.js @@ -18,6 +18,7 @@ module.exports = Structures.extend("DMChannel", DMChannel => { } if(typeof result != "object") data.content = result; + if(typeof result == "object" && !result.content) data.embeds = [result.content]; if(typeof result == "object" && typeof result.content != "object") data.content = result.content; if(typeof result == "object" && typeof result.content == "object") data.embeds = [result.content]; if(typeof result == "object" && result.allowedMentions) { data.allowedMentions = result.allowedMentions } else data.allowedMentions = { parse: [], repliedUser: true } diff --git a/src/structures/NewsChannel.js b/src/structures/NewsChannel.js index 63b8ddf21..923c30b5c 100644 --- a/src/structures/NewsChannel.js +++ b/src/structures/NewsChannel.js @@ -18,6 +18,7 @@ module.exports = Structures.extend("NewsChannel", NewsChannel => { } if(typeof result != "object") data.content = result; + if(typeof result == "object" && !result.content) data.embeds = [result.content]; if(typeof result == "object" && typeof result.content != "object") data.content = result.content; if(typeof result == "object" && typeof result.content == "object") data.embeds = [result.content]; if(typeof result == "object" && result.allowedMentions) { data.allowedMentions = result.allowedMentions } else data.allowedMentions = { parse: [], repliedUser: true } diff --git a/src/structures/TextChannel.js b/src/structures/TextChannel.js index 1b87de108..e9aabd276 100644 --- a/src/structures/TextChannel.js +++ b/src/structures/TextChannel.js @@ -16,6 +16,7 @@ module.exports = Structures.extend("TextChannel", TextChannel => { } if(typeof result != "object") data.content = result; + if(typeof result == "object" && !result.content) data.embeds = [result.content]; if(typeof result == "object" && typeof result.content != "object") data.content = result.content; if(typeof result == "object" && typeof result.content == "object") data.embeds = [result.content]; if(typeof result == "object" && result.allowedMentions) { data.allowedMentions = result.allowedMentions } else data.allowedMentions = { parse: [], repliedUser: true } From b3f363888326de7e3a7d497ac1f7e8d50c08e742 Mon Sep 17 00:00:00 2001 From: xhyrom Date: Mon, 28 Jun 2021 20:45:34 +0200 Subject: [PATCH 02/10] setLanguage fix --- src/base/GCommandsDispatcher.js | 2 +- src/structures/guild.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/base/GCommandsDispatcher.js b/src/base/GCommandsDispatcher.js index 76c5b90d6..9cbea8b4b 100644 --- a/src/base/GCommandsDispatcher.js +++ b/src/base/GCommandsDispatcher.js @@ -22,7 +22,7 @@ class GCommandsDispatcher { * Internal method to setGuildPrefix * @returns {boolean} */ - async setGuildPrefix(prefix, guildId) { + async setGuildPrefix(guildId, prefix) { if(!this.client.database) return false; let guildData = await this.client.database.get(`guild_${guildId}`) || {} diff --git a/src/structures/guild.js b/src/structures/guild.js index 17e3c4dfd..5435e1e8f 100644 --- a/src/structures/guild.js +++ b/src/structures/guild.js @@ -24,7 +24,7 @@ module.exports = Structures.extend('Guild', Guild => { * @returns {Boolean} */ async setCommandPrefix(prefix) { - this.client.dispatcher.setGuildPrefix(prefix, this.id); + this.client.dispatcher.setGuildPrefix(this.id, prefix); this.client.emit('commandPrefixChange', this, this.prefix); } @@ -41,7 +41,7 @@ module.exports = Structures.extend('Guild', Guild => { * @returns {Boolean} */ async setLanguage(lang) { - this.client.dispatcher.setGuildLanguage(lang, this.id); + this.client.dispatcher.setGuildLanguage(this.id, lang); this.client.emit('guildLanguageChange', this, this.language); } } From f657e057795a9b2d64783ed562e2062c5a29342f Mon Sep 17 00:00:00 2001 From: xhyrom Date: Thu, 1 Jul 2021 09:49:15 +0200 Subject: [PATCH 03/10] dropdowns --- src/base/GCommandsDispatcher.js | 34 ++- src/base/actions/interactions.js | 14 +- src/index.js | 7 +- src/managers/GEventLoader.js | 4 + src/structures/ButtonEvent.js | 4 + src/structures/DMChannel.js | 20 +- src/structures/MessageButton.js | 2 +- src/structures/MessageSelectMenu.js | 99 +++++++++ src/structures/MessageSelectOption.js | 105 ++++++++++ src/structures/NewsChannel.js | 20 +- src/structures/SelectMenuEvent.js | 244 ++++++++++++++++++++++ src/structures/TextChannel.js | 20 +- src/structures/message.js | 20 +- src/structures/v12/SelectMenuCollector.js | 86 ++++++++ src/structures/v13/SelectMenuCollector.js | 86 ++++++++ 15 files changed, 753 insertions(+), 12 deletions(-) create mode 100644 src/structures/MessageSelectMenu.js create mode 100644 src/structures/MessageSelectOption.js create mode 100644 src/structures/SelectMenuEvent.js create mode 100644 src/structures/v12/SelectMenuCollector.js create mode 100644 src/structures/v13/SelectMenuCollector.js diff --git a/src/base/GCommandsDispatcher.js b/src/base/GCommandsDispatcher.js index 9cbea8b4b..78361232e 100644 --- a/src/base/GCommandsDispatcher.js +++ b/src/base/GCommandsDispatcher.js @@ -1,7 +1,7 @@ const { Collector, Collection } = require('discord.js'); -const ButtonCollectorV12 = require('../structures/v12/ButtonCollector'), ButtonCollectorV13 = require('../structures/v13/ButtonCollector'), Color = require("../structures/Color") +const ButtonCollectorV12 = require('../structures/v12/ButtonCollector'), ButtonCollectorV13 = require('../structures/v13/ButtonCollector'), SelectMenuCollectorV12 = require('../structures/v12/SelectMenuCollector'), SelectMenuCollectorV13 = require('../structures/v13/SelectMenuCollector'), Color = require("../structures/Color") const updater = require("../util/updater"); -const ms = require("ms") +const ms = require("ms"); /** * The GCommansDispatcher class @@ -207,6 +207,36 @@ class GCommandsDispatcher { }); }) } + + /** + * Internal method to createSelectMenuCollector + * @param {Function} filter + * @param {Object} options + * @returns {Collector} + */ + createSelectMenuCollector(msg, filter, options = {}) { + if(updater.checkDjsVersion("13")) return new SelectMenuCollectorV13(msg, filter, options); + else return new SelectMenuCollectorV12(msg, filter, options); + } + + /** + * Internal method to createButtonCollector + * @param {Function} filter + * @param {Object} options + * @returns {Collector} + */ + awaitSelectMenus(msg, filter, options = {}) { + return new Promise((resolve, reject) => { + const collector = this.createSelectMenuCollector(msg, filter, options); + collector.once('end', (buttons, reason) => { + if (options.errors && options.errors.includes(reason)) { + reject(buttons); + } else { + resolve(buttons); + } + }); + }) + } } module.exports = GCommandsDispatcher; \ No newline at end of file diff --git a/src/base/actions/interactions.js b/src/base/actions/interactions.js index b903f6b2d..299f3503e 100644 --- a/src/base/actions/interactions.js +++ b/src/base/actions/interactions.js @@ -1,11 +1,17 @@ -const ButtonEvent = require("../../structures/ButtonEvent") - +const ButtonEvent = require("../../structures/ButtonEvent"); +const SelectMenuEvent = require("../../structures/SelectMenuEvent"); + module.exports = (client) => { client.ws.on('INTERACTION_CREATE', data => { - if(data.type != 3) return; if (!data.message) return; - if (data.data.component_type === 2) { + if(data.data.component_type == 3) { + const dropdown = new SelectMenuEvent(client, data) + + client.emit(`selectMenu`, dropdown) + } + + if (data.data.component_type == 2) { const button = new ButtonEvent(client, data); client.emit('clickButton', button); diff --git a/src/index.js b/src/index.js index 6d17f49a8..38780377a 100644 --- a/src/index.js +++ b/src/index.js @@ -20,10 +20,15 @@ module.exports = { GDMChannel: require("./structures/DMChannel"), MessageButton: require("./structures/MessageButton"), MessageActionRow: require("./structures/MessageActionRow"), - + MessageSelectMenu: require("./structures/MessageSelectMenu"), + MessageSelectOption: require("./structures/MessageSelectOption"), + ButtonCollectorV12: require("./structures/v13/ButtonCollector"), ButtonCollectorV13: require("./structures/v12/ButtonCollector"), + SelectMenuCollectorV12: require("./structures/v13/SelectMenuCollector"), + SelectMenuCollectorV13: require("./structures/v12/SelectMenuCollector"), + // Other Color: require("./structures/Color"), Util: require("./util/util"), diff --git a/src/managers/GEventLoader.js b/src/managers/GEventLoader.js index 53b6f92a3..bbe43ca45 100644 --- a/src/managers/GEventLoader.js +++ b/src/managers/GEventLoader.js @@ -569,6 +569,8 @@ class GEventLoader { apiMessage.client = this.client ? this.client : client; apiMessage.createButtonCollector = function createButtonCollector(filter, options) {return this.client.dispatcher.createButtonCollector(apiMessage, filter, options)}; apiMessage.awaitButtons = function awaitButtons(filter, options) {return this.client.dispatcher.awaitButtons(apiMessage, filter, options)}; + apiMessage.createSelectMenuCollector = function createSelectMenuCollector(filter, options) {return this.client.dispatcher.createSelectMenuCollector(apiMessage, filter, options)}; + apiMessage.awaitSelectMenus = function awaitSelectMenus(filter, options) {return this.client.dispatcher.awaitSelectMenus(apiMessage, filter, options)}; apiMessage.delete = function deleteMsg() {return this.client.api.webhooks(this.client.user.id, interaction.token).messages[apiMessageMsg.id].delete()}; } @@ -617,6 +619,8 @@ class GEventLoader { apiMessage.client = this.client; apiMessage.createButtonCollector = function createButtonCollector(filter, options) {return this.client.dispatcher.createButtonCollector(apiMessage, filter, options)}; apiMessage.awaitButtons = function awaitButtons(filter, options) {return this.client.dispatcher.awaitButtons(apiMessage, filter, options)}; + apiMessage.createSelectMenuCollector = function createSelectMenuCollector(filter, options) {return this.client.dispatcher.createSelectMenuCollector(apiMessage, filter, options)}; + apiMessage.awaitSelectMenus = function awaitSelectMenus(filter, options) {return this.client.dispatcher.awaitSelectMenus(apiMessage, filter, options)}; apiMessage.delete = function deleteMsg() {return this.client.api.webhooks(this.client.user.id, interaction.token).messages[apiMessage.id].delete()}; } diff --git a/src/structures/ButtonEvent.js b/src/structures/ButtonEvent.js index dee26e1e6..f3abfdaae 100644 --- a/src/structures/ButtonEvent.js +++ b/src/structures/ButtonEvent.js @@ -179,6 +179,8 @@ class ButtonEvent { apiMessage.client = this.client ? this.client : client; apiMessage.createButtonCollector = function createButtonCollector(filter, options) {return this.client.dispatcher.createButtonCollector(apiMessage, filter, options)}; apiMessage.awaitButtons = function awaitButtons(filter, options) {return this.client.dispatcher.awaitButtons(apiMessage, filter, options)}; + apiMessage.createSelectMenuCollector = function createSelectMenuCollector(filter, options) {return this.client.dispatcher.createSelectMenuCollector(apiMessage, filter, options)}; + apiMessage.awaitSelectMenus = function awaitSelectMenus(filter, options) {return this.client.dispatcher.awaitSelectMenus(apiMessage, filter, options)}; apiMessage.delete = function deleteMsg() {return this.client.api.webhooks(this.client.user.id, interaction.token).messages[apiMessageMsg.id].delete()}; } @@ -226,6 +228,8 @@ class ButtonEvent { apiMessage.client = this.client; apiMessage.createButtonCollector = function createButtonCollector(filter, options) {return this.client.dispatcher.createButtonCollector(apiMessage, filter, options)}; apiMessage.awaitButtons = function awaitButtons(filter, options) {return this.client.dispatcher.awaitButtons(apiMessage, filter, options)}; + apiMessage.createSelectMenuCollector = function createSelectMenuCollector(filter, options) {return this.client.dispatcher.createSelectMenuCollector(apiMessage, filter, options)}; + apiMessage.awaitSelectMenus = function awaitSelectMenus(filter, options) {return this.client.dispatcher.awaitSelectMenus(apiMessage, filter, options)}; apiMessage.delete = function deleteMsg() {return this.client.api.webhooks(this.client.user.id, interaction.token).messages[apiMessage.id].delete()}; } diff --git a/src/structures/DMChannel.js b/src/structures/DMChannel.js index 65647df28..c3a0016d3 100644 --- a/src/structures/DMChannel.js +++ b/src/structures/DMChannel.js @@ -1,5 +1,5 @@ const { Structures, MessageEmbed } = require("discord.js"); -const ButtonCollectorV12 = require('../structures/v12/ButtonCollector'), ButtonCollectorV13 = require('../structures/v13/ButtonCollector'), { createAPIMessage } = require("../util/util") +const ButtonCollectorV12 = require('../structures/v12/ButtonCollector'), ButtonCollectorV13 = require('../structures/v13/ButtonCollector'), SelectMenuCollectorV12 = require('../structures/v12/SelectMenuCollector'), SelectMenuCollectorV13 = require('../structures/v13/SelectMenuCollector'), { createAPIMessage } = require("../util/util") const updater = require("../util/updater") module.exports = Structures.extend("DMChannel", DMChannel => { @@ -68,6 +68,24 @@ module.exports = Structures.extend("DMChannel", DMChannel => { }); }) } + + createSelectMenuCollector(msg, filter, options = {}) { + if(updater.checkDjsVersion("13")) return new SelectMenuCollectorV13(msg, filter, options); + else return new SelectMenuCollectorV12(msg, filter, options); + } + + awaitSelectMenus(msg, filter, options = {}) { + return new Promise((resolve, reject) => { + const collector = this.createSelectMenuCollector(msg, filter, options); + collector.once('end', (buttons, reason) => { + if (options.errors && options.errors.includes(reason)) { + reject(buttons); + } else { + resolve(buttons); + } + }); + }) + } } return GDMChannel; diff --git a/src/structures/MessageButton.js b/src/structures/MessageButton.js index 6871cc992..06b75538e 100644 --- a/src/structures/MessageButton.js +++ b/src/structures/MessageButton.js @@ -55,7 +55,7 @@ class MessageButton { /** * Method to setLabel - * @param {String} style + * @param {String} label */ setLabel(label) { this.label = resolveString(label); diff --git a/src/structures/MessageSelectMenu.js b/src/structures/MessageSelectMenu.js new file mode 100644 index 000000000..87b7a33ee --- /dev/null +++ b/src/structures/MessageSelectMenu.js @@ -0,0 +1,99 @@ +const { resolveString } = require("../util/util"); +const Color = require("../structures/Color") +const styles = { + 'blurple': 1, + 'gray': 2, + 'grey': 2, + 'green': 3, + 'red': 4, + 'url': 5, + 'primary': 1, + 'secondary': 2, + 'success': 3, + 'danger': 4, + 'link': 5 +}; + +/** + * The MessageSelectMenu class + */ +class MessageSelectMenu { + + /** + * Creates new MessageSelectMenu instance + * @param {Object} data + */ + constructor(data = {}) { + this.options = []; + + this.setup(data); + } + + setup(data) { + this.type = 3; + + return this.toJSON(); + } + + /** + * Method to setDisabled + * @param {String} boolean + */ + setPlaceholder(string) { + this.placeholder = resolveString(string); + return this; + } + + /** + * Method to setMaxValues + * @param {Number} int + */ + setMaxValues(int = 1) { + this.max_values = Number(int) + return this; + } + + /** + * Method to setMinValues + * @param {Number} int + */ + setMinValues(int = 1) { + this.min_values = Number(int) + return this; + } + + /** + * Method to setID + * @param {String} id + */ + setID(id) { + this.custom_id = this.style === 5 ? null : resolveString(id); + return this; + } + + /** + * Method to addOption + * @param {Object} MessageSelectOption + */ + addOption(option) { + this.options.push(option) + return this; + } + + /** + * Method to toJSON + * @return {Object} + */ + toJSON() { + return { + type: 3, + min_values: this.min_values, + max_values: this.max_values || this.options.length, + placeholder: this.placeholder || "", + custom_id: this.custom_id, + options: this.options + } + } +} + +module.exports = MessageSelectMenu; \ No newline at end of file diff --git a/src/structures/MessageSelectOption.js b/src/structures/MessageSelectOption.js new file mode 100644 index 000000000..2274864e0 --- /dev/null +++ b/src/structures/MessageSelectOption.js @@ -0,0 +1,105 @@ +/* From discord-buttons edited */ +const { resolveString } = require("../util/util"); +const Color = require("../structures/Color") +const styles = { + 'blurple': 1, + 'gray': 2, + 'grey': 2, + 'green': 3, + 'red': 4, + 'url': 5, + 'primary': 1, + 'secondary': 2, + 'success': 3, + 'danger': 4, + 'link': 5 +}; + +/** + * The MessageSelectOption class + */ +class MessageSelectOption { + + /** + * Creates new MessageSelectOption instance + * @param {Object} data + */ + constructor(data = {}) { + this.setup(data); + } + + setup(data) { + this.label = 'label' in data ? resolveString(data.label) : null; + + return this.toJSON(); + } + + /** + * Method to setLabel + * @param {String} label + */ + setLabel(label) { + this.label = resolveString(label); + return this; + } + + /** + * Method to setValue + * @param {String} value + */ + setValue(value) { + this.value = resolveString(value); + return this; + } + + /** + * Method to setValue + * @param {String} desc + */ + setDescription(desc) { + this.description = resolveString(desc); + return this; + } + + /** + * Method to setEmoji + * @param {String} emoji + */ + setEmoji(emoji) { + this.emoji = this.parseEmoji(`${emoji}`); + return this; + } + + /** + * Method to setDefault + * @param {Boolean} default + */ + setDefault(def = true) { + this.default = Boolean(def) + return this; + } + + /** + * Method to toJSON + * @return {Object} + */ + toJSON() { + return { + label: this.label, + value: this.value, + description: this.description, + emoji: this.emoji, + default: this.default + } + } + + parseEmoji(text) { + if (text.includes('%')) text = decodeURIComponent(text); + if (!text.includes(':')) return { animated: false, name: text, id: null }; + const m = text.match(/?/); + if (!m) return null; + return { animated: Boolean(m[1]), name: m[2], id: m[3] || null }; + } +} + +module.exports = MessageSelectOption; \ No newline at end of file diff --git a/src/structures/NewsChannel.js b/src/structures/NewsChannel.js index 923c30b5c..2812b8f51 100644 --- a/src/structures/NewsChannel.js +++ b/src/structures/NewsChannel.js @@ -1,5 +1,5 @@ const { Structures, MessageEmbed } = require("discord.js"); -const ButtonCollectorV12 = require('../structures/v12/ButtonCollector'), ButtonCollectorV13 = require('../structures/v13/ButtonCollector'), { createAPIMessage } = require("../util/util") +const ButtonCollectorV12 = require('../structures/v12/ButtonCollector'), ButtonCollectorV13 = require('../structures/v13/ButtonCollector'), SelectMenuCollectorV12 = require('../structures/v12/SelectMenuCollector'), SelectMenuCollectorV13 = require('../structures/v13/SelectMenuCollector'), { createAPIMessage } = require("../util/util") const updater = require("../util/updater") module.exports = Structures.extend("NewsChannel", NewsChannel => { @@ -68,6 +68,24 @@ module.exports = Structures.extend("NewsChannel", NewsChannel => { }); }) } + + createSelectMenuCollector(msg, filter, options = {}) { + if(updater.checkDjsVersion("13")) return new SelectMenuCollectorV13(msg, filter, options); + else return new SelectMenuCollectorV12(msg, filter, options); + } + + awaitSelectMenus(msg, filter, options = {}) { + return new Promise((resolve, reject) => { + const collector = this.createSelectMenuCollector(msg, filter, options); + collector.once('end', (buttons, reason) => { + if (options.errors && options.errors.includes(reason)) { + reject(buttons); + } else { + resolve(buttons); + } + }); + }) + } } return GNewsChannel; diff --git a/src/structures/SelectMenuEvent.js b/src/structures/SelectMenuEvent.js new file mode 100644 index 000000000..296d21bb0 --- /dev/null +++ b/src/structures/SelectMenuEvent.js @@ -0,0 +1,244 @@ +/* From discord-buttons edited */ +const { default: axios } = require("axios"); +const {Client, MessageEmbed} = require("discord.js") +const Color = require("../structures/Color"), { createAPIMessage } = require("../util/util"); + +/** + * The SelectMenuEvent class + */ +class SelectMenuEvent { + + /** + * Creates new SelectMenuEvent instance + * @param {Client} client + * @param {Object} data + */ + constructor(client, data) { + this.client = client; + + this.selectMenuId = data.data.custom_id; + this.valueId = data.data.values; + + this.version = data.version; + + this.token = data.token; + + this.discordID = data.id; + + this.applicationID = data.application_id; + + this.guild = data.guild_id ? client.guilds.cache.get(data.guild_id) : undefined; + + this.channel = client.channels.cache.get(data.channel_id); + + this.clicker = {}; + + if (this.guild) { + this.clicker.member = this.guild.members.cache.get(data.member.user.id); + this.clicker.user = this.client.users.cache.get(data.member.user.id); + } else { + this.clicker.user = this.client.users.cache.get(data.user.id); + } + + this.message = data.message; + + this.replied = false; + this.deferred = false; + } + + /** + * Method to defer + * @param {Boolean} ephemeral + */ + async defer(ephemeral) { + if (this.deferred || this.replied) return console.log(new Color('&d[GCommands] &cThis button already has a reply').getText()); + await this.client.api.interactions(this.discordID, this.token).callback.post({ + data: { + type: 6, + data: { + flags: ephemeral ? 1 << 6 : null, + }, + }, + }); + this.deferred = true; + } + + /** + * Method to think + * @param {Boolean} ephemeral + */ + async think(ephemeral) { + if (this.deferred || this.replied) return console.log(new Color('&d[GCommands] &cThis button already has a reply').getText()); + await this.client.api.interactions(this.discordID, this.token).callback.post({ + data: { + type: 5, + data: { + flags: ephemeral ? 1 << 6 : null, + }, + }, + }); + this.deferred = true; + } + + /** + * Method to edit + * @param {Object} options + */ + async edit(result) { + if(result.autoDefer == undefined) { + await this.client.api.interactions(this.discordID, this.token).callback.post({ + data: { + type: 6, + }, + }); + } + + this.slashEdit(result) + } + + get reply() { + /** + * Method to replySend + * @param {Object} options + */ + let _send = async(result) => { + this.slashRespond(result) + } + + /** + * Method to replyEdit + * @param {Object} options + */ + let _edit = async(result) => { + this.slashEdit(result) + } + + return { + send: _send, + edit: _edit + } + } + + async slashRespond(result) { + var data = { + content: result + } + + if (typeof result === 'object') { + if(typeof result == "object" && !result.content) { + const embed = new MessageEmbed(result) + data = await createAPIMessage(this.client, interaction, embed) + } + else if(typeof result.content == "object" ) { + const embed = new MessageEmbed(result.content) + data = await createAPIMessage(this.client, interaction, embed) + } else data = { content: result.content } + } + + if(typeof result == "object" && result.allowedMentions) { data.allowedMentions = result.allowedMentions } else data.allowedMentions = { parse: [], repliedUser: true } + if(typeof result == "object" && result.ephemeral) { data.flags = 64 } + if(typeof result == "object" && result.components) { + if(!Array.isArray(result.components)) result.components = [result.components]; + data.components = result.components; + } + if(typeof result == "object" && result.embeds) { + if(!Array.isArray(result.embeds)) result.embeds = [result.embeds] + data.embeds = result.embeds; + } + + let finalFiles = []; + if(typeof result == "object" && result.attachments) { + if(!Array.isArray(result.attachments)) result.attachments = [result.attachments] + result.attachments.forEach(file => { + finalFiles.push({ + attachment: file.attachment, + name: file.name, + file: file.attachment + }) + }) + } + + let apiMessage = (await this.client.api.interactions(this.discordID, this.token).callback.post({ + data: { + type: result.thinking ? 5 : 4, + data + }, + files: finalFiles + })).toJSON(); + + let apiMessageMsg = {}; + try { + apiMessageMsg = (await axios.get(`https://discord.com/api/v8/webhooks/${this.client.user.id}/${interaction.token}/messages/@original`)).data; + } catch(e) { + apiMessage = { + id: undefined + } + } + + if(apiMessage) { + apiMessage = apiMessageMsg; + apiMessage.client = this.client ? this.client : client; + apiMessage.createButtonCollector = function createButtonCollector(filter, options) {return this.client.dispatcher.createButtonCollector(apiMessage, filter, options)}; + apiMessage.awaitButtons = function awaitButtons(filter, options) {return this.client.dispatcher.awaitButtons(apiMessage, filter, options)}; + apiMessage.createSelectMenuCollector = function createSelectMenuCollector(filter, options) {return this.client.dispatcher.createSelectMenuCollector(apiMessage, filter, options)}; + apiMessage.awaitSelectMenus = function awaitSelectMenus(filter, options) {return this.client.dispatcher.awaitSelectMenus(apiMessage, filter, options)}; + apiMessage.delete = function deleteMsg() {return this.client.api.webhooks(this.client.user.id, interaction.token).messages[apiMessageMsg.id].delete()}; + } + + return apiMessage + } + + async slashEdit(result) { + if (typeof result == "object") { + if(result.components) { + if(!Array.isArray(result.components)) result.components = [result.components]; + + result.components = result.components; + } else result.components = []; + + if(typeof result.content == "object") { + result.embeds = [result.content] + result.content = "\u200B" + } + if(typeof result == "object" && result.embeds) { + if(!Array.isArray(result.embeds)) result.embeds = [result.embeds]; + result.embeds = result.embeds; + } else result.embeds = [] + let finalFiles = []; + if(typeof result == "object" && result.attachments) { + if(!Array.isArray(result.attachments)) result.attachments = [result.attachments] + result.attachments.forEach(file => { + finalFiles.push({ + attachment: file.attachment, + name: file.name, + file: file.attachment + }) + }) + } + + let apiMessage = (await this.client.api.webhooks(this.client.user.id, this.token).messages[result.messageId ? result.messageId : "@original"].patch({ + data: { + content: result.content, + components: result.components, + embeds: result.embeds || [] + }, + files: finalFiles + })) + + if(apiMessage) { + apiMessage.client = this.client; + apiMessage.createButtonCollector = function createButtonCollector(filter, options) {return this.client.dispatcher.createButtonCollector(apiMessage, filter, options)}; + apiMessage.awaitButtons = function awaitButtons(filter, options) {return this.client.dispatcher.awaitButtons(apiMessage, filter, options)}; + apiMessage.createSelectMenuCollector = function createSelectMenuCollector(filter, options) {return this.client.dispatcher.createSelectMenuCollector(apiMessage, filter, options)}; + apiMessage.awaitSelectMenus = function awaitSelectMenus(filter, options) {return this.client.dispatcher.awaitSelectMenus(apiMessage, filter, options)}; + apiMessage.delete = function deleteMsg() {return this.client.api.webhooks(this.client.user.id, interaction.token).messages[apiMessage.id].delete()}; + } + + return apiMessage; + } + + return this.client.api.webhooks(this.client.user.id, this.token).messages["@original"].patch({ data: { content: result }}) + } +} + +module.exports = SelectMenuEvent; \ No newline at end of file diff --git a/src/structures/TextChannel.js b/src/structures/TextChannel.js index e9aabd276..e8dfa5fb7 100644 --- a/src/structures/TextChannel.js +++ b/src/structures/TextChannel.js @@ -1,5 +1,5 @@ const { Structures, MessageEmbed } = require("discord.js"); -const ButtonCollectorV12 = require('../structures/v12/ButtonCollector'), ButtonCollectorV13 = require('../structures/v13/ButtonCollector'), { createAPIMessage } = require("../util/util") +const ButtonCollectorV12 = require('../structures/v12/ButtonCollector'), ButtonCollectorV13 = require('../structures/v13/ButtonCollector'), SelectMenuCollectorV12 = require('../structures/v12/SelectMenuCollector'), SelectMenuCollectorV13 = require('../structures/v13/SelectMenuCollector'), { createAPIMessage } = require("../util/util") const updater = require("../util/updater") module.exports = Structures.extend("TextChannel", TextChannel => { @@ -66,6 +66,24 @@ module.exports = Structures.extend("TextChannel", TextChannel => { }); }) } + + createSelectMenuCollector(msg, filter, options = {}) { + if(updater.checkDjsVersion("13")) return new SelectMenuCollectorV13(msg, filter, options); + else return new SelectMenuCollectorV12(msg, filter, options); + } + + awaitSelectMenus(msg, filter, options = {}) { + return new Promise((resolve, reject) => { + const collector = this.createSelectMenuCollector(msg, filter, options); + collector.once('end', (buttons, reason) => { + if (options.errors && options.errors.includes(reason)) { + reject(buttons); + } else { + resolve(buttons); + } + }); + }) + } } return GTextChannel; diff --git a/src/structures/message.js b/src/structures/message.js index 8cce03c8a..416477063 100644 --- a/src/structures/message.js +++ b/src/structures/message.js @@ -1,5 +1,5 @@ const { APIMessage, Structures } = require('discord.js'); -const ButtonCollectorV12 = require('../structures/v12/ButtonCollector'), ButtonCollectorV13 = require('../structures/v13/ButtonCollector'), { createAPIMessage } = require("../util/util") +const ButtonCollectorV12 = require('../structures/v12/ButtonCollector'), ButtonCollectorV13 = require('../structures/v13/ButtonCollector'), SelectMenuCollectorV12 = require('../structures/v12/SelectMenuCollector'), SelectMenuCollectorV13 = require('../structures/v13/SelectMenuCollector'), { createAPIMessage } = require("../util/util") const updater = require("../util/updater") module.exports = Structures.extend("Message", Message => { @@ -274,6 +274,24 @@ module.exports = Structures.extend("Message", Message => { }); }) } + + createSelectMenuCollector(filter, options = {}) { + if(updater.checkDjsVersion("13")) return new SelectMenuCollectorV13(this, filter, options); + else return new SelectMenuCollectorV12(this, filter, options); + } + + awaitSelectMenus(filter, options = {}) { + return new Promise((resolve, reject) => { + const collector = this.createSelectMenuCollector(this, filter, options); + collector.once('end', (buttons, reason) => { + if (options.errors && options.errors.includes(reason)) { + reject(buttons); + } else { + resolve(buttons); + } + }); + }) + } } return GCommandsMessage; diff --git a/src/structures/v12/SelectMenuCollector.js b/src/structures/v12/SelectMenuCollector.js new file mode 100644 index 000000000..af06028c0 --- /dev/null +++ b/src/structures/v12/SelectMenuCollector.js @@ -0,0 +1,86 @@ +const { Collector } = require("discord.js"); +const Collection = require('discord.js').Collection; +const { Events } = require('discord.js').Constants; + +class SelectMenuCollector extends Collector { + constructor(message, filter, options = {}) { + super(message.client, filter, options); + this.message = message; + + this.users = new Collection(); + + this.total = 0; + + this.empty = this.empty.bind(this); + this._handleChannelDeletion = this._handleChannelDeletion.bind(this); + this._handleGuildDeletion = this._handleGuildDeletion.bind(this); + this._handleMessageDeletion = this._handleMessageDeletion.bind(this); + + this.client.incrementMaxListeners(); + this.client.on('selectMenu', this.handleCollect); + this.client.on(Events.MESSAGE_DELETE, this._handleMessageDeletion); + this.client.on(Events.CHANNEL_DELETE, this._handleChannelDeletion); + this.client.on(Events.GUILD_DELETE, this._handleGuildDeletion); + + this.once('end', () => { + this.client.removeListener('selectMenu', this.handleCollect); + this.client.removeListener(Events.MESSAGE_DELETE, this._handleMessageDeletion); + this.client.removeListener(Events.CHANNEL_DELETE, this._handleChannelDeletion); + this.client.removeListener(Events.GUILD_DELETE, this._handleGuildDeletion); + this.client.decrementMaxListeners(); + }); + + this.on('collect', (menu) => { + this.total++; + this.users.set(menu.clicker.user.id, menu.clicker.user); + }); + } + + collect(menu) { + if(this.message.unstable) return SelectMenuCollector.key(menu) + if (menu.message.id !== this.message.id) return null; + return SelectMenuCollector.key(menu); + } + + dispose() { + return null; + } + + empty() { + this.total = 0; + this.collected.clear(); + this.users.clear(); + this.checkEnd(); + } + + endReason() { + if (this.options.max && this.total >= this.options.max) return 'limit'; + if (this.options.maxEmojis && this.collected.size >= this.options.maxEmojis) return 'emojiLimit'; + if (this.options.maxUsers && this.users.size >= this.options.maxUsers) return 'userLimit'; + return null; + } + + _handleMessageDeletion(message) { + if (message.id === this.message.id) { + this.stop('messageDelete'); + } + } + + _handleChannelDeletion(channel) { + if (channel.id === this.message.channel.id) { + this.stop('channelDelete'); + } + } + + _handleGuildDeletion(guild) { + if (this.message.guild && guild.id === this.message.guild.id) { + this.stop('guildDelete'); + } + } + + static key(menu) { + return menu.selectMenuId; + } +} + +module.exports = SelectMenuCollector; \ No newline at end of file diff --git a/src/structures/v13/SelectMenuCollector.js b/src/structures/v13/SelectMenuCollector.js new file mode 100644 index 000000000..9ea1decce --- /dev/null +++ b/src/structures/v13/SelectMenuCollector.js @@ -0,0 +1,86 @@ +const { Collector } = require("discord.js"); +const Collection = require('discord.js').Collection; +const { Events } = require('discord.js').Constants; + +class SelectMenuCollector extends Collector { + constructor(message, filter, options = {}) { + super(message.client, filter, options); + this.message = message; + + this.users = new Collection(); + + this.total = 0; + + this.empty = this.empty.bind(this); + this._handleChannelDeletion = this._handleChannelDeletion.bind(this); + this._handleGuildDeletion = this._handleGuildDeletion.bind(this); + this._handleMessageDeletion = this._handleMessageDeletion.bind(this); + + this.client.incrementMaxListeners(); + this.client.on('selectMenu', this.handleCollect); + this.client.on(Events.MESSAGE_DELETE, this._handleMessageDeletion); + this.client.on(Events.CHANNEL_DELETE, this._handleChannelDeletion); + this.client.on(Events.GUILD_DELETE, this._handleGuildDeletion); + + this.once('end', () => { + this.client.removeListener('selectMenu', this.handleCollect); + this.client.removeListener(Events.MESSAGE_DELETE, this._handleMessageDeletion); + this.client.removeListener(Events.CHANNEL_DELETE, this._handleChannelDeletion); + this.client.removeListener(Events.GUILD_DELETE, this._handleGuildDeletion); + this.client.decrementMaxListeners(); + }); + + this.on('collect', (menu) => { + this.total++; + this.users.set(menu.clicker.user.id, menu.clicker.user); + }); + } + + collect(menu) { + if(this.message.unstable) return SelectMenuCollector.key(menu) + if (menu.message.id !== this.message.id) return null; + return SelectMenuCollector.key(menu); + } + + dispose() { + return null; + } + + empty() { + this.total = 0; + this.collected.clear(); + this.users.clear(); + this.checkEnd(); + } + + get endReason() { + if (this.options.max && this.total >= this.options.max) return 'limit'; + if (this.options.maxEmojis && this.collected.size >= this.options.maxEmojis) return 'emojiLimit'; + if (this.options.maxUsers && this.users.size >= this.options.maxUsers) return 'userLimit'; + return null; + } + + _handleMessageDeletion(message) { + if (message.id === this.message.id) { + this.stop('messageDelete'); + } + } + + _handleChannelDeletion(channel) { + if (channel.id === this.message.channel.id) { + this.stop('channelDelete'); + } + } + + _handleGuildDeletion(guild) { + if (this.message.guild && guild.id === this.message.guild.id) { + this.stop('guildDelete'); + } + } + + static key(menu) { + return menu.selectMenuId; + } +} + +module.exports = SelectMenuCollector; \ No newline at end of file From d2ebe97ac2265be9cca7e686c5753d495dd68467 Mon Sep 17 00:00:00 2001 From: xhyrom Date: Thu, 1 Jul 2021 10:41:27 +0200 Subject: [PATCH 04/10] addOptions, addComponents --- src/base/actions/interactions.js | 2 ++ src/structures/ButtonEvent.js | 16 +++++++++++++++- src/structures/MessageActionRow.js | 22 +++++++++++++++++++++- src/structures/MessageSelectMenu.js | 21 +++++++++++++++++++++ src/structures/SelectMenuEvent.js | 14 ++++++++++++++ src/structures/TextChannel.js | 2 +- 6 files changed, 74 insertions(+), 3 deletions(-) diff --git a/src/base/actions/interactions.js b/src/base/actions/interactions.js index 299f3503e..f5cadad26 100644 --- a/src/base/actions/interactions.js +++ b/src/base/actions/interactions.js @@ -9,12 +9,14 @@ module.exports = (client) => { const dropdown = new SelectMenuEvent(client, data) client.emit(`selectMenu`, dropdown) + client.emit("interaction", dropdown) } if (data.data.component_type == 2) { const button = new ButtonEvent(client, data); client.emit('clickButton', button); + client.emit("interaction", button) } }); } \ No newline at end of file diff --git a/src/structures/ButtonEvent.js b/src/structures/ButtonEvent.js index f3abfdaae..91087449f 100644 --- a/src/structures/ButtonEvent.js +++ b/src/structures/ButtonEvent.js @@ -49,7 +49,7 @@ class ButtonEvent { * Method to defer * @param {Boolean} ephemeral */ - async defer(ephemeral) { + async defer(ephemeral) { if (this.deferred || this.replied) return console.log(new Color('&d[GCommands] &cThis button already has a reply').getText()); await this.client.api.interactions(this.discordID, this.token).callback.post({ data: { @@ -238,6 +238,20 @@ class ButtonEvent { return this.client.api.webhooks(this.client.user.id, this.token).messages["@original"].patch({ data: { content: result }}) } + + /** + * Method to isSelectMenu + */ + async isSelectMenu() { + return false; + } + + /** + * Method to isButton + */ + async isButton() { + return true; + } } module.exports = ButtonEvent; \ No newline at end of file diff --git a/src/structures/MessageActionRow.js b/src/structures/MessageActionRow.js index c4221be14..1cd06ffc7 100644 --- a/src/structures/MessageActionRow.js +++ b/src/structures/MessageActionRow.js @@ -22,7 +22,7 @@ class MessageActionRow { /** * Method to addComponent - * @param {MessageButton} button + * @param {MessageButton} MessageButton */ addComponent(component) { if(typeof component != "object") return console.log(new Color("&d[GCommands] &cNeed provide MessageButton!").getText()) @@ -30,6 +30,26 @@ class MessageActionRow { return this; } + /** + * Method to addComponents + * @param {MessageButton} MessageButton + */ + addComponents(components) { + if(typeof components != "object") return console.log(new Color("&d[GCommands] &cNeed provide MessageButton!").getText()) + this.components.push(...components.flat(Infinity).map((c) => c)); + return this; + } + + /** + * Method to removeOptions + * @param {Object} MessageButton + */ + removeComponents(index, deleteCount, ...options) { + if(typeof options != "object") return console.log(new Color("&d[GCommands] &cNeed provide MessageSelectOption!").getText()) + this.components.splice(index, deleteCount, ...components.flat(Infinity).map((c) => c)); + return this; + } + /** * Method to toJSON * @return {Object} diff --git a/src/structures/MessageSelectMenu.js b/src/structures/MessageSelectMenu.js index 87b7a33ee..197ef9e55 100644 --- a/src/structures/MessageSelectMenu.js +++ b/src/structures/MessageSelectMenu.js @@ -76,10 +76,31 @@ class MessageSelectMenu { * @param {Object} MessageSelectOption */ addOption(option) { + if(typeof option != "object") return console.log(new Color("&d[GCommands] &cNeed provide MessageSelectOption!").getText()) this.options.push(option) return this; } + /** + * Method to addOptions + * @param {Object} MessageSelectOptions + */ + addOptions(...options) { + if(typeof options != "object") return console.log(new Color("&d[GCommands] &cNeed provide MessageSelectOption!").getText()) + this.options.push(...options.flat(Infinity).map((o) => o)); + return this; + } + + /** + * Method to removeOptions + * @param {Object} MessageSelectOptions + */ + removeOptions(index, deleteCount, ...options) { + if(typeof options != "object") return console.log(new Color("&d[GCommands] &cNeed provide MessageSelectOption!").getText()) + this.components.splice(index, deleteCount, ...options.flat(Infinity).map((o) => o)); + return this; + } + /** * Method to toJSON * @return {Object} diff --git a/src/structures/SelectMenuEvent.js b/src/structures/SelectMenuEvent.js index 296d21bb0..cf5e37e55 100644 --- a/src/structures/SelectMenuEvent.js +++ b/src/structures/SelectMenuEvent.js @@ -239,6 +239,20 @@ class SelectMenuEvent { return this.client.api.webhooks(this.client.user.id, this.token).messages["@original"].patch({ data: { content: result }}) } + + /** + * Method to isSelectMenu + */ + async isSelectMenu() { + return true; + } + + /** + * Method to isButton + */ + async isButton() { + return false; + } } module.exports = SelectMenuEvent; \ No newline at end of file diff --git a/src/structures/TextChannel.js b/src/structures/TextChannel.js index e8dfa5fb7..41407b2b3 100644 --- a/src/structures/TextChannel.js +++ b/src/structures/TextChannel.js @@ -1,6 +1,6 @@ const { Structures, MessageEmbed } = require("discord.js"); const ButtonCollectorV12 = require('../structures/v12/ButtonCollector'), ButtonCollectorV13 = require('../structures/v13/ButtonCollector'), SelectMenuCollectorV12 = require('../structures/v12/SelectMenuCollector'), SelectMenuCollectorV13 = require('../structures/v13/SelectMenuCollector'), { createAPIMessage } = require("../util/util") -const updater = require("../util/updater") +const updater = require("../util/updater"); module.exports = Structures.extend("TextChannel", TextChannel => { class GTextChannel extends TextChannel { From 0705343d03581b9fb30877a16bb349912e221709 Mon Sep 17 00:00:00 2001 From: xhyrom Date: Thu, 1 Jul 2021 13:03:06 +0200 Subject: [PATCH 05/10] 4.2.8-dev --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 83961f95e..7ddf7bf22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gcommands", - "version": "4.2.7", + "version": "4.2.8-dev", "description": "Open-source slash/normal command handler", "main": "src/index.js", "types": "./typings/index.d.ts", From 381c5eb511038493f0e0e45e70f29d0c02480433 Mon Sep 17 00:00:00 2001 From: xhyrom Date: Thu, 1 Jul 2021 13:04:26 +0200 Subject: [PATCH 06/10] auto ms install --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 7ddf7bf22..66860b4c1 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "types": "./typings/index.d.ts", "dependencies": { "axios": "^0.21.1", - "discord.js": "^12.5.3" + "discord.js": "^12.5.3", + "ms": "^2.1.3" }, "scripts": { "test": "node test.js" From 154fabde55613b67d98732ec256dad2abcdee28e Mon Sep 17 00:00:00 2001 From: xhyrom Date: Thu, 1 Jul 2021 14:22:15 +0200 Subject: [PATCH 07/10] more clen --- src/base/actions/interactions.js | 16 +- src/managers/GCommandLoader.js | 4 +- .../{ButtonEvent.js => InteractionEvent.js} | 19 +- src/structures/MessageSelectMenu.js | 13 - src/structures/MessageSelectOption.js | 15 - src/structures/SelectMenuEvent.js | 258 ------------------ 6 files changed, 17 insertions(+), 308 deletions(-) rename src/structures/{ButtonEvent.js => InteractionEvent.js} (95%) delete mode 100644 src/structures/SelectMenuEvent.js diff --git a/src/base/actions/interactions.js b/src/base/actions/interactions.js index f5cadad26..9cea2df15 100644 --- a/src/base/actions/interactions.js +++ b/src/base/actions/interactions.js @@ -1,22 +1,14 @@ -const ButtonEvent = require("../../structures/ButtonEvent"); -const SelectMenuEvent = require("../../structures/SelectMenuEvent"); +const InteractionEvent = require("../../structures/InteractionEvent"); module.exports = (client) => { client.ws.on('INTERACTION_CREATE', data => { if (!data.message) return; - if(data.data.component_type == 3) { - const dropdown = new SelectMenuEvent(client, data) + if(data.data.component_type) { + const dropdown = new InteractionEvent(client, data) - client.emit(`selectMenu`, dropdown) + client.emit(data.data.component_type == 3 ? `selectMenu` : `clickButton`, dropdown) client.emit("interaction", dropdown) } - - if (data.data.component_type == 2) { - const button = new ButtonEvent(client, data); - - client.emit('clickButton', button); - client.emit("interaction", button) - } }); } \ No newline at end of file diff --git a/src/managers/GCommandLoader.js b/src/managers/GCommandLoader.js index ac844622f..5a7a57720 100644 --- a/src/managers/GCommandLoader.js +++ b/src/managers/GCommandLoader.js @@ -18,13 +18,13 @@ class GCommandLoader { * @private */ async __loadCommands() { - await fs.readdirSync(`${__dirname}/../../../../${this.cmdDir}`).forEach(async(dir) => { + await fs.readdirSync(`${__dirname}/../../../${this.cmdDir}`).forEach(async(dir) => { var file; var fileName = dir.split(".").reverse()[1] var fileType = dir.split(".").reverse()[0] if(fileType == "js" || fileType == "ts") { try { - file = await require(`../../../../${this.cmdDir}${dir}`); + file = await require(`../../../${this.cmdDir}${dir}`); if (file && file.aliases && Array.isArray(file.aliases)) file.aliases.forEach(alias => this.client.aliases.set(alias, file.name)); this.client.commands.set(file.name, file); diff --git a/src/structures/ButtonEvent.js b/src/structures/InteractionEvent.js similarity index 95% rename from src/structures/ButtonEvent.js rename to src/structures/InteractionEvent.js index 91087449f..185d8049d 100644 --- a/src/structures/ButtonEvent.js +++ b/src/structures/InteractionEvent.js @@ -4,19 +4,22 @@ const {Client, MessageEmbed} = require("discord.js") const Color = require("../structures/Color"), { createAPIMessage } = require("../util/util"); /** - * The ButtonEvent class + * The InteractionEvent class */ -class ButtonEvent { +class InteractionEvent { /** - * Creates new ButtonEvent instance + * Creates new InteractionEvent instance * @param {Client} client * @param {Object} data */ constructor(client, data) { this.client = client; - this.id = data.data.custom_id; + if(data.data.values) { + this.selectMenuId = data.data.custom_id; + this.valueId = data.data.values; + } else this.id = data.data.custom_id; this.version = data.version; @@ -242,16 +245,16 @@ class ButtonEvent { /** * Method to isSelectMenu */ - async isSelectMenu() { - return false; + async isSelectMenu() { + return data.data.values ? true : false; } /** * Method to isButton */ async isButton() { - return true; + return data.data.values ? false : true; } } -module.exports = ButtonEvent; \ No newline at end of file +module.exports = InteractionEvent; \ No newline at end of file diff --git a/src/structures/MessageSelectMenu.js b/src/structures/MessageSelectMenu.js index 197ef9e55..f6935cf0b 100644 --- a/src/structures/MessageSelectMenu.js +++ b/src/structures/MessageSelectMenu.js @@ -1,18 +1,5 @@ const { resolveString } = require("../util/util"); const Color = require("../structures/Color") -const styles = { - 'blurple': 1, - 'gray': 2, - 'grey': 2, - 'green': 3, - 'red': 4, - 'url': 5, - 'primary': 1, - 'secondary': 2, - 'success': 3, - 'danger': 4, - 'link': 5 -}; /** * The MessageSelectMenu class diff --git a/src/structures/MessageSelectOption.js b/src/structures/MessageSelectOption.js index 2274864e0..a51788a68 100644 --- a/src/structures/MessageSelectOption.js +++ b/src/structures/MessageSelectOption.js @@ -1,19 +1,4 @@ -/* From discord-buttons edited */ const { resolveString } = require("../util/util"); -const Color = require("../structures/Color") -const styles = { - 'blurple': 1, - 'gray': 2, - 'grey': 2, - 'green': 3, - 'red': 4, - 'url': 5, - 'primary': 1, - 'secondary': 2, - 'success': 3, - 'danger': 4, - 'link': 5 -}; /** * The MessageSelectOption class diff --git a/src/structures/SelectMenuEvent.js b/src/structures/SelectMenuEvent.js deleted file mode 100644 index cf5e37e55..000000000 --- a/src/structures/SelectMenuEvent.js +++ /dev/null @@ -1,258 +0,0 @@ -/* From discord-buttons edited */ -const { default: axios } = require("axios"); -const {Client, MessageEmbed} = require("discord.js") -const Color = require("../structures/Color"), { createAPIMessage } = require("../util/util"); - -/** - * The SelectMenuEvent class - */ -class SelectMenuEvent { - - /** - * Creates new SelectMenuEvent instance - * @param {Client} client - * @param {Object} data - */ - constructor(client, data) { - this.client = client; - - this.selectMenuId = data.data.custom_id; - this.valueId = data.data.values; - - this.version = data.version; - - this.token = data.token; - - this.discordID = data.id; - - this.applicationID = data.application_id; - - this.guild = data.guild_id ? client.guilds.cache.get(data.guild_id) : undefined; - - this.channel = client.channels.cache.get(data.channel_id); - - this.clicker = {}; - - if (this.guild) { - this.clicker.member = this.guild.members.cache.get(data.member.user.id); - this.clicker.user = this.client.users.cache.get(data.member.user.id); - } else { - this.clicker.user = this.client.users.cache.get(data.user.id); - } - - this.message = data.message; - - this.replied = false; - this.deferred = false; - } - - /** - * Method to defer - * @param {Boolean} ephemeral - */ - async defer(ephemeral) { - if (this.deferred || this.replied) return console.log(new Color('&d[GCommands] &cThis button already has a reply').getText()); - await this.client.api.interactions(this.discordID, this.token).callback.post({ - data: { - type: 6, - data: { - flags: ephemeral ? 1 << 6 : null, - }, - }, - }); - this.deferred = true; - } - - /** - * Method to think - * @param {Boolean} ephemeral - */ - async think(ephemeral) { - if (this.deferred || this.replied) return console.log(new Color('&d[GCommands] &cThis button already has a reply').getText()); - await this.client.api.interactions(this.discordID, this.token).callback.post({ - data: { - type: 5, - data: { - flags: ephemeral ? 1 << 6 : null, - }, - }, - }); - this.deferred = true; - } - - /** - * Method to edit - * @param {Object} options - */ - async edit(result) { - if(result.autoDefer == undefined) { - await this.client.api.interactions(this.discordID, this.token).callback.post({ - data: { - type: 6, - }, - }); - } - - this.slashEdit(result) - } - - get reply() { - /** - * Method to replySend - * @param {Object} options - */ - let _send = async(result) => { - this.slashRespond(result) - } - - /** - * Method to replyEdit - * @param {Object} options - */ - let _edit = async(result) => { - this.slashEdit(result) - } - - return { - send: _send, - edit: _edit - } - } - - async slashRespond(result) { - var data = { - content: result - } - - if (typeof result === 'object') { - if(typeof result == "object" && !result.content) { - const embed = new MessageEmbed(result) - data = await createAPIMessage(this.client, interaction, embed) - } - else if(typeof result.content == "object" ) { - const embed = new MessageEmbed(result.content) - data = await createAPIMessage(this.client, interaction, embed) - } else data = { content: result.content } - } - - if(typeof result == "object" && result.allowedMentions) { data.allowedMentions = result.allowedMentions } else data.allowedMentions = { parse: [], repliedUser: true } - if(typeof result == "object" && result.ephemeral) { data.flags = 64 } - if(typeof result == "object" && result.components) { - if(!Array.isArray(result.components)) result.components = [result.components]; - data.components = result.components; - } - if(typeof result == "object" && result.embeds) { - if(!Array.isArray(result.embeds)) result.embeds = [result.embeds] - data.embeds = result.embeds; - } - - let finalFiles = []; - if(typeof result == "object" && result.attachments) { - if(!Array.isArray(result.attachments)) result.attachments = [result.attachments] - result.attachments.forEach(file => { - finalFiles.push({ - attachment: file.attachment, - name: file.name, - file: file.attachment - }) - }) - } - - let apiMessage = (await this.client.api.interactions(this.discordID, this.token).callback.post({ - data: { - type: result.thinking ? 5 : 4, - data - }, - files: finalFiles - })).toJSON(); - - let apiMessageMsg = {}; - try { - apiMessageMsg = (await axios.get(`https://discord.com/api/v8/webhooks/${this.client.user.id}/${interaction.token}/messages/@original`)).data; - } catch(e) { - apiMessage = { - id: undefined - } - } - - if(apiMessage) { - apiMessage = apiMessageMsg; - apiMessage.client = this.client ? this.client : client; - apiMessage.createButtonCollector = function createButtonCollector(filter, options) {return this.client.dispatcher.createButtonCollector(apiMessage, filter, options)}; - apiMessage.awaitButtons = function awaitButtons(filter, options) {return this.client.dispatcher.awaitButtons(apiMessage, filter, options)}; - apiMessage.createSelectMenuCollector = function createSelectMenuCollector(filter, options) {return this.client.dispatcher.createSelectMenuCollector(apiMessage, filter, options)}; - apiMessage.awaitSelectMenus = function awaitSelectMenus(filter, options) {return this.client.dispatcher.awaitSelectMenus(apiMessage, filter, options)}; - apiMessage.delete = function deleteMsg() {return this.client.api.webhooks(this.client.user.id, interaction.token).messages[apiMessageMsg.id].delete()}; - } - - return apiMessage - } - - async slashEdit(result) { - if (typeof result == "object") { - if(result.components) { - if(!Array.isArray(result.components)) result.components = [result.components]; - - result.components = result.components; - } else result.components = []; - - if(typeof result.content == "object") { - result.embeds = [result.content] - result.content = "\u200B" - } - if(typeof result == "object" && result.embeds) { - if(!Array.isArray(result.embeds)) result.embeds = [result.embeds]; - result.embeds = result.embeds; - } else result.embeds = [] - let finalFiles = []; - if(typeof result == "object" && result.attachments) { - if(!Array.isArray(result.attachments)) result.attachments = [result.attachments] - result.attachments.forEach(file => { - finalFiles.push({ - attachment: file.attachment, - name: file.name, - file: file.attachment - }) - }) - } - - let apiMessage = (await this.client.api.webhooks(this.client.user.id, this.token).messages[result.messageId ? result.messageId : "@original"].patch({ - data: { - content: result.content, - components: result.components, - embeds: result.embeds || [] - }, - files: finalFiles - })) - - if(apiMessage) { - apiMessage.client = this.client; - apiMessage.createButtonCollector = function createButtonCollector(filter, options) {return this.client.dispatcher.createButtonCollector(apiMessage, filter, options)}; - apiMessage.awaitButtons = function awaitButtons(filter, options) {return this.client.dispatcher.awaitButtons(apiMessage, filter, options)}; - apiMessage.createSelectMenuCollector = function createSelectMenuCollector(filter, options) {return this.client.dispatcher.createSelectMenuCollector(apiMessage, filter, options)}; - apiMessage.awaitSelectMenus = function awaitSelectMenus(filter, options) {return this.client.dispatcher.awaitSelectMenus(apiMessage, filter, options)}; - apiMessage.delete = function deleteMsg() {return this.client.api.webhooks(this.client.user.id, interaction.token).messages[apiMessage.id].delete()}; - } - - return apiMessage; - } - - return this.client.api.webhooks(this.client.user.id, this.token).messages["@original"].patch({ data: { content: result }}) - } - - /** - * Method to isSelectMenu - */ - async isSelectMenu() { - return true; - } - - /** - * Method to isButton - */ - async isButton() { - return false; - } -} - -module.exports = SelectMenuEvent; \ No newline at end of file From de33d7d62c603b1452132e33f20cf398e496060e Mon Sep 17 00:00:00 2001 From: xhyrom Date: Thu, 1 Jul 2021 14:23:20 +0200 Subject: [PATCH 08/10] cmd load --- src/managers/GCommandLoader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/managers/GCommandLoader.js b/src/managers/GCommandLoader.js index 5a7a57720..ac844622f 100644 --- a/src/managers/GCommandLoader.js +++ b/src/managers/GCommandLoader.js @@ -18,13 +18,13 @@ class GCommandLoader { * @private */ async __loadCommands() { - await fs.readdirSync(`${__dirname}/../../../${this.cmdDir}`).forEach(async(dir) => { + await fs.readdirSync(`${__dirname}/../../../../${this.cmdDir}`).forEach(async(dir) => { var file; var fileName = dir.split(".").reverse()[1] var fileType = dir.split(".").reverse()[0] if(fileType == "js" || fileType == "ts") { try { - file = await require(`../../../${this.cmdDir}${dir}`); + file = await require(`../../../../${this.cmdDir}${dir}`); if (file && file.aliases && Array.isArray(file.aliases)) file.aliases.forEach(alias => this.client.aliases.set(alias, file.name)); this.client.commands.set(file.name, file); From 72a75c82add0c7ae889bf96ae29e55b734cf6dc6 Mon Sep 17 00:00:00 2001 From: xhyrom Date: Thu, 1 Jul 2021 14:27:31 +0200 Subject: [PATCH 09/10] MessageSelectOption to MessageSelectMenuOption --- src/base/actions/interactions.js | 6 +++--- src/index.js | 2 +- ...{MessageSelectOption.js => MessageSelectMenuOption.js} | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) rename src/structures/{MessageSelectOption.js => MessageSelectMenuOption.js} (91%) diff --git a/src/base/actions/interactions.js b/src/base/actions/interactions.js index 9cea2df15..8a160401b 100644 --- a/src/base/actions/interactions.js +++ b/src/base/actions/interactions.js @@ -5,10 +5,10 @@ module.exports = (client) => { if (!data.message) return; if(data.data.component_type) { - const dropdown = new InteractionEvent(client, data) + const interaction = new InteractionEvent(client, data) - client.emit(data.data.component_type == 3 ? `selectMenu` : `clickButton`, dropdown) - client.emit("interaction", dropdown) + client.emit(data.data.component_type == 3 ? `selectMenu` : `clickButton`, interaction) + client.emit("interaction", interaction) } }); } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 38780377a..ab06cd8d4 100644 --- a/src/index.js +++ b/src/index.js @@ -21,7 +21,7 @@ module.exports = { MessageButton: require("./structures/MessageButton"), MessageActionRow: require("./structures/MessageActionRow"), MessageSelectMenu: require("./structures/MessageSelectMenu"), - MessageSelectOption: require("./structures/MessageSelectOption"), + MessageSelectMenuOption: require("./structures/MessageSelectMenuOption"), ButtonCollectorV12: require("./structures/v13/ButtonCollector"), ButtonCollectorV13: require("./structures/v12/ButtonCollector"), diff --git a/src/structures/MessageSelectOption.js b/src/structures/MessageSelectMenuOption.js similarity index 91% rename from src/structures/MessageSelectOption.js rename to src/structures/MessageSelectMenuOption.js index a51788a68..f1a61e927 100644 --- a/src/structures/MessageSelectOption.js +++ b/src/structures/MessageSelectMenuOption.js @@ -1,12 +1,12 @@ const { resolveString } = require("../util/util"); /** - * The MessageSelectOption class + * The MessageSelectMenuOption class */ -class MessageSelectOption { +class MessageSelectMenuOption { /** - * Creates new MessageSelectOption instance + * Creates new MessageSelectMenuOption instance * @param {Object} data */ constructor(data = {}) { @@ -87,4 +87,4 @@ class MessageSelectOption { } } -module.exports = MessageSelectOption; \ No newline at end of file +module.exports = MessageSelectMenuOption; \ No newline at end of file From 2719ae68bfc8a294f9c024b21d83f6c75f544a35 Mon Sep 17 00:00:00 2001 From: xhyrom Date: Thu, 1 Jul 2021 14:51:09 +0200 Subject: [PATCH 10/10] 4.2.8 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 66860b4c1..07be3d2a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gcommands", - "version": "4.2.8-dev", + "version": "4.2.8", "description": "Open-source slash/normal command handler", "main": "src/index.js", "types": "./typings/index.d.ts",