0

What I'm trying to do is write a bot with discord.io, the problem is, that when I came to making a command to set nicknames, I ran into a problem, I knew that I am supposed to use the message.substring(1).split(' '); syntax, but I don't know how to take the now split message string and put the substrings into separate variables. How exactly would I do that? I'm hoping to set/change their nickname to what I set it to using this ?setnick <userID> <nickname to have it set it to>

1
  • At the very least you need to show what you're expecting to get from your split. What's the end result? If I were you, I'd write code in a totally separate file to practice splitting strings and proof out what you're trying to do before trying to make it work with Discord. You can grab a message string from Discord and use that in your prototype code. Commented Mar 1, 2018 at 18:43

2 Answers 2

1

I would probably do something like this:

Let's pretend the message.content looks like this: "?setnick 0123 testName"

// first, store message content into small variable (to keep things clean later)
let msg = message.content;

// now pull out the command arguments - everything after the first space in message.content
let argString = msg.substr( msg.indexOf(' ') + 1 );

// argString should be "0123 testName"
// -> now split argString into pieces (id and new nickname)
let argArr = argString.split(' ');

// argArr should be ["0123", "testName"]
// -> now store each argument into its own new variable
let [uid, newNickname] = argArr;

Now you have two variables, uid = "0123" and newNickname = "testName" which you can use.

You probably also want to do add some validation on your argArr variable - so if the user adds extra text in the argument, you can either throw an error or add the extra data as the nickname (as examples).

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

After you split the message, you'll have an array with all the parts.

var message = " part1 part2 part3";
var array = message.substring(1).split(' ');
// array = ["part1", "part2", "part3"]

You can access these by using the array's indexes:

var part1 = array[0];
// part1 = "part1"

Or you can use forEach to do something with each part:

array.forEach(x => console.log(x));
// part1
// part2
// part3

2 Comments

would the x be all of the array parts, or the array part that you assigned the array[x]
The code will run for each part and x will hold the value of each individual part each time the code runs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.