#Button for Specific User

86 messages · Page 1 of 1 (latest)

unreal summit
trail harbor
#

you can have a collector filter or if statement in the collector that checks if the i.user.id is equal to the message.author.id

unreal summit
#

this is my first time working with buttons so i really dont know how to doo that

trail harbor
#

here's an example of a filter you can use

const collectorFilter = async i => {
if (i.user.id !== message.author.id) {
  await i.reply({ content: 'This is not for you.', ephemeral: true })
  }
} // then in your collector options, use { filter: collectorFilter }
unreal summit
#

so my collector would look like this

#
const Collector = Reply.createMessageComponentCollector({
    filter: collectorFilter
  });```
#

nd then this would be above that? js const collectorFilter = i => { if (i.user.id !== message.author.id) { await i.reply({ content: 'This is not for you.', ephemeral: true }) } }

trail harbor
#

sure

unreal summit
trail harbor
#

whoops i forgot to make it async lol thats on me

#

sorry about that

unreal summit
#

so it kinda works, i cant roll it but another person rolls it and it says this is not for you

trail harbor
#

that's what you wanted right?

unreal summit
#

yeah but when the correct person clicks on the button i want it to work lol

#

that was my 8ball game and i clicked on it

#

didnt work

#

but when it click on someone elses game it says its not my game (which works and is correct)

trail harbor
#

are there any errors?

#

i noticed you dont have a time set for the collector. are you not looking for it to be temporary?

unreal summit
unreal summit
trail harbor
#

collectors are meant to be temporary. if you want that button to be functional all the time then you should respond directly in the interactionCreate event

odd daggerBOT
#

guide Message Components: The Client#interactionCreate event
Third and finally, you may wish to have a listener setup to respond to permanent button or select menu features of your guild. For this, returning to the Client#event:interactionCreateopen in new window event is the best approach.
read more

unreal summit
#

oh okay ill add a time limit of 10,000ms

trail harbor
#

right above that if statement, do console.log(i)

#

in the future, it would be better to ask in the discord.js support channels for discord.js-related questions

trail harbor
#

did you log i?

unreal summit
#

it logged nothing

trail harbor
#

where did you put the console.log

unreal summit
#
Collector.on("collect", async (i) => {
    console.log(i);
    if (i.customId === "8ball") {
      return await i.update({ embeds: [AnswerEmbed], components: [] });
    }
  });```
trail harbor
#

you get no console output?

unreal summit
#

nope

trail harbor
#

are you clicking the button

unreal summit
#

well yeah lmfao

trail harbor
#

okay well it would appear that the collector isn't running at all

#

log Collector

odd daggerBOT
#

If you aren't getting any errors, try to place console.log checkpoints throughout your code to find out where execution stops.

  • Once you do, log relevant values and if-conditions
  • More sophisticated debugging methods are breakpoints and runtime inspections: learn more
unreal summit
#

i logged it

#

nothing

#

thats why its failing

trail harbor
#

is the message even being sent?

unreal summit
#

no

trail harbor
#

okay... is your bot even online

unreal summit
#

it looks like this when it fails

unreal summit
#

how would it send the message if it wasnt online

trail harbor
#

you said the message wasnt sent though so thats why i asked, but it would appear that it is sending

unreal summit
#

ohh i thought u ment the answer embed

trail harbor
#

no, but thats okay.

please log Reply

unreal summit
#

okay i got collector to log

unreal summit
trail harbor
#

i dont need you to do that anymore

#

i dont really believe that you need this return here so only have await i.update(...) and see what happens

unreal summit
# unreal summit okay
  channelId: '1094499496471318528',
  guildId: '1006727521166970900',
  id: '1129584470647914588',
  createdTimestamp: 1689384324467,
  type: 0,
  system: false,
  content: '',
  author: ClientUser {
    id: '1091829957614899290',
    bot: true,
    system: false,
    flags: UserFlagsBitField { bitfield: 0 },
    username: 'Impresso Espresso',
    discriminator: '9634',
    avatar: '8dfce8ea80710f693f2cc8404fe4d5bd',
    banner: null,
    accentColor: null,
    verified: true,
    mfaEnabled: true
  },
  pinned: false,
  tts: false,
  nonce: null,
  embeds: [ Embed { data: [Object] } ],
  components: [ ActionRow { data: [Object], components: [Array] } ],
  attachments: Collection(0) [Map] {},
  stickers: Collection(0) [Map] {},
  position: null,
  roleSubscriptionData: null,
  editedTimestamp: null,
  reactions: ReactionManager { message: [Circular *1] },
  mentions: MessageMentions {
    everyone: false,
    users: Collection(0) [Map] {},
    roles: Collection(0) [Map] {},
    _members: null,
    _channels: null,
    _parsedUsers: null,
    crosspostedChannels: Collection(0) [Map] {},
    repliedUser: null
  },
  webhookId: null,
  groupActivityApplication: null,
  applicationId: null,
  activity: null,
  flags: MessageFlagsBitField { bitfield: 0 },
  reference: null,
  interaction: null
}```
trail harbor
#

you removed that return ?

unreal summit
#

yes and heres my script

#
if (!args[0]) return message.reply({ embeds: [Fail] });
  const Reply = await message.channel.send({ embeds: [QuestionEmbed], components: [Button] });
  const CollectorFilter = async i => {
    if (i.user.id !== message.author.id) {
      await i.reply({ embeds: [NotYourButton], ephemeral: true });
    }
  }
  const Collector = Reply.createMessageComponentCollector({
    componentType: ComponentType.Button,
    filter: CollectorFilter,
    time: 10000
  });
  Collector.on("collect", async (i) => {
    if (i.customId === "8ball") {
      await i.update({ embeds: [AnswerEmbed], components: [] });
    }
  });
};```
trail harbor
#

alright and clicking the button still isnt updating the message?

unreal summit
#

nope

#

it says this intercation failed

trail harbor
#

are there any errors with that?

#

i have a feeling it's taking more than 3 seconds to respond

unreal summit
#

no errors! im so confuseddd

trail harbor
#

try this

- await i.update({ embeds: [AnswerEmbed], components: [] });
+ await i.deferUpdate();
+ await i.editReply({ embeds: [AnswerEmbed], components: [] });
unreal summit
#

i just changed that

#

heres my stuff

#

it didnt log anything so why would ur new script work

trail harbor
#

okay the filter might be causing some conflicts here, but it really shouldn't

#

i want you to use this instead and try it

#
const reply = await message.channel.send({ embeds: [QuestionEmbed], components: [Button] });
const collector = reply.createMessageComponentCollector({
    componentType: ComponentType.Button,
    time: 10000
  });
  collector.on("collect", async (i) => {
 if (i.user.id === message.author.id) {
    if (i.customId === "8ball") {
      await i.update({ embeds: [AnswerEmbed], components: [] });
      }
  } else {
    await i.reply({ embeds: [NotYourButton], ephemeral: true });
  }
});
#

this removes the filter and checks inside the collect event instead

#

it's better practice to use lowercase names for variables since uppercase names can sometimes conflict with other Structures from packages

odd daggerBOT
#

guide Message Components: Component collectors
read more

unreal summit
#

@trail harbor

#

error

trail harbor
#

i added an extra bracket on accident. you should be able to fix syntax errors very easily though

unreal summit
#

lemme see if it works

unreal summit
#

@trail harborit wasnt working for a bit but i had to change some stuff and it worked