如何捕获通过api加入/离开不和谐语音通道的人?

时间:2018-03-13 19:51:53

标签: discord discord.net

我有一个我正在研究的机器人,我正在使用C#和这个SDK https://github.com/RogueException/Discord.Net

我正在试图弄清楚如何根据某人加入语音频道并根据人员离开的方式启动方法。 Discord API频道说我需要使用网关和“语音状态更新”事件,但我不清楚如何做到这一点。

有没有办法使用SDK中的文档https://discord.foxbot.me/docs/api/index.html来完成?

1 个答案:

答案 0 :(得分:0)

我写了一个简短的示例,希望对您有所帮助。

private static Task Example(SocketUser user, SocketVoiceState oldVoiceState, SocketVoiceState newVoiceState)
        {
            if(oldVoiceState.VoiceChannel == null && newVoiceState.VoiceChannel != null)
            {
                //User joined
                Console.WriteLine($"User (Name: {user.Username} ID: {user.Id}) joined to a VoiceChannel (Name: {newVoiceState.VoiceChannel.Name} ID: {newVoiceState.VoiceChannel.Id})");
            }
            if (oldVoiceState.VoiceChannel != null && newVoiceState.VoiceChannel == null)
            {
                //User left
                Console.WriteLine($"User (Name: {user.Username} ID: {user.Id}) left from a VoiceChannel (Name: {oldVoiceState.VoiceChannel.Name} ID: {oldVoiceState.VoiceChannel.Id})");
            }
            return Task.CompletedTask;
        }