public static async Task<bool> CanJoinAndTalkInVoiceChannel(Channel voiceChannel, Channel callback) { if (voiceChannel.Type != ChannelType.Voice) throw new ArgumentException(nameof(voiceChannel)); if (callback.Type != ChannelType.Text) throw new ArgumentException(nameof(callback)); if (!voiceChannel.Server.CurrentUser.GetPermissions(voiceChannel).Speak) { await callback.SafeSendMessage($"I don't have permission to speak in `{voiceChannel.Name}`."); return false; } if (!voiceChannel.CanJoinChannel(voiceChannel.Server.CurrentUser)) { await callback.SafeSendMessage($"I don't have permission to join `{voiceChannel.Name}`"); return false; } return true; }
/// <summary> /// Attempts to join a voice channel. /// </summary> /// <param name="textCallback">Text callback channel to which we will write when we failed joining the audio channel.</param> /// <returns>Null if failed joining.</returns> public static async Task<IAudioClient> SafeJoin(this AudioService audio, Channel voiceChannel, Channel textCallback) { if (voiceChannel.Type != ChannelType.Voice) throw new ArgumentException(nameof(voiceChannel)); if (textCallback.Type != ChannelType.Text) throw new ArgumentException(nameof(textCallback)); if (voiceChannel.CanJoinChannel(voiceChannel.Server.CurrentUser)) { try { return await audio.Join(voiceChannel); } catch { await textCallback.SafeSendMessage($"Failed joining voice channel `{voiceChannel.Name}`."); } } await textCallback.SafeSendMessage($"I don't have permission to join `{voiceChannel.Name}`."); return null; }