Reputation: 43
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
Reputation: 4451
In addition to the answer above client.on()
, I believe you also want this:
message.content.split(Prefix)
.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
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