From 55959930805a862cf511af5070d3e5a3cfce2e76 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 6 Jun 2020 01:43:54 +0200 Subject: [PATCH] Add nick command --- ChaosBot/Discord/Modules/LodestoneCommands.cs | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/ChaosBot/Discord/Modules/LodestoneCommands.cs b/ChaosBot/Discord/Modules/LodestoneCommands.cs index 3dfcc4d..52c20ab 100644 --- a/ChaosBot/Discord/Modules/LodestoneCommands.cs +++ b/ChaosBot/Discord/Modules/LodestoneCommands.cs @@ -5,8 +5,10 @@ using Discord.Commands; using System.Threading.Tasks; using System.Collections.Generic; using System.Linq; +using ChaosBot.Database; using ChaosBot.Lodestone; using ChaosBot.Services; +using Discord.Net; using Microsoft.Extensions.Configuration; using NLog; @@ -106,6 +108,95 @@ namespace ChaosBot.Discord.Modules } } + [Command("nick set")] + [Alias("lodestone nick set")] + [RequireBotPermission(GuildPermission.ManageNicknames)] + public async Task SetNickname(string server, params string[] name) + { + await Context.Channel.TriggerTypingAsync(); + CharacterDetailed character = LodestoneManager.GetCharacter(server, string.Join(" ", name)); + await NickNameBuilder(character); + } + + [Command("nick set")] + [Alias("lodestone nick set")] + [RequireBotPermission(GuildPermission.ManageNicknames)] + public async Task SetNickname(long id) + { + await Context.Channel.TriggerTypingAsync(); + CharacterDetailed character = LodestoneManager.GetCharacter(id); + await NickNameBuilder(character); + } + + private async Task NickNameBuilder(CharacterDetailed character) + { + try + { + IUserMessage message = + await ReplyAsync( + $"Found character {character.Character.Name} on server {character.Character.Server}. Is this correct?"); + List<(Emoji emoji, bool state)> reactions = new List<(Emoji emoji, bool state)> + { + (emoji: new Emoji("✅"), state: true), + (emoji: new Emoji("❎"), state: false) + }; + + foreach ((Emoji emoji, bool state) reaction in reactions) + await message.AddReactionAsync(reaction.emoji); + + int timerWaitId = -1, + timerCancelWaitId = -1; + + async void WaitForReaction() + { + foreach ((Emoji emoji, bool state) reaction in reactions) + { + IReadOnlyCollection users = (await message.GetReactionUsersAsync(reaction.emoji, 50, null).ToArrayAsync()).First(); + if (users.Any(u => u.Id == Context.User.Id)) + { + bool state = reaction.state; + + Timer.CancelTimer(timerWaitId); + Timer.CancelTimer(timerCancelWaitId); + await message.DeleteAsync(); + + if (state) + { + await ReplyAsync("Confirmed your username!"); + IGuildUser user = await Context.Guild.GetUserAsync(Context.User.Id); + try + { + await user.ModifyAsync(x => { x.Nickname = character.Character.Name; }); + } + catch (HttpException ex) + { + await ReplyAsync("Cannot update your role. Is your role above mine?"); + _logger.Warn($"LodestoneCommands.NickNameBuilder.WaitForReaction: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + + return; + } + } + } + } + + async void StopWaitingForReaction() + { + await message.DeleteAsync(); + await ReplyAsync("Timed out. Please try again."); + + Timer.CancelTimer(timerWaitId); + } + + timerWaitId = Timer.RunTimer(WaitForReaction, new TimeSpan(TimeSpan.TicksPerSecond)); + timerCancelWaitId = Timer.RunIn(StopWaitingForReaction, new TimeSpan(TimeSpan.TicksPerSecond)*20.5); + } + catch (Exception ex) + { + _logger.Error($"LodestoneCommands.NickNameBuilder: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + private static EmbedBuilder BuildEmbedFromFreeCompany(FreeCompanyDetailed freeCompany) { var sb = new StringBuilder();