te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Discord.js bot responds when mentioned - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Discord.js bot responds when mentioned - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Sep 10, 2020 at 8:52 mkrieger1 23.2k7 gold badges63 silver badges79 bronze badges asked Sep 10, 2020 at 8:09 LalluLallu 1684 gold badges7 silver badges21 bronze badges 2
  • If you replace the <@745648345216712825> with a simple string, like hello and then you say hello on Discord, does this work? Also, did you get any error message that can help? – Thibault Husté Commented Sep 10, 2020 at 8:40
  • What exactly does "this doesn't work" mean? – mkrieger1 Commented Sep 10, 2020 at 8:52
Add a ment  | 

3 Answers 3

Reset to default 11

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("messageCreate", (message) => {
    if (message.author.bot) return false;

    if (message.content.includes("@here") || message.content.includes("@everyone") || message.type == "REPLY") 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.

discord.js just got updated you can use

client.on('message', message => {
    if (message.mentions.has(client.user)) {
        message.channel.send('your message');
    }
});

One of the best ways to check if only your bot is mentioned in the entire message is, regex. You can use this regular expression to check if only the client is mentioned:

/^<@!?${<client>.user.id}>( |)$/

You can check message by using the match method of String. In our case, String is message.content:

if (message.content.match(/^<@!?${client.user.id}>( |)$/)) {
  return message.channel.send("Thanks for mentioning me! my prefix is ...");
};
发布评论

评论列表(0)

  1. 暂无评论