【问题标题】:Saving a discord emoji in the database DISCORD:JS在数据库 DISCORD:JS 中保存不和谐的表情符号
【发布时间】:2020-11-27 07:28:22
【问题描述】:

我正在制作一个反应机器人,我目前正在制作添加表情符号命令。该命令需要消息 ID、它将赋予用户的角色以及将要显示的表情符号。但我不知道如何将表情符号变成一种 id?我不知道如何将角色的提及变成 id,谁能帮我解决这个问题?

let ARGmsgId = args[0];
let ARGemoji = args[1]; // USER IS GOING TO ENTER IN AN EMOJI - I WANT TO TURN IT INTO A SORT OF EMOJI ID THAT I CAN STORE IN THE DATABASE
let ARGrole = args[2]; //USER IS GOING TO MENTION A ROLE - I WANT TO TURN IT INTO A ROLE ID THAT I CAN STORE IN THE DATABASE

这些是我目前的论点

【问题讨论】:

  • 不是表情符号是 discord.js 检索为字符串吗? (:example:) 在同一个服务器上不能有两个同名的表情符号。为什么不使用属性来存储表情符号名称?此外,当您想让它们恢复为 emoji 格式时,discord 会自动将字符串序列化回 GUI 中的 emoji
  • 你能举个例子吗?我很迷茫啊@avivgood2

标签: javascript node.js discord.js


【解决方案1】:

自定义表情符号将以<a:name:id> 的格式解析(如果未定义,则使用a)。 Unicode 表情符号将被解析为:name:。无论哪种方式,您都可以使用String#match() 提取ID。

const emoji = args[1].match(/<a?:(.+):\d{17,18}>|:(.+):/)[1];

// regex breakdown:

// a? - question mark after any character means optional
// () - this will capture the name for later recognition
// .+ - one or more of any character (the name)
// \d{17,18} - the 17-18 digit ID
// | - or operator- either use the format for the custom emoji or the unicode

要获得提及的角色,请使用 MessageMentions#roles 和 Collection#first

const role = message.mentions.roles.first();

【讨论】:

猜你喜欢
  • 2016-08-13
  • 2019-12-08
  • 1970-01-01
  • 2021-01-09
  • 2020-04-30
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
相关资源
最近更新 更多