【发布时间】:2020-11-16 07:07:45
【问题描述】:
【问题讨论】:
-
查看消息反应收集器和事件'collect',可以将数组拼接成2lvl数组,并在反应句柄上编辑消息。
标签: arrays pagination discord discord.js
【问题讨论】:
标签: arrays pagination discord discord.js
我已经为此创建了一个库。您可以按原样使用该库,也可以查看其源代码并自行实现该功能。
注意:该库依赖于 typescript,因此如果您想按原样使用它,则必须使用 typescript。
https://github.com/Olian04/discord-dynamic-messages
$ npm i discord-dynamic-messages
import { MessageEmbed } from 'discord.js';
import { DynamicMessage, OnReaction } from '../src/api';
const first = () => new MessageEmbed()
.setAuthor('TOTO', 'https://i.imgur.com/ezC66kZ.png')
.setColor('#AAA')
.setTitle('First')
.setDescription('First');
const second = () => new MessageEmbed()
.setAuthor('TOTO', 'https://i.imgur.com/ezC66kZ.png')
.setColor('#548')
.setTitle('Second')
.setDescription('Second');
const third = () => new MessageEmbed()
.setAuthor('TOTO', 'https://i.imgur.com/ezC66kZ.png')
.setColor('#35D')
.setTitle('Third')
.setDescription('Third');
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
export class MessageEmbedMessage extends DynamicMessage {
private embeds = [first, second, third];
private embedIndex = 0;
@OnReaction(':arrow_left:')
public previousEmbed() {
this.embedIndex = clamp(this.embedIndex - 1, 0, this.embeds.length - 1);
}
@OnReaction(':arrow_right:')
public nextEmbed() {
this.embedIndex = clamp(this.embedIndex + 1, 0, this.embeds.length - 1);
}
public render() {
return this.embeds[this.embedIndex]()
.setTimestamp()
.setFooter(`Page ${this.embedIndex + 1}`);
}
}
【讨论】: