using System; using System.Linq; using ChaosBot.Discord; using Discord.WebSocket; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace ChaosBot.WebServer.Services { public static class CheckPermissions { private static readonly AccessTokenCache Cache = WebServer.Cache; public static bool GetResult(HttpRequest request, ulong guildId, out IActionResult result) { result = null; if (!request.Cookies.TryGetValue("access_token", out string accessToken)) result = new UnauthorizedObjectResult("No access_token cookie sent"); SocketGuild guild = DiscordConnect._client.GetGuild(guildId); if (guild == null) { result = new NotFoundObjectResult("Bot is not part of that guild"); } else { if (!Cache.HasKey(accessToken)) { result = new NotFoundObjectResult("Could not find your access token in cache, please logout and log back in."); } else { SocketGuildUser user = guild.GetUser(Convert.ToUInt64(Cache.Get(accessToken))); if (user == null) result = new NotFoundObjectResult("Bot could not find you in that guild"); else if (!user.GuildPermissions.Administrator && !user.GuildPermissions.ManageGuild) result = new UnauthorizedObjectResult("You have invalid permissions on the guild. Need at least ManageGuild"); } } return result == null; } } }