如何检查机器人是否具有管理Webhook的权限?

时间:2020-03-29 22:54:08

标签: discord.js

我的问题是,每当我使用以下代码时都会出错:

else if (cmd === "sayw") {
const embed2 = new Discord.RichEmbed()
.setDescription("❌ You can't make me say nothing! \nWait, you just did-")
.setFooter(`Requsted by ${message.author.tag}.`, message.author.avatarURL)
if(!args[0]) return message.channel.send(embed2)
const guild = message.guild
const embed = new Discord.RichEmbed()
.setAuthor("Say Webhook", bot.user.avatarURL)
.setDescription("❌ I need the `MANAGE_WEBHOOKS` permision first!" )
.setImage(`https://i.gyazo.com/d1d5dc57aa1dd20d38a22b2f0d4bd2f6.png`)
const member = guild.members.get(bot.id)
if (member.hasPermission("MANAGE_WEBHOOKS")) {
message.channel.createWebhook(message.author.username, message.author.avatarURL)
  .then(webhook => webhook.edit(message.author.username, message.author.avatarURL))
  .then(wb => {wb.send(args.join(" "))
setTimeout(() => {
  bot.fetchWebhook(wb.id, wb.token)
    .then(webhook => {
    console.log("deleted!")
webhook.delete("Deleted!")
  }, 5000)})})
} else {
message.channel.send(embed)  
}}   

我希望机器人告诉用户它是否无法创建Webhook,但是,当尝试使用此命令时出现此错误: (node:10044) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'hasPermission' of undefined

如何使此代码起作用?

1 个答案:

答案 0 :(得分:2)

如果您使用Discord.js v12(最新版本),则guild.members.get(bot.id)将不起作用,因为guild.memebrs现在是GuildMemberManager

使用

const member = guild.members.cache.get(bot.id)

或者您甚至可以将其用作快捷方式:

const member = guild.me