2

Here is my current code. I want to, as the title says, take everything from an array then react to a message.

 reactionCount = args.length // how many choices there are
        reactions = [
            '🇦',
            '🇧',
            '🇨', 
            '🇩',
            '🇪'
        ] // chosen reactions in an array
        
        let toReact = reactions.split(0, reactionCount) //here i try to get the first [n] reactions. This comes out as an array. This is the array I want to take everything from and then react
        console.log(toReact) //test
        message.react(toReact) //test

Right now this is trying to react to the message with an array. I am aware I can do this individually, kind of like this (old code)

if (args[4]) {
        const sentMessage = await message.channel.send(`**${message.member.displayName} Started a poll!**\n**A)** ${args[0]}\n**B)** ${args[1]}\n**C)** ${args[2]}\n**D)** ${args[3]}\n**E)** ${args[4]}`)
            sentMessage.react('🇦')
            sentMessage.react('🇧')
            sentMessage.react('🇨')
            sentMessage.react('🇩')
            sentMessage.react('🇪')
        } else if (args[3]) {
            const sentMessage = await message.channel.send(`**${message.member.displayName} Started a poll!**\n**A)** ${args[0]}\n**B)** ${args[1]}\n**C)** ${args[2]}\n**D)** ${args[3]}`)
            sentMessage.react('🇦')
            sentMessage.react('🇧')
            sentMessage.react('🇨')
            sentMessage.react('🇩')
        } else if (args[2]) {
            const sentMessage = await message.channel.send(`**${message.member.displayName} Started a poll!**\n**A)** ${args[0]}\n**B)** ${args[1]}\n**C)** ${args[2]}`)
            sentMessage.react('🇦')
            sentMessage.react('🇧')
            sentMessage.react('🇨')
        } else if (args[1]) {
            const sentMessage = await message.channel.send(`**${message.member.displayName} Started a poll!**\n**A)** ${args[0]}\n**B)** ${args[1]}`)
            sentMessage.react('🇦')
            sentMessage.react('🇧')
        } else if (args[0]) {
            const sentMessage = await message.channel.send(`**${message.member.displayName} Started a poll!**\n**A)** ${args[0]}`)
            sentMessage.react('🇦')
        } 

But I want to save some space. I want to preface, I am kind of beginner right now, in terms of javascript. I used to be really good, and I could have done this, but I took a year and a half long break and now I am essentially useless.

5
  • 1
    Do you mean a for loop? Commented Jun 23, 2022 at 20:23
  • @ITgoldman sure. How can that be used in this context? Commented Jun 23, 2022 at 20:27
  • reactions is already an array, why are you using the string split function on it? Commented Jun 23, 2022 at 20:38
  • @Mike'Pomax'Kamermans reactions is the acceptable emojis to use. I split it because the user who runs the command, inputs a flexible amount of options. If he makes 2 options, it would split the array and erase c, d, and e and leave a and b. Same with 3.. 4.. etc. That is the array I needed to grab everything from, not reactions Also that's a typo. I meant splice lol Commented Jun 23, 2022 at 20:44
  • and it is an array, not a string, so reactions.split doesn't exist, and would have thrown a JS error if you tried to run it. To get a subarray from an array, you use slice. Commented Jun 23, 2022 at 20:46

1 Answer 1

1

Ok...

for (var i = 0; i < reactions.length; i++) {
    var toReact = reactions[i];
    console.log(toReact) //test
    message.react(toReact) //test
}
Sign up to request clarification or add additional context in comments.

1 Comment

Or, so much easier, reactions.forEach(v => message.react(v)). No need for all the for loop syntax and value unpacking when .forEach is right there.

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.