Discord.js bot responds when mentioned

ぐ巨炮叔叔 提交于 2021-02-01 05:14:35

问题


I'm trying to make my discord.js bot send a message when it is pinged. I was unsure how to do this so I referred to this code:

client.on('message', message => {
    if (message.content === '<@745648345216712825>') {
        message.channel.send('Message Here');
    }
});

However, this doesn't work.

Also, is it possible that my bot responds when a person mentions a specific user for example if I am mentioned by the user anywhere in a message the bot responds? If yes, can you show me how to do it?


回答1:


Message has a property called mentions, which contains all the channels, members, roles, and users mentioned in the message. You can use the method .has(data, [options]) of MessageMentions to see if your bot was mentioned.


client.on("message", message => {
    if (message.author.bot) return false;

    if (message.content.includes("@here") || message.content.includes("@everyone")) return false;

    if (message.mentions.has(client.user.id)) {
        message.channel.send("Hello there!");
    };
});


来源:https://stackoverflow.com/questions/63825452/discord-js-bot-responds-when-mentioned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!