Skip to content

Commit

Permalink
Breaking: Partial support for Discord.JS v13 (#68)
Browse files Browse the repository at this point in the history
* feat: add support for discord.js v13

* Fix(base): filter should be in awaitReactions options

Signed-off-by: gazmull <[email protected]>

* Rebuild bin

Signed-off-by: gazmull <[email protected]>

* Update bot test script

Signed-off-by: gazmull <[email protected]>

* Update package.json

Signed-off-by: gazmull <[email protected]>

Co-authored-by: gazmull <[email protected]>
  • Loading branch information
Androz2091 and gazmull authored Jul 16, 2021
1 parent 4ef3476 commit 38488a6
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 115 deletions.
88 changes: 44 additions & 44 deletions bin/Embeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,105 +4,105 @@ Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.Embeds = void 0;

const r = require("discord.js"), t = require("./base");
const t = require("discord.js"), r = require("./base");

class s extends t.PaginationEmbed {
class s extends r.PaginationEmbed {
get currentEmbed() {
return this.array[this.page - 1];
}
get pages() {
return this.array.length;
}
addField(r, t, s = !1) {
addField(t, r, s = !1) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const e of this.array) e.addField(r, t, s);
for (const e of this.array) e.addField(t, r, s);
return this;
}
addFields(...r) {
addFields(...t) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const t of this.array) t.addFields(...r);
return this;
}
attachFiles(r) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const t of this.array) t.attachFiles(r);
for (const r of this.array) r.addFields(...t);
return this;
}
async build() {
return await this._verify(), this._loadList();
}
setArray(t) {
if (!(Array.isArray(t) && Boolean(t.length))) throw new TypeError("Cannot invoke Embeds class without a valid array to paginate.");
for (const [s, e] of t.entries()) {
setArray(r) {
if (!(Array.isArray(r) && Boolean(r.length))) throw new TypeError("Cannot invoke Embeds class without a valid array to paginate.");
for (const [s, e] of r.entries()) {
if (!Boolean(e) || e.constructor !== Object || !Object.keys(e).length) {
if (e instanceof r.MessageEmbed) continue;
if (e instanceof t.MessageEmbed) continue;
throw new TypeError(`(MessageEmbeds[${s}]) Cannot invoke Embeds class with an invalid MessageEmbed instance.`);
}
t[s] = new r.MessageEmbed(e);
r[s] = new t.MessageEmbed(e);
}
return this.array = t, this;
return this.array = r, this;
}
setAuthor(r, t, s) {
setAuthor(t, r, s) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const e of this.array) e.setAuthor(r, t, s);
for (const e of this.array) e.setAuthor(t, r, s);
return this;
}
setColor(r) {
setColor(t) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const t of this.array) t.setColor(r);
for (const r of this.array) r.setColor(t);
return this;
}
setDescription(r) {
setDescription(t) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const t of this.array) t.setDescription(r);
for (const r of this.array) r.setDescription(t);
return this;
}
setFooter(r, t) {
setFooter(t, r) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const s of this.array) s.setFooter(r, t);
for (const s of this.array) s.setFooter(t, r);
return this;
}
setImage(r) {
setImage(t) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const t of this.array) t.setImage(r);
for (const r of this.array) r.setImage(t);
return this;
}
setThumbnail(r) {
setThumbnail(t) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const t of this.array) t.setThumbnail(r);
for (const r of this.array) r.setThumbnail(t);
return this;
}
setTimestamp(r) {
setTimestamp(t) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const t of this.array) t.setTimestamp(r);
for (const r of this.array) r.setTimestamp(t);
return this;
}
setTitle(r) {
setTitle(t) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const t of this.array) t.setTitle(r);
for (const r of this.array) r.setTitle(t);
return this;
}
setURL(r) {
setURL(t) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const t of this.array) t.setURL(r);
for (const r of this.array) r.setURL(t);
return this;
}
spliceFields(r, t, s, e, i) {
spliceFields(t, r, s, e, i) {
if (!this.array) throw new TypeError("this.array must be set first.");
for (const a of this.array) a.spliceFields(r, t, s, e, i);
for (const a of this.array) a.spliceFields(t, r, [ {
name: s,
value: e,
inline: i
} ]);
return this;
}
toJSON() {
if (!this.array) throw new TypeError("this.array must be set first.");
return this.array.map((r => r.toJSON()));
return this.array.map((t => t.toJSON()));
}
async _loadList(t = !0) {
async _loadList(r = !0) {
this.listenerCount("pageUpdate") && this.emit("pageUpdate");
const s = new r.MessageEmbed(this.currentEmbed), e = "footer" === this.usePageIndicator, i = this.usePageIndicator && !e ? 1 === this.pages ? "" : this.pageIndicator : "", {separator: a, text: o} = this.content, h = [ `${o ? `${r.Util.resolveString(o)}${a}` : ""}${i}`, {
embed: s
} ];
return e && s.setFooter(this.pageIndicator, s.footer.iconURL), this.clientAssets.message ? await this.clientAssets.message.edit(...h) : this.clientAssets.message = await this.channel.send(...h),
super._loadList(t);
const s = new t.MessageEmbed(this.currentEmbed), e = "footer" === this.usePageIndicator, i = this.usePageIndicator && !e ? 1 === this.pages ? "" : this.pageIndicator : "", {separator: a, text: o} = this.content, n = {
embeds: [ s ],
content: `${o ? `${o}${a}` : ""}${i}` || null
};
return e && s.setFooter(this.pageIndicator, s.footer.iconURL), this.clientAssets.message ? await this.clientAssets.message.edit(n) : this.clientAssets.message = await this.channel.send(n),
super._loadList(r);
}
}

Expand Down
13 changes: 7 additions & 6 deletions bin/FieldsEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ class s extends t.PaginationEmbed {
for (const e of this.embed.fields) t.addField(e.name, "function" == typeof e.value ? this.elementList.map(e.value).join("\n") : e.value, e.inline);
return t;
}
async _loadList(t = !0) {
async _loadList(e = !0) {
this.listenerCount("pageUpdate") && this.emit("pageUpdate");
const s = await this._drawList(), i = "footer" === this.usePageIndicator, a = this.usePageIndicator && !i ? 1 === this.pages ? "" : this.pageIndicator : "", {separator: n, text: r} = this.content, o = [ `${r ? `${e.Util.resolveString(r)}${n}` : ""}${a}`, {
embed: s
} ];
return i && s.setFooter(this.pageIndicator, s.footer.iconURL), this.clientAssets.message ? await this.clientAssets.message.edit(...o) : this.clientAssets.message = await this.channel.send(...o),
super._loadList(t);
const t = await this._drawList(), s = "footer" === this.usePageIndicator, i = this.usePageIndicator && !s ? 1 === this.pages ? "" : this.pageIndicator : "", {separator: n, text: a} = this.content, r = {
embeds: [ t ],
content: `${a ? `${a}${n}` : ""}${i}` || null
};
return s && t.setFooter(this.pageIndicator, t.footer.iconURL), this.clientAssets.message ? await this.clientAssets.message.edit(r) : this.clientAssets.message = await this.channel.send(r),
super._loadList(e);
}
}

Expand Down
6 changes: 4 additions & 2 deletions bin/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ class e extends t.EventEmitter {
return this.authorizedUsers.length ? this.authorizedUsers.includes(i.id) && s : !i.bot && s;
}, s = this.clientAssets.message;
try {
const t = (await s.awaitReactions(i, {
const t = (await s.awaitReactions({
filter: i,
max: 1,
time: this.timeout,
errors: [ "time" ]
Expand Down Expand Up @@ -220,7 +221,8 @@ class e extends t.EventEmitter {
return i.author.id === t.id && (!isNaN(Number(i.content)) && s !== this.page && s >= 1 && s <= this.pages || e.includes(i.content.toLowerCase()));
}, s = this.clientAssets.message.channel, n = await s.send(this.clientAssets.prompt.replace(/\{\{user\}\}/g, t.toString()));
try {
const t = (await s.awaitMessages(i, {
const t = (await s.awaitMessages({
filter: i,
max: 1,
time: this.timeout,
errors: [ "time" ]
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "discord-paginationembed",
"version": "2.1.0",
"version": "3.0.0-dev.0",
"description": "A pagination utility for MessageEmbed in Discord.JS",
"main": "./bin/index.js",
"types": "./typings",
"engines": {
"node": ">= 12.0.0"
"node": ">= 14.0.0"
},
"scripts": {
"test": "gulp",
Expand Down Expand Up @@ -40,7 +40,7 @@
},
"homepage": "https://github.com/gazmull/discord-paginationembed#readme",
"peerDependencies": {
"discord.js": "^12.0.0"
"discord.js": "^13.0.0-dev.4206e35.1626393827"
},
"devDependencies": {
"@types/node": "^14.14.35",
Expand Down
43 changes: 12 additions & 31 deletions src/Embeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import {
ColorResolvable,
EmbedField,
EmbedFieldData,
FileOptions,
Message,
MessageAttachment,
MessageEmbed,
StringResolvable,
Util
MessageEmbed
} from 'discord.js';
import { PaginationEmbed } from './base';

Expand Down Expand Up @@ -70,7 +66,7 @@ export class Embeds extends PaginationEmbed<MessageEmbed> {
* @param value - The value of the field.
* @param inline - Whether the field is inline to the other fields.
*/
public addField (name: string, value: StringResolvable, inline = false) {
public addField (name: string, value: string, inline = false) {
if (!this.array) throw new TypeError('this.array must be set first.');

for (const el of this.array)
Expand All @@ -88,19 +84,6 @@ export class Embeds extends PaginationEmbed<MessageEmbed> {
return this;
}

/**
* Files to attach to all embeds.
* @param files - Files to attach.
*/
public attachFiles (files: (FileOptions|string|MessageAttachment)[]) {
if (!this.array) throw new TypeError('this.array must be set first.');

for (const el of this.array)
el.attachFiles(files);

return this;
}

/**
* Build the Pagination Embeds.
*
Expand Down Expand Up @@ -204,7 +187,7 @@ export class Embeds extends PaginationEmbed<MessageEmbed> {
* Sets the description of all embeds.
* @param description - The description of all embeds.
*/
public setDescription (description: StringResolvable) {
public setDescription (description: string) {
if (!this.array) throw new TypeError('this.array must be set first.');

for (const el of this.array)
Expand All @@ -218,7 +201,7 @@ export class Embeds extends PaginationEmbed<MessageEmbed> {
* @param text - The footer text.
* @param iconURL - URL for the footer's icon.
*/
public setFooter (text: StringResolvable, iconURL?: string) {
public setFooter (text: string, iconURL?: string) {
if (!this.array) throw new TypeError('this.array must be set first.');

for (const el of this.array)
Expand Down Expand Up @@ -270,7 +253,7 @@ export class Embeds extends PaginationEmbed<MessageEmbed> {
* Sets the title of all embeds.
* @param title - The title of all embeds.
*/
public setTitle (title: StringResolvable) {
public setTitle (title: string) {
if (!this.array) throw new TypeError('this.array must be set first.');

for (const el of this.array)
Expand Down Expand Up @@ -303,14 +286,14 @@ export class Embeds extends PaginationEmbed<MessageEmbed> {
public spliceFields (
index: number,
deleteCount: number,
name?: StringResolvable,
value?: StringResolvable,
name?: string,
value?: string,
inline?: boolean
) {
if (!this.array) throw new TypeError('this.array must be set first.');

for (const el of this.array)
el.spliceFields(index, deleteCount, name, value, inline);
el.spliceFields(index, deleteCount, [ { name, value, inline } ]);

return this;
}
Expand All @@ -335,17 +318,15 @@ export class Embeds extends PaginationEmbed<MessageEmbed> {
: '';
const { separator, text } = this.content;
// Fixes no-argument TS error
const args: [ any, any ] = [
`${text ? `${Util.resolveString(text)}${separator}` : ''}${shouldIndicate}`,
{ embed },
];
const content = `${text ? `${text}${separator}` : ''}${shouldIndicate}`;
const options = { embeds: [ embed ], content: content || null };

if (isFooter)
embed.setFooter(this.pageIndicator, embed.footer.iconURL);
if (this.clientAssets.message)
await this.clientAssets.message.edit(...args);
await this.clientAssets.message.edit(options);
else
this.clientAssets.message = await this.channel.send(...args) as Message;
this.clientAssets.message = await this.channel.send(options) as Message;

return super._loadList(callNavigation);
}
Expand Down
12 changes: 5 additions & 7 deletions src/FieldsEmbed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, MessageEmbed, Util } from 'discord.js';
import { Message, MessageEmbed } from 'discord.js';
import { PaginationEmbed } from './base';

/**
Expand Down Expand Up @@ -155,17 +155,15 @@ export class FieldsEmbed<Element> extends PaginationEmbed<Element> {
: '';
const { separator, text } = this.content;
// Fixes no-arguemnt TS error
const args: [ any, any ] = [
`${text ? `${Util.resolveString(text)}${separator}` : ''}${shouldIndicate}`,
{ embed },
];
const content = `${text ? `${text}${separator}` : ''}${shouldIndicate}`;
const options = { embeds: [ embed ], content: content || null };

if (isFooter)
embed.setFooter(this.pageIndicator, embed.footer.iconURL);
if (this.clientAssets.message)
await this.clientAssets.message.edit(...args);
await this.clientAssets.message.edit(options);
else
this.clientAssets.message = await this.channel.send(...args) as Message;
this.clientAssets.message = await this.channel.send(options) as Message;

return super._loadList(callNavigation);
}
Expand Down
9 changes: 4 additions & 5 deletions src/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
MessageReaction,
NewsChannel,
Snowflake,
StringResolvable,
TextChannel,
User
} from 'discord.js';
Expand Down Expand Up @@ -412,7 +411,7 @@ export abstract class PaginationEmbed<Element> extends EventEmitter {
* @param text - The message content.
* @param separator - The string to separate the content from the page indicator.
*/
public setContent (text: StringResolvable, separator = '\n') {
public setContent (text: string, separator = '\n') {
if (typeof separator !== 'string')
throw new TypeError('setContent()\'s `separator` parameter only accepts string type.');

Expand Down Expand Up @@ -544,7 +543,7 @@ export abstract class PaginationEmbed<Element> extends EventEmitter {
const clientMessage = this.clientAssets.message;

try {
const responses = await clientMessage.awaitReactions(filter, { max: 1, time: this.timeout, errors: [ 'time' ] });
const responses = await clientMessage.awaitReactions({ filter, max: 1, time: this.timeout, errors: [ 'time' ] });
const response = responses.first();
const user = response.users.cache.last();
const emoji = [ response.emoji.name, response.emoji.id ];
Expand Down Expand Up @@ -654,7 +653,7 @@ export abstract class PaginationEmbed<Element> extends EventEmitter {
.send(this.clientAssets.prompt.replace(/\{\{user\}\}/g, user.toString())) as Message;

try {
const responses = await channel.awaitMessages(filter, { max: 1, time: this.timeout, errors: [ 'time' ] });
const responses = await channel.awaitMessages({ filter, max: 1, time: this.timeout, errors: [ 'time' ] });
const response = responses.first();
const content = response.content;
const missing = (channel as TextChannel).permissionsFor(channel.client.user)
Expand Down Expand Up @@ -779,7 +778,7 @@ export interface ClientAssets {
/** Options for client's message content. */
export interface ClientMessageContent {
/** The message content. */
text?: StringResolvable;
text?: string;
/**
* The string to separate the content from the page indicator.
*
Expand Down
Loading

0 comments on commit 38488a6

Please sign in to comment.