The-SourceCode / Discord.js-Bot-Development

143 stars 262 forks source link

My bot responds to any prefix??? #41

Open Slurpity opened 4 years ago

Slurpity commented 4 years ago

const Discord = require('discord.js'); const bot = new Discord.Client();

const token = '(token taken out for obvious reasons)';

const PREFIX = '.';

var version = '0.420.69'

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

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

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

switch(args[0]){
    case 'website':
        message.channel.send('https://www.coolmathgames.com/')
        break;
    case 'info':
        message.channel.send('Version ' + version);
        break;
    case 'purge':
        if(!message.member.hasPermission("PRIORITY_SPEAKER", explicit = true)) return message.channel.send('You do not have permission to execute this command!')
        if(!args[1]) return message.channel.send('Please send an amount of messages to delete!')
        message.channel.bulkDelete(args[1]);
        break;

}

})

bot.login(token);


Okay, so my bot responds to any prefix... The prefix I use is "." so I should do .(command) for it to work right? no. I can do anything; f(command) y(command) ?(command). Any help? I am a complete noob to this.

(For the screenshot, Slurp-Bot's prefix is ";" and Slurp-Test-Bot's prefix is ".") poop

MrDiamond64 commented 4 years ago

It’s because your bot doesn’t check if the message starts with the prefix

Add this to the command handler: If(!message.content.startsWith(prefix) return;

PantheraRed commented 4 years ago

@Slurpity, a representation of what @MrDiamond64 meant is shown below:

const Discord = require('discord.js'); // Imports discord.js library
const client = new Discord.Client({disableMentions: 'everyone'}); // You can use "bot" instead of "client"

client.on('ready', () => {
  console.log(`${client.user.username} is online!`);
  // This will get the username of your bot without you having to set it manually
});

client.on('message', (message) => {
  var prefix = '!' // This is your bot's prefix, in your case you can use anything
  var args = message.content.slice(prefix.length).replace('^\\s*', '').split(' ');
  var command = args.shift().toLowerCase();

  if (message.content.startsWith(prefix)) {
    if (command === 'ping') message.channel.send('pong!');
  }
  // That's just an example, you can make your own commands
});

client.login(token); // Your Bot's token here

Btw, make sure to close this thread if you're satisfied with your answer. If you have any doubts you may ask us. We are here to help. :+1: