使用正则表达式在频道中获取最近发布的表情符号信息的最佳方法是什么?



我的目标是能够获取频道中最近发布的表情符号,并将其信息发布到频道中,而不包含表情符号信息中的<::>

这是我迄今为止的代码:

"yoink": {
description: "Yoink an emoji",
usage: "`$yoink`",
category: "images",
process: async function(msg, parameters) {
let a1 = await msg.channel.messages.fetch()
let a2 = a1.filter(m => msg.content.includes('<'))
let emoji = a2.first()
if (a2) {
msg.channel.send(`${emoji}`)
}
}
},

这是有效的,但它只是显示表情符号和命令语法,这实际上创建了一个无休止的命令循环,哈哈。

现在,我没有使用RegEx,但我正在努力学习它的功能。

最好的方法是什么?

您可以使用RegEx捕获表情符号ID,然后通过client获取该表情符号以显示其信息。以下是代码片段中的一个示例:

// example message
const message = '<:BBwave:562730391362994178> <:MarioWave:725159909758337055> Welcome to the server!'
const [lastEmoji, ...others] = message.match(/<a?:.+:(d{18})>/).reverse();
console.log(lastEmoji);
// const emote = message.client.emojis.cache.get(emojiID);
// console.log(emote.name, emote.id)


在我的String.prototype.match()函数中,我使用了这个RexEx:

/<a?:.+:(d{18})>/
a? - there is only an 'a' in the emoji if it is animated. question mark means optional
.+ - the emojis name. '.' means any character, and '+' means one or more
d{18} - the emojis id. 'd' means any standard digit, and {18} means 18 of them in a row
(d{18}) - by putting the id in parentheses, I can capture it
<::> - every other character is literal

最新更新