Skip to content

Commit

Permalink
4.2.8
Browse files Browse the repository at this point in the history
4.2.8
  • Loading branch information
xhyrom authored Jul 1, 2021
2 parents 266f037 + 2719ae6 commit 4e95a35
Show file tree
Hide file tree
Showing 17 changed files with 552 additions and 27 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "gcommands",
"version": "4.2.7",
"version": "4.2.8",
"description": "Open-source slash/normal command handler",
"main": "src/index.js",
"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"
Expand Down
36 changes: 33 additions & 3 deletions src/base/GCommandsDispatcher.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}`) || {}
Expand Down Expand Up @@ -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;
12 changes: 6 additions & 6 deletions src/base/actions/interactions.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const ButtonEvent = require("../../structures/ButtonEvent")
const InteractionEvent = require("../../structures/InteractionEvent");

module.exports = (client) => {
client.ws.on('INTERACTION_CREATE', data => {
if(data.type != 3) return;
if (!data.message) return;

if (data.data.component_type === 2) {
const button = new ButtonEvent(client, data);
if(data.data.component_type) {
const interaction = new InteractionEvent(client, data)

client.emit('clickButton', button);
client.emit(data.data.component_type == 3 ? `selectMenu` : `clickButton`, interaction)
client.emit("interaction", interaction)
}
});
}
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ module.exports = {
GDMChannel: require("./structures/DMChannel"),
MessageButton: require("./structures/MessageButton"),
MessageActionRow: require("./structures/MessageActionRow"),

MessageSelectMenu: require("./structures/MessageSelectMenu"),
MessageSelectMenuOption: require("./structures/MessageSelectMenuOption"),

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"),
Expand Down
4 changes: 4 additions & 0 deletions src/managers/GEventLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()};
}

Expand Down Expand Up @@ -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()};
}

Expand Down
21 changes: 20 additions & 1 deletion src/structures/DMChannel.js
Original file line number Diff line number Diff line change
@@ -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 => {
Expand All @@ -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 }
Expand Down Expand Up @@ -67,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -49,7 +52,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: {
Expand Down Expand Up @@ -179,6 +182,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()};
}

Expand Down Expand Up @@ -226,6 +231,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()};
}

Expand All @@ -234,6 +241,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 data.data.values ? true : false;
}

/**
* Method to isButton
*/
async isButton() {
return data.data.values ? false : true;
}
}

module.exports = ButtonEvent;
module.exports = InteractionEvent;
22 changes: 21 additions & 1 deletion src/structures/MessageActionRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,34 @@ 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())
this.components.push(component)
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}
Expand Down
2 changes: 1 addition & 1 deletion src/structures/MessageButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MessageButton {

/**
* Method to setLabel
* @param {String} style
* @param {String} label
*/
setLabel(label) {
this.label = resolveString(label);
Expand Down
Loading

0 comments on commit 4e95a35

Please sign in to comment.