简体   繁体   English

我如何获得机器人频道的频道 ID 和消息作者所在的频道

[英]How do i get just the channel id of the channel the bot and the channel the message author is in

im trying to make a leave command for my bot and im trying to make it so if the person using the command is not in the same voice channel as the bot it will respond and tell them they have to be in the same voice channel and if they are it will leave我正在尝试为我的机器人发出离开命令,并且我正在尝试这样做,所以如果使用该命令的人与机器人不在同一个语音通道中,它将响应并告诉他们他们必须在同一个语音通道中,如果他们会离开

i have this so far but i cant figure out how to get just the id for the channel the bot and and user are in to compare them到目前为止我有这个,但我不知道如何只获取机器人和用户所在频道的 ID 以比较它们

let userVoiceChannel = message.member.voice.connection;
    let crashbotVoiceChannel = Crashbot.voice.connections;

return all the info about the channels the bot and the user are in i just cant figure out how to pick out just the id's using that to compare those返回有关机器人和用户所在频道的所有信息

Crashbot.on('message', async message =>{

    //creates an array called args and removes the first amount of charactors equal to the length of the prefix variable
    let args = message.content.substring(PREFIX.length).split(" ");
    let userVoiceChannel = message.member.voice.connection;
    let crashbotVoiceChannel = Crashbot.voice.connections;

    console.log(userVoiceChannel[0])
    console.log(crashbotVoiceChannel)

    //the switch equals true if the first word after the prefix is "leave"
    switch (args[0]) {
        case 'leave':

        if (userVoiceChannel === null) return; message.reply("You must be a voice channel to use this command");
    
        //if the message author is not in the same voice channel as the bot the bot replies and tells them that that have to be in the same channel to use that command
        if (userVoiceChannel !== crashbotVoiceChannel) {
            message.reply("You must be in the same channel as me to use this command");
        return;
        }

    //if the message author is in the same voice channel as the bot it leaves the channel it is in
    else if (userVoiceChannel === crashbotVoiceChannel) {
        const connection = await message.member.voice.channel.leave();
            message.channel.send("Successfully left the voice channel");
    }
    break;
}
});

Client.voice.connections is returning a Collection with all of the voice connections your bot has among all of the guilds. Client.voice.connections正在返回一个Collection ,其中包含您的机器人在所有公会中的所有语音连接。

You'll have to get the connection for the current guild.您必须获得当前公会的连接。 You can use:您可以使用:

message.guild.me.voice

// Getting the author's voice connection.
const userVoiceChannel = message.member.voice;
// Getting the bot's voice connection.
const botVoiceChannel = message.guild.me.voice;

// Checking if the bot is in a voice channel.
if (!botVoiceChannel.channel) return message.reply("The bot is not in a voice channel.");
// Checking if the member is in a voice channel.
if (!userVoiceChannel.channel) return message.reply("You need to be in a voice channel.");
// Checking if the member is in the same voice channel as the bot.
if (botVoiceChannel.channelID !== userVoiceChannel.channelID) return message.reply("You need to be in the same channel as the bot.");

message.reply("Everything works.");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何获取我的 discord 机器人所在频道的频道 ID 以及消息作者的频道 ID - How do i get the id of the channel the channel my discord bot is in and the channel id of the message author 如何让机器人提及频道? - How do I get a bot to mention a channel? 如何让机器人找到频道所有者/与作者的ID进行比较 - How to make a bot find a channel owner / compare to the author's ID 无论在任何频道中发出命令,我如何通过机器人向特定频道发送消息 - How do i send a message to a specific channel by the bot no matter command is issued in any channel 如何让我的机器人向另一个频道发送消息? - How can I get my bot to send a message to another channel? 如何获取运行 Discord 机器人命令的通道的通道 ID? - How can I get the channel ID of the channel that a Discord bot's command was run in? 如何通过消息 ID 编辑另一个频道内的消息? - How do I edit a message inside of another channel by message ID? 如何获得消息的渠道 - How to get the channel of a message 我希望我的不和谐机器人向用户提供的文本房间发送随机消息 [按频道 ID 或频道名称] - I want my discord bot to send a random message to a text room given by user [by channel id or channel name] DiscordJS Bot - 如何在通道中的另一条消息之前立即获取消息? - DiscordJS Bot - How to get the message immediately before another message in a channel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM