diff --git a/Gatekeeper/Commands/WhitelistModule.cs b/Gatekeeper/Commands/WhitelistModule.cs new file mode 100644 index 0000000..fc7b438 --- /dev/null +++ b/Gatekeeper/Commands/WhitelistModule.cs @@ -0,0 +1,42 @@ +using Discord.Interactions; +using Discord.WebSocket; +using Gatekeeper.Models; +using Gatekeeper.Services; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Gatekeeper.Commands +{ + [Group("whitelist", "Remote whitelisting commands available to staff.")] + public class WhitelistModule : InteractionModuleBase + { + HttpClientService _restClient; + DiscordSocketClient _client; + + public WhitelistModule(IServiceProvider services) + { + _client = services.GetRequiredService(); + _restClient = services.GetRequiredService(); + } + + [DefaultMemberPermissions(Discord.GuildPermission.MentionEveryone)] + [SlashCommand("add", "Add a player to the whitelist manually.")] + public async Task AddToWhitelistAsync([Summary("name", "The player's minecraft name.")] string name) + { + CommandResponse commandResponse = _restClient.AddToWhitelist(name).Result; + await RespondAsync(embed: commandResponse.RespondAsEmbed(Context)); + } + + [DefaultMemberPermissions(Discord.GuildPermission.MentionEveryone)] + [SlashCommand("remove", "Remove a player from the whitelist manually.")] + public async Task RemoveFromWhitelistAsync([Summary("name", "The player's minecraft name.")] string name) + { + CommandResponse commandResponse = _restClient.RemoveFromWhitelist(name).Result; + await RespondAsync(embed: commandResponse.RespondAsEmbed(Context)); + } + + } +} diff --git a/Gatekeeper/Data/config.json b/Gatekeeper/Data/config.json index 3d92391..db247a6 100644 --- a/Gatekeeper/Data/config.json +++ b/Gatekeeper/Data/config.json @@ -5,5 +5,7 @@ "database_username": "", "database_ip": "", "database": "", - "password": "" + "password": "", + "apitoken": "", + "apiserveruri": "" } \ No newline at end of file diff --git a/Gatekeeper/Models/BotConfig.cs b/Gatekeeper/Models/BotConfig.cs index 96c94a1..fd9bbe4 100644 --- a/Gatekeeper/Models/BotConfig.cs +++ b/Gatekeeper/Models/BotConfig.cs @@ -21,5 +21,9 @@ public class BotConfig public string Database { get; set; } [JsonProperty("password")] public string Password { get; set; } + [JsonProperty("apitoken")] + public string ApiToken { get; set; } + [JsonProperty("apiserveruri")] + public string ApiServerUri { get; set; } } } diff --git a/Gatekeeper/Models/CommandResponse.cs b/Gatekeeper/Models/CommandResponse.cs new file mode 100644 index 0000000..0f7d25b --- /dev/null +++ b/Gatekeeper/Models/CommandResponse.cs @@ -0,0 +1,59 @@ +using Discord; +using Discord.Interactions; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Gatekeeper.Models +{ + public class CommandResponse + { + public string PlayerName { get; set; } + public string Command { get; set; } + public string Message { get; set; } + public CommandResponse() { } + public CommandResponse(string playerName, string command, string message) + { + PlayerName = playerName; + Command = command; + Message = message; + } + + public Embed RespondAsEmbed(SocketInteractionContext context) + { + EmbedBuilder eb = new EmbedBuilder() + { + Author = new EmbedAuthorBuilder + { + IconUrl = context.Client.CurrentUser.GetAvatarUrl(), + Name = context.Client.CurrentUser.Username + }, + Color = new Color(52, 85, 235), + Title = "Command Executed", + Description = "This message was returned by the API.", + Footer = new EmbedFooterBuilder() + { + Text = "Bot created by @Faceman and @Riki.", + IconUrl = context.Guild.IconUrl + } + }; + eb.AddField(new EmbedFieldBuilder() + { + Name = "Minecraft Name", + Value = PlayerName, + IsInline = true + }).AddField(new EmbedFieldBuilder() + { + Name = "Command Run", + Value = Command, + IsInline = true + }).AddField(new EmbedFieldBuilder() + { + Name = "Message returned from the API", + Value = Message, + IsInline = false + }); + return eb.Build(); + } + } +} diff --git a/Gatekeeper/Program.cs b/Gatekeeper/Program.cs index 29991be..063a411 100644 --- a/Gatekeeper/Program.cs +++ b/Gatekeeper/Program.cs @@ -25,6 +25,7 @@ class Program private RoleService _manager; private BotConfig _token; private DatabaseService _database; + private HttpClientService _restClient; public static void Main() => new Program().MainAsync().GetAwaiter().GetResult(); @@ -40,6 +41,7 @@ public async Task MainAsync() _auditer = services.GetRequiredService(); _manager = services.GetRequiredService(); _database = services.GetRequiredService(); + _restClient = services.GetRequiredService(); _client.Log += Log; _client.Ready += OnReady; @@ -85,6 +87,7 @@ private ServiceProvider ConfigureServices() .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .BuildServiceProvider(); } } diff --git a/Gatekeeper/Services/HttpClientService.cs b/Gatekeeper/Services/HttpClientService.cs new file mode 100644 index 0000000..701cfd7 --- /dev/null +++ b/Gatekeeper/Services/HttpClientService.cs @@ -0,0 +1,53 @@ +using Discord.WebSocket; +using Gatekeeper.Models; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace Gatekeeper.Services +{ + public class HttpClientService + { + private readonly DiscordSocketClient _client; + private readonly IServiceProvider _services; + private readonly ConfigService _config; + private readonly DataService _data; + private HttpClient _restClient; + + public HttpClientService(IServiceProvider services) + { + _client = services.GetRequiredService(); + _config = services.GetRequiredService(); + _data = services.GetRequiredService(); + _restClient = new HttpClient(); + _services = services; + } + + public async Task AddToWhitelist(string name) + { + CommandResponse commandResponse = new CommandResponse(); + string path = String.Format($"{_config.BotConfig.ApiServerUri}/cmd/add/{name}?={_config.BotConfig.ApiToken}"); + HttpResponseMessage response = await _restClient.PostAsJsonAsync(path, commandResponse); + response.EnsureSuccessStatusCode(); + + await response.Content.ReadAsAsync(); + + return commandResponse; + } + + public async Task RemoveFromWhitelist(string name) + { + CommandResponse commandResponse = new CommandResponse(); + string path = String.Format($"{_config.BotConfig.ApiServerUri}/cmd/remove/{name}?={_config.BotConfig.ApiToken}"); + HttpResponseMessage response = await _restClient.PostAsJsonAsync(path, commandResponse); + response.EnsureSuccessStatusCode(); + + commandResponse = await response.Content.ReadAsAsync(); + + return commandResponse; + } + } +}