Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

discord-buttons

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

discord-buttons - npm Package Compare versions

Comparing version 1.0.3-beta to 1.0.4

src/Classes/APIMessage.js

18

package.json
{
"name": "discord-buttons",
"version": "1.0.3-beta",
"version": "1.0.4",
"description": "Discord.js buttons",
"main": "src/index.js",
"types": "types/index.d.ts",
"funding": {
"type" : "individual",
"url" : "https://ko-fi.com/angelocore"
},
"keywords": [

@@ -19,5 +24,2 @@ "discord.js",

"license": "Apache-2.0",
"dependencies": {
"discord.js": "^12.5.3"
},
"repository": {

@@ -30,3 +32,9 @@ "type": "git",

},
"homepage": "https://angelocore.gitbook.io/discord-buttons"
"homepage": "https://angelocore.gitbook.io/discord-buttons",
"devDependencies": {
"@types/node": "^15.3.0"
},
"dependencies": {
"discord.js": "^12.5.3"
}
}

@@ -12,3 +12,3 @@ <div align="center">

<img src="https://cdn.discordapp.com/attachments/805407285659959356/834779256776032276/unknown.png">
<img src="http://pays.host/uploads/390c2d6f-7281-4ebd-9429-5dbff5bcee44/RBcQvq7V_.png">
<br> <br>

@@ -25,34 +25,63 @@ </div>

const client = new discord.Client(); //Creating discord.js client (constructor)
require('discord-buttons')(client); //Starting the discord-buttons class
//or if you want to use MessageButton class
const disbut = require('discord-buttons')(client);
//or if you use just "require('discord-buttons')(client);"
const { MessageButton } = require('discord-buttons');
```
## Example
<br />
## [Documentation](https://angelocore.gitbook.io/discord-buttons)
<br />
## Example with `MessageButton`
```js
const discord = require('discord.js');
const client = new discord.Client();
require('discord-buttons')(client);
let button = new disbut.MessageButton()
.setStyle('red') //default: blurple
.setLabel('My First Button!') //default: NO_LABEL_PROVIDED
.setID('click_to_function') //note: if you use the style "url" you must provide url using .setURL('https://example.com')
.setDisabled(); //disables the button | default: false
client.on('ready', () => console.log(client.user.tag));
message.channel.send('Hey, i am powered by https://npmjs.com/discord-buttons', button);
```
client.on('message', message => {
if (message.content.startsWith('!button')) {
### Multiple Buttons
```js
let button = new disbut.MessageButton()
.setStyle('red') //default: blurple
.setLabel('My First Button!') //default: NO_LABEL_PROVIDED
.setID('click_to_function') //note: if you use the style "url" you must provide url using .setURL('https://example.com')
.setDisabled(); //disables the button | default: false
message.buttons('Hello World!', {
buttons: [
{
style: 'green',
label: 'Click to function!',
id: 'click_to_function'
},
{
style: 'url',
label: 'Vote for me!',
url: 'https://npmjs.com/top.gg-core'
}
]
})
let button2 = new disbut.MessageButton()
.setStyle('url') //default: blurple
.setLabel('My Second Button!') //default: NO_LABEL_PROVIDED
.setURL('[click_to_function](https://npmjs.com/discord-buttons)') //note: if you use other style you must provide id using .setID('myid')
message.channel.send('Hey, i am powered by https://npmjs.com/discord-buttons', {
buttons: [
button, button2
]
});
```
## Example
```js
message.buttons('Hello World!', {
buttons: [
{
style: 'green',
label: 'Click to function!',
id: 'click_to_function'
},
{
style: 'url',
label: 'Vote for me!',
url: 'https://npmjs.com/top.gg-core'
}
]
})
client.login("TOKEN");
```

@@ -59,0 +88,0 @@

const { Structures } = require("discord.js");
const { replyAPIMessage, sendAPIMessage } = require('./APIMessage');
const Message = require('./Classes/Message');
const TextChannel = require('./Classes/TextChannel');
const DMChannel = require('./Classes/DMChannel');
const MessageButton = require('./Classes/MessageButton');
const INTERACTION_CREATE = require('./Events/INTERACTION_CREATE');
class Message extends Structures.get("Message") {
module.exports = (client) => {
async buttons(content, options) {
if (!options || !options.buttons) {
throw new Error('Please provide buttons array');
}
if (!Array.isArray(options.buttons)) {
throw new Error('The buttons must be an array');
}
let buttons = [];
let styles = {
'blupurple': 1,
'grey': 2,
'green': 3,
'red': 4,
'url': 5
};
options.buttons.forEach((x, i) => {
if (!x.style) x.style = 'blupurple';
if (!Object.keys(styles).includes(x.style)) {
throw new Error(`#${i} button has invalid style, recived ${x.style}`);
}
if (!x.label) {
throw new Error(`#${i} button don't has a label`);
}
if (typeof (x.label) !== 'string') x.label = String(x.label);
if (x.style === 'url') {
if (!x.url) {
throw new Error(`If the button style is "url", you must provide url`);
}
} else {
if (!x.id) {
throw new Error(`If the button style is not "url", you must provide id`);
}
}
x.disabled = Boolean(x.disabled);
let style = styles[x.style];
let data = {
type: 2,
style: style,
label: x.label,
custom_id: x.style === 'url' ? null : x.id,
url: x.style === 'url' ? x.url : null,
disabled: x.disabled || false
}
buttons.push(data);
})
options.buttons = buttons;
let { data, files } = sendAPIMessage.create(this, content, options).resolveData();
this.client.api.channels[this.channel.id].messages.post({
headers: {
"Content-Type": 'applications/json'
},
data,
files
});
if (require('./Util').checkDjsVersion() === false) {
throw new Error('The discord.js version must be v12 or high');
}
async buttonsEdit(content, options) {
Structures.extend("Message", () => Message);
Structures.extend("TextChannel", () => TextChannel);
Structures.extend("DMChannel", () => DMChannel);
if (!options || !options.buttons) {
throw new Error('Please provide buttons array');
}
client.ws.on('INTERACTION_CREATE', data => INTERACTION_CREATE(client, data))
if (!Array.isArray(options.buttons)) {
throw new Error('The buttons must be an array');
}
let buttons = [];
let styles = {
'blupurple': 1,
'grey': 2,
'green': 3,
'red': 4,
'url': 5
};
options.buttons.forEach((x, i) => {
if (!x.style) x.style = 'blupurple';
if (!Object.keys(styles).includes(x.style)) {
throw new Error(`#${i} button has invalid style, recived ${x.style}`);
}
if (!x.label) {
throw new Error(`#${i} button don't has a label`);
}
if (typeof (x.label) !== 'string') x.label = String(x.label);
if (x.style === 'url') {
if (!x.url) {
throw new Error(`If the button style is "url", you must provide url`);
}
} else {
if (!x.id) {
throw new Error(`If the button style is not "url", you must provide id`);
}
}
x.disabled = Boolean(x.disabled);
let style = styles[x.style];
let data = {
type: 2,
style: style,
label: x.label,
custom_id: x.style === 'url' ? null : x.id,
url: x.style === 'url' ? x.url : null,
disabled: x.disabled || false
}
buttons.push(data);
})
options.buttons = buttons;
let { data, files } = replyAPIMessage.create(this, content, options).resolveData();
this.client.api.channels[this.channel.id].messages[this.id].patch({
headers: {
"Content-Type": 'applications/json'
},
data,
files
});
}
async buttonsReply(content, options) {
if (!options || !options.buttons) {
throw new Error('Please provide buttons array');
}
if (!Array.isArray(options.buttons)) {
throw new Error('The buttons must be an array');
}
let buttons = [];
let styles = {
'blupurple': 1,
'grey': 2,
'green': 3,
'red': 4,
'url': 5
};
options.buttons.forEach((x, i) => {
if (!x.style) x.style = 'blupurple';
if (!Object.keys(styles).includes(x.style)) {
throw new Error(`#${i} button has invalid style, recived ${x.style}`);
}
if (!x.label) {
throw new Error(`#${i} button don't has a label`);
}
if (typeof (x.label) !== 'string') x.label = String(x.label);
if (x.style === 'url') {
if (!x.url) {
throw new Error(`If the button style is "url", you must provide url`);
}
} else {
if (!x.id) {
throw new Error(`If the button style is not "url", you must provide id`);
}
}
x.disabled = Boolean(x.disabled);
let style = styles[x.style];
let data = {
type: 2,
style: style,
label: x.label,
custom_id: x.style === 'url' ? null : x.id,
url: x.style === 'url' ? x.url : null,
disabled: x.disabled || false
}
buttons.push(data);
})
options.buttons = buttons;
let { data, files } = replyAPIMessage.create(this, content, options, { replyTo: this }).resolveData();
this.client.api.channels[this.channel.id].messages.post({
headers: {
"Content-Type": 'applications/json'
},
data,
files
});
}
return {
MessageButton: MessageButton
};
}
Structures.extend("Message", () => Message);
module.exports = (client) => {
client.ws.on('INTERACTION_CREATE', async (data) => {
let typeStyles = {
1: 'blupurple',
2: 'grey',
3: 'green',
4: 'red',
5: 'url'
};
await client.channels.cache.get(data.channel_id).messages.fetch();
let message = client.channels.cache.get(data.channel_id).messages.cache.get(data.message.id);
let clicker = client.users.fetch(data.member.user.id) || client.members.cache.get(data.member.user.id);
await client.api.interactions(data.id, data.token).callback.post({ data: { type: 6 } });
client.emit('clickButton', {
version: data.version,
type: data.type,
style: typeStyles[data.type],
token: data.token,
id: data.data.custom_id,
discordId: data.id,
applicationId: data.application_id,
clicker: clicker,
message
})
});
}
module.exports.MessageButton = MessageButton;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc