From c4ecb512d753f1a81bb1b9fd385fd5e2e509af91 Mon Sep 17 00:00:00 2001 From: Sarah Faey Date: Mon, 21 Nov 2022 18:27:26 +0100 Subject: [PATCH] Added renaming users and main gw2 accounts --- DiscordBot/CommandHandler.cs | 2 +- .../CommandHandlers/HandlerFunctions.cs | 36 +++++++++++++++++++ DiscordBot/CommandHandlers/ModalHandler.cs | 12 +++---- DiscordBot/Controllers/RaidController.cs | 7 ++++ DiscordBot/Services/HttpService.cs | 15 ++++++++ Lieb/Controllers/DiscordBotController.cs | 20 +++++++---- Lieb/Data/DbInitializer.cs | 4 +-- Lieb/Data/DiscordService.cs | 35 ++++++++++++++++++ Lieb/Data/GuildWars2AccountService.cs | 29 +++++++++++++-- Lieb/Data/UserService.cs | 15 +++++++- Lieb/Models/LiebUser.cs | 1 + Lieb/Pages/Raids/RaidOverview/RaidRoles.razor | 4 ++- SharedClasses/SharedModels/ApiRenameUser.cs | 14 ++++++++ 13 files changed, 172 insertions(+), 22 deletions(-) create mode 100644 SharedClasses/SharedModels/ApiRenameUser.cs diff --git a/DiscordBot/CommandHandler.cs b/DiscordBot/CommandHandler.cs index fe29f7c..475997a 100644 --- a/DiscordBot/CommandHandler.cs +++ b/DiscordBot/CommandHandler.cs @@ -28,7 +28,7 @@ namespace DiscordBot _slashCommandHandler = new SlashCommandHandler(_client, _httpService); _buttonHandler = new ButtonHandler(_httpService); _selectMenuHandler = new SelectMenuHandler(_httpService); - _modalHandler = new ModalHandler(_httpService); + _modalHandler = new ModalHandler(_client, _httpService); } public async Task InstallCommandsAsync() diff --git a/DiscordBot/CommandHandlers/HandlerFunctions.cs b/DiscordBot/CommandHandlers/HandlerFunctions.cs index 5f70ccb..b28d9f1 100644 --- a/DiscordBot/CommandHandlers/HandlerFunctions.cs +++ b/DiscordBot/CommandHandlers/HandlerFunctions.cs @@ -39,6 +39,42 @@ namespace DiscordBot.CommandHandlers } return true; } + + public async Task> CreateAccount(SocketInteraction interaction, DiscordSocketClient client, string name, string account) + { + //create Account + ApiRaid.Role.User user = new ApiRaid.Role.User() + { + UserName = name, + AccountName = account, + UserId = interaction.User.Id + }; + Tuple createAccountResult = await _httpService.CreateAccount(user); + if(createAccountResult.Item1) + { + List serverList = await _httpService.GetUserRenameServers(); + await RenameUser(client, interaction.User.Id, name, account, serverList); + } + return createAccountResult; + } + + + public static async Task RenameUser(DiscordSocketClient client, ulong userId, string name, string account, List serverList) + { + string nickname = $"{name} | {account}"; + foreach(ulong serverId in serverList) + { + SocketGuild guild = client.Guilds.FirstOrDefault(g => g.Id == serverId); + if(guild != null) + { + SocketGuildUser user = guild.GetUser(userId); + if(user != null) + { + await user.ModifyAsync(p => p.Nickname = nickname); + } + } + } + } //to avoid error messages because of no response... public async Task Respond(SocketInteraction component) diff --git a/DiscordBot/CommandHandlers/ModalHandler.cs b/DiscordBot/CommandHandlers/ModalHandler.cs index 106adc1..fc43f05 100644 --- a/DiscordBot/CommandHandlers/ModalHandler.cs +++ b/DiscordBot/CommandHandlers/ModalHandler.cs @@ -11,11 +11,13 @@ namespace DiscordBot.CommandHandlers { public class ModalHandler { + private readonly DiscordSocketClient _client; private readonly HttpService _httpService; private readonly HandlerFunctions _handlerFunctions; - public ModalHandler(HttpService httpService) + public ModalHandler(DiscordSocketClient client, HttpService httpService) { + _client = client; _httpService = httpService; _handlerFunctions = new HandlerFunctions(_httpService); } @@ -31,13 +33,7 @@ namespace DiscordBot.CommandHandlers string account = components.First(x => x.CustomId == Constants.ComponentIds.ACCOUNT_TEXT_BOX).Value; //create Account - ApiRaid.Role.User user = new ApiRaid.Role.User() - { - UserName = name, - AccountName = account, - UserId = modal.User.Id - }; - Tuple createAccountResult = await _httpService.CreateAccount(user); + Tuple createAccountResult = await _handlerFunctions.CreateAccount(modal, _client, name, account); if(!createAccountResult.Item1) { await modal.RespondAsync(createAccountResult.Item2, ephemeral: true); diff --git a/DiscordBot/Controllers/RaidController.cs b/DiscordBot/Controllers/RaidController.cs index f9bd003..3587afa 100644 --- a/DiscordBot/Controllers/RaidController.cs +++ b/DiscordBot/Controllers/RaidController.cs @@ -88,5 +88,12 @@ namespace DiscordBot.Controllers await messageChannel.SendMessageAsync(reminder.Message); } } + + [HttpPost] + [Route("[action]")] + public async Task RenameUser(ApiRenameUser user) + { + await DiscordBot.CommandHandlers.HandlerFunctions.RenameUser(_client, user.userId, user.Name, user.Account, user.ServerIds); + } } } \ No newline at end of file diff --git a/DiscordBot/Services/HttpService.cs b/DiscordBot/Services/HttpService.cs index e1a72d9..06e06a1 100644 --- a/DiscordBot/Services/HttpService.cs +++ b/DiscordBot/Services/HttpService.cs @@ -156,5 +156,20 @@ namespace DiscordBot.Services return new ApiRaid(); } + public async Task> GetUserRenameServers() + { + var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); + + var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/GetUserRenameServers"); + + if (httpResponseMessage.IsSuccessStatusCode) + { + using var contentStream = + await httpResponseMessage.Content.ReadAsStreamAsync(); + + return await JsonSerializer.DeserializeAsync>(contentStream, _serializerOptions); + } + return new List(); + } } } \ No newline at end of file diff --git a/Lieb/Controllers/DiscordBotController.cs b/Lieb/Controllers/DiscordBotController.cs index 3badc73..1a2653f 100644 --- a/Lieb/Controllers/DiscordBotController.cs +++ b/Lieb/Controllers/DiscordBotController.cs @@ -18,12 +18,14 @@ namespace Lieb.Controllers RaidService _raidService; UserService _userService; GuildWars2AccountService _gw2AccountService; + DiscordService _discordService; - public DiscordBotController(RaidService raidService, UserService userService, GuildWars2AccountService gw2AccountService) + public DiscordBotController(RaidService raidService, UserService userService, GuildWars2AccountService gw2AccountService, DiscordService discordService) { _raidService = raidService; _userService = userService; _gw2AccountService = gw2AccountService; + _discordService = discordService; } [HttpGet] @@ -81,7 +83,7 @@ namespace Lieb.Controllers { if(signUp.userId != 0) { - int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId; + int accountId = _userService.GetMainAccount(signUp.userId).GuildWars2AccountId; await _raidService.SignUp(signUp.raidId, signUp.userId, accountId, signUp.roleId, SignUpType.SignedUp, signUp.signedUpByUserId); } else @@ -96,7 +98,7 @@ namespace Lieb.Controllers { if(signUp.userId != 0) { - int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId; + int accountId = _userService.GetMainAccount(signUp.userId).GuildWars2AccountId; await _raidService.SignUp(signUp.raidId, signUp.userId, accountId, signUp.roleId, SignUpType.Maybe, signUp.signedUpByUserId); } else @@ -111,7 +113,7 @@ namespace Lieb.Controllers { if(signUp.userId != 0) { - int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId; + int accountId = _userService.GetMainAccount(signUp.userId).GuildWars2AccountId; await _raidService.SignUp(signUp.raidId, signUp.userId, accountId, signUp.roleId, SignUpType.Backup, signUp.signedUpByUserId); } else @@ -126,7 +128,7 @@ namespace Lieb.Controllers { if(signUp.userId != 0) { - int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId; + int accountId = _userService.GetMainAccount(signUp.userId).GuildWars2AccountId; await _raidService.SignUp(signUp.raidId, signUp.userId, accountId, signUp.roleId, SignUpType.Flex, signUp.signedUpByUserId); } else @@ -141,7 +143,6 @@ namespace Lieb.Controllers { if(signUp.userId != 0) { - int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId; await _raidService.SignOff(signUp.raidId, signUp.userId, signUp.signedUpByUserId); } else @@ -176,5 +177,12 @@ namespace Lieb.Controllers return DiscordService.ConvertRaid(raid); } + + [HttpGet] + [Route("[action]")] + public List GetUserRenameServers() + { + return _discordService.GetUserRenameServers(); + } } } \ No newline at end of file diff --git a/Lieb/Data/DbInitializer.cs b/Lieb/Data/DbInitializer.cs index 2ee24a9..6ca0b2a 100644 --- a/Lieb/Data/DbInitializer.cs +++ b/Lieb/Data/DbInitializer.cs @@ -40,8 +40,8 @@ namespace Lieb.Data GuildWars2Account bloodseeker = new GuildWars2Account() { AccountName = "Bloodseeker.2043" }; var users = new LiebUser[] { - //new LiebUser{Id=0, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List(){ linaith, sarah} }, - new LiebUser{Id=194863625477816321, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List(){ linaith, sarah} }, + new LiebUser{Id=0, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List(){ linaith, sarah} }, + //new LiebUser{Id=194863625477816321, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List(){ linaith, sarah} }, #if DEBUG //new LiebUser{Id=194455125769715713, Name="Lisa", GuildWars2Accounts = new List(){ hierpiepts}}, new LiebUser{Id=1, Name="Lisa", GuildWars2Accounts = new List(){ hierpiepts}}, diff --git a/Lieb/Data/DiscordService.cs b/Lieb/Data/DiscordService.cs index 6421de0..7d7a777 100644 --- a/Lieb/Data/DiscordService.cs +++ b/Lieb/Data/DiscordService.cs @@ -51,6 +51,15 @@ namespace Lieb.Data } } + public List GetUserRenameServers() + { + using var context = _contextFactory.CreateDbContext(); + return context.DiscordSettings + .Where(s => s.ChangeUserNames) + .Select(s => s.DiscordSettingsId) + .ToList(); + } + public async Task PostRaidMessage(int raidId) { try @@ -320,5 +329,31 @@ namespace Lieb.Data Message = message }; } + + public async Task RenameUser(ulong userId, string name, string account) + { + try + { + var httpClient = _httpClientFactory.CreateClient(Constants.HttpClientName); + + ApiRenameUser renameUser = new ApiRenameUser() + { + userId = userId, + Name = name, + Account = account, + ServerIds = GetUserRenameServers() + }; + + var messageItemJson = new StringContent( + JsonSerializer.Serialize(renameUser), + Encoding.UTF8, + Application.Json); + + var httpResponseMessage = await httpClient.PostAsync("raid/RenameUser", messageItemJson); + + httpResponseMessage.EnsureSuccessStatusCode(); + } + catch {} + } } } \ No newline at end of file diff --git a/Lieb/Data/GuildWars2AccountService.cs b/Lieb/Data/GuildWars2AccountService.cs index 31d0434..8b49ae4 100644 --- a/Lieb/Data/GuildWars2AccountService.cs +++ b/Lieb/Data/GuildWars2AccountService.cs @@ -7,10 +7,12 @@ namespace Lieb.Data public class GuildWars2AccountService { private readonly IDbContextFactory _contextFactory; + private readonly DiscordService _discordService; - public GuildWars2AccountService(IDbContextFactory contextFactory) + public GuildWars2AccountService(IDbContextFactory contextFactory, DiscordService discordService) { _contextFactory = contextFactory; + _discordService = discordService; } public GuildWars2Account GetAccount(int gw2AccountId) @@ -29,17 +31,29 @@ namespace Lieb.Data using var context = _contextFactory.CreateDbContext(); if (account.GuildWars2AccountId == 0) { - LiebUser? user = context.LiebUsers.FirstOrDefault(u => u.Id == userId); + LiebUser? user = context.LiebUsers.Include(u => u.GuildWars2Accounts).FirstOrDefault(u => u.Id == userId); if(user != null) { user.GuildWars2Accounts.Add(account); + await context.SaveChangesAsync(); + if(user.GuildWars2Accounts.Count == 1) + { + user.MainGW2Account = account.GuildWars2AccountId; + await _discordService.RenameUser(userId, user.Name, account.AccountName); + } + await context.SaveChangesAsync(); } } else { context.Update(account); + await context.SaveChangesAsync(); + LiebUser? user = context.LiebUsers.Include(u => u.GuildWars2Accounts).FirstOrDefault(u => u.Id == userId); + if(user != null && user.MainGW2Account == account.GuildWars2AccountId) + { + await _discordService.RenameUser(userId, user.Name, account.AccountName); + } } - await context.SaveChangesAsync(); } } @@ -50,9 +64,18 @@ namespace Lieb.Data if (account != null) { context.Equipped.RemoveRange(account.EquippedBuilds); + context.RaidSignUps.RemoveRange(context.RaidSignUps.Where(s => s.GuildWars2AccountId == accountId)); await context.SaveChangesAsync(); context.GuildWars2Accounts.Remove(account); + LiebUser? user = context.LiebUsers.Include(u => u.GuildWars2Accounts).FirstOrDefault(u => u.GuildWars2Accounts.Contains(account)); await context.SaveChangesAsync(); + if(user != null && user.MainGW2Account == account.GuildWars2AccountId) + { + GuildWars2Account newMain = user.GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()); + user.MainGW2Account = newMain.GuildWars2AccountId; + await context.SaveChangesAsync(); + await _discordService.RenameUser(user.Id, user.Name, newMain.AccountName); + } } } diff --git a/Lieb/Data/UserService.cs b/Lieb/Data/UserService.cs index 16a41e1..7bdebba 100644 --- a/Lieb/Data/UserService.cs +++ b/Lieb/Data/UserService.cs @@ -7,10 +7,12 @@ namespace Lieb.Data public class UserService { private readonly IDbContextFactory _contextFactory; + private readonly DiscordService _discordService; - public UserService(IDbContextFactory contextFactory) + public UserService(IDbContextFactory contextFactory, DiscordService discordService) { _contextFactory = contextFactory; + _discordService = discordService; } public List GetLiebUsers() @@ -56,6 +58,16 @@ namespace Lieb.Data return new LiebUser(); } + public GuildWars2Account GetMainAccount(ulong userId) + { + using var context = _contextFactory.CreateDbContext(); + LiebUser user = context.LiebUsers + .Include(u => u.GuildWars2Accounts) + .ToList() + .FirstOrDefault(u => u.Id == userId, new LiebUser()); + return user.GuildWars2Accounts.FirstOrDefault(g => g.GuildWars2AccountId == user.MainGW2Account, new GuildWars2Account()); + } + public async Task CreateUser(ulong discordId, string userName) { using var context = _contextFactory.CreateDbContext(); @@ -94,6 +106,7 @@ namespace Lieb.Data userToChange.Birthday = user.Birthday; } await context.SaveChangesAsync(); + await _discordService.RenameUser(user.Id, user.Name, GetMainAccount(user.Id).AccountName); } public async Task UpdateBannedUntil(ulong userId, DateTime? date) diff --git a/Lieb/Models/LiebUser.cs b/Lieb/Models/LiebUser.cs index c0c42ce..bcff35f 100644 --- a/Lieb/Models/LiebUser.cs +++ b/Lieb/Models/LiebUser.cs @@ -18,6 +18,7 @@ namespace Lieb.Models public DateTime? Birthday { get; set; } public DateTime? BannedUntil { get; set; } + public int MainGW2Account { get; set; } public ICollection GuildWars2Accounts { get; set; } = new List(); public ICollection RoleAssignments { get; set; } = new List(); } diff --git a/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor b/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor index 460762a..8278f7a 100644 --- a/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor +++ b/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor @@ -3,6 +3,7 @@ @using Lieb.Models.GuildWars2 @using Lieb.Models.GuildWars2.Raid @inject RaidService RaidService +@inject UserService UserService @{ @@ -145,7 +146,8 @@ } else { - await RaidService.SignUp(_raid.RaidId, _liebUserId, _usableAccounts.FirstOrDefault().GuildWars2AccountId, role.RaidRoleId, signUpType); + int gw2AccountId = UserService.GetMainAccount(_liebUserId).GuildWars2AccountId; + await RaidService.SignUp(_raid.RaidId, _liebUserId, gw2AccountId, role.RaidRoleId, signUpType); } _Parent.HasChanged(); } diff --git a/SharedClasses/SharedModels/ApiRenameUser.cs b/SharedClasses/SharedModels/ApiRenameUser.cs new file mode 100644 index 0000000..c7b80d5 --- /dev/null +++ b/SharedClasses/SharedModels/ApiRenameUser.cs @@ -0,0 +1,14 @@ + +namespace SharedClasses.SharedModels +{ + public class ApiRenameUser + { + public ulong userId { get; set; } + + public string Name { get; set; } = String.Empty; + + public string Account { get; set; } = String.Empty; + + public List ServerIds { get; set; } = new List(); + } +} \ No newline at end of file