skip to Main Content

The code is a response after a user clicked on a button with id "pohovor_request". The bot should check if there is any user with a specific role on server (that works) and the second IF should check if the user that clicked on the button is in a specific channel.

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isButton()) return;
  const { customId, user, guild } = interaction;
  if (customId === 'pohovor_request') {
    const targetRoleID = roles['aktivniSpravcePohovoru'];
    const targetChannelID = channels['cekarna'];
    
    const role = guild.roles.cache.get(targetRoleID);
    if (!role || !guild.members.cache.filter(m => m.roles.cache.has(role.id)).size) {
      await interaction.reply('Je nám líto, ale momentálně na serveru není nikdo s požadovanou rolí.');
      return;
    }
    
    const channel = guild.channels.cache.get(targetChannelID);
    if (!channel || !channel.members.has(user.id)) {
      await interaction.reply('Je nám líto, ale musíte být připojeni ve specifickém hlasovém kanálu pro žádost o pohovor.');
      return;
    }
    
    console.log("Ok");
    
  }
});

When i tested the code, the bot always responed Ok, even tho i was ort wasnt in the specific channel.

2

Answers


  1. The button interaction has a channel and channelId property you can use to check if the channel is the target channel

    client.on('interactionCreate', async (interaction) => {
        if (!interaction.isButton()) return;
        const { customId, user, guild, channelId } = interaction;
        if (customId === 'pohovor_request') {
            const targetRoleID = roles['aktivniSpravcePohovoru'];
            const targetChannelID = channels['cekarna'];
    
            const role = guild.roles.cache.get(targetRoleID);
            if (!role || !guild.members.cache.filter(m => m.roles.cache.has(role.id)).size) {
                await interaction.reply('Je nám líto, ale momentálně na serveru není nikdo s požadovanou rolí.');
                return;
            }
    
            const channel = guild.channels.cache.get(targetChannelID);
            if (!channel || channelId !== targetChannelID) {
                await interaction.reply('Je nám líto, ale musíte být připojeni ve specifickém hlasovém kanálu pro žádost o pohovor.');
                return;
            }
    
            console.log("Ok");
        }
    });
    
    Login or Signup to reply.
  2. The user.id refers to the ID of the user who clicked the button, not the ID of a GuildMember.

    You can use the guild.members.fetch() method to fetch the member object for the user, and then check if the member is in the specified channel using the member.voice.channel property.

    Here’s the modified code:

    client.on('interactionCreate', async (interaction) => {
      if (!interaction.isButton()) return;
      const { customId, user, guild } = interaction;
      if (customId === 'pohovor_request') {
        const targetRoleID = roles['aktivniSpravcePohovoru'];
        const targetChannelID = channels['cekarna'];
        
        const role = guild.roles.cache.get(targetRoleID);
        if (!role || !guild.members.cache.filter(m => m.roles.cache.has(role.id)).size) {
          await interaction.reply('Je nám líto, ale momentálně na serveru není nikdo s požadovanou rolí.');
          return;
        }
        
        const channel = guild.channels.cache.get(targetChannelID);
        // Fetch the guild member using the user ID.
        const member = await guild.members.fetch(user.id);
        
        if (!channel || member.voice.channelId !== targetChannelID) {
          await interaction.reply('Je nám líto, ale musíte být připojeni ve specifickém hlasovém kanálu pro žádost o pohovor.');
          return;
        }
        
        console.log("Ok");
      }
    });
    

    I hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search