【问题标题】:Discord.Js - Uncaught DiscordAPIError: Cannot send messages to this userDiscord.Js - 未捕获的 DiscordAPIError:无法向该用户发送消息
【发布时间】:2021-02-07 12:46:00
【问题描述】:

当我尝试使用 DM 中的机器人回复用户时,我收到以下错误:Discord.Js - Uncaught DiscordAPIError: Cannot send messages to this user

有什么问题?

代码:

client.on('message', message=>{

    
    if(!message.author.send('$$')) return

    if(message.author.send('$$')){
        
        message.channel.send('Hi! Please describe your message to get help with it.').catch(error => {
            message.channel.send('I was unable to send a message.')
        })
    }

【问题讨论】:

  • 他们要么屏蔽了你,要么不接受来自非朋友的 dms
  • 我允许了这些消息,但它仍然显示此错误...

标签: javascript discord discord.js bots


【解决方案1】:

如果用户不接受来自不在其好友列表中、被阻止等帐户的 DM,则会返回此 API 错误。只要抓住该错误,一切都会按预期运行 - 这不是错误,而是Discord API 功能。

编辑:代码本身似乎也有问题,它缺少一些括号:

client.on('message', message=> {

    if(message.author.bot) return;
    if(!message.author.send('$$')) return;

    if(message.author.send('$$')){
        
        message.channel.send('Hi! Please describe your message to get help with it.').catch(error => {
            message.channel.send('I was unable to send a message.')
        })
    }
})

【讨论】:

  • 谢谢 - 它成功了。问题是 BOT 不断发送垃圾邮件('嗨,请描述您的消息以获得帮助。')
  • 这是因为代码告诉机器人在用户发送消息时发送消息,它不会忽略来自机器人的消息并创建一个循环,机器人在其中注册自己的消息并回复它。您可以通过添加“ if (message.author.bot) return; ”来防止这种情况,我将编辑原始邮政编码。
  • 感谢您抽出宝贵时间提供帮助 :)
猜你喜欢
  • 2021-11-06
  • 2020-10-01
  • 2020-06-04
  • 2021-11-02
  • 2020-09-24
  • 2021-10-15
  • 2021-12-01
  • 1970-01-01
  • 2020-04-07
相关资源
最近更新 更多