I need to get the text after someone issues a command and use it to search information on an API. Right now it works with set variables. How would I get the text after the command and create a variable with it.
EX. !command Name I need to turn NAME into a variable, so I can use it in my API request.
I am using TMI.js and Node.js
Code I’m using with the name replaced so you can see what I need to do.
hypixel.getPlayerByName(key, NAME).then(player => {
const bedKills = player.player.stats.Bedwars.kills_bedwars;
client.on('message', (channel, userstate, message, self) => {
if(self) return;
if(message.toLowerCase() === '!bedkills') {
client.say(channel, `@${userstate.username} ${NAME} has killed ${bedKills} players!`);
}
});
});
The variable is called NAME already.
You passed it to the hypixel library function
OK but I need to change the variable with tmi.js how do I do that?
var NAME = 'whatever';
whatever can be passed in from another function/variable whatever
In your code NAME is cast outside of the client.on('messsage'
So I’m where is NAME supposed to come from? Whats the chat input and expected chat output?
Okay let me explain this better.
I need the variable NAME to change with
client.on(‘message’, (channel, userstate, message, self)
Someone will type the command !bedkills TheirName
I need the variable NAME to change to TheirName
This will change it on the message that is sent in twitch chat and the variable that the hypixel library needs.
What I don’t know how to do is isolate “theirName” from the chat command and how to set it as a variable that I can use.
client.on('message', (channel, userstate, message, self) => {
if(self) return;
var words = message.split(' ');
if(words[0].toLowerCase() === '!bedkills') {
var name_to_get = words[1];
hypixel.getPlayerByName(key, name_to_get).then(player => {
const bedKills = player.player.stats.Bedwars.kills_bedwars;
client.say(channel, `@${userstate.username} ${name_to_get} has killed ${bedKills} players!`);
});
}
});
Basic javascript take the message split it into words.
Then reorder to code so it works as expected
1 Like
THANK YOU!
I’m new to coding and I’m just making this bot for a friend you really helped out a lot.
system
Closed
August 16, 2020, 12:30pm
8
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.