【问题标题】:Ban module, Discord bot error.. discord.js禁止模块,Discord 机器人错误.. discord.js
【发布时间】:2020-09-02 19:26:47
【问题描述】:

1 天前,我为一个不和谐的机器人发布了一个问题,说我的 id 不是 null 的属性。现在它的作品。但它仍然无法禁止,它给了我代码上标记的错误:message.reply("I was unable to ban the member :(");


这是代码:

const Discord = require('discord.js');
const client = new Discord.Client();



module.exports = {
   name: 'ban',
   description: "ban peoples ;D",
   execute(message, args, client) {
       if (!message.member.hasPermission("BAN_MEMBERS") ||
           !message.member.hasPermission("ADMINISTRATOR")) return message.channel.send("You don't have a permissions to do this, maybe later ;) ");

       const user = message.mentions.users.first();
       const member = message.guild.member(user);
       

       if (!user) return message.channel.send("Please mention the user to make this action");

       if (user.id === message.author.id) return message.channel.send("You can't ban yourself, I tried :(");


       member.ban(() => {
           message.channel.send('Successfully banned **${user.tag}**');
       }).catch(err => {
           message.reply("I was unable to ban the member :(");
       })
   }
}

我检查了机器人是否需要权限,我给了他,但它仍然无法正常工作。

【问题讨论】:

  • 在您的 catch 块中,写上console.log(err) 以更好地诊断问题。
  • at ``` }).catch(err => {``` ?
  • 是的。 catch(err => { console.log(err)

标签: javascript visual-studio discord.js


【解决方案1】:

问题来了

member.ban(() => {
    message.channel.send('Successfully banned **${user.tag}**');
}).catch(err => {
    message.reply("I was unable to ban the member :(");
})

您将整个函数传递给 ban 方法

你真的应该调用函数然后使用.then(() => {}) 来处理承诺

应该是这样的

member.ban()
    .then(() => {
        message.channel.send('Successfully banned **${user.tag}**');
    })
    .catch((err) => {
        message.reply("I was unable to ban the member :(");
        console.error(err);
    });

【讨论】:

    猜你喜欢
    • 2021-07-01
    • 2021-09-04
    • 2021-08-09
    • 2021-07-30
    • 2021-10-12
    • 2020-12-03
    • 2019-01-12
    • 2021-08-17
    • 2021-08-14
    相关资源
    最近更新 更多