Discord Bot 没有权限时发生错误
Discord Bot error occurs when it has no permissions
我正在努力做到这一点,这样我的机器人就可以检查是否满足一个权限,我已经尝试了一种用 ['Permission']
包围权限的方法,到目前为止它已经解决了,问题是如果不满足权限,则机器人会发出错误。
TypeError: Cannot read property 'edit' of undefined
该机器人仍然可以正常工作,但它应该发出类似 "I don't have permissions" 的消息(我添加的),而只是发出
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,没有任何错误,但是当你剥夺机器人的 ADMINISTRATOR
和 MANAGE_WEBHOOKS
权限时,它会发出 "An error occurred while running the command." 错误,不是输入命令代码的错误。
一个问题是您使用的 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.');
我正在努力做到这一点,这样我的机器人就可以检查是否满足一个权限,我已经尝试了一种用 ['Permission']
包围权限的方法,到目前为止它已经解决了,问题是如果不满足权限,则机器人会发出错误。
TypeError: Cannot read property 'edit' of undefined
该机器人仍然可以正常工作,但它应该发出类似 "I don't have permissions" 的消息(我添加的),而只是发出
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,没有任何错误,但是当你剥夺机器人的 ADMINISTRATOR
和 MANAGE_WEBHOOKS
权限时,它会发出 "An error occurred while running the command." 错误,不是输入命令代码的错误。
一个问题是您使用的 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.');