Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Gatekeeper/Commands/WhitelistModule.cs
Original file line number Diff line number Diff line change
@@ -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<SocketInteractionContext>
{
HttpClientService _restClient;
DiscordSocketClient _client;

public WhitelistModule(IServiceProvider services)
{
_client = services.GetRequiredService<DiscordSocketClient>();
_restClient = services.GetRequiredService<HttpClientService>();
}

[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));
}

}
}
4 changes: 3 additions & 1 deletion Gatekeeper/Data/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"database_username": "",
"database_ip": "",
"database": "",
"password": ""
"password": "",
"apitoken": "",
"apiserveruri": ""
}
4 changes: 4 additions & 0 deletions Gatekeeper/Models/BotConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
59 changes: 59 additions & 0 deletions Gatekeeper/Models/CommandResponse.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
3 changes: 3 additions & 0 deletions Gatekeeper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -40,6 +41,7 @@ public async Task MainAsync()
_auditer = services.GetRequiredService<AuditService>();
_manager = services.GetRequiredService<RoleService>();
_database = services.GetRequiredService<DatabaseService>();
_restClient = services.GetRequiredService<HttpClientService>();

_client.Log += Log;
_client.Ready += OnReady;
Expand Down Expand Up @@ -85,6 +87,7 @@ private ServiceProvider ConfigureServices()
.AddSingleton<AuditService>()
.AddSingleton<RoleService>()
.AddSingleton<DatabaseService>()
.AddSingleton<HttpClientService>()
.BuildServiceProvider();
}
}
Expand Down
53 changes: 53 additions & 0 deletions Gatekeeper/Services/HttpClientService.cs
Original file line number Diff line number Diff line change
@@ -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<DiscordSocketClient>();
_config = services.GetRequiredService<ConfigService>();
_data = services.GetRequiredService<DataService>();
_restClient = new HttpClient();
_services = services;
}

public async Task<CommandResponse> 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<CommandResponse>();

return commandResponse;
}

public async Task<CommandResponse> 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<CommandResponse>();

return commandResponse;
}
}
}