discord-buttons
Advanced tools
Comparing version 3.2.1 to 4.0.0-deprecated
@@ -1,40 +0,9 @@ | ||
<div align="center"><h1>Pre-made Buttons and Examples</h1></div> | ||
<<<<<<< HEAD | ||
# Examples moved | ||
## Examples | ||
## See [new examples](https://discord-buttons.js.org/d/examples/buttons.html) | ||
======= | ||
# Examples moved | ||
### Normal Button | ||
```js | ||
let button = new disbut.MessageButton() | ||
.setStyle('red') | ||
.setLabel('My First Button!') | ||
.setID('click_to_function') | ||
.setDisabled(); | ||
message.channel.send('Hey, i am powered by https://npmjs.com/discord-buttons', button); | ||
``` | ||
<img align="center" src="https://cdn.discordapp.com/attachments/846455339419172874/848302344323072041/Outputs.png"></img> | ||
### URL Buttons | ||
```js | ||
let button = new disbut.MessageButton() | ||
.setStyle('url') | ||
.setURL('https://npmjs.com/discord-buttons') | ||
.setLabel('My First URL Button!') | ||
.setDisabled(); | ||
message.channel.send('Hey, i am powered by https://npmjs.com/discord-buttons', button); | ||
``` | ||
<b>P.S:</b> You need to Change your `style` to `url`, you don't have `id` for this kind of buttons too, and also make sure you have your URL in button with `setURL` and URL is valid. | ||
<img align="center" src="https://cdn.discordapp.com/attachments/846455339419172874/848314748239085608/Main1.png"></img> | ||
## Button Styles | ||
| Styles | Preview | | ||
| -------------- | ----------------------------------------------------------------------------------------------------------------- | | ||
| Red | ![Red_Style](https://cdn.discordapp.com/attachments/846455339419172874/848285563936047124/Button_Green2.png) | | ||
| Green | ![Green_Style](https://cdn.discordapp.com/attachments/846455339419172874/848283811942498344/Button_Green1.png) | | ||
| Blurple | ![Blurple_Style](https://cdn.discordapp.com/attachments/846455339419172874/848282426395852830/Button_Blurple.png) | | ||
| grey (or gray) | ![Grey_Style](https://cdn.discordapp.com/attachments/846455339419172874/848291827736117308/Button_Green5.png) | | ||
| URL | ![URL_Style](https://cdn.discordapp.com/attachments/846455339419172874/848290582706782308/Button_Green4.png) | | ||
## See [new examples](https://discord-buttons.js.org/d/examples/buttons.html) | ||
>>>>>>> 345ff6e0bf8f19d64d70b63606aebf05b57622e6 |
{ | ||
"name": "discord-buttons", | ||
"version": "3.2.1", | ||
"version": "4.0.0-deprecated", | ||
"description": "Discord.js buttons", | ||
"main": "src/index.js", | ||
"types": "typings/index.d.ts", | ||
"scripts": { | ||
"format": "prettier --write \"typings/*.ts\" \"src/**/*.js\"", | ||
"version": "npm run format" | ||
}, | ||
"funding": { | ||
@@ -26,11 +30,12 @@ "type": "individual", | ||
"type": "git", | ||
"url": "git+https://github.com/AngeloCore/discord-buttons.git" | ||
"url": "git+https://github.com/discord-buttons/discord-buttons.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/AngeloCore/discord-buttons/issues" | ||
"url": "https://github.com/discord-buttons/discord-buttons/issues" | ||
}, | ||
"homepage": "https://discord-buttons.js.org", | ||
"devDependencies": { | ||
"@types/node": "^15.3.0" | ||
"@types/node": "^15.3.0", | ||
"prettier": "^2.3.2" | ||
} | ||
} |
@@ -0,1 +1,4 @@ | ||
> # Deprecated: New lib is coming! Join our support server for more help [here](https://discord.gg/KUS5pjyA4J) | ||
<br><br><br> | ||
<div align="center"> | ||
@@ -23,4 +26,4 @@ <h1>discord-buttons</h1> | ||
```js | ||
const discord = require('discord.js'); //Define the discord.js module | ||
const client = new discord.Client(); //Creating discord.js client (constructor) | ||
const discord = require('discord.js'); // Define the discord.js module | ||
const client = new discord.Client(); // Create a discord.js client (constructor) | ||
const disbut = require('discord-buttons'); | ||
@@ -31,4 +34,4 @@ disbut(client); | ||
## ✍ | Examples | ||
All of our Examples and Button Styles are in [examples.md]([http://](https://github.com/AngeloCore/discord-buttons/blob/main/examples.md)) | ||
Also you can check our [Documentation](https://discord-buttons.js.org), if you have any other problems/questions, you can join our [Support Server!](https://discord.gg/5JtyYqW) | ||
All of our Examples and button styles found in [examples.md]([http://](https://github.com/AngeloCore/discord-buttons/blob/main/examples.md)) and | ||
you can also check our [documentation](https://discord-buttons.js.org), you can join our [support server](https://discord.gg/5JtyYqW) if you are having any issues | ||
@@ -35,0 +38,0 @@ ## 👥 | Contact |
108
src/index.js
@@ -1,64 +0,98 @@ | ||
const { Structures, Client } = require("discord.js"); | ||
const { Structures, Client } = require('discord.js'); | ||
const MessageComponent = require('./v12/Classes/MessageComponent'); | ||
const TextChannel = require('./v12/Classes/TextChannel'); | ||
const DMChannel = require('./v12/Classes/DMChannel'); | ||
const NewsChannel = require('./v12/Classes/NewsChannel'); | ||
const Message = require('./v12/Classes/Message'); | ||
const { MessageComponentTypes } = require('./v12/Constants'); | ||
var version = require('discord.js').version.split(''); | ||
if (version.includes('(')) version = version.join('').split('(').pop().split(''); | ||
version = parseInt(version[0] + version[1]); | ||
module.exports = (client) => { | ||
if (version != 12) { | ||
throw new Error('Your discord.js version must be v12 or higher.'); | ||
} | ||
const { Events } = require('discord.js').Constants; | ||
if (!(client instanceof Client)) throw new Error('INVALID_CLIENT_PROVIDED: Your discord.js Client is invalid or has not been provided.'); | ||
Events.CLICK_BUTTON = 'clickButton'; | ||
const message = Structures.get('Message'); | ||
if (!message.createButtonCollector || typeof message.createButtonCollector !== 'function') { | ||
Structures.extend('TextChannel', () => TextChannel); | ||
Structures.extend('DMChannel', () => DMChannel); | ||
Structures.extend('NewsChannel', () => NewsChannel); | ||
Structures.extend('Message', () => Message); | ||
} | ||
if (version < 11 || version === 11) { | ||
throw new Error('The discord.js version must be v12 or high'); | ||
client.ws.on('INTERACTION_CREATE', (data) => { | ||
if (!data.data.component_type) return; | ||
switch (data.data.component_type) { | ||
case MessageComponentTypes.BUTTON: | ||
client.emit('clickButton', new MessageComponent(client, data)); | ||
break; | ||
case MessageComponentTypes.SELECT_MENU: | ||
client.emit('clickMenu', new MessageComponent(client, data, true)); | ||
break; | ||
default: | ||
client.emit('debug', `Unknown interaction component type, ${data.data.component_type}`); | ||
break; | ||
} | ||
}); | ||
}; | ||
if (!client || !client instanceof Client) throw new Error("INVALID_CLIENT_PROVIDED: The discord.js client is not provided or is invalid...") | ||
module.exports.multipleImport = (...clients) => { | ||
if (version != 12) { | ||
throw new Error('Your discord.js version must be v12 or higher.'); | ||
} | ||
const TextChannel = require('./v12/Classes/TextChannel'); | ||
const DMChannel = require('./v12/Classes/DMChannel'); | ||
const NewsChannel = require('./v12/Classes/NewsChannel'); | ||
const message = Structures.get('Message'); | ||
if (!message.createButtonCollector || typeof message.createButtonCollector !== 'function') { | ||
Structures.extend('TextChannel', () => TextChannel); | ||
Structures.extend('DMChannel', () => DMChannel); | ||
Structures.extend('NewsChannel', () => NewsChannel); | ||
Structures.extend('Message', () => Message); | ||
} | ||
if (version === 12) { | ||
const Message = require('./v12/Classes/Message'); | ||
Structures.extend('Message', () => Message); | ||
} else if (version === 13) { | ||
const Message = require('./v13/Classes/Message'); | ||
const CommandInteraction = require('./v13/Classes/CommandInteraction'); | ||
Structures.extend('Message', () => Message); | ||
Structures.extend('CommandInteraction', () => CommandInteraction); | ||
} | ||
clients.forEach((client) => { | ||
if (!(client instanceof Client)) throw new Error('INVALID_CLIENT_PROVIDED: Your discord.js Client is invalid or has not been provided.'); | ||
client.ws.on('INTERACTION_CREATE', (data) => { | ||
if (!data.data.component_type) return; | ||
if (!data.message) return; | ||
switch (data.data.component_type) { | ||
case MessageComponentTypes.BUTTON: | ||
client.emit('clickButton', new MessageComponent(client, data)); | ||
break; | ||
if (data.data.component_type) { | ||
const MessageComponent = require('./v12/Classes/clickButton'); | ||
const button = new MessageComponent(client, data); | ||
client.emit('clickButton', button); | ||
} | ||
case MessageComponentTypes.SELECT_MENU: | ||
client.emit('clickMenu', new MessageComponent(client, data, true)); | ||
break; | ||
default: | ||
client.emit('debug', `Unknown interaction component type, ${data.data.component_type}`); | ||
break; | ||
} | ||
}); | ||
}); | ||
}; | ||
return; | ||
} | ||
module.exports.MessageButton = require(`./v${version}/Classes/MessageButton`); | ||
module.exports.MessageButton = require(`./v12/Classes/MessageButton`); | ||
module.exports.MessageMenu = require(`./v12/Classes/MessageMenu`); | ||
module.exports.MessageMenuOption = require(`./v12/Classes/MessageMenuOption`); | ||
module.exports.MessageActionRow = require('./v12/Classes/MessageActionRow'); | ||
module.exports.ButtonInteraction = require('./v12/Classes/clickButton'); | ||
module.exports.Message = require(`./v${version}/Classes/Message`); | ||
module.exports.ButtonCollector = require(`./v${version}/Classes/ButtonCollector`); | ||
module.exports.MessageComponent = require('./v12/Classes/MessageComponent'); | ||
module.exports.Message = Message; | ||
module.exports.ButtonCollector = require(`./v12/Classes/ButtonCollector`); | ||
module.exports.MenuCollector = require(`./v12/Classes/MenuCollector`); | ||
module.exports.APIMessage = require('./v12/Classes/APIMessage').APIMessage; | ||
module.exports.sendAPICallback = require('./v12/Classes/APIMessage').sendAPICallback; | ||
module.exports.DMChannel = require('./v12/Classes/DMChannel'); | ||
module.exports.NewsChannel = require('./v12/Classes/NewsChannel'); | ||
module.exports.TextChannel = require('./v12/Classes/TextChannel'); | ||
module.exports.DMChannel = DMChannel; | ||
module.exports.NewsChannel = NewsChannel; | ||
module.exports.TextChannel = TextChannel; | ||
module.exports.WebhookClient = require('./v12/Classes/WebhookClient'); | ||
module.exports.Util = require('./v12/Util'); | ||
module.exports.Constants = require('./v12/Constants'); | ||
version === 13 ? module.exports.CommandInteraction = require('./v13/Classes/CommandInteraction') : null | ||
module.exports.InteractionReply = require(`./v12/Classes/managers/InteractionReply`); |
@@ -1,214 +0,159 @@ | ||
const { APIMessage: dAPIMessage, MessageEmbed } = require("discord.js"); | ||
const Util = require('../Util'); | ||
const { APIMessage: dAPIMessage, MessageEmbed } = require('discord.js'); | ||
const { MessageComponentTypes } = require('../Constants.js'); | ||
const BaseMessageComponent = require('./interfaces/BaseMessageComponent'); | ||
const MessageActionRow = require('./MessageActionRow'); | ||
const MessageButton = require('./MessageButton'); | ||
const MessageMenu = require('./MessageMenu'); | ||
class sendAPICallback extends dAPIMessage { | ||
resolveData() { | ||
class APIMessage extends dAPIMessage { | ||
resolveData() { | ||
if (this.data) { | ||
return this; | ||
} | ||
if (this.data) { | ||
return this; | ||
} | ||
super.resolveData(); | ||
if (typeof (this.options.content) === 'object') { | ||
this.options = this.options.content; | ||
this.options.content = null; | ||
} | ||
if (this.options.content instanceof MessageEmbed) { | ||
this.data.embed = this.options.content; | ||
this.data.embeds.push(this.options.content); | ||
this.data.content = undefined; | ||
} | ||
super.resolveData(); | ||
let components = []; | ||
let hasActionRow = false; | ||
let hasComponent = false; | ||
if (MessageComponentTypes[this.options.type]) { | ||
hasComponent = true; | ||
if (this.options.type === MessageComponentTypes.ACTION_ROW) { | ||
components.push(new MessageActionRow(this.options)); | ||
hasActionRow = true; | ||
} else if (this.options.type === MessageComponentTypes.BUTTON || this.options.type === MessageComponentTypes.SELECT_MENU) { | ||
components.push(new MessageActionRow().addComponents(this.options)); | ||
} | ||
} | ||
if (this.options.content instanceof MessageEmbed) { | ||
this.data.embed = this.options.content; | ||
this.data.content = null; | ||
} | ||
if (this.options.flags) { | ||
this.data.flags = parseInt(this.options.flags); | ||
} | ||
if (typeof (this.options.ephemeral) === 'boolean' && this.options.ephemeral === true) { | ||
this.data.flags = 64; | ||
} | ||
let components = []; | ||
let hasActionRow = false; | ||
if (MessageComponentTypes[this.options.type]) { | ||
if (this.options.type === MessageComponentTypes.ACTION_ROW) { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: this.options.components.map(b => BaseMessageComponent.create(Util.resolveButton(b))) | ||
}); | ||
hasActionRow = true; | ||
} else { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: [BaseMessageComponent.create(Util.resolveButton(this.options))] | ||
}); | ||
if (this.options.component) { | ||
hasComponent = true; | ||
if (Array.isArray(this.options.component)) { | ||
if (hasActionRow === false) { | ||
let buttons = []; | ||
this.options.component.map((c) => { | ||
if (c instanceof MessageActionRow) { | ||
components.push(new MessageActionRow(c)); | ||
} else if (c instanceof MessageButton) { | ||
buttons.push(c); | ||
} else if (c instanceof MessageMenu) { | ||
components.push(new MessageActionRow().addComponent(c)); | ||
} | ||
}); | ||
if (buttons.length > 0) components.push(new MessageActionRow().addComponents(buttons)); | ||
} | ||
if (this.options.component) { | ||
if (this.options.component instanceof MessageActionRow) { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: this.options.component.components.map(b => BaseMessageComponent.create(Util.resolveButton(b))) | ||
}); | ||
} else { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: [BaseMessageComponent.create(Util.resolveButton(this.options.component))] | ||
}); | ||
} | ||
} else { | ||
let buttons = []; | ||
if (this.options.component instanceof MessageActionRow) { | ||
components.push(new MessageActionRow(this.options.component)); | ||
} else if (this.options.component instanceof MessageButton) { | ||
buttons.push(this.options.component); | ||
} else if (this.options.component instanceof MessageMenu) { | ||
components.push(new MessageActionRow().addComponent(this.options.component)); | ||
} | ||
if (buttons.length > 0) components.push(new MessageActionRow().addComponents(buttons)); | ||
} | ||
} | ||
if (this.options.components) { | ||
if (Array.isArray(this.options.components)) { | ||
if (hasActionRow === false) { | ||
components.push(...this.options.components.map(c => { | ||
let buttons = []; | ||
buttons.push(...c.components.map(b => BaseMessageComponent.create(Util.resolveButton(b)))); | ||
return { | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: buttons | ||
} | ||
})); | ||
} | ||
} else { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: this.options.components.components.map(b => BaseMessageComponent.create(Util.resolveButton(b))) | ||
}) | ||
if (this.options.components) { | ||
hasComponent = true; | ||
if (Array.isArray(this.options.components)) { | ||
if (hasActionRow === false) { | ||
let buttons = []; | ||
this.options.components.map((c) => { | ||
if (c instanceof MessageActionRow) { | ||
components.push(new MessageActionRow(c)); | ||
} else if (c instanceof MessageButton) { | ||
buttons.push(c); | ||
} else if (c instanceof MessageMenu) { | ||
components.push(new MessageActionRow().addComponent(c)); | ||
} | ||
}); | ||
if (buttons.length > 0) components.push(new MessageActionRow().addComponents(buttons)); | ||
} | ||
if (this.options.buttons) { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: Array.isArray(this.options.buttons) ? this.options.buttons.map(b => BaseMessageComponent.create(Util.resolveButton(b))) : [BaseMessageComponent.create(Util.resolveButton(this.options.buttons))] | ||
}); | ||
} else { | ||
let buttons = []; | ||
if (this.options.components instanceof MessageActionRow) { | ||
components.push(new MessageActionRow(this.options.components)); | ||
} else if (this.options.components instanceof MessageButton) { | ||
buttons.push(this.options.components); | ||
} else if (this.options.components instanceof MessageMenu) { | ||
components.push(new MessageActionRow().addComponent(this.options.components)); | ||
} | ||
if (buttons.length > 0) components.push(new MessageActionRow().addComponents(buttons)); | ||
} | ||
} | ||
if (this.options.button) { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: Array.isArray(this.options.button) ? this.options.button.map(b => BaseMessageComponent.create(Util.resolveButton(b))) : [BaseMessageComponent.create(Util.resolveButton(this.options.button))] | ||
}); | ||
} | ||
if (this.options.buttons) { | ||
hasComponent = true; | ||
components.push(new MessageActionRow().addComponents(this.options.buttons)); | ||
} | ||
if (this.options === null && !this.options === undefined) components = []; | ||
if (this.options.button) { | ||
hasComponent = true; | ||
components.push(new MessageActionRow().addComponents(this.options.button)); | ||
} | ||
if (typeof components.length == 'number') { | ||
this.data.components = components.length === 0 ? [] : components; | ||
} | ||
if (this.options.menus) { | ||
hasComponent = true; | ||
Array.isArray(this.options.menus) | ||
? this.options.menus.map((m) => components.push(new MessageActionRow().addComponent(m))) | ||
: components.push(new MessageActionRow().addComponent(this.options.menus)); | ||
} | ||
return this; | ||
if (this.options.menu) { | ||
hasComponent = true; | ||
Array.isArray(this.options.menu) | ||
? this.options.menu.map((m) => components.push(new MessageActionRow().addComponent(m))) | ||
: components.push(new MessageActionRow().addComponent(this.options.menu)); | ||
} | ||
} | ||
class APIMessage extends dAPIMessage { | ||
resolveData() { | ||
if ( | ||
this.options.components === null || | ||
this.options.component === null || | ||
this.options.buttons === null || | ||
this.options.button === null || | ||
this.options.menus === null || | ||
this.options.menu === null | ||
) { | ||
hasComponent = true; | ||
components = []; | ||
} | ||
if (this.data) { | ||
return this; | ||
} | ||
if (components.length > 5) components.length = 5; | ||
if (typeof (this.options.content) === 'object') { | ||
this.options = this.options.content; | ||
this.options.content = null; | ||
} | ||
if (typeof components.length == 'number' && hasComponent === true) this.data.components = components.length === 0 ? [] : components; | ||
super.resolveData(); | ||
return this; | ||
} | ||
} | ||
if (this.options.content instanceof MessageEmbed) { | ||
this.data.embed = this.options.content; | ||
this.data.content = null; | ||
} | ||
class sendAPICallback extends APIMessage { | ||
resolveData() { | ||
if (this.data) { | ||
return this; | ||
} | ||
let components = []; | ||
let hasActionRow = false; | ||
if (MessageComponentTypes[this.options.type]) { | ||
if (this.options.type === MessageComponentTypes.ACTION_ROW) { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: this.options.components.map(b => BaseMessageComponent.create(Util.resolveButton(b))) | ||
}); | ||
hasActionRow = true; | ||
} else { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: [BaseMessageComponent.create(Util.resolveButton(this.options))] | ||
}); | ||
} | ||
} | ||
super.resolveData(); | ||
if (this.options.component) { | ||
if (this.options.flags) { | ||
this.data.flags = parseInt(this.options.flags); | ||
} | ||
if (this.options.component instanceof MessageActionRow) { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: this.options.component.components.map(b => BaseMessageComponent.create(Util.resolveButton(b))) | ||
}); | ||
} else { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: [BaseMessageComponent.create(Util.resolveButton(this.options.component))] | ||
}); | ||
} | ||
} | ||
if (typeof this.options.ephemeral === 'boolean' && this.options.ephemeral === true) { | ||
this.data.flags = 1 << 6; | ||
} | ||
if (this.options.components) { | ||
if (Array.isArray(this.options.components)) { | ||
if (hasActionRow === false) { | ||
components.push(...this.options.components.map(c => { | ||
let buttons = []; | ||
buttons.push(...c.components.map(b => BaseMessageComponent.create(Util.resolveButton(b)))); | ||
return { | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: buttons | ||
} | ||
})); | ||
} | ||
} else { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: this.options.components.components.map(b => BaseMessageComponent.create(Util.resolveButton(b))) | ||
}) | ||
} | ||
} | ||
if (this.options.buttons) { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: Array.isArray(this.options.buttons) ? this.options.buttons.map(b => BaseMessageComponent.create(Util.resolveButton(b))) : [BaseMessageComponent.create(Util.resolveButton(this.options.buttons))] | ||
}); | ||
} | ||
if (this.options.button) { | ||
components.push({ | ||
type: MessageComponentTypes.ACTION_ROW, | ||
components: Array.isArray(this.options.button) ? this.options.button.map(b => BaseMessageComponent.create(Util.resolveButton(b))) : [BaseMessageComponent.create(Util.resolveButton(this.options.button))] | ||
}); | ||
} | ||
if (this.options === null && !this.options === undefined) components = []; | ||
if (typeof components.length == 'number') { | ||
this.data.components = components.length === 0 ? [] : components; | ||
} | ||
return this; | ||
} | ||
return this; | ||
} | ||
} | ||
module.exports = { | ||
sendAPICallback, | ||
APIMessage | ||
} | ||
APIMessage, | ||
sendAPICallback, | ||
}; |
@@ -1,2 +0,2 @@ | ||
const { Collector } = require("discord.js"); | ||
const { Collector } = require('discord.js'); | ||
const Collection = require('discord.js').Collection; | ||
@@ -88,2 +88,2 @@ const { Events } = require('discord.js').Constants; | ||
module.exports = ButtonCollector; | ||
module.exports = ButtonCollector; |
@@ -6,29 +6,27 @@ const DMChannel = require('discord.js').Structures.get('DMChannel'); | ||
class ExtendedDMChannel extends DMChannel { | ||
async send(content, options) { | ||
const User = Structures.get('User'); | ||
const GuildMember = Structures.get('GuildMember'); | ||
async send(content, options) { | ||
const User = Structures.get('User'); | ||
const GuildMember = Structures.get('GuildMember'); | ||
if (this instanceof User || this instanceof GuildMember) { | ||
return this.createDM().then(dm => dm.send(content, options)); | ||
} | ||
if (this instanceof User || this instanceof GuildMember) { | ||
return this.createDM().then((dm) => dm.send(content, options)); | ||
} | ||
let apiMessage; | ||
let apiMessage; | ||
if (content instanceof APIMessage) { | ||
apiMessage = content.resolveData(); | ||
} else { | ||
apiMessage = APIMessage.create(this, content, options).resolveData(); | ||
} | ||
if (content instanceof APIMessage) { | ||
apiMessage = content.resolveData(); | ||
} else { | ||
apiMessage = APIMessage.create(this, content, options).resolveData(); | ||
} | ||
if (Array.isArray(apiMessage.data.content)) { | ||
return Promise.all(apiMessage.data.content.map(this.send.bind(this))); | ||
} | ||
if (Array.isArray(apiMessage.data.content)) { | ||
return Promise.all(apiMessage.data.content.map(this.send.bind(this))); | ||
} | ||
const { data, files } = await apiMessage.resolveFiles(); | ||
return this.client.api.channels[this.id].messages | ||
.post({ data, files }) | ||
.then(d => this.client.actions.MessageCreate.handle(d).message); | ||
} | ||
const { data, files } = await apiMessage.resolveFiles(); | ||
return this.client.api.channels[this.id].messages.post({ data, files }).then((d) => this.client.actions.MessageCreate.handle(d).message); | ||
} | ||
} | ||
module.exports = ExtendedDMChannel; | ||
module.exports = ExtendedDMChannel; |
@@ -1,54 +0,72 @@ | ||
const Message = require("discord.js").Structures.get('Message'); | ||
const Message = require('discord.js').Structures.get('Message'); | ||
const ButtonCollector = require('./ButtonCollector'); | ||
const MenuCollector = require('./MenuCollector'); | ||
const APIMessage = require('./APIMessage').APIMessage; | ||
const BaseMessageComponent = require('./interfaces/BaseMessageComponent'); | ||
const MessageActionRow = require('./MessageActionRow'); | ||
class ExtendedMessage extends Message { | ||
_patch(data) { | ||
super._patch(data); | ||
_patch(data) { | ||
super._patch(data); | ||
this.components = (data.components || []).map(c => BaseMessageComponent.create(c, this.client)); | ||
return this; | ||
this.components = []; | ||
if (Array.isArray(data.components)) { | ||
data.components.map((c) => { | ||
this.components.push(new MessageActionRow(c, true)); | ||
}); | ||
} | ||
createButtonCollector(filter, options = {}) { | ||
return new ButtonCollector(this, filter, options); | ||
} | ||
return this; | ||
} | ||
awaitButtons(filter, options = {}) { | ||
return new Promise((resolve, reject) => { | ||
const collector = this.createButtonCollector(filter, options); | ||
collector.once('end', (buttons, reason) => { | ||
if (options.errors && options.errors.includes(reason)) { | ||
reject(buttons); | ||
} else { | ||
resolve(buttons); | ||
} | ||
}); | ||
}) | ||
} | ||
createButtonCollector(filter, options = {}) { | ||
return new ButtonCollector(this, filter, options); | ||
} | ||
reply(content, options) { | ||
return this.channel.send( | ||
content instanceof APIMessage | ||
? content | ||
: APIMessage.transformOptions(content, options, { reply: this.member || this.author }), | ||
); | ||
} | ||
awaitButtons(filter, options = {}) { | ||
return new Promise((resolve, reject) => { | ||
const collector = this.createButtonCollector(filter, options); | ||
collector.once('end', (buttons, reason) => { | ||
if (options.errors && options.errors.includes(reason)) { | ||
reject(buttons); | ||
} else { | ||
resolve(buttons); | ||
} | ||
}); | ||
}); | ||
} | ||
edit(content, options = {}) { | ||
if (this.components.length > 0 && options !== null && options.component === undefined && options.components === undefined) { | ||
options.components = this.components.map(c => BaseMessageComponent.create(c, this.client)); | ||
createMenuCollector(filter, options = {}) { | ||
return new MenuCollector(this, filter, options); | ||
} | ||
awaitMenus(filter, options = {}) { | ||
return new Promise((resolve, reject) => { | ||
const collector = this.createMenuCollector(filter, options); | ||
collector.once('end', (menus, reason) => { | ||
if (options.errors && options.errors.includes(reason)) { | ||
reject(menus); | ||
} else { | ||
resolve(menus); | ||
} | ||
const { data } = | ||
content instanceof APIMessage ? content.resolveData() : APIMessage.create(this, content, options).resolveData(); | ||
return this.client.api.channels[this.channel.id].messages[this.id].patch({ data }).then(d => { | ||
const clone = this._clone(); | ||
clone._patch(d); | ||
return clone; | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
reply(content, options) { | ||
return this.channel.send( | ||
content instanceof APIMessage ? content : APIMessage.transformOptions(content, options, { reply: this.member || this.author }), | ||
); | ||
} | ||
edit(content, options) { | ||
if (options === null && options !== undefined) options = { components: null }; | ||
const { data } = content instanceof APIMessage ? content.resolveData() : APIMessage.create(this, content, options).resolveData(); | ||
return this.client.api.channels[this.channel.id].messages[this.id].patch({ data }).then((d) => { | ||
const clone = this._clone(); | ||
clone._patch(d); | ||
return clone; | ||
}); | ||
} | ||
} | ||
module.exports = ExtendedMessage; | ||
module.exports = ExtendedMessage; |
@@ -0,41 +1,104 @@ | ||
const MessageButton = require('./MessageButton'); | ||
const MessageMenu = require('./MessageMenu'); | ||
const { MessageComponentTypes } = require('../Constants'); | ||
const BaseMessageComponent = require('./interfaces/BaseMessageComponent'); | ||
const Util = require('../Util'); | ||
class MessageActionRow extends BaseMessageComponent { | ||
class MessageActionRow { | ||
constructor(data = {}, turnit) { | ||
this.setup(data, turnit); | ||
} | ||
constructor(data = {}) { | ||
super({ type: 'ACTION_ROW' }); | ||
this.setup(data); | ||
} | ||
setup(data, turnit = false) { | ||
this._hasMenu = false; | ||
this._turnit = turnit; | ||
setup(data) { | ||
if ('component' in data) { | ||
this.component = BaseMessageComponent.create(component, null, true); | ||
} | ||
this.components = []; | ||
if ('components' in data) { | ||
this.components = data.components.map(c => BaseMessageComponent.create(c, null, true)); | ||
} | ||
this.type = MessageComponentTypes.ACTION_ROW; | ||
return this; | ||
this.components = []; | ||
if ('component' in data) { | ||
if (data.type === MessageComponentTypes.BUTTON) { | ||
Util.checkButton(data); | ||
this.components.push(new MessageButton(data, turnit)); | ||
} else if (data.type === MessageComponentTypes.SELECT_MENU) { | ||
if (this.components.length > 0) throw new Error('TOO_MUCH_COMPONENTS: You can use 1 select menu per row, without other components.'); | ||
Util.checkMenu(data); | ||
this.components.push(new MessageMenu(data, turnit)); | ||
this._hasMenu = true; | ||
} | ||
} | ||
addComponents(...components) { | ||
this.components.push(...components.flat(2).map(c => BaseMessageComponent.create(c, null, true))); | ||
return this; | ||
if ('components' in data) { | ||
if (!Array.isArray(data.components)) | ||
throw new Error('INVALID_ACTION_ROW_COMPONENTS: The typeof components of MessageActionRow must be an Array.'); | ||
data.components.map((c) => { | ||
if (c.type === MessageComponentTypes.BUTTON) { | ||
if (this._hasMenu) throw new Error('TOO_MUCH_COMPONENTS: You can use 1 select menu per row, without other components.'); | ||
if (this.components.length > 4) throw new Error('TOO_MUCH_COMPONENTS: You can provide 5 buttons per row'); | ||
Util.checkButton(c); | ||
return this.components.push(new MessageButton(c, turnit)); | ||
} else if (c.type === MessageComponentTypes.SELECT_MENU) { | ||
if (this.components.length > 0) throw new Error('TOO_MUCH_COMPONENTS: You can use 1 select menu per row, without other components.'); | ||
return this.components.push(new MessageMenu(c, turnit)); | ||
} | ||
}); | ||
} | ||
addComponent(component) { | ||
return this.addComponents(component); | ||
return this; | ||
} | ||
addComponents(...components) { | ||
components.flat(Infinity).map((c) => { | ||
if (c.type === MessageComponentTypes.BUTTON) { | ||
if (this._hasMenu) throw new Error('TOO_MUCH_COMPONENTS: You can use 1 select menu per row, without other components.'); | ||
if (this.components.length > 4) throw new Error('TOO_MUCH_COMPONENTS: You can provide 5 buttons per row'); | ||
Util.checkButton(c); | ||
return this.components.push(new MessageButton(c, this._turnit)); | ||
} else if (c.type === MessageComponentTypes.SELECT_MENU) { | ||
if (this.components.length > 0) throw new Error('TOO_MUCH_COMPONENTS: You can use 1 select menu per row, without other components.'); | ||
Util.checkMenu(c); | ||
return this.components.push(new MessageMenu(c, this._turnit)); | ||
} | ||
}); | ||
return this; | ||
} | ||
addComponent(data) { | ||
if (data.type === MessageComponentTypes.BUTTON) { | ||
if (this._hasMenu) throw new Error('TOO_MUCH_COMPONENTS: You can use 1 select menu per row, without other components.'); | ||
if (this.components.length > 4) throw new Error('TOO_MUCH_COMPONENTS: You can provide 5 buttons per row'); | ||
Util.checkButton(data); | ||
this.components.push(new MessageButton(data, this._turnit)); | ||
} else if (data.type === MessageComponentTypes.SELECT_MENU) { | ||
if (this.components.length > 0) throw new Error('TOO_MUCH_COMPONENTS: You can use 1 select menu per row, without other components.'); | ||
Util.checkMenu(data); | ||
this.components.push(new MessageMenu(data, this._turnit)); | ||
this._hasMenu = true; | ||
} | ||
return this; | ||
} | ||
toJSON() { | ||
return { | ||
components: this.components.map(c => c.toJSON()), | ||
type: MessageComponentTypes[this.type], | ||
}; | ||
} | ||
removeComponents(index, deleteCount) { | ||
this.components.splice(index, deleteCount); | ||
return this; | ||
} | ||
toJSON() { | ||
return { | ||
components: this.components, | ||
type: MessageComponentTypes.ACTION_ROW, | ||
}; | ||
} | ||
} | ||
module.exports = MessageActionRow; |
const { MessageComponentTypes } = require('../Constants.js'); | ||
const BaseMessageComponent = require('./interfaces/BaseMessageComponent'); | ||
const { resolveString } = require('discord.js').Util; | ||
const { resolveStyle, isEmoji } = require('../Util'); | ||
const Util = require('../Util'); | ||
class MessageButton extends BaseMessageComponent { | ||
class MessageButton { | ||
constructor(data = {}, turnit) { | ||
this.setup(data, turnit); | ||
} | ||
constructor(data = {}) { | ||
super({ type: 'BUTTON' }); | ||
this.setup(data); | ||
} | ||
setup(data, turnit = false) { | ||
this.type = MessageComponentTypes.BUTTON; | ||
setup(data) { | ||
this.style = 'style' in data ? Util.resolveStyle(data.style, turnit) : undefined; | ||
this.style = 'style' in data ? resolveStyle(data.style) : null; | ||
this.label = 'label' in data && data.label ? Util.verifyString(data.label) : undefined; | ||
this.label = ('label' in data && data.label) ? resolveString(data.label) : undefined; | ||
this.disabled = 'disabled' in data ? data.disabled : false; | ||
this.disabled = 'disabled' in data ? data.disabled : false; | ||
if (turnit) this.hash = data.hash; | ||
this.emoji = 'emoji' in data ? data.emoji : undefined; | ||
if (data.emoji) this.setEmoji(data.emoji); | ||
if ('url' in data && data.url) | ||
this.url = resolveString(data.url) | ||
else this.url = undefined; | ||
if ('url' in data && data.url) this.url = Util.verifyString(data.url); | ||
else this.url = undefined; | ||
if (('id' in data && data.id) || ('custom_id' in data && data.custom_id)) | ||
this.custom_id = data.id || data.custom_id; | ||
else this.custom_id = undefined | ||
let id; | ||
if (data.id || data.custom_id) id = data.id || data.custom_id; | ||
return this; | ||
} | ||
turnit ? (this.id = id) : (this.custom_id = id); | ||
setStyle(style) { | ||
style = resolveStyle(style); | ||
this.style = style; | ||
return this; | ||
} | ||
return this; | ||
} | ||
setLabel(label) { | ||
label = resolveString(label); | ||
this.label = label; | ||
return this; | ||
} | ||
setStyle(style) { | ||
style = Util.resolveStyle(style); | ||
this.style = style; | ||
return this; | ||
} | ||
setDisabled(disabled = true) { | ||
this.disabled = disabled; | ||
return this; | ||
} | ||
setLabel(label) { | ||
label = Util.verifyString(label); | ||
this.label = label; | ||
return this; | ||
} | ||
setURL(url) { | ||
this.url = resolveString(url); | ||
return this; | ||
} | ||
setDisabled(disabled) { | ||
if (disabled === false) this.disabled = false; | ||
else this.disabled = true; | ||
return this; | ||
} | ||
setID(id) { | ||
this.custom_id = resolveString(id); | ||
return this; | ||
} | ||
setURL(url) { | ||
this.url = Util.verifyString(url); | ||
return this; | ||
} | ||
setEmoji(emoji, animated) { | ||
if (!emoji) return this; | ||
if (isEmoji(emoji) === true) this.emoji = { name: resolveString(emoji) } | ||
else if (emoji.id) this.emoji = { id: emoji.id } | ||
else if (resolveString(emoji).length > 0) this.emoji = { id: resolveString(emoji) } | ||
else this.emoji = { name: null, id: null }; | ||
if ((animated && typeof (animated) !== 'boolean') || (emoji.animated && typeof (emoji.animated) !== 'boolean')) throw new SyntaxError('The emoji animated option must be boolean'); | ||
if (this.emoji && typeof (emoji.animated) === 'boolean') this.emoji.animated = emoji.animated; | ||
if (this.emoji && typeof (animated) === 'boolean') this.emoji.animated = animated; | ||
return this; | ||
} | ||
setID(id) { | ||
this.custom_id = Util.verifyString(id); | ||
return this; | ||
} | ||
toJSON() { | ||
return { | ||
type: MessageComponentTypes.BUTTON, | ||
style: this.style, | ||
label: this.label, | ||
emoji: this.emoji, | ||
disabled: this.disabled, | ||
url: this.url, | ||
custom_id: this.custom_id | ||
} | ||
} | ||
setEmoji(emoji, animated) { | ||
this.emoji = Util.resolveEmoji(emoji, animated); | ||
return this; | ||
} | ||
toJSON() { | ||
return { | ||
type: MessageComponentTypes.BUTTON, | ||
style: this.style, | ||
label: this.label, | ||
emoji: this.emoji, | ||
disabled: this.disabled, | ||
url: this.url, | ||
custom_id: this.custom_id, | ||
}; | ||
} | ||
} | ||
module.exports = MessageButton; | ||
module.exports = MessageButton; |
@@ -6,29 +6,27 @@ const NewsChannel = require('discord.js').Structures.get('NewsChannel'); | ||
class ExtendedNewsChannel extends NewsChannel { | ||
async send(content, options) { | ||
const User = Structures.get('User'); | ||
const GuildMember = Structures.get('GuildMember'); | ||
async send(content, options) { | ||
const User = Structures.get('User'); | ||
const GuildMember = Structures.get('GuildMember'); | ||
if (this instanceof User || this instanceof GuildMember) { | ||
return this.createDM().then(dm => dm.send(content, options)); | ||
} | ||
if (this instanceof User || this instanceof GuildMember) { | ||
return this.createDM().then((dm) => dm.send(content, options)); | ||
} | ||
let apiMessage; | ||
let apiMessage; | ||
if (content instanceof APIMessage) { | ||
apiMessage = content.resolveData(); | ||
} else { | ||
apiMessage = APIMessage.create(this, content, options).resolveData(); | ||
} | ||
if (content instanceof APIMessage) { | ||
apiMessage = content.resolveData(); | ||
} else { | ||
apiMessage = APIMessage.create(this, content, options).resolveData(); | ||
} | ||
if (Array.isArray(apiMessage.data.content)) { | ||
return Promise.all(apiMessage.data.content.map(this.send.bind(this))); | ||
} | ||
if (Array.isArray(apiMessage.data.content)) { | ||
return Promise.all(apiMessage.data.content.map(this.send.bind(this))); | ||
} | ||
const { data, files } = await apiMessage.resolveFiles(); | ||
return this.client.api.channels[this.id].messages | ||
.post({ data, files }) | ||
.then(d => this.client.actions.MessageCreate.handle(d).message); | ||
} | ||
const { data, files } = await apiMessage.resolveFiles(); | ||
return this.client.api.channels[this.id].messages.post({ data, files }).then((d) => this.client.actions.MessageCreate.handle(d).message); | ||
} | ||
} | ||
module.exports = ExtendedNewsChannel; | ||
module.exports = ExtendedNewsChannel; |
@@ -6,29 +6,27 @@ const TextChannel = require('discord.js').Structures.get('TextChannel'); | ||
class ExtendedTextChannel extends TextChannel { | ||
async send(content, options) { | ||
const User = Structures.get('User'); | ||
const GuildMember = Structures.get('GuildMember'); | ||
async send(content, options) { | ||
const User = Structures.get('User'); | ||
const GuildMember = Structures.get('GuildMember'); | ||
if (this instanceof User || this instanceof GuildMember) { | ||
return this.createDM().then(dm => dm.send(content, options)); | ||
} | ||
if (this instanceof User || this instanceof GuildMember) { | ||
return this.createDM().then((dm) => dm.send(content, options)); | ||
} | ||
let apiMessage; | ||
let apiMessage; | ||
if (content instanceof APIMessage) { | ||
apiMessage = content.resolveData(); | ||
} else { | ||
apiMessage = APIMessage.create(this, content, options).resolveData(); | ||
} | ||
if (content instanceof APIMessage) { | ||
apiMessage = content.resolveData(); | ||
} else { | ||
apiMessage = APIMessage.create(this, content, options).resolveData(); | ||
} | ||
if (Array.isArray(apiMessage.data.content)) { | ||
return Promise.all(apiMessage.data.content.map(this.send.bind(this))); | ||
} | ||
if (Array.isArray(apiMessage.data.content)) { | ||
return Promise.all(apiMessage.data.content.map(this.send.bind(this))); | ||
} | ||
const { data, files } = await apiMessage.resolveFiles(); | ||
return this.client.api.channels[this.id].messages | ||
.post({ data, files }) | ||
.then(d => this.client.actions.MessageCreate.handle(d).message); | ||
} | ||
const { data, files } = await apiMessage.resolveFiles(); | ||
return this.client.api.channels[this.id].messages.post({ data, files }).then((d) => this.client.actions.MessageCreate.handle(d).message); | ||
} | ||
} | ||
module.exports = ExtendedTextChannel; | ||
module.exports = ExtendedTextChannel; |
@@ -1,46 +0,53 @@ | ||
const { WebhookClient, MessageEmbed } = require('discord.js'); | ||
const { MessageEmbed, WebhookClient } = require('discord.js'); | ||
const APIMessage = require('./APIMessage').sendAPICallback; | ||
class ExtendedWebhookClient extends WebhookClient { | ||
async editMessage(message, content, options) { | ||
async editMessage(message, content, options) { | ||
if (content ? content.embed : null instanceof MessageEmbed) { | ||
options | ||
? options.embeds && Array.isArray(options.embeds) | ||
? options.embeds.push(content.embed) | ||
: (options.embeds = [content.embed]) | ||
: (options = {}) && (options.embeds = [content.embed]); | ||
content = null; | ||
} | ||
if (content ? content.embed : null instanceof MessageEmbed) { | ||
options ? (options.embeds && Array.isArray(options.embeds) ? options.embeds.push(content.embed) : options.embeds = [content.embed]) : (options = {}) && (options.embeds = [content.embed]); | ||
content = null; | ||
} | ||
if (options && options.embed) { | ||
options.embeds && Array.isArray(options.embeds) ? options.embeds.push(options.embed) : (options.embeds = [options.embed]); | ||
options.embed = null; | ||
} | ||
if (options && options.embed) { | ||
options ? (options.embeds && Array.isArray(options.embeds) ? options.embeds.push(options.embed) : options.embeds = [options.embed]) : (options = {}) && (options.embeds = [options.embed]); | ||
options.embed = null; | ||
} | ||
let apiMessage; | ||
let apiMessage; | ||
if (content instanceof APIMessage) { | ||
apiMessage = content.resolveData(); | ||
} else { | ||
apiMessage = APIMessage.create(this, content, options).resolveData(); | ||
} | ||
if (content instanceof APIMessage) { | ||
apiMessage = content.resolveData(); | ||
} else { | ||
apiMessage = APIMessage.create(this, content, options).resolveData(); | ||
} | ||
const { data, files } = await apiMessage.resolveFiles(); | ||
const { data, files } = await apiMessage.resolveFiles(); | ||
return this.client.api | ||
.webhooks(this.id, this.token) | ||
.messages(typeof message === 'string' ? message : message.id) | ||
.patch({ data, files }); | ||
} | ||
return this.client.api | ||
.webhooks(this.id, this.token) | ||
.messages(typeof message === 'string' ? message : message.id) | ||
.patch({ data, files }); | ||
} | ||
async deleteMessage(message) { | ||
await this.client.api | ||
.webhooks(this.id, this.token) | ||
.messages(typeof message === 'string' ? message : message.id) | ||
.delete(); | ||
} | ||
async deleteMessage(message) { | ||
await this.client.api | ||
.webhooks(this.id, this.token) | ||
.messages(typeof message === 'string' ? message : message.id) | ||
.delete(); | ||
} | ||
async fetchMessage(message, cache = true) { | ||
const data = await this.client.api.webhooks(this.id, this.token).messages(message).get(); | ||
return this.client.channels ? (this.client.channels.cache.get(data.channel_id) ? this.client.channels.cache.get(data.channel_id).messages.add(data, cache) : null) : data; | ||
} | ||
async fetchMessage(message, cache = true) { | ||
const data = await this.client.api.webhooks(this.id, this.token).messages(message).get(); | ||
return this.client.channels | ||
? this.client.channels.cache.get(data.channel_id) | ||
? this.client.channels.cache.get(data.channel_id).messages.add(data, cache) | ||
: null | ||
: data; | ||
} | ||
} | ||
module.exports = ExtendedWebhookClient; | ||
module.exports = ExtendedWebhookClient; |
exports.MessageComponentTypes = createEnum([null, 'ACTION_ROW', 'BUTTON', 'SELECT_MENU']); | ||
exports.MessageButtonStyles = createEnum([null, 'blurple', 'grey', 'green', 'red', 'url']); | ||
exports.MessageButtonStylesAliases = createEnum([null, 'PRIMARY', 'SECONDARY', 'SUCCESS', 'DESTRUCTIVE', 'LINK']); | ||
exports.InteractionReplyTypes = createEnum([ | ||
null, | ||
'PONG', | ||
null, | ||
null, | ||
'CHANNEL_MESSAGE_WITH_SOURCE', | ||
'DEFFERED_CHANNEL_MESSAGE_WITH_SOURCE', | ||
'DEFFERED_UPDATE_MESSAGE', | ||
'UPDATE_MESSAGE', | ||
]); | ||
function createEnum(keys) { | ||
const obj = {}; | ||
for (const [index, key] of keys.entries()) { | ||
if (key === null) continue; | ||
obj[key] = index; | ||
obj[index] = key; | ||
} | ||
return obj; | ||
} | ||
const obj = {}; | ||
for (const [index, key] of keys.entries()) { | ||
if (key === null) continue; | ||
obj[key] = index; | ||
obj[index] = key; | ||
} | ||
return obj; | ||
} |
@@ -1,62 +0,154 @@ | ||
const { MessageButtonStyles, MessageButtonStylesAliases, MessageComponentTypes } = require('./Constants.js'); | ||
const { MessageButtonStyles, MessageButtonStylesAliases, MessageComponentTypes } = require('./Constants'); | ||
module.exports = { | ||
resolveStyle(style) { | ||
if (!style || style === undefined || style === null) throw new TypeError('NO_BUTTON_STYLE: Please provide button style'); | ||
class Util extends null { | ||
static resolveStyle(style, turnit) { | ||
if (!style) throw new TypeError('NO_BUTTON_STYLE: Please provide a button style.'); | ||
if (style === 'gray') style = 'grey'; | ||
if (style === 'gray') style = 'grey'; | ||
if ((!MessageButtonStyles[style] | ||
|| | ||
MessageButtonStyles[style] === undefined | ||
|| | ||
MessageButtonStyles[style] === null) | ||
&& | ||
(!MessageButtonStylesAliases[style] | ||
|| | ||
MessageButtonStylesAliases[style] === undefined | ||
|| | ||
MessageButtonStylesAliases[style] === null)) throw new TypeError('INVALID_BUTTON_STYLE: An invalid button styles was provided'); | ||
if (!MessageButtonStyles[style] && !MessageButtonStylesAliases[style]) | ||
throw new TypeError('INVALID_BUTTON_STYLE: An invalid button style was provided.'); | ||
return typeof style === 'string' ? MessageButtonStyles[style] : style; | ||
}, | ||
resolveButton(data) { | ||
if (typeof style === (turnit ? 'string' : 'number')) return style; | ||
if (data.type !== 2 && data.type !== 3) throw new TypeError('NO_BUTTON_TYPE: Invalid type'); | ||
return MessageButtonStyles[style] ? MessageButtonStyles[style] : MessageButtonStylesAliases[style]; | ||
} | ||
if (!data.style) throw new TypeError('NO_BUTTON_STYLE: Please provide button style'); | ||
static checkButton(data) { | ||
if (data.type !== MessageComponentTypes.BUTTON) throw new TypeError('INVALID_BUTTON_TYPE: Invalid type.'); | ||
if (!data.label && !data.emoji) throw new TypeError('NO_BUTTON_LABEL_AND_EMOJI: Please provide button label and/or emoji'); | ||
if (!data.style) throw new TypeError('NO_BUTTON_STYLE: Please provide a button style.'); | ||
if ('disabled' in data && typeof (data.disabled) !== 'boolean') throw new TypeError('BUTTON_DISABLED: The button disabled option must be boolean (true/false)') | ||
if (!data.label && !data.emoji) throw new TypeError('NO_BUTTON_LABEL_AND_EMOJI: Please provide a button label and/or an emoji.'); | ||
if (data.style === MessageButtonStyles['url'] && !data.url) throw new TypeError('NO_BUTTON_URL: You provided url style, you must provide an URL'); | ||
if ('disabled' in data && typeof data.disabled !== 'boolean') | ||
throw new TypeError('BUTTON_DISABLED: The button disabled option must be a boolean type. (true/false)'); | ||
if (data.style !== MessageButtonStyles['url'] && data.url) throw new TypeError('BOTH_URL_CUSTOM_ID: A custom id and url cannot both be specified'); | ||
if (data.style === MessageButtonStyles['url'] && !data.url) | ||
throw new TypeError('NO_BUTTON_URL: You provided a url style, but did not provide a URL.'); | ||
if (data.style === MessageButtonStyles['url'] && data.custom_id) throw new TypeError('BOTH_URL_CUSTOM_ID: A custom id and url cannot both be specified'); | ||
if (data.style !== MessageButtonStyles['url'] && data.url) throw new TypeError('BUTTON_STYLE_MISMATCH: A url button must have url style.'); | ||
if (data.style !== MessageButtonStyles['url'] && !data.custom_id) throw new TypeError('NO_BUTTON_ID: Please provide button id'); | ||
if (data.style === MessageButtonStyles['url'] && data.custom_id) | ||
throw new TypeError('BOTH_URL_CUSTOM_ID: A custom id and url cannot both be specified.'); | ||
if (data.emoji && data.emoji.id && isNaN(data.emoji.id)) throw new TypeError('INCORRECT_EMOJI_ID: Please provide correct emoji id'); | ||
if (data.style !== MessageButtonStyles['url'] && !data.custom_id) throw new TypeError('NO_BUTTON_ID: Please provide a button id'); | ||
if (data.emoji && data.emoji.name && this.isEmoji(data.emoji.name) === false) throw new TypeError('INCORRECT_EMOJI_NAME: Please provide correct emoji'); | ||
return true; | ||
} | ||
return { | ||
style: data.style, | ||
label: data.label, | ||
emoji: data.emoji, | ||
disabled: data.disabled, | ||
url: data.url, | ||
custom_id: data.custom_id, | ||
type: 2 | ||
} | ||
}, | ||
resolveType(type) { | ||
return typeof type === 'string' ? MessageComponentTypes[type] : type; | ||
}, | ||
isEmoji(string) { | ||
let emojiRagex = /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/g; | ||
return emojiRagex.test(string) | ||
} | ||
} | ||
static checkMenu(data) { | ||
if (data.type !== MessageComponentTypes.SELECT_MENU) throw new TypeError('INVALID_MENU_TYPE: Invalid type.'); | ||
if (!data.custom_id) throw new Error('NO_MENU_ID: Please provide a menu id.'); | ||
if (typeof data.custom_id != 'string') | ||
throw new Error(`INVALID_MENU_ID: The typeof MessageMenu.id must be a string, received ${typeof data.custom_id} instead.`); | ||
if (data.custom_id.length > 100) | ||
throw new Error( | ||
`TOO_MANY_CHARACTERS_OF_MENU_ID: The maximum length of MessageMenu.id is 100 characters, received ${data.custom_id.length} instead.`, | ||
); | ||
if (data.placeholder && typeof data.placeholder != 'string') | ||
throw new Error(`INVALID_MENU_PLACEHOLDER: The typeof MessageMenu.placeholder must be a string, received ${typeof data.placeholder} instead.`); | ||
if (data.placeholder && data.placeholder.length > 100) | ||
throw new Error( | ||
`TOO_MANY_CHARACTERS_OF_MENU_PLACEHOLDER: The maximum length of MessageMenu.placeholder is 100 characters, received ${data.placeholder.length} instead.`, | ||
); | ||
if (data.min_values && typeof data.min_values != 'number') | ||
throw new Error(`INVALID_MENU_MIN_VALUES: The typeof MessageMenu.minValues must be a number, received ${typeof data.min_values} instead.`); | ||
if (data.min_values && (data.min_values > 25 || data.min_values < 0)) | ||
throw new Error(`INVALID_MENU_MIN_VALUES: MessageMenu.minValues must be above 0 and below 25.`); | ||
if (typeof data.disabled != 'boolean') | ||
throw new Error(`INVALID_MENU_DISABLED_OPTION: The typeof MessageMenu.disabled must be boolean, received ${typeof data.disabled} instead.`); | ||
this.checkMenuOptions(data.options); | ||
return true; | ||
} | ||
static checkMenuOptions(data) { | ||
if (!Array.isArray(data)) throw new Error('INVALID_OPTIONS: The select menu options must be an array.'); | ||
if (data.length < 1) throw new Error('TOO_LITTLE_MENU_OPTIONS: Please provide at least one MessageMenu option.'); | ||
if (data.length > 25) throw new Error(`TOO_MUCH_MENU_OPTIONS: The limit of MessageMenu.options is 25, you provided ${options.length} options.`); | ||
let hasDefault = false; | ||
data.map((d) => { | ||
if (!d.label) throw new Error('NO_MENU_LABEL: Please provide a menu label.'); | ||
if (typeof d.label != 'string') | ||
throw new Error(`INVALID_MENU_LABEL: The typeof MessageMenuOption.label must be a string, received ${typeof d.label} instead.`); | ||
if (d.label.length > 25) | ||
throw new Error( | ||
`TOO_MANY_CHARACTERS_OF_MENU_LABEL: The maximum length of MessageMenuOption.label is 25 characters, received ${d.label.length} instead.`, | ||
); | ||
if (!d.value) throw new Error('NO_MENU_VALUE: Please provide a menu value.'); | ||
if (typeof d.value != 'string') | ||
throw new Error(`INVALID_MENU_VALUE: The typeof MessageMenuOption.value must be a string, received ${typeof d.value} instead.`); | ||
if (d.value.length > 100) | ||
throw new Error( | ||
`TOO_MANY_CHARACTERS_OF_MENU_VALUE: The maximum length of MessageMenuOption.value is 100 characters, received ${d.value.length} instead.`, | ||
); | ||
if (d.description && typeof d.description != 'string') | ||
throw new Error( | ||
`INVALID_MENU_OPTION_DESCRIPTION: The typeof MessageMenuOption.description must be a string, received ${typeof d.description} instead.`, | ||
); | ||
if (d.description && d.description.length > 50) | ||
throw new Error( | ||
`TOO_MANY_CHARACTERS_OF_MENU_DESCRIPTION: The maximum length of MessageMenuOption.description is 100 characters, received ${d.description.length} instead.`, | ||
); | ||
if (typeof d.default != 'boolean') | ||
throw new Error(`INVALID_MENU_DEFAULT_OPTION: The typeof MessageMenu.default must be boolean, received ${typeof d.default} instead.`); | ||
return d; | ||
}); | ||
return true; | ||
} | ||
static resolveType(type) { | ||
return typeof type === 'string' ? MessageComponentTypes[type] : type; | ||
} | ||
static resolveMenuValues(m1, m2) { | ||
return m2 === undefined ? m1 : m2; | ||
} | ||
static resolveEmoji(emoji, animated) { | ||
if (!emoji) return {}; | ||
if (typeof emoji === 'string') | ||
return /^\d{17,19}$/.test(emoji) ? { id: emoji, animated: typeof animated === 'boolean' ? animated : false } : this.parseEmoji(emoji, animated); | ||
if (!emoji.id && !emoji.name) return null; | ||
if (typeof animated === 'boolean') emoji.animated = animated; | ||
return emoji; | ||
} | ||
static parseEmoji(emoji, animated) { | ||
if (emoji.includes('%')) emoji = decodeURIComponent(text); | ||
if (!emoji.includes(':')) return { animated: typeof animated === 'boolean' ? animated : false, name: emoji, id: null }; | ||
const match = emoji.match(/<?(?:(a):)?(\w{2,32}):(\d{17,19})?>?/); | ||
return match && { animated: typeof animated === 'boolean' ? animated : Boolean(match[1]), name: match[2], id: match[3] || null }; | ||
} | ||
static verifyString(data, allowEmpty = true, errorMessage = `Expected a string, got ${data} instead.`, error = Error) { | ||
if (typeof data !== 'string') throw new error(errorMessage); | ||
if (!allowEmpty && data.length === 0) throw new error(errorMessage); | ||
return data; | ||
} | ||
} | ||
module.exports = Util; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
59277
24
38
2
1067
2
6
1