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

@ -39,6 +39,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>();
}
}
}