类型错误:无法读取未定义的属性“get” - Discord bot

时间:2021-06-12 19:40:44

标签: javascript node.js discord discord.js

(编码新手,我只是跟着教程,一边理解一边学习) 我最近想编写我自己的 Discord 机器人,但我遇到了事件处理程序部分的问题,所以我尝试了另一种方法,但现在我遇到了另一个问题。

不是对“p!ping”响应“pong”,而是说:

<块引用>

client.commands.get('ping').execute(message, args); ^

类型错误:无法读取未定义的属性“get”

在 Object.execute (.../events/message.js:18:23)

在客户端。<匿名 (.../main.js:19:44)

我也尝试过替换

client.commands.get('ping').execute(message, args);

client.commands.cache.get('ping').execute(message, args); 甚至 client.commands.find('ping').execute(message, args); 但它说 “TypeError:无法读取未定义的属性 'get' - Discord bot” 甚至

主文件:

const Discord = require('discord.js');

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


const client = new Discord.Client();
client.commands = new Discord.Collection();



const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
    const event = require(`./events/${file}`);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args));
    } else {
        client.on(event.name, (...args) => event.execute(...args));
    }
};


const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    // set a new item in the Collection
    // with the key as the command name and the value as the exported module
    client.commands.set(command.name, command);
};

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();

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

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


client.login(token);

client.on("ready", () => {
  client.user.setPresence({
      activity: { 
      name: 'p!',
      type: 'WATCHING'
      },status: 'idle'
  });
});

Message.js:

const client = new Discord.Client();
const prefix = 'p!';

module.exports = {
   name: 'message',
    execute(message) { 
      
    console.log(`${message.author.tag} in #${message.channel.name} sent: ${message.content}`);

    
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase ();

    if (command === 'ping'){
      client.commands.get('ping').execute(message, args);
    } else if (command === 'help'){
      client.commands.get('trick').execute(message, args);
     } 
   }
};

ping.js:

module.exports = {
    name: 'ping',
    description: 'Ping!',
    execute(message, args) {
        message.channel.send('pong');
    },
};

希望我提供的信息对您有所帮助。如果这是一个小错误,例如缺少 ;,我很抱歉浪费您的时间。提前致谢

2 个答案:

答案 0 :(得分:0)

我改变了

if (command === 'ping'){

if (command === `${prefix}ping`){

而且它有效,我想我只需要使用所有命令来做到这一点。 如果您有更简单的解决方案,请随时分享,或者如果您发现代码有问题,请告诉我。 (因为在没有这个修改之前它可以工作),

谢谢

答案 1 :(得分:0)

找到一种更简单的方法,无需键入新行 ( if (command === 'ping'){ client.commands.get('ping').execute(message, args);)

这是我的 Message.js 文件现在看起来像:

const client = new Discord.Client();
client.commands = new Discord.Collection();


module.exports = (Discord, client, message) => {
  const {prefix} = require('../config.json');
  if(!message.content.startsWith(prefix) || message.author.bot) return;

  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const cmd = args.shift().toLowerCase ();

  const command = client.commands.get(cmd) || client.commands.find (a => a.aliases && a.aliases.includes(cmd));

  if(command) command.execute(client, message, args, Discord);

    
};