0

我得到了一个 unban 命令代码,并在控制台中收到以下错误:

(节点:9348)UnhandledPromiseRejectionWarning:TypeError:无法在客户端读取 Object.execute(C:\Users\19nik\Documents\GitHub\bot-project\commands\unban.js:9:22)处未定义的属性“成员”。(C:\Users\19nik\Documents\GitHub\bot-project\gb.js:81:17) 在 Client.emit (events.js:315:20) 在 MessageCreateAction.handle (C:\Users\19nik\Documents \GitHub\bot-project\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14) 在 Object.module.exports [as MESSAGE_CREATE] (C:\Users\19nik\Documents\GitHub\bot -project\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) 在 WebSocketManager.handlePacket (C:\Users\19nik\Documents\GitHub\bot-project\node_modules\discord.js \src\client\websocket\WebSocketManager.js:384:31) 在 WebSocketShard.onPacket (C:\Users\19nik\Documents\GitHub\bot-project\node_modules\discord.

这是我的代码:

const { MessageEmbed } = require('discord.js');
const fs = require("fs");

module.exports = {
    name: `unban`,
    description: `Unbans given user ID or mentioned user.`,
    async execute(bot, args, message) {

        if (!message.member.hasPermission(["BAN_MEMBERS"])) return message.channel.send("You do not have the required permissions to use the unban command.")

        if (!args[0]) return message.channel.send("Provide me a valid USER ID.");
        //This if() checks if we typed anything after "!unban"

        let bannedMember;
        //This try...catch solves the problem with the await
        try {
            bannedMember = await bot.users.cache.fetch(args[0])
        } catch (e) {
            if (!bannedMember) return message.channel.send("That's not a valid USER ID.")
        }

        //Check if the user is not banned
        try {
            await message.guild.fetchBan(args[0])
        } catch (e) {
            message.channel.send('This user is not banned.');
            return;
        }

        let reason = args.slice(1).join(" ")
        if (!reason) reason = "No reason provided."

        if (!message.guild.me.hasPermission(["BAN_MEMBERS"])) return message.channel.send("I am missing permissions to unban.")
        message.delete()
        try {
            message.guild.members.unban(bannedMember, { reason: reason })
            message.channel.send(`${bannedMember.tag} has been unbanned.`)
            console.log(`AUDIT LOG: [UNBAN] ${message.author.tag} unbanned ${member.user.tag} from ${message.guild.name}.`);
            var readmessagefile = fs.readFileSync('./logging/UnbanLog.txt', 'utf-8');
            var writemessagefile = fs.writeFileSync('./logging/UnbanLog.txt', 'Type: [UNBAN] ' + 'Time ' + '(' + message.createdAt + ')' + ' | ' + member.user.tag + ' from ' + message.guild.name + ' | Moderator ' + message.author.tag + '\n' + readmessagefile)
            console.log('BOT LOG: [INTERNAL] Writing to unban log file.');
        } catch (e) {
            console.log(e.message)
        }
    }
}

我不知道该怎么办。它还弹出无法读取未定义的属性'hasPermissions'的错误。

4

6 回答 6

0

const findUser = message.guild.members.cache.get(message.author.id)
if(!findUSer) return;
const isMemberHavePermission = findUser.hasPermission("BAN_MEMBERS")
iF(isMemberHavePermission){
  // Member Have Permission
 }else{
  // Member Have not Permission
 }

于 2021-08-31T12:00:28.480 回答
0

我相信您的问题的主要来源是您的回调,在和client之前调用了您的第一个。由于 JS 对回调参数的放置很敏感,因此您应该将回调编写如下:messageargs

async execute(message, args, bot)
于 2021-01-13T15:43:51.260 回答
0

两个可能的原因:

打回来

确保 execute() 的参数与主文件中的顺序完全相同

命令在公会外使用

该错误可能已经发生,因为该命令在公会外部使用来修复此问题,请将其添加到您的代码中 execute()

if (!message.guild) return;
于 2021-05-14T12:48:00.480 回答
0

第 9 行似乎有错误

if (!message.member.hasPermission(["BAN_MEMBERS"])) return message.channel.send("You do not have the required permissions to use the unban command.")

哪个没有错误,也许命令是在 DMS 中运行的?也许使用:

if(!message.guild) return;

哪个应该解决问题。

于 2021-02-01T07:36:53.683 回答
0

很确定您使用的是 discord.js v12,还要确保您使用的是 node.js 的最新(或 v14)版本,如果不是至少在 v14 中,请在https://node.js.org下载

这是来自我的机器人,试试这个,它有效,我已将其更改为您的处理程序:


const { MessageEmbed } = require("discord.js")
const fs = require("fs")

module.exports = {
  name: "unban",
  description: "Unbans given user ID or mentioned user.",
  async execute(bot, message, args) {
    
    if (!message.member.hasPermission('BAN_MEMBERS')) return message.channel.send(`You need the permission **Ban Members** to execute this command.`)
    if (!message.guild.me.hasPermission('BAN_MEMBERS')) return message.channel.send(`**${client.user.tag}** needs the permission **Ban Members** to execute this command.`)
    
    const id = args[0];
    
    if (!id) return message.channel.send(`Please mention the user's ID to unban.`)

const bannedMember = client.users.fetch(args[0]);    

    const bans = await message.guild.fetchBans();
    if (bans.size == 0) return message.channel.send(`There are no users banned on this server.`);
    let bUser = bans.find(b => b.user.id == id);
    if(!bUser) return message.channel.send(`This user is not banned or was not found.`)

      const reason = args.slice(1).join(" ")
      if (!reason) return message.channel.send('Please include a reason!');

    try {
                    message.guild.members.unban(bannedMember, { reason: reason })
            message.channel.send(`${bannedMember.tag} has been unbanned.`)
            console.log(`AUDIT LOG: [UNBAN] ${message.author.tag} unbanned ${member.user.tag} from ${message.guild.name}.`);
            var readmessagefile = fs.readFileSync('./logging/UnbanLog.txt', 'utf-8');
            var writemessagefile = fs.writeFileSync('./logging/UnbanLog.txt', 'Type: [UNBAN] ' + 'Time ' + '(' + message.createdAt + ')' + ' | ' + member.user.tag + ' from ' + message.guild.name + ' | Moderator ' + message.author.tag + '\n' + readmessagefile)
            console.log('BOT LOG: [INTERNAL] Writing to unban log file.');

     } catch (e) {
      console.error(e);
}
 }
}

于 2021-10-10T14:46:17.180 回答
-1

我认为这会奏效。

const { MessageEmbed } = require('discord.js');
const fs = require("fs");

module.exports = {
    name: `unban`,
    description: `Unbans given user ID or mentioned user.`,
    async execute(bot, args, message) {
        if(!message.guild) return message.channel.send("This Command Can't Be Executed In DMs.");
        if (!message.author.hasPermission(["BAN_MEMBERS"])) return message.channel.send("You do not have the required permissions to use the unban command.")

        if (!args[0]) return message.channel.send("Provide me a valid USER ID.");
        //This if() checks if we typed anything after "!unban"

        let bannedMember;
        //This try...catch solves the problem with the await
        try {
            bannedMember = await bot.users.cache.fetch(args[0])
        } catch (e) {
            if (!bannedMember) return message.channel.send("That's not a valid USER ID.")
        }

        //Check if the user is not banned
        try {
            await message.guild.fetchBan(args[0])
        } catch (e) {
            message.channel.send('This user is not banned.');
            return;
        }

        let reason = args.slice(1).join(" ")
        if (!reason) reason = "No reason provided."

        if (!message.guild.me.hasPermission(["BAN_MEMBERS"])) return message.channel.send("I am missing permissions to unban.")
        message.delete()
        try {
            message.guild.members.unban(bannedMember, { reason: reason })
            message.channel.send(`${bannedMember.tag} has been unbanned.`)
        } catch (e) {
            console.log(e.message)
        }
    }
}

我删除了控制台日志记录部分,因为它可能会开始出现一些错误。

这可能是错误的,因为我是新手

于 2021-03-15T13:49:16.977 回答