1

In discord.js I'm trying to read args having no space issues, for example:

const { MessageEmbed } = require("discord.js");

module.exports = {

run: async (client, message, args, { GuildDB }) => {
    let roleName = args[0];
    let roleColor = args[1];
},
};

Problem is, "roleName" doesn't allow to me to use spaces and it counts the second word as roleColor when I use spaces in roleName

How do I easy control args? splits? or use spaces between args?

Any help is appreciated.

6
  • What is args? process.argv? Some args passed by discord.js? Where are the arguments parsed from? Just a string message that's split by spaces? Commented Sep 9, 2021 at 11:18
  • Traditionally in console applications to pass a string with spaces as a single argument, you would enclose it in quotes ("this is a single argument"). Commented Sep 9, 2021 at 11:19
  • edited the code, apologize for making you confused, check again @cbr Commented Sep 9, 2021 at 11:23
  • the question is not clear Commented Sep 9, 2021 at 11:28
  • Again, how is this run function used? Where do you pass the function? Who calls this function and passes it its arguments? Commented Sep 9, 2021 at 11:37

2 Answers 2

1

You can handle how the array and split and joined.

To make this easier for yourslef I first recommend you switch the places of your arguments. [command] [roleColor] [roleName] instead of [command] [roleName] [roleColor].

This way we can isolate the color and then use the remaining elements to assign and join to roleName

const { MessageEmbed } = require("discord.js");

module.exports = {
    run: async (client, message, args, { GuildDB }) => {
        // Deconstruct from the args array
        let [roleColor, roleName] = [args[0], args.slice(1)];

        // If there are more than 1 element, join them into a string via a space
        roleName = roleName.join(' ');
    },
};
Sign up to request clarification or add additional context in comments.

4 Comments

I got an error: roleName.join is not a function why is that so?
@Zuher Laith, My mistake, the rest operator is not needed for this method. I have edited and tested my answer and this should now work!
this works, although I didn't get to understand why we slize "arg[1]" and join it with a space?
Let's say we have the args array as ['foo', 'bar', 'baz'] (Foo is color and Bar Baz is the name). To get the name we remove the color arg, which is done with slice(1) (Removes 1st element), leaving ['bar', 'baz']. We use join(' ') to join the 2 separate array elements together into a string which results in 'bar baz'. Documentations for Array.prototype.slice() and Array.prototype.join()
0

Best thing you could do here is create a TextChannel#createMessageCollector since you have multiple arguments to get as input easiest way to do it would be like so: ( Example user command !createrole )

let rolename = args.join(" ") 
message.channel.send("Alright now what would be it's color?").then( msg => 
const filter = (m) => { m.author.id === message.author.id }
const collector = msg.channel.createMessageCollector(filter, { time: 15000 })
collector.on('collect', collected => {
message.channel.send(`Alright creating the role ${rolename} with color ${collected.first().content}`); 
message.guild.roles.create({
    name: rolename
    color: collected.first().content;
})
});

5 Comments

This is not working for me in [email protected]
Does not work won't help me improve my answer , could you add further details such as errors n stuff?
Does not work won't help me improve my answer , could you add further details such as errors n stuff?
The code is not well formatted, it gives me errors on: Ln3 Col1 " const { expected " Ln4 Col 78 " ',' expected " I couldn't get to format it correctly..
Fixed that, the first error doesnt seem valid since I am not destructuring anything although added { just for the sake of it maybe for the arrow function that is

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.