Add nick command

This commit is contained in:
Daniel_I_Am 2020-06-06 01:43:54 +02:00
parent 87036fea37
commit 5595993080
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84

View File

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