【问题标题】:client not defined in discord bot module客户端未在 discord bot 模块中定义
【发布时间】:2020-10-13 04:02:12
【问题描述】:

我对任何类型的编程都不熟悉。在我的 discord bot 中,主文件查找执行命令的模块。

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

require('dotenv').config();
const bot = new Discord.Client();

bot.commands = new Discord.Collection();
const botCommands = require('./commands');

Object.keys(botCommands).map(key => {
  bot.commands.set(botCommands[key].name, botCommands[key]);
});

const TOKEN = process.env.TOKEN;

bot.login(TOKEN);

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

bot.on('message', msg => {
  const args = msg.content.split(/ +/);
  const command = args.shift().toLowerCase();
  console.info(`Called command: ${command}`);

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

  try {
    bot.commands.get(command).execute(msg, args);
  } catch (error) {
    console.error(error);
    msg.reply('Check it out! You *failed*.');
  }
});

其中一个命令应该在触发后向某个频道发送消息:

module.exports ={
    name: '!start',
    description: 'Starts the killing game',
    execute(msg, args) {
        client.on('ready', client => {
        client.channels.cache.get('724557257484009516').send('The killing game is about to start!');
        });
    },
};

但它出现了这个错误:

ReferenceError: client is not defined
    at Object.execute (D:\drpa-bot\big bin\commands\game-start.js:5:3)
    at Client.<anonymous> (D:\drpa-bot\big bin\index.js:28:31)
    at Client.emit (events.js:315:20)
    at MessageCreateHandler.handle (D:\drpa-bot\big bin\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (D:\drpa-bot\big bin\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (D:\drpa-bot\big bin\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (D:\drpa-bot\big bin\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (D:\drpa-bot\big bin\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (D:\drpa-bot\big bin\node_modules\ws\lib\websocket.js:789:20)
Called command: <@253506821438832640>,

我该如何解决?

【问题讨论】:

  • 如果我的回答解决了你的问题,欢迎采纳,让其他人也能看到

标签: javascript node.js discord


【解决方案1】:

您没有将客户端变量从 index.js 文件传递​​到命令文件,也不要在命令中放置事件侦听器,这是非常糟糕的主意,而是执行以下操作:

execute(msg, args) {
    Let guild = msg.client.guilds.cache.get(‘ID of the guild in which the channel is in);
    guild.channels.cache.get('724557257484009516').send('The killing game is about to start!');
},

我使用了msg.client,因为来自discord.js documentationmessage.client 实例化此消息的客户端,所以你的机器人。

【讨论】:

  • 好吧,我试过了,但现在我得到了一个不同的错误:TypeError: Cannot read property 'get' of undefined at Object.execute (D:\drpa-bot\big bin\commands\game- start.js:5:31)
  • 我的错,检查编辑的答案,你首先需要获取频道所在的公会,然后从该公会获取频道
猜你喜欢
  • 2022-01-13
  • 1970-01-01
  • 2021-04-16
  • 2019-06-26
  • 2018-02-15
  • 2019-03-25
  • 2019-09-04
  • 2021-10-28
  • 2021-12-20
相关资源
最近更新 更多