catto.js 0.1.3 → 0.1.5

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/Application.js CHANGED
@@ -58,7 +58,7 @@ module.exports = class {
58
58
  var p = this.options.flags;
59
59
  var f = [];
60
60
  while (--i > -1) {
61
- if (![22, 21, 20, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0].includes(i) && p >= (1 << i)) {
61
+ if (p >= (1 << i)) {
62
62
  p -= (1 << i);
63
63
  f.push(i);
64
64
  }
@@ -89,7 +89,7 @@ module.exports = class {
89
89
  null,
90
90
  "APPLICATION_COMMAND_BADGE"
91
91
  ];
92
- return f.map(n => fl[n]);
92
+ return f.map(n => fl[n]).filter(n => n);
93
93
  }
94
94
  get tags() {
95
95
  return this.options.tags;
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()) {
@@ -121,15 +123,17 @@ module.exports = class extends EventEmitter {
121
123
  });
122
124
  this.client.on("interactionCreate", interaction => {
123
125
  if (interaction.isChatInputCommand()) {
124
- interaction.user = new User(interaction.user);
126
+ interaction.user = new User(interaction.user, this);
125
127
  if (interaction.member) {
126
- interaction.member.user = new User(interaction.member.user);
128
+ interaction.member.user = new User(interaction.member.user, this);
127
129
  }
128
130
  var command = this.slashCommands.get(interaction.commandName);
129
131
  if (command) {
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,11 +143,31 @@ 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
- message.author = new User(message.author);
168
+ message.author = new User(message.author, this);
145
169
  if (message.member) {
146
- message.member.user = new User(message.member.user);
170
+ message.member.user = new User(message.member.user, this);
147
171
  }
148
172
  var args = message.content.split(" ");
149
173
  var cmd = args.shift();
@@ -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.includes(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/User.js CHANGED
@@ -1,9 +1,8 @@
1
1
  var request = require("./request");
2
2
  var Application = require("./Application");
3
3
  module.exports = class {
4
- constructor(options) {
4
+ constructor(options, bot) {
5
5
  this.options = Object.assign({
6
- "client": null,
7
6
  "id": "",
8
7
  "username": "",
9
8
  "avatar": "",
@@ -21,8 +20,14 @@ module.exports = class {
21
20
  "bot": !1,
22
21
  "system": !1
23
22
  }, options || {});
23
+ this.bot = bot;
24
24
  if (this.isBot) {
25
- request.get(`https://discord.com/api/v${(this.options.client ? this.options.client.rest.version.toString() : "10")}/oauth2/applications/${this.id}/rpc`).then(application => {
25
+ request.get({
26
+ "url": `https://discord.com/api/v${(this.bot ? (this.bot.client.rest.version ? this.bot.client.rest.version.toString() : "10") : "10")}/oauth2/applications/${this.id}/rpc`,
27
+ "headers": {
28
+ "Authorization": `Bot ${this.bot.client.token}`
29
+ }
30
+ }).then(application => {
26
31
  this.application = new Application(application.body);
27
32
  });
28
33
  }
@@ -38,7 +43,7 @@ module.exports = class {
38
43
  }
39
44
  get avatar() {
40
45
  var avataru = this.avatarHash;
41
- if (avataru.startsWith("a_")) {
46
+ if (avataru && avataru.startsWith("a_")) {
42
47
  avataru = `https://cdn.discordapp.com/avatars/${this.id}/${avataru}.gif?size=4096`;
43
48
  } else if (avataru) {
44
49
  avataru = `https://cdn.discordapp.com/avatars/${this.id}/${avataru}.webp?size=4096`;
@@ -98,7 +103,7 @@ module.exports = class {
98
103
  }
99
104
  get banner() {
100
105
  var banneru = this.bannerHash;
101
- if (banneru.startsWith("a_")) {
106
+ if (banneru && banneru.startsWith("a_")) {
102
107
  banneru = `https://cdn.discordapp.com/banners/${this.id}/${banneru}.gif?size=4096`;
103
108
  } else if (banneru) {
104
109
  banneru = `https://cdn.discordapp.com/banners/${this.id}/${banneru}.webp?size=4096`;
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.3",
3
+ "version": "0.1.5",
4
4
  "description": "Universal module for everything.",
5
5
  "main": "index.js",
6
6
  "scripts": {