Alexander Wallace
Alexander Wallace

Reputation: 43

Discord bot stops working after one response

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

const client = new Discord.Client();

const Prefix = '!';

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

client.once('message', message=>{

    let args = message.content.substring(Prefix.length).split(" ");

    switch(args[0]){
        case 'ping':
            message.channel.send('pong');
        break;
    };
})


client.login('[Token]');

When i say !ping to my bot it says pong like i wanted but when i say it again it wont respond with ping

Upvotes: 4

Views: 152

Answers (2)

Rahul Bhobe
Rahul Bhobe

Reputation: 4451

In addition to the answer above client.on(), I believe you also want this:

  1. You want to split with prefix message.content.split(Prefix).
  2. And then use args[1].

(Note that there are other ways to do this as well. I made least modifications to your code.)

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

const client = new Discord.Client();

const Prefix = '!';

client.on('ready', () => {
    console.log('Bot is online!');
});

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

    let args = message.content.split(Prefix);

    switch(args[1]){
        case 'ping':
            message.channel.send('pong');
        break;
    };
})


client.login('[Token]');

Upvotes: 1

Šimon Slabý
Šimon Slabý

Reputation: 603

I would say, that you need to have your code like client.on not client.once. Also you can check discord.js they have an easy example there about this.

Also talking about other parts of you code I would suggest to use .split(' ') .split(" ") but I think there would be no difference in this situation.

And maybe it would be useful to convert all the message to lowercase because if somebody will sand PING this command will not work.

let command = args.shift().toLowerCase();

Upvotes: 3

Related Questions