类型错误:无法读取未定义 Discord.js node.js v12.16.3 的属性“first”

问题描述

我正在编写一个不和谐的机器人,但我不知道如何处理 first()。有人可以帮忙。 kick.js:

'''module.exports = {
name: 'kick',category: 'moderation',description: 'This will kick a user of your choice from the server',async execute(msg,args) {
    const member = msg.mentions.user.first();
    if(member) {
        const memberTarget = message.guild.members.cache.get(member.id);
        memberTarget.kick();
        msg.channel.send("User has been kicked.")
    }else{
        msg.channel.reply('*You* *Could* *not* *kick* *that* *user.*')
    }
  }
} 

'''

index.js:

  '''   
    const discord = require('discord.js')
  const client = new discord.Client();


  const guildId = '825243185588469820'

  const fs = require('fs');
  const {
    get
  } = require('http');

 client.commands = new discord.Collection();
 const commandFiles = fs.readdirsync('./commands/').filter(file => file.endsWith('.js'));

for (file of commandFiles) {
const command = require(`./commands/${file}`);

client.commands.set(command.name,command);
}

   const config = require('./config.json');


   client.on('ready',() => {
      console.log(`Logged in as ${client.user.tag}!`);


     });

    client.on('message',msg => {
    if (!msg.content.startsWith(prefix) || msg.author.bot) return;
    const args = msg.content.slice(prefix.length).trim().split('/ +/ ');
    const command = args.shift();
    try {
    command.execute(msg,args);
    } catch (error) {
    //...
   } 

   try {
    client.commands.get(command).execute(msg,args);
  } catch (error) {
    console.error(error);
    msg.reply('*There* *was* *an* *error* *trying* *to* *execute* *that* *command!*');
  }



});


client.on('message',msg => {

if (msg.content == 'ping') {
    client.commands.get('ping').execute(msg,args);
} else if (msg.content == 'pong') {
    client.commands.get('pong').execute(msg,args);
} else if (msg.content == `beep`) {
    client.commands.get('beep').execute(msg,args);
} else if (msg.content == `boop`) {
    client.commands.get('boop').execute(msg,args);
} else if (msg.content == `server`) {
    client.commands.get('server').execute(msg,args);
} else if (msg.content == `user-info`) {
    msg.channel.send(`Your username: ${msg.author.username}\nYour ID: ${msg.author.id}`);
} else if (msg.content == `ban`) {
    client.commands.get('ban').execute(msg,args);
} else if (msg.content == `kick`) {
    client.commands.get('kick').execute(msg,args);
}else if (msg.content == `twitch`) {
    client.commands.get('twitch').execute(msg,args);
}

});



   const {
    prefix,token
 } = require('./config.json');
  //...

 client.login(config.token);

'''

这是我的错误

''' 类型错误:无法读取未定义的属性“执行” 在客户端。 (C:\Users\willi\Desktop\spagehtt\index.js:41:31) 在 Client.emit (events.js:322:22) 在 MessageCreateAction.handle (C:\Users\willi\Desktop\spagehtt\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14) 在 Object.module.exports [as MESSAGE_CREATE] (C:\Users\willi\Desktop\spagehtt\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) 在 WebSocketManager.handlePacket (C:\Users\willi\Desktop\spagehtt\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
在 WebSocketShard.onPacket (C:\Users\willi\Desktop\spagehtt\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22) 在 WebSocketShard.onMessage (C:\Users\willi\Desktop\spagehtt\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10) 在 WebSocket.onMessage (C:\Users\willi\Desktop\spagehtt\node_modules\ws\lib\event-target.js:132:16) 在 WebSocket.emit (events.js:310:20) 在 Receiver.receiverOnMessage (C:\Users\willi\Desktop\spagehtt\node_modules\ws\lib\websocket.js:835:20) '''

解决方法

好的,我发现您的代码中有一些错误。大多数是简单的错误,但不要担心它们,因为这就是你学习的方式。任何人,这是我发现的...

(命令文件)kick.js:

module.exports = {
name: 'kick',category: 'moderation',description: 'This will kick a user of your choice from the server',async execute(msg,args) {
    const member = msg.mentions.users.first();
    if (!member) return msg.channel.reply(`*You couldn\'t ${this.name} that member!*`);
    try {
        msg.guild.members.cache.get(member.id).kick();
        msg.channel.send(`User has been ${this.name}ed.`);
    } catch {
        msg.reply(`I wasn\'t able to ${this.name} ` + msg.members.mentions.first());
    }
  },};

(主文件)index.js:

const Discord = require('discord.js')
const client = new Discord.Client();
const guildId = '825243185588469820'
const fs = require('fs');
  
const {
    prefix,token
} = require('./config.json');

const {
  get
} = require('http');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name,command);
}
const config = require('./config.json');


client.on('ready',() => {
      console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message',msg => {
    if (!msg.content.startsWith(prefix) || msg.author.bot) return;
    const args = msg.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();

    if (!client.commands.has(command)) return;

    try {
        client.commands.get(command).execute(msg,args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

client.login(config.token);

这应该可以解决您遇到的问题。希望这能奏效,并祝您制作 Discord API 好运!!!

,

您犯了一个小错误 - 而不是 msg.mentions.user.first(),您需要做 msg.mentions.users.first()