Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

javascript - Discord.js bot responds when mentioned

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!");
    };
});

The message event has been renamed to messageCreate in Discord.JS v13. Using message will still work, but you'll receive a deprecation warning until you switch over.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...