private async Task CreateVoiceChannelAndMoveAsync(string clientId, int?toFrequency) { var connection = await discordConnectionStorage.GetConnectionAsync(clientId); if (connection == null) { return; } SocketGuildUser guildUser = null; DiscordServerOptions serverOptions = null; foreach (var options in discordOptions.Servers) { guildUser = botClient.Guilds.SingleOrDefault(o => o.Id == options.ServerId)?.GetUser(connection.UserId); serverOptions = options; if (guildUser?.VoiceChannel != null) { break; } } if (guildUser == null) { logger.LogDebug("Cannot find connected user {userId} in any server!", connection.UserId); return; } if (guildUser.VoiceChannel?.CategoryId != serverOptions.ChannelCategoryId) { // Do not touch user not connecting to voice or connecting outside the channel logger.LogDebug("Cannot move because connected user {userId} is in another voice channel category {categoryId}!", connection.UserId, guildUser.VoiceChannel?.CategoryId); return; } var guild = guildUser.Guild; var channel = await channelMaker.CreateVoiceChannelAsync(serverOptions, guild, toFrequency); await MoveMemberAsync(guildUser, channel); }
private async Task CreateVoiceChannelAndMoveAsync(string clientId, int?toFrequency) { var connection = await discordConnectionStorage.GetConnectionAsync(clientId); if (connection == null) { return; } SocketGuildUser guildUser = null; DiscordServerOptions serverOptions = null; foreach (var options in discordOptions.Servers) { guildUser = botClient.Guilds.SingleOrDefault(o => o.Id == options.ServerId)?.GetUser(connection.UserId); serverOptions = options; if (guildUser?.VoiceChannel != null) { break; } } if (guildUser == null) { return; } if (guildUser.VoiceChannel?.CategoryId != serverOptions.ChannelCategoryId) { // Do not touch user not connecting to voice or connecting outside the channel return; } var guild = guildUser.Guild; var channelName = toFrequency.HasValue ? (toFrequency.Value / 1000d).ToString("N3") : serverOptions.LoungeChannelName; var channel = guild.Channels.FirstOrDefault(c => c.Name == channelName); if (channel == null) { var voiceChannel = await guild.CreateVoiceChannelAsync(channelName, props => { props.CategoryId = serverOptions.ChannelCategoryId; props.Bitrate = serverOptions.ChannelBitrate; }); try { await voiceChannel.AddPermissionOverwriteAsync(guild.EveryoneRole, new OverwritePermissions(useVoiceActivation : PermValue.Deny)); } catch (Exception ex) { logger.LogError(ex, "Cannot change channel permission"); } logger.LogInformation("Created new channel {channelName}", channelName); await MoveMemberAsync(guildUser, voiceChannel); } else { await MoveMemberAsync(guildUser, channel); } }
private async Task CreateVoiceChannelAndMoveAsync(string clientId, int?toFrequency) { var connection = await discordConnectionStorage.GetConnectionAsync(clientId); if (connection == null) { return; } SocketGuildUser guildUser = null; DiscordServerOptions serverOptions = null; foreach (var options in discordOptions.Servers) { guildUser = botClient.Guilds.SingleOrDefault(o => o.Id == options.ServerId)?.GetUser(connection.UserId); serverOptions = options; if (guildUser?.VoiceChannel != null) { break; } } if (guildUser == null) { return; } if (guildUser.VoiceChannel?.CategoryId != serverOptions.ChannelCategoryId) { // Do not touch user not connecting to voice or connecting outside the channel return; } var guild = guildUser.Guild; var channelName = toFrequency.HasValue ? CreateChannelNameFromFrequency(serverOptions, toFrequency) : serverOptions.LoungeChannelName; var channel = guild.Channels.FirstOrDefault(c => c.Name == channelName); if (channel == null) { var voiceChannel = await guild.CreateVoiceChannelAsync(channelName, props => { props.CategoryId = serverOptions.ChannelCategoryId; props.Bitrate = serverOptions.ChannelBitrate; }); // Note: Bot will not try to add permission. // Instead, permission should be set at the category level so that the channel can inherit. logger.LogInformation("Created new channel {channelName}", channelName); await MoveMemberAsync(guildUser, voiceChannel); } else { await MoveMemberAsync(guildUser, channel); } }