users are now renamed after joining the discord server

This commit is contained in:
Sarah Faey 2022-11-21 23:05:10 +01:00
parent 601bd10c39
commit 17d8983fb0
4 changed files with 74 additions and 0 deletions

View file

@ -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;
}
}
}

View file

@ -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<ulong>(){user.Guild.Id});
}
}
}
}
}

View file

@ -171,5 +171,21 @@ namespace DiscordBot.Services
}
return new List<ulong>();
}
public async Task<ApiRaid.Role.User> 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<ApiRaid.Role.User>(contentStream, _serializerOptions);
}
return new ApiRaid.Role.User();
}
}
}

View file

@ -184,5 +184,23 @@ namespace Lieb.Controllers
{
return _discordService.GetUserRenameServers();
}
[HttpGet]
[Route("[action]/{userId}")]
public ActionResult<ApiRaid.Role.User> 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");
}
}
}