没有权限时发生Discord Bot错误

时间:2019-03-23 05:46:27

标签: node.js visual-studio-code discord.js

我正在努力做到这一点,因此我的机器人只能检查是否满足一个许可权,并且我尝试了一种用['Permission']将许可权括起来的方法,到目前为止,解决方法是,问题是如果不符合权限,则漫游器会发出错误消息。

TypeError: Cannot read property 'edit' of undefined

机器人仍然可以正常工作,但是它应该发出“我没有权限”这样的消息(我添加了),相反,它只是发出

An error occurred while running the command: TypeError: Cannot read property 'edit' of undefined
You shouldn't ever receive an error like this.
Please contact the bot owner.

错误。

我尝试更改权限代码的位置,并尝试查找有关此内容的其他文章,但这只是普通的javascript,而不是discord.js。

我在hasPermission("MANAGE_WEBHOOKS", "ADMINISTRATOR")处使用了该方法,但是它会检查是否同时满足两个权限,对我来说,如果仅满足一个权限就可以,而且我不希望机器人本身和消息作者拥有这两个权限。

const Discord = require('discord.js');
const commando = require('discord.js-commando');

class pingy2 extends commando.Command 
{
    constructor(client) {
        super(client, {
            name: 'pinghook2',
            group: 'help',
            memberName: 'pinghook2',
            description: 'This is where you can set the pinghook.',
            aliases: ['ph2'],
        })
    }
async run(message, args){
if(message.member.guild.me.hasPermission(["MANAGE_WEBHOOKS"], ["ADMINISTRATOR"]))
return message.channel.send("I don't have the permissions to make webhooks, please contact an admin or change my permissions!")
if (!message.member.hasPermission(["MANAGE_WEBHOOKS"], ["ADMINISTRATOR"])) 
return message.channel.send("You need to be an admin or webhook manager to use this command.")

const avatar = `https://cdn.discordapp.com/attachments/515307677656678420/557050444954992673/Generic5.png`;
const name2 = "PingBot";
const hook = await message.channel.createWebhook(name2, avatar).catch(error => console.log(error))
await hook.edit(name2, avatar).catch(error => console.log(error))
message.channel.send("Your webhook is now created! You can delete it at any time and can be re-added by using this command! You can also edit the webhook's name or avatar.")

setInterval(() => {
    hook.send("success!")
}, 1200);

}};
module.exports = pingy2;

我希望在聊天中发送命令时,该机器人会创建一个Webhook,并且如果该机器人发现仅满足一个权限,它仍会继续执行该命令。

实际发生的是该机器人确实创建了Webhook,没有任何错误,但是当您剥夺该机器人的ADMINISTRATORMANAGE_WEBHOOKS权限时,它会发出“运行时发生错误命令。”错误,而不是命令代码中的错误。

1 个答案:

答案 0 :(得分:1)

一个问题是您使用GuildMember#hasPermission有点错误,并且在if语句之一中忘记了Book Book Book Book Cabinet 120x30x145

!