【问题标题】:How can I execute certain part of code from a reaction on a private message (not a channel message) with Discord.JS?如何使用 Discord.JS 从对私人消息(不是频道消息)的反应中执行某些代码?
【发布时间】:2020-06-19 23:25:53
【问题描述】:

我正在创建一个 Discord 机器人,用于向进入我服务器的用户发送私人欢迎消息。

我想执行一些代码行(我想为用户添加不同的角色),如果用户对这三个表情符号做出反应,这些代码行会有所不同。
我在网上找到了仅提及频道消息的指南,我不明白该使用哪种方法处理私人消息。
谢谢!

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    私人消息的处理方式应该与频道消息相同。

    // Create a reaction filter that only will collect those three emojis
    const filter = (reaction, user) => ['??', '??', '?'].includes(reaction.emoji.name)
    
    // Create reaction collector (message is the message the bot sent).
    // The time is the time in milliseconds that the collector should run for
    // (i.e. how long the user has to react).
    // Discord.js v12:
    // const collector = message.createReactionCollector(filter, {time: 15000})
    // Discord.js v13:
    const collector = message.createReactionCollector({filter, time: 15000})
    
    // Fired when the user reacts
    collector.on('collect', (reaction, user) => {
      switch (reaction.name) {
        case '??':
          message.reply('you chose Italian!')
          break
        case '??':
          message.reply('you chose English!')
          break
        case '?':
          message.reply('you have a secret code!')
      }
    })
    

    有关详细信息,请参阅the Discord.js guide (archive)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-17
      • 1970-01-01
      • 2020-01-11
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多