【问题标题】:What's the best method of grabbing info on the most recently posted emoji in a channel using RegEx?使用 RegEx 在频道中获取最新发布的表情符号信息的最佳方法是什么?
【发布时间】:2020-09-25 02:04:29
【问题描述】:

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

这是我目前的代码:

  "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,但我正在尝试了解它的功能。

最好的方法是什么?

【问题讨论】:

  • 可以像messages.map(MSG_TO_EMOJI_WITH_PATTERN).filter(IS_EMOJI_MSG)一样处理得到表情符号列表

标签: javascript node.js discord discord.js


【解决方案1】:

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

// 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

【讨论】:

  • 再次感谢您的帮助和小教程。我绝对需要它!
猜你喜欢
  • 2021-01-09
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
相关资源
最近更新 更多