discord.js - 如何检查机器人是否有权管理 webhooks?

标签 discord.js

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

如何让这段代码工作?

最佳答案

如果您使用的是 Discord.js v12(最新版本),guild.members.get(bot.id) 将无法像 guild.memebrs 一样工作现在是GuildMemberManager .

使用

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

或者您甚至可以使用它作为快捷方式:

const member = guild.me

关于discord.js - 如何检查机器人是否有权管理 webhooks?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60921773/

相关文章:

javascript - 根据 react 添加角色

javascript - Discord js,震耳欲聋的提到的用户

discord - 当我们进行私信时如何在discord js中获取权限

javascript - Discord.js 关于消息命令不起作用

javascript - 我的 Discord.js 机器人正在运行(在线并显示在控制台中),但它不会响应命令

javascript - 在 discord.js 中使用音乐可能无法正常工作

javascript - 尝试为我的不和谐机器人发出抽奖命令,但始终返回未定义的获胜者

javascript - 如何统计已使用邀请的成员(member)数量?

javascript - 有时机器人不会添加角色并设置昵称

javascript - 找不到 Discord.js 音乐机器人 ffmpeg?