#Button collector

1 messages · Page 1 of 1 (latest)

jovial girder
#

Are you trying to collect that one button? Or have one general handler for them?

dense halo
#

well eventually i would want to specify buttons for doing specific functions

#

im making a pagination button

#

for an embed

#

ive tried using dependencies and such to do it for me but none of them work

jovial girder
#

Are you calling that client.on inside or outside of a function?

dense halo
#

oh inside a function

jovial girder
#

If it's inside, I would move it outside on the same level as the client.login

dense halo
#

im using a command handler so i get errors when i put it outside the function

#

let me try using any other event collector

jovial girder
#

You can use the client.on() just make sure it's outside of a function, and if you're going to do it in the function you'll want to use .awaitMessageComponent or .createMessageComponentCollector

dense halo
#

i dont think i can use client.on outside of a function so i could use either of those two message components to collect a button interaction?

jovial girder
dense halo
#

btw, im using slash commands. i hope that doesnt mess anything up

jovial girder
#

It doesn't

dense halo
#

oh wait nvm i can use it

#

let me see if that was the problem

#

here is my code

#

the command handler works btw im using reconlx

#

i have several other commands that i use with it and it works fine

jovial girder
#

If you're trying to do it in a command function, I would just use a .createMessageComponentCollector()

dense halo
#

yeah i think i'll try that

jovial girder
#

👍

dense halo
#

works thanks for your time

jovial girder
#

Np

#

Gives me a break from working on my dashboard haha

dense halo
#

oh cool i'll probably have to make one when my bot picks up

jovial girder
#

My bot is just for a group I'm in, but with all the features we need things like staff applications and verification management

dense halo
#

makes sense, final question, since you cant go back pages once youre at the first page how would i use .setDisabled(true) on the back button for that message?

#

like this for example

#

but since it wont work until you interact with a button, should i incorporate it before hand as well?

jovial girder
#

You could either create the button and disable it from the start if it's the first page, or just don't even create it if it's the first page

dense halo
#

true

#

i think i like disabling it

#

so the buttons dont shift around

#

so i would need to make two more buttons?

jovial girder
#

Also, be careful with what you have there, because it may just move other ones forward as well

jovial girder
#

Back & Next

#

I mean if you run the command twice, and click the second one, it's currently going to go through and edit each message since it's just listening for a button with customId forward

dense halo
#

the else is for the other button

#

i only have two

#

if its not one, then it has to be the other

#

i thought

jovial girder
#

I mean if you execute the command twice

#

Like

#

Hey [back] [next]

#

Hey [back] [next]

#

And click next on the second one

#

They'll both change

dense halo
#

ah

#

what should i do to solve that issue

jovial girder
#

😉

#
.setCustomId(JSON.stringify({ type: "forward", id: message.id }))
``` ```js
const data = JSON.parse(i.customId);
// data.type == "forward"
// data.id == 012345678912345678
dense halo
#

hm so let me understand this, it only fires for the message of the button you clicked on

#

like if you hit next on the second one, it only updates for the second one?

#

im not familiar with anything there but if it works it works

jovial girder
#

Yup

#

I'm using that for one of my commands

dense halo
#

oh what does the json file do

jovial girder
#

Well, I use it for every button, but more specifically one of the event_cancel buttons

#

the JSON stringify and parse just turn out object into a string, then back into an object

#

Basically allowing us to send over custom data along with it

#

So we can work with a specific element

#
if (interaction.isButton()) {
      const interactionData = JSON.parse(interaction.customId)
      const button = Client.buttons.get(interactionData.type);

      if (!button) return;

      try {
        await button.execute(Client, interaction, interactionData.extraData);
      } catch (e) {
        console.error(`[ERROR] ${e}`);
        await interaction.followUp({ content: `There was an error executing this button function!`, ephemeral: true });
      }
    }```
dense halo
#

so just having that i can have that in the back and i can keep my code the same as i had

jovial girder
#

Yeah

#

You can leave your code and add that stuff in there

#

Then that way you can go and edit the message for the button if the message id is the same

#

I can make a quick demo if you want

dense halo
#

oh ok how do you use ephemeral i swear theres something you need to add for it to work

jovial girder
#

ephemeral is just if it's only going to be shown to the user who interacted with it

#

i.e., clicked a button, ran a command

dense halo
#

yeah ik but it doesnt work for me

jovial girder
#

Wdym?

dense halo
#

just get an unknown interaction

jovial girder
#

It's most likely because you have editReply when no reply has been sent. So try interaction.reply()

dense halo
#

the editReply was when it said bot is thinking....

#

because this is a message so i can edit it inplace of a command

jovial girder
#

Oh do you have it go and immediately do interaction.deferReply() in your command handler?

#

Or does the command handler do that

dense halo
#

so, im not extremely familiar with the typescript and everything used for the handler but i assume youre talking about this deferReply

jovial girder
#

Yes

#

Then you can change it back to editReply

#

Not sure why it's saying unknown interaction..

#

Ah

#

But you would also need to have the ephemeral: true inside of the deferReply if you want it to only show to that user

dense halo
#

would that be for all commands then?

#

or why dont i get rid of the deferReply in that file

#

and then i specify where needed

jovial girder
#

If you put it in there, yes it would be for all commands. that's why for the command handler I made, I just do the deferReply at the start of each command

dense halo
#

oh lets go finally pops up as an option now

#

should work

#

yep it does work thank you, btw something annoying me, i have this slash command and i have no clue where its from, its not in my file

jovial girder
#

?

dense halo
#

nvm

#

now the empheral works but not the content

#

where do i put the content of the message in?

jovial girder
#

.editReply({content:"", ephemeral: true});

dense halo
#

and where does the deferReply go??

#

it seems the ephermal only works inside a deferReply

dense halo
jovial girder
#

So, you don't have to defer them, that's just if the reply can or will be more than 3 seconds late

#

So for the ones that need deferReply, just add it at the top of each run function you have.

#

And if you want it to only show to the caller, set ephemeral

dense halo
#

this is what i have so far

#

then i get this

#

the error kind of makes sense but not really

jovial girder
#

Yeah, so you don't have a deferReply anymore, so now you don't need to editReply, unless you .reply() to it

#

So you can either defer at the top so it thinks, or just interaction.reply()

dense halo
#

ohh thank you

#

wait a minute... not this again

jovial girder
#

wtf..

dense halo
#

i looked it up people said it had to do with the 3 second wait or something like that

jovial girder
#

...

dense halo
#

whyy this is so dumb

jovial girder
#

Right

#

I need to go to sleep because it's midnight

dense halo
#

oh alr thanks for the help

jovial girder
#

Np, hope you can get that last thing solved

dense halo
#

yeah it seems like a weird bug its not really happening now 🤷‍♂️

#

sometimes closing and opening vscode helps