A-a-ron-ie Youtube
A-a-ron-ie Youtube

Reputation: 3

How to make multiple arguments into 1

So I want to have my code be able to take the last set of arguments as one sentence. My code currently is:

const Discord = require('discord.js');

const { User, ClientUser, GuildMember, TeamMember, Message} = require("discord.js");

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "USER", "REACTION"]});

module.exports = {
name: 'report',
description: "report a naughty person",

async execute(message, args, Discord, client){ 

const guild = message.guild   

const reason = args.splice(1)

let embed = new Discord.MessageEmbed()
    .setColor('#1ed700')
    .setTitle('Report \n')
    .setDescription(`Person who reported ${message.author} \n`
        + `Channel reported in: ${message.channel}\n`
        + `Person reported: ${args[0]} \n` //The second argument
        + `Reason reported: ${reason}`) // The third argument
     

let messageEmbed = await message.channel.send(embed);
message.channel.send(`<@&${process.env.DUMMY_ROLE}>`);

}};

The issue being is the output makes the final set of arguments have commas in between. So like if I do the command:

-report @BubGum for being to cool!

it responds:

Person who reported @AaronieYT

reported in: #test-channel

Person reported: @BubGum

Reason reported: for,being,to,cool!

@Dummy mod

I want the reasons to have no commas but just be spaces.

Upvotes: 0

Views: 85

Answers (2)

0xLogN
0xLogN

Reputation: 3805

Array.prototype.toString vs Array.prototype.join

Array.prototype.toString

Array.prototype.toString, simply speaking, converts an array to a string by calling this.join(',') (see Array.prototype.join).

<this>.prototype.toString() is called when integrating a non-string into a string, as seen here:

class MyCoolClass {
    constructor() {
        this.coolValue = 'some cool value';
    }
    
    toString() {
        console.log('MyCoolClass: toString called');
        return `[MyCoolClass: ${this.coolValue}]`;
    }
}

console.log('creating instance')
const obj = new MyCoolClass();
console.log(`Instance: ${obj}`); // convert obj to string

As you can see above, we can create our own toString functions, on our own objects!

Quote from MDN:

Description

Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString() method is inherited by every object descended from Object. If this method is not overridden in a custom object, toString() returns "[object type]", where type is the object type. The following code illustrates this:

const o = new Object();
o.toString(); // returns [object Object]

Array.prototype.join

This method takes an array and converts it into a string.

Calling it with (['foo', 'bar'], ', ') returns "foo, bar", etc.

Result

Just do this :)

`Reason reported: ${reason.join(' ')}`

i have managed to fry my own brain

Upvotes: 0

Arun Kumar Mohan
Arun Kumar Mohan

Reputation: 11915

Array.prototype.toString returns a string joining the array elements by a comma. You can use Array.prototype.join to create a string separated by spaces.

`Reason reported: ${reason.join(' ')}`

Upvotes: 1

Related Questions