diff --git a/DiscordBot/CommandHandler.cs b/DiscordBot/CommandHandler.cs index 475997a..cbee1af 100644 --- a/DiscordBot/CommandHandler.cs +++ b/DiscordBot/CommandHandler.cs @@ -18,6 +18,7 @@ namespace DiscordBot private readonly ButtonHandler _buttonHandler; private readonly SelectMenuHandler _selectMenuHandler; private readonly ModalHandler _modalHandler; + private readonly UserHandler _userHandler; // Retrieve client and CommandService instance via ctor public CommandHandler(DiscordSocketClient client, CommandService commands, HttpService httpService) @@ -29,6 +30,7 @@ namespace DiscordBot _buttonHandler = new ButtonHandler(_httpService); _selectMenuHandler = new SelectMenuHandler(_httpService); _modalHandler = new ModalHandler(_client, _httpService); + _userHandler = new UserHandler(_client, _httpService); } public async Task InstallCommandsAsync() @@ -37,6 +39,7 @@ namespace DiscordBot _client.ButtonExecuted += _buttonHandler.Handler; _client.SelectMenuExecuted += _selectMenuHandler.Handler; _client.ModalSubmitted += _modalHandler.Handler; + _client.UserJoined += _userHandler.HandleUserJoined; } } } diff --git a/DiscordBot/CommandHandlers/UserHandler.cs b/DiscordBot/CommandHandlers/UserHandler.cs new file mode 100644 index 0000000..26bc301 --- /dev/null +++ b/DiscordBot/CommandHandlers/UserHandler.cs @@ -0,0 +1,37 @@ +using Discord; +using Discord.Commands; +using Discord.WebSocket; +using System.Reflection; +using DiscordBot.Services; +using SharedClasses.SharedModels; +using DiscordBot.Messages; + +namespace DiscordBot.CommandHandlers +{ + public class UserHandler + { + private readonly DiscordSocketClient _client; + private readonly HttpService _httpService; + private readonly HandlerFunctions _handlerFunctions; + + public UserHandler(DiscordSocketClient client,HttpService httpService) + { + _client = client; + _httpService = httpService; + _handlerFunctions = new HandlerFunctions(_httpService); + } + + public async Task HandleUserJoined(SocketGuildUser user) + { + if((await _httpService.GetUserRenameServers()).Contains(user.Guild.Id)) + { + if(await _httpService.DoesUserExist(user.Id)) + { + ApiRaid.Role.User apiUser = await _httpService.GetUser(user.Id); + await HandlerFunctions.RenameUser(_client, user.Id, apiUser.UserName, apiUser.AccountName, new List(){user.Guild.Id}); + } + } + } + + } +} \ No newline at end of file diff --git a/DiscordBot/Services/HttpService.cs b/DiscordBot/Services/HttpService.cs index 06e06a1..01a0507 100644 --- a/DiscordBot/Services/HttpService.cs +++ b/DiscordBot/Services/HttpService.cs @@ -171,5 +171,21 @@ namespace DiscordBot.Services } return new List(); } + + public async Task GetUser(ulong userId) + { + var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); + + var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/GetUser/{userId}"); + + if (httpResponseMessage.IsSuccessStatusCode) + { + using var contentStream = + await httpResponseMessage.Content.ReadAsStreamAsync(); + + return await JsonSerializer.DeserializeAsync(contentStream, _serializerOptions); + } + return new ApiRaid.Role.User(); + } } } \ No newline at end of file diff --git a/Lieb/Controllers/DiscordBotController.cs b/Lieb/Controllers/DiscordBotController.cs index 1a2653f..962f2f7 100644 --- a/Lieb/Controllers/DiscordBotController.cs +++ b/Lieb/Controllers/DiscordBotController.cs @@ -184,5 +184,23 @@ namespace Lieb.Controllers { return _discordService.GetUserRenameServers(); } + + [HttpGet] + [Route("[action]/{userId}")] + public ActionResult GetUser(ulong userId) + { + LiebUser user = _userService.GetLiebUser(userId); + + if(user != null) + { + return Ok(new ApiRaid.Role.User(){ + UserId = user.Id, + UserName = user.Name, + AccountName = user.GuildWars2Accounts.FirstOrDefault(a => a.GuildWars2AccountId == user.MainGW2Account, new GuildWars2Account()).AccountName + }); + } + + return Problem("user not found"); + } } } \ No newline at end of file