Discord Bot错误在没有权限时发生

问题描述 投票:0回答:1

我正在尝试这样做,所以我的机器人可以检查是否满足一个权限,我已经尝试了一个方法,用['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")方法,但它检查是否满足BOTH权限,对我来说,如果只满足一个权限就可以了,我不希望机器人要求自己和消息作者都拥有这两个权限。

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权限的机器人时,它会发出“运行命令时出错”。错误,而不是输入命令代码的错误。

node.js visual-studio-code discord.js
1个回答
1
投票

一个问题是你使用GuildMember#hasPermission有点不对,而且你忘记了一个if语句中的!

// At the first statement you dont really need Administrator Perms as MANAGE_WEBHOOKS is enough
// Also you forget the ! in front of the check so it would return the error message only if the Bot did  have Permissions to edit the Webhook
if(!message.member.guild.me.hasPermission(['MANAGE_WEBHOOKS'])) return message.channel.send('I don\'t have the permissions to make webhooks, please contact an admin or change my permissions!');
// To check if the Member is Admin or Has Webhook Manager you only need to check for WebHook as Administrator already gives manage_webhooks 
if (!message.member.hasPermission(['MANAGE_WEBHOOKS'])) return message.channel.send('You need to be an admin or webhook manager to use this command.');
© www.soinside.com 2019 - 2024. All rights reserved.