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
The button interaction has a channel and channelId property you can use to check if the channel is the target channel
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 themember.voice.channel
property.Here’s the modified code:
I hope this helps!