A
A
Alham_GG2019-11-15 08:26:47
JavaScript
Alham_GG, 2019-11-15 08:26:47

How to solve a bug in a bot?

As Visual Studio Code says there are no errors.
But when I try to run it I get an error

C:\Users\dimas\OneDrive\Рабочий стол\ALibaba\main.js:141
    client.on('ready', () => {
    ^

ReferenceError: client is not defined
    at Object.<anonymous> (C:\Users\dimas\OneDrive\Рабочий стол\ALibaba\main.js:141:5)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11

Please speak more clearly, as I'm just a beginner
Full code: https://pastebin.com/Xf59kWQE

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Neverov, 2019-11-15
@TTATPuOT

You are listening to the bot's message event before the bot is initialized with the .login('token') method. Moreover, inside the message event, you listen to ready from some client, which is not in the code at all. You think with your head what you write where. If there is not enough knowledge, start with something simpler, and not with a subscription to asynchronous events.
The fully working code is on the main discord.js page and in the documentation :

/**
 * A ping pong bot, whenever you send "ping", it replies "pong".
 */

// Import the discord.js module
const Discord = require('discord.js');

// Create an instance of a Discord client
const client = new Discord.Client();

/**
 * The ready event is vital, it means that only _after_ this will your bot start reacting to information
 * received from Discord
 */
client.on('ready', () => {
  console.log('I am ready!');
});

// Create an event listener for messages
client.on('message', message => {
  // If the message is "ping"
  if (message.content === 'ping') {
    // Send "pong" to the same channel
    message.channel.send('pong');
  }
});

// Log our bot in using the token from https://discordapp.com/developers/applications/me
client.login('your token here');

W
whiteblackness, 2019-11-15
@whiteblackness

Replace client with bot or vice versa .
You named it that way
. You have
const bot = new Discord.Client();
In the dock
const client = new Discord.Client();
https://discord.js.org

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question