diff --git a/ChaosBot/WebServer/App/DiscordController.cs b/ChaosBot/WebServer/App/DiscordController.cs index fa7a594..6a2b8a5 100644 --- a/ChaosBot/WebServer/App/DiscordController.cs +++ b/ChaosBot/WebServer/App/DiscordController.cs @@ -1,8 +1,12 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; +using ChaosBot.Discord; +using ChaosBot.Repositories; +using Discord; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; @@ -49,6 +53,25 @@ namespace ChaosBot.WebServer.App return LocalRedirect($"/#/?access_token={responseObject.access_token}"); } + [HttpGet("invite")] + public IActionResult Invite() + { + string clientId = Program.AppSettingsHandler.GetValue("Discord:ClientId"); + const ulong permissions = + 0x00000020 + // MANAGE_CHANNELS + 0x04000000 + // CHANGE_NICKNAME + 0x00010000 + // READ_MESSAGE_HISTORY + 0x00000800 + // SEND_MESSAGES + 0x00002000 + // MANAGE_MESSAGES + 0x00008000 + // ATTACH_FILES + 0x00040000 + // USE_EXTERNAL_EMOJIS + 0x00000040 + // ADD_REACTIONS + 0x00000400 // VIEW_CHANNEL + ; + + return Redirect($"https://discord.com/oauth2/authorize?client_id={clientId}&scope=bot&permissions={permissions}"); + } + [HttpGet("user")] public IActionResult GetUser() { @@ -77,7 +100,13 @@ namespace ChaosBot.WebServer.App string responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); List userResponse = JsonConvert.DeserializeObject>(responseString); - return Json(userResponse); + List presentGuilds = DiscordConnect._client.Guilds.Select(g => g.Id).ToList(); + List userResponseWithPresence = + userResponse + .Select(guild => guild.AddPresence(presentGuilds.Contains(guild.id))) + .ToList(); + + return Json(userResponseWithPresence); } private static DiscordUserResponse GetDiscordUser(string accessToken) @@ -127,11 +156,30 @@ namespace ChaosBot.WebServer.App public class DiscordGuildResponse { - public string id { get; set; } + public ulong id { get; set; } public string name { get; set; } public string icon { get; set; } public bool owner { get; set; } public int permissions { get; set; } public int permissions_new { get; set; } + + public DiscordGuildResponseWithPresence AddPresence(bool presence) + { + return new DiscordGuildResponseWithPresence + { + id = id, + name = name, + icon = icon, + owner = owner, + permissions = permissions, + permissions_new = permissions_new, + bot_present = presence + }; + } + } + + public class DiscordGuildResponseWithPresence : DiscordGuildResponse + { + public bool bot_present { get; set; } } }