Add endpoints for CustomCommands

This commit is contained in:
Daniel_I_Am 2020-08-25 21:58:52 +02:00
parent ccff9a40b5
commit bd03e898c7
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
4 changed files with 146 additions and 2 deletions

View File

@ -13,7 +13,7 @@ namespace ChaosBot.Discord
{
public class DiscordConnect
{
private static DiscordSocketClient _client;
public static DiscordSocketClient _client = null;
private static ILogger _logger = Program.Logger;
public static async Task StartUp()
@ -24,7 +24,7 @@ namespace ChaosBot.Discord
{
// get the client and assign to client
// you get the services via GetRequiredService<T>
var client = services.GetRequiredService<DiscordSocketClient>();
DiscordSocketClient client = services.GetRequiredService<DiscordSocketClient>();
_client = client;
// setup logging and the ready event

View File

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChaosBot.Discord;
using ChaosBot.Models;
using ChaosBot.WebServer.Models;
using ChaosBot.WebServer.Services;
using Discord.WebSocket;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace ChaosBot.WebServer.App
{
[ApiController]
[Route("/api/custom-commands")]
public class CustomCommandApi : Controller
{
[HttpGet]
[Route("{guildId}")]
public async Task<IActionResult> GetCustomCommands([FromRoute]ulong guildId)
{
if (!CheckPermissions.GetResult(Request, guildId, out IActionResult result))
return result;
await using ChaosbotContext dbContext = new ChaosbotContext();
IQueryable<CustomCommand> customCommandsQuery = dbContext.CustomCommands;
List<CustomCommand> customCommands = customCommandsQuery
.Where(cc => cc.DiscordGuildId == guildId)
.ToList();
return Json(customCommands);
}
[HttpPost]
[Route("{guildId}")]
public async Task<IActionResult> UpsertCustomCommands([FromRoute]ulong guildId, [FromBody]CustomCommandRequest customCommandRequest)
{
if (!CheckPermissions.GetResult(Request, guildId, out IActionResult result))
return result;
await using ChaosbotContext dbContext = new ChaosbotContext();
CustomCommand customCommand = new CustomCommand
{
DiscordGuildId = guildId,
Command = customCommandRequest.Command,
Type = customCommandRequest.Type,
Content = customCommandRequest.Content
};
await dbContext.CustomCommands.Upsert(customCommand)
.On(cc => new {cc.DiscordGuildId, cc.Command}).RunAsync();
await dbContext.SaveChangesAsync();
return NoContent();
}
[HttpDelete]
[Route("{guildId}")]
public async Task<IActionResult> DeleteCustomCommands([FromRoute]ulong guildId, [FromBody]CustomCommandRequest customCommandRequest)
{
if (!CheckPermissions.GetResult(Request, guildId, out IActionResult result))
return result;
await using ChaosbotContext dbContext = new ChaosbotContext();
CustomCommand customCommand = new CustomCommand
{
DiscordGuildId = guildId,
Command = customCommandRequest.Command,
Type = customCommandRequest.Type,
Content = customCommandRequest.Content
};
dbContext.CustomCommands.Remove(customCommand);
await dbContext.SaveChangesAsync();
return NoContent();
}
}
}

View File

@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
using ChaosBot.Models;
namespace ChaosBot.WebServer.Models
{
#region Required
public class CustomCommandRequest
{
[Required]
[MaxLength(128)]
public string Command { get; set; }
[Required]
public CustomCommandType Type { get; set; }
[Required]
public string Content { get; set; }
}
#endregion
}

View File

@ -0,0 +1,45 @@
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;
}
}
}