@sapphire/discord.js-utilities
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -6,2 +6,8 @@ # Change Log | ||
# [1.3.0](https://github.com/sapphire-project/utilities/compare/@sapphire/discord.js-utilities@1.2.0...@sapphire/discord.js-utilities@1.3.0) (2021-01-20) | ||
### Features | ||
- added MessageOptions, make PM and LPM work stateless ([#62](https://github.com/sapphire-project/utilities/issues/62)) ([733eab8](https://github.com/sapphire-project/utilities/commit/733eab81e5db4aaf7e70aa48b31ae87a3370cc56)) | ||
# [1.2.0](https://github.com/sapphire-project/utilities/compare/@sapphire/discord.js-utilities@1.1.0...@sapphire/discord.js-utilities@1.2.0) (2021-01-16) | ||
@@ -8,0 +14,0 @@ |
export * from '@sapphire/discord-utilities'; | ||
export * from './lib/builders/MessageBuilder'; | ||
export * from './lib/LazyPaginatedMessage'; | ||
export * from './lib/PaginatedMessage'; | ||
export * from './lib/type-guards'; | ||
export * from './lib/PaginatedMessage'; | ||
export * from './lib/LazyPaginatedMessage'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@sapphire/discord-utilities");class PaginatedMessage{constructor({pages:e,actions:t=PaginatedMessage.defaultActions}={}){Object.defineProperty(this,"pages",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"response",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"collector",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"messages",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"actions",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"index",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"idle",{enumerable:!0,configurable:!0,writable:!0,value:2e4}),this.pages=null!=e?e:[];for(const e of this.pages)this.messages.push("function"==typeof e?null:e);for(const e of t)this.actions.set(e.id,e)}setIndex(e){return this.index=e,this}setIdle(e){return this.idle=e,this}setActions(e){return this.actions.clear(),this.addActions(e)}addActions(e){for(const t of e)this.addAction(t);return this}addAction(e){return this.actions.set(e.id,e),this}setPages(e){return this.pages=[],this.messages=[],this.addPages(e),this}addPages(e){for(const t of e)this.addPage(t);return this}addPage(e){return this.pages.push(e),this.messages.push("function"==typeof e?null:e),this}async run(e,t){var s,i;if(await this.resolvePagesOnRun(),!this.messages.length)throw new Error("There are no messages.");if(!this.actions.size)throw new Error("There are no messages.");const n=this.messages[this.index];null!==(s=this.response)&&void 0!==s||(this.response=await t.send(n));for(const e of this.actions.keys())await this.response.react(e);return null!==(i=this.collector)&&void 0!==i||(this.collector=this.response.createReactionCollector(((t,s)=>(this.actions.has(t.emoji.identifier)||this.actions.has(t.emoji.name))&&s.id===e.id),{idle:this.idle}).on("collect",this.handleCollect.bind(this,e,t)).on("end",this.handleEnd.bind(this))),this}async resolvePagesOnRun(){for(let e=0;e<this.pages.length;e++)await this.resolvePage(e)}async resolvePage(e=this.index){var t,s;return this.pages[e]&&(null!==(t=(s=this.messages)[e])&&void 0!==t||(s[e]=await this.pages[e](e,this.pages,this))),this.messages[e]}clone(){const e=new PaginatedMessage({pages:this.pages,actions:[]}).setIndex(this.index).setIdle(this.idle);return e.actions=this.actions,e.response=this.response,e.collector=this.collector,e}async handleCollect(e,t,s,i){var n,a;await s.users.remove(i);const r=null!==(n=this.actions.get(s.emoji.identifier))&&void 0!==n?n:this.actions.get(s.emoji.name),o=this.index;await r.run({handler:this,author:e,channel:t,response:this.response,collector:this.collector}),o!==this.index&&await(null===(a=this.response)||void 0===a?void 0:a.edit(await this.resolvePage()))}async handleEnd(e){this.collector&&(this.collector.removeAllListeners(),this.collector=null),this.response&&!PaginatedMessage.deletionStopReasons.includes(e)&&await this.response.reactions.removeAll()}}Object.defineProperty(PaginatedMessage,"defaultActions",{enumerable:!0,configurable:!0,writable:!0,value:[{id:"🔢",run:async({handler:e,author:t,channel:s})=>{const i=await s.send("What page would you like to jump to?"),n=await s.awaitMessages((e=>e.author.id===t.id),{max:1,idle:15e3}).catch((()=>null));if(n){const t=n.first();if(i.deletable&&await i.delete(),t){t.deletable&&await t.delete();const s=Number(t.content)-1;!isNaN(s)&&e.pages[s]&&(e.index=s)}}}},{id:"⏪",run:({handler:e})=>e.index=0},{id:"◀️",run:({handler:e})=>{0!==e.index&&--e.index}},{id:"▶️",run:({handler:e})=>{e.index!==e.pages.length-1&&++e.index}},{id:"⏩",run:({handler:e})=>e.index=e.pages.length-1},{id:"⏹️",run:async({response:e,collector:t})=>{await e.reactions.removeAll(),t.stop()}}]}),Object.defineProperty(PaginatedMessage,"deletionStopReasons",{enumerable:!0,configurable:!0,writable:!0,value:["messageDelete","channelDelete","guildDelete"]});Object.keys(e).forEach((function(t){"default"!==t&&Object.defineProperty(exports,t,{enumerable:!0,get:function(){return e[t]}})})),exports.LazyPaginatedMessage=class LazyPaginatedMessage extends PaginatedMessage{async resolvePagesOnStart(){await this.resolvePage(this.index)}async resolvePage(e=this.index){return await super.resolvePage(e-1),await super.resolvePage(e+1),super.resolvePage(e)}},exports.PaginatedMessage=PaginatedMessage,exports.isCategoryChannel=function isCategoryChannel(e){return"category"===e.type},exports.isDMChannel=function isDMChannel(e){return"dm"===e.type},exports.isGroupChannel=function isGroupChannel(e){return"group"===e.type},exports.isGuildBasedChannel=function isGuildBasedChannel(e){return"dm"!==e.type&&"group"!==e.type&&"unknown"!==e.type},exports.isNewsChannel=function isNewsChannel(e){return"news"===e.type},exports.isStoreChannel=function isStoreChannel(e){return"store"===e.type},exports.isTextChannel=function isTextChannel(e){return"text"===e.type},exports.isVoiceChannel=function isVoiceChannel(e){return"voice"===e.type}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@sapphire/discord-utilities"),t=require("discord.js");class MessageBuilder{constructor(e){var t,s,i,n,a,r,l,o;Object.defineProperty(this,"tts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"nonce",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"content",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"embed",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"allowedMentions",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"files",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"code",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"split",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.tts=null!==(t=null==e?void 0:e.tts)&&void 0!==t?t:MessageBuilder.defaults.tts,this.nonce=null!==(s=null==e?void 0:e.nonce)&&void 0!==s?s:MessageBuilder.defaults.nonce,this.content=null!==(i=null==e?void 0:e.content)&&void 0!==i?i:MessageBuilder.defaults.content,this.embed=null!==(n=null==e?void 0:e.embed)&&void 0!==n?n:MessageBuilder.defaults.embed,this.allowedMentions=null!==(a=null==e?void 0:e.allowedMentions)&&void 0!==a?a:MessageBuilder.defaults.allowedMentions,this.files=null!==(r=null==e?void 0:e.files)&&void 0!==r?r:MessageBuilder.defaults.files,this.code=null!==(l=null==e?void 0:e.code)&&void 0!==l?l:MessageBuilder.defaults.code,this.split=null!==(o=null==e?void 0:e.split)&&void 0!==o?o:MessageBuilder.defaults.split}setTTS(e){return this.tts=e,this}setNonce(e){return this.nonce=e,this}setContent(e){return this.content=e,this}setEmbed(e){return this.embed=e,this}setAllowedMentions(e){return this.allowedMentions=e,this}addFile(e){var t,s;return this.files=null!==(s=null===(t=this.files)||void 0===t?void 0:t.concat(e))&&void 0!==s?s:[e],this}setFile(e){return this.files=[e],this}setFiles(e){return this.files=e,this}setCode(e){return this.code=e,this}setSplit(e){return this.split=e,this}}Object.defineProperty(MessageBuilder,"defaults",{enumerable:!0,configurable:!0,writable:!0,value:{}});class PaginatedMessage{constructor({pages:e,actions:s=PaginatedMessage.defaultActions}={}){Object.defineProperty(this,"pages",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"response",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"collector",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"messages",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"actions",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"index",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"idle",{enumerable:!0,configurable:!0,writable:!0,value:2e4}),this.pages=null!=e?e:[];for(const e of this.pages)this.messages.push(e instanceof t.APIMessage?e:null);for(const e of s)this.actions.set(e.id,e)}setIndex(e){return this.index=e,this}setIdle(e){return this.idle=e,this}setActions(e){return this.actions.clear(),this.addActions(e)}addActions(e){for(const t of e)this.addAction(t);return this}addAction(e){return this.actions.set(e.id,e),this}setPages(e){return this.pages=[],this.messages=[],this.addPages(e),this}addPages(e){for(const t of e)this.addPage(t);return this}addPage(e){return this.pages.push(e),this.messages.push(e instanceof t.APIMessage?e:null),this}async run(e,t){var s,i;if(await this.resolvePagesOnRun(t),!this.messages.length)throw new Error("There are no messages.");if(!this.actions.size)throw new Error("There are no messages.");const n=this.messages[this.index];null!==(s=this.response)&&void 0!==s||(this.response=await t.send(n));for(const e of this.actions.keys())await this.response.react(e);return null!==(i=this.collector)&&void 0!==i||(this.collector=this.response.createReactionCollector(((t,s)=>(this.actions.has(t.emoji.identifier)||this.actions.has(t.emoji.name))&&s.id===e.id),{idle:this.idle}).on("collect",this.handleCollect.bind(this,e,t)).on("end",this.handleEnd.bind(this))),this}async resolvePagesOnRun(e){for(let t=0;t<this.pages.length;t++)await this.resolvePage(e,t)}async resolvePage(e,s){const i=this.messages[s];if(null!==i)return i;const n=this.pages[s],a="function"==typeof n?await n(s,this.pages,this):n,r=a instanceof t.APIMessage?a:await new t.APIMessage(e,a).resolve();return this.messages[s]=r,r}clone(){const e=new PaginatedMessage({pages:this.pages,actions:[]}).setIndex(this.index).setIdle(this.idle);return e.actions=this.actions,e.response=this.response,e.collector=this.collector,e}async handleCollect(e,t,s,i){var n,a;await s.users.remove(i);const r=null!==(n=this.actions.get(s.emoji.identifier))&&void 0!==n?n:this.actions.get(s.emoji.name),l=this.index;await r.run({handler:this,author:e,channel:t,response:this.response,collector:this.collector}),l!==this.index&&await(null===(a=this.response)||void 0===a?void 0:a.edit(await this.resolvePage(t,this.index)))}async handleEnd(e){this.collector&&(this.collector.removeAllListeners(),this.collector=null),this.response&&!PaginatedMessage.deletionStopReasons.includes(e)&&await this.response.reactions.removeAll()}}Object.defineProperty(PaginatedMessage,"defaultActions",{enumerable:!0,configurable:!0,writable:!0,value:[{id:"🔢",run:async({handler:e,author:t,channel:s})=>{const i=await s.send("What page would you like to jump to?"),n=await s.awaitMessages((e=>e.author.id===t.id),{max:1,idle:15e3}).catch((()=>null));if(n){const t=n.first();if(i.deletable&&await i.delete(),t){t.deletable&&await t.delete();const s=Number(t.content)-1;!isNaN(s)&&e.pages[s]&&(e.index=s)}}}},{id:"⏪",run:({handler:e})=>e.index=0},{id:"◀️",run:({handler:e})=>{0!==e.index&&--e.index}},{id:"▶️",run:({handler:e})=>{e.index!==e.pages.length-1&&++e.index}},{id:"⏩",run:({handler:e})=>e.index=e.pages.length-1},{id:"⏹️",run:async({response:e,collector:t})=>{await e.reactions.removeAll(),t.stop()}}]}),Object.defineProperty(PaginatedMessage,"deletionStopReasons",{enumerable:!0,configurable:!0,writable:!0,value:["messageDelete","channelDelete","guildDelete"]});Object.keys(e).forEach((function(t){"default"!==t&&Object.defineProperty(exports,t,{enumerable:!0,get:function(){return e[t]}})})),exports.LazyPaginatedMessage=class LazyPaginatedMessage extends PaginatedMessage{async resolvePagesOnStart(e){await this.resolvePage(e,this.index)}async resolvePage(e,t){const[,,s]=await Promise.all([super.resolvePage(e,t-1),super.resolvePage(e,t+1),super.resolvePage(e,t)]);return s}},exports.MessageBuilder=MessageBuilder,exports.PaginatedMessage=PaginatedMessage,exports.isCategoryChannel=function isCategoryChannel(e){return"category"===e.type},exports.isDMChannel=function isDMChannel(e){return"dm"===e.type},exports.isGroupChannel=function isGroupChannel(e){return"group"===e.type},exports.isGuildBasedChannel=function isGuildBasedChannel(e){return"dm"!==e.type&&"group"!==e.type&&"unknown"!==e.type},exports.isNewsChannel=function isNewsChannel(e){return"news"===e.type},exports.isStoreChannel=function isStoreChannel(e){return"store"===e.type},exports.isTextChannel=function isTextChannel(e){return"text"===e.type},exports.isVoiceChannel=function isVoiceChannel(e){return"voice"===e.type}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
/// <reference types="discord.js" /> | ||
import type { NewsChannel, TextChannel } from 'discord.js'; | ||
import { PaginatedMessage } from './PaginatedMessage'; | ||
@@ -10,3 +10,3 @@ /** | ||
*/ | ||
resolvePagesOnStart(): Promise<void>; | ||
resolvePagesOnStart(channel: TextChannel | NewsChannel): Promise<void>; | ||
/** | ||
@@ -16,4 +16,4 @@ * Resolves the page corresponding with the given index. This also resolves the index's before and after the given index. | ||
*/ | ||
resolvePage(index?: number): Promise<import("discord.js").APIMessage | null>; | ||
resolvePage(channel: TextChannel | NewsChannel, index: number): Promise<import("discord.js").APIMessage>; | ||
} | ||
//# sourceMappingURL=LazyPaginatedMessage.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import type { APIMessage, Message, MessageReaction, NewsChannel, ReactionCollector, TextChannel, User } from 'discord.js'; | ||
import { APIMessage, Message, MessageOptions, MessageReaction, NewsChannel, ReactionCollector, TextChannel, User } from 'discord.js'; | ||
/** | ||
@@ -126,3 +126,3 @@ * This is a [[PaginatedMessage]], a utility to paginate messages (usually embeds). | ||
*/ | ||
resolvePagesOnRun(): Promise<void>; | ||
resolvePagesOnRun(channel: TextChannel | NewsChannel): Promise<void>; | ||
/** | ||
@@ -132,3 +132,3 @@ * This function is executed whenever an action is triggered and resolved. | ||
*/ | ||
resolvePage(index?: number): Promise<APIMessage | null>; | ||
resolvePage(channel: TextChannel | NewsChannel, index: number): Promise<APIMessage>; | ||
/** | ||
@@ -198,12 +198,33 @@ * This clones the current handler into a new instance. | ||
* | ||
* Furthermore, {@link https://discord.js.org/#/docs/main/stable/typedef/MessageOptions MessageOptions} can be used to | ||
* construct the pages without state, this library also provides [[MessageBuilder]], which can be used as a chainable | ||
* alternative to raw objects, similar to how {@link https://discord.js.org/#/docs/main/stable/class/MessageEmbed MessageEmbed} | ||
* works. | ||
* | ||
* @example | ||
* ```typescript | ||
* // Direct usage as a MessageBuilder | ||
* new MessageBuilder().setContent('Test content!'); | ||
* ``` | ||
* | ||
* @example | ||
* ```typescript | ||
* // An awaited function. This function also passes index, pages, and handler. | ||
* (index, pages) => | ||
* new MessageBuilder().setEmbed( | ||
* new MessageEmbed().setFooter(`Page ${index + 1} / ${pages.length}`) | ||
* ); | ||
* ``` | ||
* | ||
* @example | ||
* ```typescript | ||
* // Direct usage as an APIMessage | ||
* | ||
* new APIMessage(message.channel, { | ||
* context: 'Test content!', | ||
* content: 'Test content!', | ||
* }); | ||
* ``` | ||
* | ||
* @example | ||
* ```typescript | ||
* // An awaited function. This function also passes index, pages, and handler. | ||
* | ||
* (index, pages) => | ||
@@ -215,5 +236,5 @@ * new APIMessage(message.channel, { | ||
*/ | ||
export declare type MessagePage = ((index: number, pages: MessagePage[], handler: PaginatedMessage) => Awaited<APIMessage>) | APIMessage; | ||
export declare type MessagePage = ((index: number, pages: MessagePage[], handler: PaginatedMessage) => Awaited<APIMessage | MessageOptions>) | APIMessage | MessageOptions; | ||
declare type Awaited<T> = PromiseLike<T> | T; | ||
export {}; | ||
//# sourceMappingURL=PaginatedMessage.d.ts.map |
{ | ||
"name": "@sapphire/discord.js-utilities", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Discord.js specific utilities for your JavaScript/TypeScript bots", | ||
@@ -58,3 +58,3 @@ "author": "@sapphire", | ||
}, | ||
"gitHead": "29d02276c22b3295d0225412ffdf140cec0b30fb" | ||
"gitHead": "aa7204a01d70a6ed20cb52cc46e529a9c331a5dc" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
108990
18
471