Jacob Humston
Jacob Humston

Reputation: 25

How do I check if the bot has permmsion to manage webhooks?

My problem is that I'm getting an error whenever I use the following code:

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)  
}}   

I want the bot to tell the user if it can't create the webhook, but instead, I get this error when trying to use this command: (node:10044) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'hasPermission' of undefined

How do I make this code work?

Upvotes: 2

Views: 226

Answers (1)

Lauren Yim
Lauren Yim

Reputation: 14118

If you're using Discord.js v12 (the latest verison), guild.members.get(bot.id) won't work as guild.memebrs is now a GuildMemberManager.

Use

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

or you could even use this as a shortcut:

const member = guild.me

Upvotes: 2

Related Questions