【问题标题】:Discord.js bot replies/responds a message when mentionedDiscord.js 机器人在提及时回复/响应消息
【发布时间】:2022-01-24 09:13:04
【问题描述】:

当有人提到它时,我正试图做出一个机器人回复。我为此使用 TypeScript,我很困惑。我搜索了多个解决方案,但我认为它适用于 js,因为它总是一个错误并且某些代码不存在。 这是我最初尝试的代码

    const client = new DiscordJS.Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES,
    ]
})

client.on('ready',()=>{
    console.log('bot is ready')
})
client.on('messageCreate',(message)=>{
    if(message.content === '@bot-test'){
        message.reply({
            content: 'I heard you'
        })
    }
})

它不适用于提及,但适用于消息。

【问题讨论】:

    标签: typescript discord discord.js


    【解决方案1】:

    原因与提及时的外观有关

    当您看到提及时,它会显示为 @username,但不和谐机器人会将其视为:<@!userid>

    例如@abisammy实际上是<@!468128787884670986>

    要解决此问题,请更改 if 语句

    if (message.content === "@bot-test"){
    

    到:

    if(message.content === `<@!${client.user.id}>`){
    

    这应该可以工作

    请注意,使用 typescript 时,您必须将 client.user 添加到 if 语句中

    仅限打字稿

    if(client.user && message.content === `<@!${client.user.id}>`){
    

    【讨论】:

      猜你喜欢
      • 2020-12-28
      • 2021-07-02
      • 2021-06-26
      • 2022-06-10
      • 2019-12-16
      • 2018-06-05
      • 2021-02-01
      • 1970-01-01
      • 2019-10-29
      相关资源
      最近更新 更多