如何检查discord.js中特定语音通道中是否存在具有特定角色的成员

时间:2020-08-01 04:52:20

标签: discord.js

我正在尝试在特定的语音频道中是否存在具有特定角色ID的成员。如果在该特定语音通道中甚至只有一个具有特定角色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语句中究竟要写什么才能使我的代码正常工作。

2 个答案:

答案 0 :(得分:1)

因此,据我所知,您希望该命令仅在角色为“示例角色”的用户在“示例频道”频道中时才执行

为此,您需要角色 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")
    }
});

答案 1 :(得分:0)

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");
    };
});