【问题标题】:How do I make a Discord bot react to the last message of someone I tagged?如何让 Discord 机器人对我标记的人的最后一条消息做出反应?
【发布时间】:2020-11-28 02:30:36
【问题描述】:

我正在尝试制作一个 js Discord 机器人,它可以对我在服务器中提到的人的最后一条消息做出自定义反应。过去一天我只尝试过使用javascript,所以我为菜鸟道歉。我尝试寻找类似的解决方案并应用逻辑无济于事。就是这个思路

Example: 
> User1: I just ran a marathon!
> User2: !goodjob @User1
> (Bot reacts to 'I just ran a marathon' with multiple reactions like :happy: :1thumbsup: and/or a custom react)

这是我到目前为止所拥有的,但这只会使机器人对启动命令的人做出反应(没有我现在在示例中以 # 开头的 3 行)

if (cmdName === 'goodjob') {
  let mention = message.mentions.users.first();
  #function getUserFromMention(mention){
  #const matches = mention.match(/^<@!?(\d+)>$/);
  #const id = matches[1];
  message.react('<:emoji:ID>');
  message.react('<:emoji:ID>');
  message.react('<:emoji:ID>');
}
})
});

我不是很熟练,所以我可能在某个地方搞砸了逻辑。如果解决方案在其他地方,我很抱歉!感谢您提供任何和所有指导!

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    所以,为了确保用户可以写入其他频道而不破坏它,您不能使用 lastMessageID 属性。所以你必须得到最后N条消息并检查作者,反向。你可以用fetchMessageManager 来做到这一点

    message.channel.messages.fetch({ limit: 100 }).then(messages => {
        let mention = message.mentions.users.first();
        const lastUserMessages = messages.filter(v =>  v.author.id === mention.id);
        if (!lastUserMessages) {
            message.reply('This user didn\'t wrote something the last 100 message');
            return;
        }
        const lastUserMessage = lastUserMessages.last();
        if (!lastUserMessage) {
            message.reply('This user didn\'t wrote something the last 100 message');
            return;
        }
    
        lastUserMessage.react('<:emoji:ID>');
        
    }).catch(console.error);
    

    【讨论】:

    • 这正是我想要的,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-07-29
    • 2020-12-13
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 2020-12-14
    相关资源
    最近更新 更多