当某人有太多角色无法嵌入嵌入时,如何发送“无法显示的角色太多”消息?
Posted
技术标签:
【中文标题】当某人有太多角色无法嵌入嵌入时,如何发送“无法显示的角色太多”消息?【英文标题】:How do I send a "too many roles to show" message when someone has too many roles to fit in an embed? 【发布时间】:2021-10-08 17:27:06 【问题描述】:这是我的whois
命令:
const Discord = require("discord.js")
const moment = require('moment');
const status =
online: "Online",
idle: "Idle",
dnd: "Do Not Disturb",
offline: "Offline/Invisible"
;
module.exports =
config:
name: "whois",
description: "userinfo",
usage: "m/whois <mention a member/member id>",
aliases: ['ui', 'ifno']
,
run: async (bot, message, args) =>
var permissions = [];
var acknowledgements = 'None';
let whoisPermErr = new Discord.MessageEmbed()
.setTitle("**User Permission Error!**")
.setDescription("**Sorry, you don't have permissions to use this! ❌**")
const member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;
if(member.hasPermission("KICK_MEMBERS"))
permissions.push("Kick Members");
if(member.hasPermission("BAN_MEMBERS"))
permissions.push("Ban Members");
if(member.hasPermission("ADMINISTRATOR"))
permissions.push("Administrator");
if(member.hasPermission("MANAGE_MESSAGES"))
permissions.push("Manage Messages");
if(member.hasPermission("MANAGE_CHANNELS"))
permissions.push("Manage Channels");
if(member.hasPermission("MENTION_EVERYONE"))
permissions.push("Mention Everyone");
if(member.hasPermission("MANAGE_NICKNAMES"))
permissions.push("Manage Nicknames");
if(member.hasPermission("MANAGE_ROLES"))
permissions.push("Manage Roles");
if(member.hasPermission("MANAGE_WEBHOOKS"))
permissions.push("Manage Webhooks");
if(member.hasPermission("MANAGE_EMOJIS"))
permissions.push("Manage Emojis");
if(permissions.length == 0)
permissions.push("No Key Permissions Found");
if(member.user.id == message.guild.ownerID)
acknowledgements = 'Server Owner';
const embed = new Discord.MessageEmbed()
.setDescription(`<@$member.user.id>`)
.setAuthor(`$member.user.tag`, member.user.displayAvatarURL())
.setColor('#2F3136')
.setFooter(`ID: $message.author.id`)
.setThumbnail(member.user.displayAvatarURL())
.setTimestamp()
.addField('Joined at: ',`$moment(member.joinedAt).format("dddd, MMMM Do YYYY, HH:mm:ss")`)
.addField('Created On', member.user.createdAt.toLocaleString())
.addField(`Roles [$member.roles.cache.filter(r => r.id !== message.guild.id).map(roles => `\`$roles.name\``).length]`,`$member.roles.cache.filter(r => r.id !== message.guild.id).map(roles => `<@&$roles.id >`).join(" **|** ") || "No Roles"`)
.addField("\nAcknowledgements: ", `$acknowledgements`)
.addField("\nPermissions: ", `$permissions.join(` | `)`);
message.channel.send(embed);
确实有效,但是当提到的成员角色过多时,会出现错误,因为长度太长,问题是角色过多。
如果提到的用户的角色太多,我该如何做到这一点,它会出现“角色太多”的消息?
我正在使用 Discord.js v12。
【问题讨论】:
【参考方案1】:discord embed 只能容纳一定数量的字符(如this question's answer 中所述)。您可以在构建嵌入之前尝试使用所有角色创建字符串,然后测试字符数。如果数量过多,您可以返回“角色过多”消息。
我之前提到的答案的作者有这个解决方案:
您可能希望在发送嵌入之前进行一些检查,然后通过将它们拆分为多个嵌入消息或使用基于反应的页面来处理超大的嵌入
【讨论】:
【参考方案2】:使用 try/catch 语句
//embed is instance of MessageEmbed with description as the member's roles
try
await message.channel.send(embed)
catch(err)
embed.setDescription('Too many roles');
message.channel.send(embed)
或使用.catch
message.channel.send(embed).catch(() =>
embed.setDescription('Too many roles');
message.channel.send(embed)
)
【讨论】:
以上是关于当某人有太多角色无法嵌入嵌入时,如何发送“无法显示的角色太多”消息?的主要内容,如果未能解决你的问题,请参考以下文章