如何通过 api 捕获谁 joined/left 不和谐的语音频道?
How do I capture who joined/left a discord voice channel via api?
我有一个正在开发的机器人,我正在使用 C# 和这个 SDK https://github.com/RogueException/Discord.Net
我正在尝试弄清楚如何根据某人加入语音频道和离开的时间以及它是谁来触发一种方法。 Discord API 频道说我需要使用网关和 "Voice State Update" 事件,但我不太清楚该怎么做。
有没有办法使用 SDK 中的文档 https://discord.foxbot.me/docs/api/index.html 来做到这一点?
我写了一个简短的例子,希望对你有所帮助。
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;
}
我有一个正在开发的机器人,我正在使用 C# 和这个 SDK https://github.com/RogueException/Discord.Net
我正在尝试弄清楚如何根据某人加入语音频道和离开的时间以及它是谁来触发一种方法。 Discord API 频道说我需要使用网关和 "Voice State Update" 事件,但我不太清楚该怎么做。
有没有办法使用 SDK 中的文档 https://discord.foxbot.me/docs/api/index.html 来做到这一点?
我写了一个简短的例子,希望对你有所帮助。
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;
}