【问题标题】:How to check if members having a particular role are present in a particular voice channel in discord.js如何检查具有特定角色的成员是否存在于 discord.js 的特定语音通道中
【发布时间】:2020-11-21 21:29:45
【问题描述】:

我正在尝试查找具有特定角色 ID 的任何成员是否存在于特定语音频道中。如果该特定语音频道中甚至存在 1 个具有特定角色 id 的成员,则只有这些成员可以使用音乐机器人的命令。 根据我的研究,我知道voiceStateUpdate 可能会有所帮助,但问题是我不知道如何在我的代码中使用它(因为我是 JavaScript 新手)。这是文档的链接: https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-voiceStateUpdate 。 这是我的代码的一部分:

client.on('voiceStateUpdate', (oldMember, newMember) => {
  });
client.on('message', async message => {
    if (message.author.bot) return
    if (!message.content.startsWith(prefix)) return

    let messageArray = message.content.split(" ");
    let cmd = messageArray[0];
    const args = message.content.substring(prefix.length).split(' ');
    const searchString = args.slice(1).join(' ')
    const url = args[1] ? args[1].replace(/<(._)>/g, '$1') : ''
    const serverQueue = queue.get(message.guild.id)
   
 if() {        //the conditional statement I am trying to put here but I don't know how to do it properly
    if (message.content.startsWith(`${prefix}play`)) {
        const voiceChannel = message.member.voice.channel
        if (!voiceChannel) return message.channel.send("You need to be in a voice channel to play music")
    .......

所以主要是我不知道在 if 语句中确切地写什么才能使我的代码正常工作。

【问题讨论】:

    标签: discord.js


    【解决方案1】:
    client.on("message", async message => {
        // Checking if the message author is a bot.
        if (message.author.bot) return false;
        // Checking if the message was sent in a DM channel.
        if (!message.guild) return false;
    
        // The role that can use the command.
        const Role = message.guild.roles.cache.get("RoleID");
        // The voice channel in which the command can be used.
        const VoiceChannel = message.guild.channels.cache.get("VoiceChannelID");
    
        if (message.content.toLowerCase() == "test") {
            // Checking if the GuildMember is in a VoiceChannel.
            if (!message.member.voice.channel) return message.reply("You need to be in a voice channel.");
            // Checking if the GuildMember is in the required VoiceChannel.
            if (message.member.voice.channelID !== VoiceChannel.id) return message.reply("You are in the wrong VoiceChannel.");
            // Checking if the GuildMember has the required Role.
            if (!message.member.roles.cache.has(Role.id)) return message.reply("You are not allowed to execute this command.");
    
            // Execute your command.
            return message.reply("Command executed");
        };
    });
    

    【讨论】:

    • 感谢您的回复。我希望if (!message.member.roles.cache.has(Role.id)) return message.reply("You are not allowed to execute this command."); 仅在至少有一个具有该角色的成员在该语音通道中时执行。如果具有特定角色的成员不在该语音频道中,则任何成员都可以使用该命令。
    • 您可以遍历VoiceChannel的成员,并检查是否有成员具有角色。
    【解决方案2】:

    据我所知,您希望该命令仅在具有“示例角色”角色的用户位于“示例频道”频道中时执行

    为此,您需要角色 ID 和频道 ID。

    client.on("message", async message => {
        if (message.author.bot) return;
        if (!(message.guild)) return;
    
        var roleID = 'RoleID';
        var vc = message.guild.channels.cache.get("VoiceChannelID");
    
        if (message.content.toLowerCase() == "test") {
    
            canUse = false;
            vc.members.forEach((member) => {
                if (member.roles.has(roleID)) {
                    canUse = True;
                }
            })
            if (!(canUse)) { // nobody in the voice channel has the role specified.
                return;
            }
            console.log("A member of the voice channel has the role specified")
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2020-07-08
      • 2020-10-12
      • 2021-10-25
      • 2023-03-03
      • 2021-07-07
      • 2021-11-17
      • 2020-12-25
      • 2020-10-20
      • 2020-12-05
      相关资源
      最近更新 更多