The-SourceCode/Discord.js-Bot-Development

Prefix Error (SOLVED!)

PantheraRed opened this issue · 11 comments

The following part ;-
bot.on("message", async message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;

let prefix = botconfig.prefix;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = messageArray.slice(1);

let commandfile = bot.commands.get(cmd.slice(prefix.length));
if(commandfile) commandfile.run(bot,message,args);

});
contains "cmd.slice(prefix.length)" due to which no matter what you type within the length of actual prefix the command always works. This happens because my prefix length is equal to (any random two characters).
Consider an example ;- "t!ping instead of x!ping("x!" is my prefix), the command will still work with "t!").
Any idea on what can be used instead of that so if i use other bot my bot doesn't respond due to same prefix problem?

Same :,(

There is one solution, but it requires you to scarifies your commands files separately, put all of them in index.js together, it's ok till you are not trying to make a very special bot to be released for discord publicly and contains a lot of features...
-Simple thing to do is, if using any packages put:- const "term" = require("package"); in index.js
-Now put your commands in:- if(cmd === `${prefix}your choice`){"command code"}
And that's it, remember to remove the line:- let commandfile = bot.commands.get(cmd.slice(prefix.length)); if(commandfile) commandfile.run(bot,message,args); from index.js
I know it makes things messy but it's the only way i found...till we get an actual solution.

Mmm, k, ty btw, if you found any other solution, pls tell me (?
:)

No wonder my bot doesn't respond, but even if the problem didn't happen, for some odd reason all of it is undefined.

IDK why you guys think you cant use a fixed prefix with the command handler, but its super easy, just add this line:

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

And then it will work fine

Dont listen to this PantheraRed guy

Just Use This Bit Of Code In Each Command File

if
(message.content.indexOf(process.env.PREFIX) !== 0) return;

This code stops the use of other prefixes

@ThePandazManYT No man, thats ridiculous, u can fix this in your message event, instead of spamming it in every command itself, just add the code i put above in ur message event.

@miniDeathStriker That Code Doesn't Work In My Project So I Developed A New Solution

String.prototype.hasPrefix(prefix) {
if(!this.toString().startsWith(prefix)) return false;
if(this.toString().startsWith(prefix)) return true;
};
if (message.content.hasPrefix('!')) { //In my case ! is the prefix, you can use anything you want.
//do something
};

The provided solution works well enough. But if you wanna go a little complex try this one. 👍

Yeah I realized after that video that I didn't include a little bit of code to check we actually had the right prefix to the command. 😛

@miniDeathStriker was correct with his line of code, but I'll try to make it a little more clear with this.

if(message.content.startsWith(prefix){
let commandfile = bot.commands.get(cmd.slice(prefix.length));
if(commandfile) commandfile.run(bot,message,args);
}else{
//you can put whatever you'd like here. like a coin system or something.
}

the bit you are missing is this
if (!message.content.startsWith(prefix)) return;

you want to placed it under this section
let prefix = prefixes[message.guild.id].prefixes;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = messageArray.slice(1);
if (!message.content.startsWith(prefix)) return; <===== Here
let commandfile = bot.commands.get(cmd.slice(prefix.length));
if(commandfile) commandfile.run(bot,message,args);