【问题标题】:Dividing an array into embed pages - Discord.JS将数组划分为嵌入页面 - Discord.JS
【发布时间】:2020-11-16 07:07:45
【问题描述】:

这就是数组在数据库中的样子。 (数组对象)

我希望显示一个包含所有“名称”的列表,但如果消息“大”(达到我决定的字符限制),请 +1 页等等

【问题讨论】:

  • 查看消息反应收集器和事件'collect',可以将数组拼接成2lvl数组,并在反应句柄上编辑消息。

标签: arrays pagination discord discord.js


【解决方案1】:

我已经为此创建了一个库。您可以按原样使用该库,也可以查看其源代码并自行实现该功能。

注意:该库依赖于 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}`);
  }
}

【讨论】:

    猜你喜欢
    • 2021-07-23
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多