user14589538
user14589538

Reputation:

Discord bot not coming online?

I recently made a discord bot but it wont come online there are no errors also idk why this is my code.

const TOKEN = "MyBotsToken";
const fs = require('fs')
const Discord = require('discord.js');
const Client = require('./client/Client');
const {
    prefix,
    token,
} = require('./config.json');

const client = new Client();
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);
}

console.log(client.commands);

client.once('ready', () => {
    console.log('Ready!');
});

client.once('reconnecting', () => {
    console.log('Reconnecting!');
});

client.once('disconnect', () => {
    console.log('Disconnect!');
});

client.on('message', async message => {
    const args = message.content.slice(prefix.length).split(/ +/);
    const commandName = args.shift().toLowerCase();
    const command = client.commands.get(commandName);

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

    try {
        if(commandName == "ban" || commandName == "userinfo") {
            command.execute(message, client);
        } else {
            command.execute(message);
        }
    } catch (error) {
        console.error(error);
        message.reply('There was an error trying to execute that command!');
    }

    console.log("bot is ready for use")

    bot.login(MyBotsToken);
});

My bot is also using node.js and javascript. I have also tried node index.js in cmd. nothing else nothing else nothing else nothing else nothing else nothing else nothing else nothing else nothing else nothing else nothing else nothing else nothing else nothing else nothing else

Thanks Extremepro999

Upvotes: 0

Views: 204

Answers (2)

Scoop
Scoop

Reputation: 41

bot.login(MyBotsToken) should be outside of client.on('message', async message => {});

Upvotes: 0

Worthy Alpaca
Worthy Alpaca

Reputation: 1245

The problem here is that your login action is within your onMessage event.

That means it will only be used when your bot detects a message. Since it won't go online it can't detect a message and so on...

Good news is that you can fix this simply by putting bot.login(MyBotsToken); outside of the onReady event. What you also need to do is use .login() on your client object.

So it should look like this.

client.on('message', message => {
    // your code
})

client.login(MyBotsToken);

Upvotes: 2

Related Questions