Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My bot responds to any prefix??? #41

Open
Slurpity opened this issue Mar 6, 2020 · 2 comments
Open

My bot responds to any prefix??? #41

Slurpity opened this issue Mar 6, 2020 · 2 comments

Comments

@Slurpity
Copy link

Slurpity commented Mar 6, 2020

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
Copy link

MrDiamond64 commented Mar 10, 2020

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
Copy link

PantheraRed commented Mar 14, 2020

@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. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants