#Using collector.on('end') to edit reply?

34 messages · Page 1 of 1 (latest)

short hazel
#

I'm trying to create a polling bot that edits their reply once the poll has concluded, but i can't figure out how to do that, and was wondering if anyone had any advice? I'm not sure if I should figure out a way to do it through collector.on('collect') or if there is a way to do it with collector.on('end'). If the right way to do it is through collector.on('collect'), i'm not sure if i can combine a switch statement with an await 60 _ 000 to update the message when the timer has ticked down

fossil quiverBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
  • Marked as resolved by OP
short hazel
#
collector.on('collect', i => {
            console.log('Hello from the collector');
            if (voters.has(i.user.id)) {
                i.reply({content: `You've already voted on this poll!`, ephemeral: true});
            }
            else {
                voters.add(i.user.id);
                switch(i.customId){
                    case 'firstChoiceButton':
                        votes.firstChoice = votes.firstChoice += 1;
                        votes.total = votes.total += 1;
                        console.log(votes);
                        i.update({embeds: [createPollEmbed(votes)], components: [row], fetchReply: true})
                        break;
                    case 'secondChoiceButton':
                        votes.secondChoice = votes.secondChoice += 1;
                        votes.total = votes.total += 1;
                        console.log(votes);
                        i.update({embeds: [createPollEmbed(votes)], components: [row], fetchReply: true})
                        break;
                    case 'thirdChoiceButton':
                        votes.thirdChoice = votes.thirdChoice += 1;
                        votes.total = votes.total += 1;
                        console.log(votes);
                        i.update({embeds: [createPollEmbed(votes)], components: [row], fetchReply: true})
                }
            }
        });
        collector.on('end', i => {
            console.log(`${i} has ended`)
        })```
#

for reference this is my code

austere lark
#

You can just set the timeout to 60000 and use

collector.on('end', (i, reason) => {
    if (reason === 'time') {
        // Edit your reply here
    }
});
short hazel
#

yuh but i tried doing i.update(0) and i.editReply() but both give me an error

#

i shouldve mentioned that

#

my bad

austere lark
#

"an error" is pretty vague. What error did you get?

short hazel
#

neither i.update() nor i.editReply() are actual functions, and im pretty sure its cause i is an object map

austere lark
#

You should edit the original interaction of the command instead, I don't believe the 'end' event callback has an interaction since the collector has ended

short hazel
#

fair point, i have this line which sends the original embed:
const response = await modalSubmission.reply({ embeds: [createPollEmbed(votes)], components: [row], fetchReply: true });
and i tried updating it with response.editReply, but that is also apparently not a valid function (i got editReply from this: https://discordjs.guide/slash-commands/response-methods.html#ephemeral-responses)

#

also if i try interaction.editreply

#

" throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
^

DiscordAPIError[10008]: Unknown Message"

fair kiln
#
  1. just making sure you're aware that the native polls beta launched last week #discord-dev-news message
  2. the end event does emit with a Collection of the collected items, so it does contain the previous interactions
    you'd need to pick one (<Collection>.first() just being the first one if it doesn't matter)
short hazel
#

i did not know about native polls 😅 i'll def take a look

#

the collected items are interactions to buttons

#

i really only wanna edit the message itself

#

I wanna take this message and update it with saying "the poll has ended, B has the most votes with 100% of the votes"

#

or something like that

fair kiln
#

yes, but while i.editReply() doesn't exist because i is a Collection, <ButtonInteraction>.editReply() does exist

short hazel
#

ahhh okay, that makes sense, so then it would be i.first().editReply()?

fair kiln
#

assuming it does collect an interaction, yes

short hazel
#

ahhh it works! however it doesnt remove the embed

#

just leaves me like this for some reason:

fair kiln
#

are you passing an empty array for embeds?
care to share your updated code?

short hazel
#
const response = await modalSubmission.reply({ embeds: [createPollEmbed(votes)], components: [row], fetchReply: true });

        const collector = response.createMessageComponentCollector({ componentType: ComponentType.Button, time: 5_000 });

        collector.on('collect', i => {
            if (voters.has(i.user.id)) {
                i.reply({content: `You've already voted on this poll!`, ephemeral: true});
            }
            else {
                voters.add(i.user.id);
                switch(i.customId){
                    case 'firstChoiceButton':
                        votes.firstChoice = votes.firstChoice += 1;
                        votes.total = votes.total += 1;
                        console.log(votes);
                        i.update({embeds: [createPollEmbed(votes)], components: [row], fetchReply: true})
                        break;
                    case 'secondChoiceButton':
                        votes.secondChoice = votes.secondChoice += 1;
                        votes.total = votes.total += 1;
                        console.log(votes);
                        i.update({embeds: [createPollEmbed(votes)], components: [row], fetchReply: true})
                        break;
                    case 'thirdChoiceButton':
                        votes.thirdChoice = votes.thirdChoice += 1;
                        votes.total = votes.total += 1;
                        console.log(votes);
                        i.update({embeds: [createPollEmbed(votes)], components: [row], fetchReply: true})
                }
            }

        });
        collector.on('end', i => {
            i.first().editReply({content: 'hello'})
        })```
#
function createPollEmbed(){
            console.log(votes)
        
            const pollEmbed = new EmbedBuilder()
                .setColor(0x0099FF)
                .setTitle(pollTitle)
                .setDescription('Choose one of the following options')
                .addFields(
                    { name: '\u200B', value: `${firstChoice} - ${calculatePercentage(votes.firstChoice, votes.total)}
                    ${':blue_square:'.repeat(calculateNumSquares(votes.firstChoice, votes.total))}${':black_large_square:'.repeat((10 - calculateNumSquares(votes.firstChoice, votes.total)))}
                    ${secondChoice} - ${calculatePercentage(votes.secondChoice, votes.total)}
                    ${':blue_square:'.repeat(calculateNumSquares(votes.secondChoice, votes.total))}${':black_large_square:'.repeat((10 - calculateNumSquares(votes.secondChoice, votes.total)))}
                    ${thirdChoice} - ${calculatePercentage(votes.thirdChoice, votes.total)}
                    ${':blue_square:'.repeat(calculateNumSquares(votes.thirdChoice, votes.total))}${':black_large_square:'.repeat((10 - calculateNumSquares(votes.thirdChoice, votes.total)))}` },
                );
            return pollEmbed;
        }```
#

its an array of only 1 element

fair kiln
#

not sure what you mean by it's an array of only 1 element
to remove all embeds from a message when editing, you should pass an empty array for embeds
it does not edit properties you don't specify

short hazel
#

ahh sorry i misunderstood

#

i thought you were asking if in the initial interaction i was passing an empty array for embeds and then populating

#

sorry