如何检查我的 Discord.Net 机器人是否已连接到语音通道
How do I check if my Discord.Net bot is already connected to a Voice Channel
我的机器人可以毫无问题地加入和离开语音频道,但我如何验证它是否已经连接到语音聊天?
我的音频服务 .cs 文件中的代码
public async Task<IAudioClient> ConnecttoVC(SocketCommandContext ctx)
{
SocketGuildUser user = ctx.User as SocketGuildUser;
IVoiceChannel chnl = user.VoiceChannel;
if(chnl == null)
{
await ctx.Channel.SendMessageAsync("Not connected!");
return null;
}
await ctx.Channel.SendMessageAsync("Joining Voice!");
return await chnl.ConnectAsync();
}
所以我使用 .CurrentUser
的 VoiceChannel
属性 检查机器人是否已连接,我遇到了机器人同时加入和离开或不加入的问题根本。所以我决定只向我的 class 添加一个 bool 变量,它对我有用。
private bool voiceCheck;
public AudioService() { voiceCheck = false; }
public async Task<IAudioClient> ConnecttoVC(SocketCommandContext ctx)
{
SocketGuildUser user = ctx.User as SocketGuildUser;
IVoiceChannel chnl = user.VoiceChannel;
if(chnl == null)
{
await ctx.Channel.SendMessageAsync("Not connected!");
return null;
}
if(voiceCheck == true)
{
await ctx.Channel.SendMessageAsync("Already connected!");
return null;
}
await ctx.Channel.SendMessageAsync("Joining Voice!");
voiceCheck = true;
return await chnl.ConnectAsync();
}
在我的 ConnecttoVC
和 Leave
(省略)方法之间,voiceCheck 会在分别检查每个方法时在 true 和 false 之间切换。
我知道你说你在使用 CurrentUser
的 VoiceChannel
属性 加入和离开机器人时遇到问题,但我需要我的机器人知道它是否在同一个channel 作为执行命令的用户。所以我采用了这种方法,到目前为止在测试时没有遇到任何问题。我想我会在这里为任何试图弄清楚如何做到这一点的人分享我的代码。我已经对代码进行了注释,因此它应该很容易理解。
public async Task<IAudioClient> ConnectAudio(SocketCommandContext context)
{
SocketGuildUser user = context.User as SocketGuildUser; // Get the user who executed the command
IVoiceChannel channel = user.VoiceChannel;
bool shouldConnect = false;
if (channel == null) // Check if the user is in a channel
{
await context.Message.Channel.SendMessageAsync("Please join a voice channel first.");
} else
{
var clientUser = await context.Channel.GetUserAsync(context.Client.CurrentUser.Id); // Find the client's current user (I.e. this bot) in the channel the command was executed in
if (clientUser != null)
{
if (clientUser is IGuildUser bot) // Cast the client user so we can access the VoiceChannel property
{
if (bot.VoiceChannel == null)
{
Console.WriteLine("Bot is not in any channels");
shouldConnect = true;
}
else if (bot.VoiceChannel.Id == channel.Id)
{
Console.WriteLine($"Bot is already in requested channel: {bot.VoiceChannel.Name}");
}
else
{
Console.WriteLine($"Bot is in channel: {bot.VoiceChannel.Name}");
shouldConnect = true;
}
}
}
else
{
Console.WriteLine($"Unable to find bot in server: {context.Guild.Name}");
}
}
return (shouldConnect ? await channel.ConnectAsync() : null); // Return the IAudioClient or null if there was no need to connect
}
我的机器人可以毫无问题地加入和离开语音频道,但我如何验证它是否已经连接到语音聊天?
我的音频服务 .cs 文件中的代码
public async Task<IAudioClient> ConnecttoVC(SocketCommandContext ctx)
{
SocketGuildUser user = ctx.User as SocketGuildUser;
IVoiceChannel chnl = user.VoiceChannel;
if(chnl == null)
{
await ctx.Channel.SendMessageAsync("Not connected!");
return null;
}
await ctx.Channel.SendMessageAsync("Joining Voice!");
return await chnl.ConnectAsync();
}
所以我使用 .CurrentUser
的 VoiceChannel
属性 检查机器人是否已连接,我遇到了机器人同时加入和离开或不加入的问题根本。所以我决定只向我的 class 添加一个 bool 变量,它对我有用。
private bool voiceCheck;
public AudioService() { voiceCheck = false; }
public async Task<IAudioClient> ConnecttoVC(SocketCommandContext ctx)
{
SocketGuildUser user = ctx.User as SocketGuildUser;
IVoiceChannel chnl = user.VoiceChannel;
if(chnl == null)
{
await ctx.Channel.SendMessageAsync("Not connected!");
return null;
}
if(voiceCheck == true)
{
await ctx.Channel.SendMessageAsync("Already connected!");
return null;
}
await ctx.Channel.SendMessageAsync("Joining Voice!");
voiceCheck = true;
return await chnl.ConnectAsync();
}
在我的 ConnecttoVC
和 Leave
(省略)方法之间,voiceCheck 会在分别检查每个方法时在 true 和 false 之间切换。
我知道你说你在使用 CurrentUser
的 VoiceChannel
属性 加入和离开机器人时遇到问题,但我需要我的机器人知道它是否在同一个channel 作为执行命令的用户。所以我采用了这种方法,到目前为止在测试时没有遇到任何问题。我想我会在这里为任何试图弄清楚如何做到这一点的人分享我的代码。我已经对代码进行了注释,因此它应该很容易理解。
public async Task<IAudioClient> ConnectAudio(SocketCommandContext context)
{
SocketGuildUser user = context.User as SocketGuildUser; // Get the user who executed the command
IVoiceChannel channel = user.VoiceChannel;
bool shouldConnect = false;
if (channel == null) // Check if the user is in a channel
{
await context.Message.Channel.SendMessageAsync("Please join a voice channel first.");
} else
{
var clientUser = await context.Channel.GetUserAsync(context.Client.CurrentUser.Id); // Find the client's current user (I.e. this bot) in the channel the command was executed in
if (clientUser != null)
{
if (clientUser is IGuildUser bot) // Cast the client user so we can access the VoiceChannel property
{
if (bot.VoiceChannel == null)
{
Console.WriteLine("Bot is not in any channels");
shouldConnect = true;
}
else if (bot.VoiceChannel.Id == channel.Id)
{
Console.WriteLine($"Bot is already in requested channel: {bot.VoiceChannel.Name}");
}
else
{
Console.WriteLine($"Bot is in channel: {bot.VoiceChannel.Name}");
shouldConnect = true;
}
}
}
else
{
Console.WriteLine($"Unable to find bot in server: {context.Guild.Name}");
}
}
return (shouldConnect ? await channel.ConnectAsync() : null); // Return the IAudioClient or null if there was no need to connect
}