catto.js 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Bot.js CHANGED
@@ -1,6 +1,7 @@
1
1
  var events = require("events");
2
2
  var Discord = require("discord.js");
3
3
  var User = require("./User");
4
+ var MessageBuilder = require("./MessageBuilder");
4
5
  if (typeof EventEmitter !== "undefined") {} else {
5
6
  var { EventEmitter } = events;
6
7
  }
@@ -23,8 +24,9 @@ module.exports = class extends EventEmitter {
23
24
  }
24
25
  });
25
26
  }
26
- this.slashCommands = new Map();
27
+ this.buttons = new Map();
27
28
  this.commands = new Map();
29
+ this.slashCommands = new Map();
28
30
  this.client.on("ready", () => {
29
31
  var cmds = [];
30
32
  for (var cmd of this.slashCommands.values()) {
@@ -130,6 +132,8 @@ module.exports = class extends EventEmitter {
130
132
  try {
131
133
  command.execute({
132
134
  Discord,
135
+ User,
136
+ MessageBuilder,
133
137
  interaction,
134
138
  "cmd": command.name,
135
139
  "bot": this
@@ -139,6 +143,26 @@ module.exports = class extends EventEmitter {
139
143
  }
140
144
  }
141
145
  }
146
+ if (interaction.isButton()) {
147
+ interaction.user = new User(interaction.user, this);
148
+ if (interaction.member) {
149
+ interaction.member.user = new User(interaction.member.user, this);
150
+ }
151
+ var button = this.buttons.get(interaction.customId);
152
+ if (button) {
153
+ try {
154
+ button.execute({
155
+ Discord,
156
+ User,
157
+ MessageBuilder,
158
+ interaction,
159
+ "bot": this
160
+ });
161
+ } catch(e) {
162
+ console.error(e);
163
+ }
164
+ }
165
+ }
142
166
  });
143
167
  this.client.on("messageCreate", message => {
144
168
  message.author = new User(message.author, this);
@@ -152,6 +176,8 @@ module.exports = class extends EventEmitter {
152
176
  try {
153
177
  command.execute({
154
178
  Discord,
179
+ User,
180
+ MessageBuilder,
155
181
  message,
156
182
  cmd,
157
183
  args,
@@ -0,0 +1,94 @@
1
+ var Discord = require("discord.js");
2
+ module.exports = class MessageBuilder {
3
+ constructor(bot) {
4
+ this.bot = bot;
5
+ this.data = {};
6
+ }
7
+ text(t) {
8
+ this.data.content += t;
9
+ return this;
10
+ }
11
+ link(text, link) {
12
+ this.data.content += `[${text}](${link})`;
13
+ return this;
14
+ }
15
+ button(basic, execute) {
16
+ var btn = new Discord.ButtonBuilder();
17
+ if (!basic.id && !basic.url) {
18
+ throw new Error("All buttons except URL must have ID.");
19
+ }
20
+ btn.setCustomId(basic.id);
21
+ btn.setDisabled(basic.disabled);
22
+ if (basic.emoji) {
23
+ btn.setEmoji(basic.emoji);
24
+ }
25
+ if (basic.text) {
26
+ btn.setLabel(basic.text);
27
+ }
28
+ if (!basic.emoji && !basic.text) {
29
+ throw new Error("Can't have an empty button.");
30
+ }
31
+ var colorlib = {
32
+ "blue": "Primary",
33
+ "gray": "Secondary",
34
+ "lime": "Success",
35
+ "red": "Danger"
36
+ };
37
+ basic.color = basic.color.toLowerCase();
38
+ if (!colorlib[basic.color] && !basic.url) {
39
+ throw new Error("Unknown color.");
40
+ }
41
+ if (basic.url) {
42
+ btn.setStyle(Discord.ButtonStyle.Link);
43
+ btn.setURL(basic.url);
44
+ } else {
45
+ btn.setStyle(Discord.ButtonStyle[colorlib[basic.color]]);
46
+ }
47
+ if (!this.data.components) {
48
+ this.data.components = [];
49
+ }
50
+ if (!this.data.components[0]) {
51
+ this.data.components[0] = new Discord.ActionRowBuilder();
52
+ }
53
+ if (this.data.components[0].components.length < 5) {
54
+ this.data.components[0].addComponents(btn);
55
+ } else {
56
+ if (!this.data.components[1]) {
57
+ this.data.components[1] = new Discord.ActionRowBuilder();
58
+ }
59
+ if (this.data.components[1].components.length < 5) {
60
+ this.data.components[1].addComponents(btn);
61
+ } else {
62
+ if (!this.data.components[2]) {
63
+ this.data.components[2] = new Discord.ActionRowBuilder();
64
+ }
65
+ if (this.data.components[2].components.length < 5) {
66
+ this.data.components[2].addComponents(btn);
67
+ } else {
68
+ if (!this.data.components[3]) {
69
+ this.data.components[3] = new Discord.ActionRowBuilder();
70
+ }
71
+ if (this.data.components[3].components.length < 5) {
72
+ this.data.components[3].addComponents(btn);
73
+ } else {
74
+ if (!this.data.components[4]) {
75
+ this.data.components[4] = new Discord.ActionRowBuilder();
76
+ }
77
+ if (this.data.components[4].components.length < 5) {
78
+ this.data.components[4].addComponents(btn);
79
+ } else {
80
+ throw new Error("Can't have more than 25 buttons in a single message.");
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }
86
+ if (!bot.url) {
87
+ if (bot.buttons.get(basic.id)) {
88
+ throw new Error("ID must be unique.");
89
+ }
90
+ bot.buttons.set(basic.id, execute);
91
+ }
92
+ return this;
93
+ }
94
+ };
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  var mod = {};
2
- ["random", "Server", "HTML", "request", "AuthClient", "utils", "GitHub", "Base64", "User", "Bitfield", "Flags", "Bot", "Application"].forEach(part => {
2
+ ["random", "Server", "HTML", "request", "AuthClient", "utils", "GitHub", "Base64", "User", "Bitfield", "Flags", "Bot", "Application", "MessageBuilder"].forEach(part => {
3
3
  mod[part] = require(`./${part}`);
4
4
  });
5
5
  module.exports = mod;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catto.js",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Universal module for everything.",
5
5
  "main": "index.js",
6
6
  "scripts": {