I am currently coding a Minecraft Bot for my guild and can’t seem to find a solution to a problem with getting a substring from a message. If you don’t know what Hypixel is, it doesn’t matter, I will take it out of context as much as I can.
Here is code for my current try:
if(message.startsWith("From") && !message.includes(":"))
{
const username = message.substr(5).startsWith("[")
? message.substr(5).split(" ")[2].trim()
: message.substr(5).split(" ")[1].trim();
username = username.length - 1;
this.chat(`/gc ${username}`);
}
The given message always looks like this:
“From [Rank] username: o/” or “From username: o/” if the user doesn’t have a Paid Rank on the Network.
and I would like to get the username.
After that, the bot will write “/gc username” on the Network, which would let me know that it worked.
This doesn’t happen.
I sadly don’t have any Error Messages (if there are any relevant ones)
I already made a different one which works:
if (this.isPartyMessage(message) && config.minecraft.fragBot.enabled === true)
{
const username = message.substr(54).startsWith("[")
? message.substr(54).split(" ")[1].trim()
: message.substr(54).split(" ")[0].trim();
this.send(`/party accept ${username}`);
await delay(Math.floor(Math.random() * (6900 - 4200 + 1)) + 4200);
this.send(`/pc ${username} Yo.`);
await delay(Math.floor(Math.random() * (6900 - 4200 + 1)) + 4200)/3;
this.send(`/party leave`);
}
The given message looks like this “———-[Rank] username” or “———- username” but with 54 “-“‘s.
I’ve been trying to get this to work but I can’t figure how to do it.
I also apologize if I’ve described poorly or if I didn’t describe enough out of context.
Thanks to anyone who could maybe help me with this problem, if there are any questions I will try to instantly answer. 🙂
user25020470 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1