public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { app.Run(async context => { string response = string.Empty; if (BotInstance is DiscordBot discordBot) { if (context.Request.Query["guilds"] == "1" && BotInstance != null) { response = string.Join($",{Environment.NewLine}", discordBot.Client.Guilds.OrderByDescending(g => g.Users.Count).Select(g => $"{g.Id} | {g.Name} | {g.Users.Count}")); } else if (!string.IsNullOrEmpty(context.Request.Query["guildId"]) && BotInstance != null) { if (ulong.TryParse(context.Request.Query["guildId"], out ulong guildId)) { var guild = discordBot.Client.GetGuild(guildId); if (guild != null) { var channelsResponse = new GuildPermisssionsData(); var botGuildUser = guild.CurrentUser; foreach (var chan in guild.Channels) { if (chan.Name != null) { var channelPermissions = botGuildUser.GetPermissions(chan); channelsResponse.Channels.Add(chan.Id, new GuildChannelPermissions { CanRead = channelPermissions.ReadMessages, CanSend = channelPermissions.SendMessages }); } } response = JsonConvert.SerializeObject(channelsResponse); } } } } // if empty, response was not handled if (string.IsNullOrEmpty(response)) { response = $"Online{Environment.NewLine}"; } context.Response.ContentLength = Encoding.UTF8.GetByteCount(response); context.Response.ContentType = "text/plain"; try { await context.Response.WriteAsync(response); } catch (Exception e) { Console.WriteLine(e); } }); }
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { app.Run(async context => { string response = string.Empty; try { if (BotInstance is DiscordBot discordBot && BotConfig.Instance.WebListenerInboundAddresses.Contains(context.Connection.RemoteIpAddress.ToString())) { if (context.Request.Query["guilds"] == "1") { response = string.Join($",{Environment.NewLine}", discordBot.Client.Guilds.OrderByDescending(g => g.MemberCount).Select(g => $"{g.Id} | {g.Name} | {g.MemberCount}")); } else if (ulong.TryParse(context.Request.Query["guildId"], out ulong guildId)) { var guild = discordBot.Client.GetGuild(guildId); if (guild != null) { var channelsResponse = new GuildPermisssionsData(); var botGuildUser = guild.CurrentUser; var channels = guild.Channels.Where(c => c is ITextChannel || c is IVoiceChannel); foreach (var chan in channels) { var channelPermissions = botGuildUser.GetPermissions(chan); channelsResponse.Channels.Add(chan.Id, new GuildChannelPermissions { CanRead = channelPermissions.ViewChannel, CanSend = channelPermissions.SendMessages, CanEmbed = channelPermissions.EmbedLinks }); } foreach (var emoji in guild.Emotes) { channelsResponse.Emoji.Add(emoji.Id, new EmojiData { Name = emoji.Name, Url = emoji.Url }); } channelsResponse.HighestRolePosition = botGuildUser.Roles.OrderByDescending(r => r.Position).FirstOrDefault()?.Position ?? 0; response = JsonConvert.SerializeObject(channelsResponse); } } else { response = Assembly.GetEntryAssembly().GetName().Version.ToString(); } } } catch (Exception ex) { Log.Error(ex, "Exception in kesteral handler"); } // if empty, response was not handled if (string.IsNullOrEmpty(response)) { response = $"Online{Environment.NewLine}"; } context.Response.ContentLength = Encoding.UTF8.GetByteCount(response); context.Response.ContentType = "text/plain"; try { await context.Response.WriteAsync(response); } catch (Exception ex) { Log.Error(ex, "Error writing response in kestral handler"); } }); }