diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-03-12 16:45:21 -0500 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-03-12 16:45:21 -0500 |
commit | 9e8e5aee80159730ec1d631acb5c3a1e581f1d69 (patch) | |
tree | 81776b91bd4bc66ea8c046fd871df03bae432497 | |
parent | c4e6a313f8c34fdacc07232e12d9a44432aee472 (diff) | |
download | tanzanite-9e8e5aee80159730ec1d631acb5c3a1e581f1d69.tar.gz tanzanite-9e8e5aee80159730ec1d631acb5c3a1e581f1d69.tar.bz2 tanzanite-9e8e5aee80159730ec1d631acb5c3a1e581f1d69.zip |
refactor: create and use util.overflowEmbed
-rw-r--r-- | src/commands/moderation/massBan.ts | 37 | ||||
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 38 | ||||
-rw-r--r-- | src/listeners/member-custom/massBan.ts | 36 |
3 files changed, 70 insertions, 41 deletions
diff --git a/src/commands/moderation/massBan.ts b/src/commands/moderation/massBan.ts index 529063d..faea94b 100644 --- a/src/commands/moderation/massBan.ts +++ b/src/commands/moderation/massBan.ts @@ -8,7 +8,7 @@ import { type OptionalArgType } from '#lib'; import assert from 'assert'; -import { ApplicationCommandOptionType, Collection, Embed, PermissionFlagsBits } from 'discord.js'; +import { ApplicationCommandOptionType, Collection, PermissionFlagsBits } from 'discord.js'; export default class MassBanCommand extends BushCommand { public constructor() { @@ -94,28 +94,21 @@ export default class MassBanCommand extends BushCommand { const success = (res: BanResponse): boolean => [banResponse.SUCCESS, banResponse.DM_ERROR].includes(res as any); - const embeds: Embed[] = []; - - for (let i = 0; i < res.length; i++) { - const embed = () => { - embeds.push(new Embed().setColor(util.colors.DarkRed)); - return embeds.at(-1)!; - }; - - const row = `${success(res[i]) ? util.emojis.success : util.emojis.error} ${ids[i]}${ - success(res[i]) ? '' : ` - ${res[i]}` - }`; - - let currentEmbed = embeds.length ? embeds.at(-1)! : embed(); - - const joinedRows = currentEmbed.description ? `${currentEmbed.description}\n${row}` : row; - if (joinedRows.length >= 2096) currentEmbed = embed(); - currentEmbed.setDescription(joinedRows); - } - - assert(embeds.length >= 1); + const lines = res.map((_, i) => { + const id = ids[i]; + const status = res[i]; + const isSuccess = success(status); + const emoji = isSuccess ? util.emojis.success : util.emojis.error; + return `${emoji} ${id}${isSuccess ? '' : ` - ${status}`}`; + }); - embeds[0].setTitle(`Mass Ban Results`); + const embeds = util.overflowEmbed( + { + color: util.colors.DarkRed, + title: 'Mass Ban Results' + }, + lines + ); return message.util.send({ embeds }); } diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index 51daf70..04014ef 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -22,9 +22,10 @@ import assert from 'assert'; import { exec } from 'child_process'; import deepLock from 'deep-lock'; import { ClientUtil, Util as AkairoUtil } from 'discord-akairo'; -import { APIMessage, OAuth2Scopes } from 'discord-api-types/v9'; +import { APIEmbed, APIMessage, OAuth2Scopes } from 'discord-api-types/v9'; import { Constants as DiscordConstants, + Embed, GuildMember, Message, PermissionFlagsBits, @@ -962,6 +963,41 @@ export class BushClientUtil extends ClientUtil { } /** + * Overflows the description of an embed into multiple embeds. + * @param embed The options to be applied to the (first) embed. + * @param lines Each line of the description as an element in an array. + */ + public overflowEmbed(embed: Omit<APIEmbed, 'description'>, lines: string[], maxLength = 4096): Embed[] { + const embeds: Embed[] = []; + + const makeEmbed = () => { + embeds.push(new Embed().setColor(embed.color ?? null)); + return embeds.at(-1)!; + }; + + for (const line of lines) { + let current = embeds.length ? embeds.at(-1)! : makeEmbed(); + const joined = current.description ? `${current.description}\n${line}` : line; + if (joined.length >= maxLength) current = makeEmbed(); + + current.setDescription(joined); + } + + if (!embeds.length) makeEmbed(); + + if (embed.author) embeds.at(0)?.setAuthor(embed.author); + if (embed.title) embeds.at(0)?.setTitle(embed.title); + if (embed.url) embeds.at(0)?.setURL(embed.url); + if (embed.fields) embeds.at(-1)?.setFields(...embed.fields); + if (embed.thumbnail) embeds.at(-1)?.setThumbnail(embed.thumbnail.url); + if (embed.footer) embeds.at(-1)?.setFooter(embed.footer); + if (embed.image) embeds.at(-1)?.setImage(embed.image.url); + if (embed.timestamp) embeds.at(-1)?.setTimestamp(new Date(embed.timestamp)); + + return embeds; + } + + /** * A wrapper for the Argument class that adds custom typings. */ public get arg() { diff --git a/src/listeners/member-custom/massBan.ts b/src/listeners/member-custom/massBan.ts index 93089a3..693c09c 100644 --- a/src/listeners/member-custom/massBan.ts +++ b/src/listeners/member-custom/massBan.ts @@ -1,5 +1,4 @@ import { BanResponse, banResponse, BushListener, type BushClientEvents } from '#lib'; -import { Embed } from 'discord.js'; export default class BushBanListener extends BushListener { public constructor() { @@ -16,23 +15,24 @@ export default class BushBanListener extends BushListener { const success = (res: BanResponse): boolean => [banResponse.SUCCESS, banResponse.DM_ERROR].includes(res as any); - const logEmbed = new Embed() - .setColor(util.colors.DarkRed) - .setTimestamp() - .setTitle('Mass Ban') - .addFields( - { name: '**Moderator**', value: `${moderator} (${moderator.user.tag})` }, - { name: '**Reason**', value: `${reason ? reason : '[No Reason Provided]'}` } - ) - .setDescription( - results - .map( - (reason, user) => - `${success(reason) ? util.emojis.success : util.emojis.error} ${user}${success(reason) ? '' : ` - ${reason}`}` - ) - .join('\n') - ); + const lines = results.map( + (reason, user) => + `${success(reason) ? util.emojis.success : util.emojis.error} ${user}${success(reason) ? '' : ` - ${reason}`}` + ); - return await logChannel.send({ embeds: [logEmbed] }); + const embeds = util.overflowEmbed( + { + color: util.colors.DarkRed, + title: 'Mass Ban', + timestamp: new Date().toISOString(), + fields: [ + { name: '**Moderator**', value: `${moderator} (${moderator.user.tag})` }, + { name: '**Reason**', value: `${reason ? reason : '[No Reason Provided]'}` } + ] + }, + lines + ); + + return await logChannel.send({ embeds }); } } |