node.js - 将消息分成 2 个嵌入
问题描述
我有命令显示服务器的所有角色。我知道消息可以发送的字符数量有上限,所以我做了这样的设置,这样如果超过限制,它就不会显示角色。但我试图让它拆分消息并发送包含服务器角色的 2 个嵌入
const Discord = require('discord.js');
const chalk = require('chalk');
console.log(chalk.white("Roles Loaded"));
module.exports = {
name: 'roles',
description: 'List the roles of the server',
serveronly: 'Yes',
aliases: ['title', 'role'],
usage:'.role',
category: 'Info',
Info:'Role',
cooldown: 1,
execute(client, message) {
let rolemap = message.guild.roles.cache
.sort((a, b) => b.position - a.position)
.map(r => r)
.join(",");
if (rolemap.length > 1024) rolemap = "To many roles to display";
if (!rolemap) rolemap = "No roles";
const embed = new Discord.MessageEmbed()
.setTitle('Sever roles')
.setThumbnail(message.guild.iconURL({ dynamic: true }))
.addField("Role List" , rolemap)
.setColor(0x00ffff)
.setFooter(`Requested By:${message.author.username}`, message.author.displayAvatarURL({ dynamic: true }));
message.channel.send(embed );
}
}
解决方案
尝试使用 for 循环,如下所示:
let rolemap = message.guild.roles.cache
.sort((a, b) => b.position - a.position)
.map(r => r)
.join(",");
for(let i = 0; i < rolemap.length; i += 1024) {
const toSend = rolemap.substring(i, Math.min(rolemap.length, i + 2000));
const roleEmbed = new Discord.MessageEmbed()
.setTitle(`Roles`)
.setDescription(toSend)
message.channel.send(roleEmbed);
}
它使用子字符串将角色拆分为多个嵌入
推荐阅读
- vba - 在 VBA 中,为什么比较两个双精度比比较两个长
- c# - 具有多个数据库设置的 Web 应用程序 - ASP.NET MVC
- javascript - 如何将 php date_diff 值转换为 javascript 整数?
- apache-spark - spark:工作之间的长时间延迟,为什么 spark 会在驱动程序上保存表格?
- python - 超过每个分区允许的最大接收器数量 eventthub 中的错误
- excel - 对具有相同值的excel公式值(动态)进行排序
- c# - 我应该执行每个查询还是单个查询?
- python - 从我的数组中删除“数组”,然后可能将其转换为数据框
- c++ - 如何从子目录中制作 CMake 递归 OBJECT 库?
- python - 使用 Matplotlib 创建具有定义的箱和高度的对数直方图