Added renaming users and main gw2 accounts

This commit is contained in:
Sarah Faey 2022-11-21 18:27:26 +01:00
parent aad07809d2
commit c4ecb512d7
13 changed files with 172 additions and 22 deletions

View file

@ -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()

View file

@ -40,6 +40,42 @@ namespace DiscordBot.CommandHandlers
return true;
}
public async Task<Tuple<bool, string>> 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<bool, string> createAccountResult = await _httpService.CreateAccount(user);
if(createAccountResult.Item1)
{
List<ulong> 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<ulong> 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)
{

View file

@ -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<bool, string> createAccountResult = await _httpService.CreateAccount(user);
Tuple<bool, string> createAccountResult = await _handlerFunctions.CreateAccount(modal, _client, name, account);
if(!createAccountResult.Item1)
{
await modal.RespondAsync(createAccountResult.Item2, ephemeral: true);

View file

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

View file

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

View file

@ -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<ulong> GetUserRenameServers()
{
return _discordService.GetUserRenameServers();
}
}
}

View file

@ -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<GuildWars2Account>(){ linaith, sarah} },
new LiebUser{Id=194863625477816321, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
new LiebUser{Id=0, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
//new LiebUser{Id=194863625477816321, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
#if DEBUG
//new LiebUser{Id=194455125769715713, Name="Lisa", GuildWars2Accounts = new List<GuildWars2Account>(){ hierpiepts}},
new LiebUser{Id=1, Name="Lisa", GuildWars2Accounts = new List<GuildWars2Account>(){ hierpiepts}},

View file

@ -51,6 +51,15 @@ namespace Lieb.Data
}
}
public List<ulong> 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 {}
}
}
}

View file

@ -7,10 +7,12 @@ namespace Lieb.Data
public class GuildWars2AccountService
{
private readonly IDbContextFactory<LiebContext> _contextFactory;
private readonly DiscordService _discordService;
public GuildWars2AccountService(IDbContextFactory<LiebContext> contextFactory)
public GuildWars2AccountService(IDbContextFactory<LiebContext> 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);
}
}
}

View file

@ -7,10 +7,12 @@ namespace Lieb.Data
public class UserService
{
private readonly IDbContextFactory<LiebContext> _contextFactory;
private readonly DiscordService _discordService;
public UserService(IDbContextFactory<LiebContext> contextFactory)
public UserService(IDbContextFactory<LiebContext> contextFactory, DiscordService discordService)
{
_contextFactory = contextFactory;
_discordService = discordService;
}
public List<LiebUser> 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)

View file

@ -18,6 +18,7 @@ namespace Lieb.Models
public DateTime? Birthday { get; set; }
public DateTime? BannedUntil { get; set; }
public int MainGW2Account { get; set; }
public ICollection<GuildWars2Account> GuildWars2Accounts { get; set; } = new List<GuildWars2Account>();
public ICollection<RoleAssignment> RoleAssignments { get; set; } = new List<RoleAssignment>();
}

View file

@ -3,6 +3,7 @@
@using Lieb.Models.GuildWars2
@using Lieb.Models.GuildWars2.Raid
@inject RaidService RaidService
@inject UserService UserService
<table class="table">
@{
@ -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();
}

View file

@ -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<ulong> ServerIds { get; set; } = new List<ulong>();
}
}