public async Task <IGuildChannel> CreateVoiceChannelAsync(DiscordServerOptions serverOptions, SocketGuild guild, int?frequency) { var channelName = frequency.HasValue ? CreateChannelNameFromFrequency(serverOptions, frequency) : serverOptions.LoungeChannelName; var channel = guild.Channels.FirstOrDefault(c => c.Name == channelName); if (channel != null) { return(channel); } 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); return(voiceChannel); }
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 HandleStartCommandAsync(Match match, SocketMessage message, SocketTextChannel channel, DiscordServerOptions serverOptions) { if (double.TryParse(match.Groups[1].Value, out var frequency)) { if (message.Attachments.Count > 0) { Task.Run(async() => { await ProcessBotRequestAsync(message, channel, serverOptions, frequency, match.Groups.Count > 2 ? match.Groups[2].Value.Trim() : null); }); } else { await message.Channel.SendMessageAsync("Please attach audio file!"); } } else { await message.Channel.SendMessageAsync("Invalid frequency!"); } }
private async Task ProcessBotRequestAsync(SocketMessage message, SocketTextChannel channel, DiscordServerOptions serverOptions, double frequency, string nickname) { var response = await message.Channel.SendMessageAsync("", embed : new EmbedBuilder().WithDescription("⬜ Loading the audio...").Build()); var filePath = await SaveAudioAsync(message); await response.ModifyAsync(props => { props.Embed = new EmbedBuilder().WithDescription("✅ Audio loaded\n⬜ Creating channel...").Build(); }); var voiceChannel = await channelMaker.CreateVoiceChannelAsync(serverOptions, channel.Guild, (int)(frequency * 1000)); await response.ModifyAsync(props => { props.Embed = new EmbedBuilder().WithDescription("✅ Audio loaded\n✅ Channel created\n⬜ Activating ATIS bot...").Build(); }); try { await atisProcessManager.StartAtisAsync(voiceChannel, filePath, nickname); await response.ModifyAsync(props => { props.Embed = new EmbedBuilder().WithDescription("✅ Audio loaded\n✅ Channel created\n✅ ATIS Bot activated").Build(); }); } catch (Exception ex) { logger.LogError(ex, "Cannot start process"); await response.ModifyAsync(props => { props.Embed = new EmbedBuilder().WithDescription($"✅ Audio loaded\n✅ Channel created\n🟥 Failed to create bot. {ex.Message}").Build(); }); } }
private async Task HandleStopCommandAsync(Match match, SocketMessage message, SocketGuild guild, DiscordServerOptions serverOptions) { if (double.TryParse(match.Groups[1].Value, out var frequency)) { var channelName = channelMaker.CreateChannelNameFromFrequency(serverOptions, (int)(frequency * 1000)); if (await atisProcessManager.StopAtisAsync(guild.Id, channelName)) { await message.Channel.SendMessageAsync("", embed : new EmbedBuilder().WithDescription($"⏹ ATIS on channel {channelName} is stopped.").Build()); } else { await message.Channel.SendMessageAsync("", embed : new EmbedBuilder().WithDescription($"🟥 Cannot find any running bot on {channelName}!").Build()); } } else { await message.Channel.SendMessageAsync("Invalid frequency!"); } }
public string CreateChannelNameFromFrequency(DiscordServerOptions serverOptions, int?toFrequency) => (toFrequency.Value / 1000d).ToString("N3") + (serverOptions.ChannelNameSuffix ?? "");
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); } }
private bool IsExtraChannel(SocketVoiceChannel channel, DiscordServerOptions serverOptions) => channel.CategoryId == serverOptions.ChannelCategoryId && (string.IsNullOrWhiteSpace(serverOptions.LoungeChannelName) || channel.Name != serverOptions.LoungeChannelName) && (serverOptions.LoungeChannelId == null || channel.Id != serverOptions.LoungeChannelId.Value) && (serverOptions.ExternalChannelIds == null || !serverOptions.ExternalChannelIds.Contains(channel.Id));