【问题标题】:Discord Bot error occurs when it has no permissionsDiscord Bot 在没有权限时发生错误
【发布时间】:2019-08-14 02:41:22
【问题描述】:

我正在尝试这样做,以便我的机器人可以检查是否满足一个权限,并且我尝试了一种方法,其中使用 ['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 权限,对我来说,如果只满足 ONE 权限就可以了,我不希望机器人要求它本身和消息作者拥有这两个权限。

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】:

    一个问题是您使用 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.');
    

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 2019-10-24
      • 2018-05-14
      • 2020-12-23
      • 2020-09-29
      • 2020-07-18
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      相关资源
      最近更新 更多