【问题标题】:Discord.js dming problem when using dm command on my disocrd serverDiscord.js 在我的不和谐服务器上使用 df 命令时添加问题
【发布时间】:2020-08-04 21:18:07
【问题描述】:

我为我的私人不和谐服务器制作了一个大规模 dm 机器人,我一直在使用它并做赠品。因此,我编写了一个机器人,它将为每个人发送消息:如果有赠品或游戏锦标赛等。每次我运行命令 +damall 时,一切都开始正常运行,但是当没有我成为朋友的用户得到来自机器人的 dm 完全停止,我收到错误消息。

现在这里是代码:

const { Command } = require('discord-akairo');
const { resolve } = require('path');
const delay = (msec) => new Promise((resolve) => setTimeout(resolve, msec));

class RecentCommand extends Command {
    constructor() {
        super('massdm',{
            aliases: ['dmall'],
            args: [
                {
                    id: 'ID'
                }
            ],
            channel: 'guild'
        });
    }

    async exec(message, client) {
        let Owner = message.author;
        if(Owner.id !== "727447049892659200") return message.reply("Only the bot owner can use this command!")
          let text = message.content.slice('+dmall'.length); // cuts off the /private part
          setTimeout(function(){
              try {
                  message.guild.members.cache.forEach(member => {
                      delay(100);
                      member.send(text)
                  })

              }catch(e) {
                  
              }
        }, 1);

        return message.channel.send(`dming ${message.guild.members.cache.size} members`)

    }
}

module.exports = RecentCommand;

这是当机器人向一个没有朋友的人发送消息时我得到的错误:

Debug Error for the bot

【问题讨论】:

  • 我相当肯定,这是 Discord 方面经过深思熟虑的设计决定 - 它可以防止网络内出现大量广告和垃圾邮件等事情。使用适当的异常处理/try/catch 块来适当地处理这些错误并从这些错误中恢复。

标签: javascript discord.js


【解决方案1】:

有些用户关闭了服务器 dms,所以你不能 dm 他们。但是,您可以捕获并忽略这些错误,以防止您的进程崩溃:

const { Command } = require('discord-akairo');
const { resolve } = require('path');
const delay = (msec) => new Promise((resolve) => setTimeout(resolve, msec));

class RecentCommand extends Command {
    constructor() {
        super('massdm',{
            aliases: ['dmall'],
            args: [
                {
                    id: 'ID'
                }
            ],
            channel: 'guild'
        });
    }

    async exec(message, client) {
        let Owner = message.author;
        if(Owner.id !== "727447049892659200") return message.reply("Only the bot owner can use this command!")
          let text = message.content.slice('+dmall'.length).split(/ +/); // cuts off the /private part
          setTimeout(async function(){
              try {
                  message.guild.members.cache.forEach(member => {
                      await delay(1_000);
                      member.send(text.join(" ")).catch(x => { console.log("Couldn't DM " + member.user.tag) })//catch the error and do NOTHING with it.
                  })

              }catch(e) {
                  
              }
        }, 1);

        return message.channel.send(`dming ${message.guild.members.cache.size} members`)

    }
}

module.exports = RecentCommand;

【讨论】:

    猜你喜欢
    • 2020-05-10
    • 2022-01-06
    • 2019-09-04
    • 2021-08-05
    • 2021-08-30
    • 2021-05-23
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多