366 lines
15 KiB
C#
366 lines
15 KiB
C#
using System;
|
|
using Discord;
|
|
using System.Text;
|
|
using Discord.Commands;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using ChaosBot.Discord.PreConditions;
|
|
using ChaosBot.Lodestone;
|
|
using ChaosBot.Models;
|
|
using ChaosBot.Repositories;
|
|
using ChaosBot.Services;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ChaosBot.Discord.Modules.User
|
|
{
|
|
public class Lodestone : ModuleBase
|
|
{
|
|
[Command("lodestone")]
|
|
[Alias("ls")]
|
|
[CheckCommandPerm("User")]
|
|
public async Task LodestoneRun(string cmd, params String[] args)
|
|
{
|
|
try
|
|
{
|
|
if ((cmd.ToLower() == "fc") || (cmd.ToLower() == "freecompany"))
|
|
{
|
|
if(args.Length >= 1)
|
|
{
|
|
await GetFreeCompany(args[0], string.Join(" ", args.Skip(1)));
|
|
}
|
|
else
|
|
{
|
|
await LodestoneHelp();
|
|
}
|
|
}
|
|
else if (((cmd.ToLower() == "link") || (cmd.ToLower() == "l") || (cmd.ToLower() == "attach") || (cmd.ToLower() == "claim") || (cmd.ToLower() == "take")) && (CheckPermissions.CheckPerms(Context, "lodestone.link")))
|
|
{
|
|
if(args.Length >= 1)
|
|
{
|
|
await ClaimCharacter(args[0], string.Join(" ", args.Skip(1)));
|
|
}
|
|
else
|
|
{
|
|
await LodestoneHelp();
|
|
}
|
|
}
|
|
else if ((cmd.ToLower() == "c") || (cmd.ToLower() == "character")|| (cmd.ToLower() == "char")|| (cmd.ToLower() == "toon"))
|
|
{
|
|
if(args.Length >= 1)
|
|
{
|
|
await GetCharacter(args[0], string.Join(" ", args.Skip(1)));
|
|
}
|
|
else
|
|
{
|
|
await LodestoneHelp();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
await LodestoneHelp();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
}
|
|
}
|
|
|
|
public async Task LodestoneHelp()
|
|
{
|
|
try
|
|
{
|
|
var sb = new StringBuilder();
|
|
var embed = new EmbedBuilder();
|
|
Configuration config = new Configuration();
|
|
|
|
embed.WithColor(new Color(255, 255, 0));
|
|
embed.Title = "Lodestone API Help";
|
|
sb.AppendLine();
|
|
sb.AppendLine();
|
|
sb.AppendLine("To get FreeCompany Info:");
|
|
sb.AppendLine($"By Id: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone freecompany 9231394073691143535");
|
|
sb.AppendLine($"By Name: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone freecompany Siren Helix");
|
|
sb.AppendLine();
|
|
sb.AppendLine("To get Character Info:");
|
|
sb.AppendLine($"By Id: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone character 9231394073691143535");
|
|
sb.AppendLine($"By Name: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone character Siren Luna Kaisar");
|
|
if (CheckPermissions.CheckPerms(Context, "lodestone.link"))
|
|
{
|
|
sb.AppendLine("To Link your Character:");
|
|
sb.AppendLine($"By Id: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone link 9231394073691143535");
|
|
sb.AppendLine($"By Name: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone link Siren Luna Kaisar");
|
|
}
|
|
sb.AppendLine();
|
|
sb.AppendLine();
|
|
sb.AppendLine("To view this help:");
|
|
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}config help");
|
|
|
|
/*
|
|
* Add the string to the Embed
|
|
*/
|
|
embed.Description = sb.ToString();
|
|
|
|
/*
|
|
* Reply with the Embed created above
|
|
*/
|
|
await ReplyAsync(null, false, embed.Build());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
}
|
|
}
|
|
|
|
public async Task GetFreeCompany(string value, [Remainder] string name)
|
|
{
|
|
FreeCompanyDetailed freeCompany;
|
|
try
|
|
{
|
|
await Context.Channel.TriggerTypingAsync();
|
|
|
|
if (name.Length == 0)
|
|
freeCompany = LodestoneManager.GetFreeCompanyById(value);
|
|
else
|
|
{
|
|
freeCompany = LodestoneManager.GetFreeCompany(value, name);
|
|
}
|
|
|
|
var embed = BuildEmbedFromFreeCompany(freeCompany);
|
|
|
|
/*
|
|
* Reply with the Embed created above
|
|
*/
|
|
await ReplyAsync(null, false, embed.Build());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
}
|
|
}
|
|
|
|
public async Task ClaimCharacter(string value, [Remainder] string name)
|
|
{
|
|
CharacterDetailed character;
|
|
try
|
|
{
|
|
await Context.Channel.TriggerTypingAsync();
|
|
|
|
var sb = new StringBuilder();
|
|
var embed = new EmbedBuilder();
|
|
|
|
embed.WithColor(new Color(255, 255, 0));
|
|
embed.Title = $"Discord to FFXIV Linking";
|
|
sb.AppendLine();
|
|
sb.AppendLine();
|
|
|
|
if (name.Length == 0)
|
|
{
|
|
character = LodestoneManager.GetCharacter(Convert.ToInt64(value));
|
|
}
|
|
else
|
|
{
|
|
character = LodestoneManager.GetCharacter(value, string.Join(" ", name));
|
|
}
|
|
|
|
bool linked;
|
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
|
{
|
|
IQueryable<LodestoneCharacter> ctxlsChars = dbContext.LodestoneCharacter;
|
|
IQueryable<LodestoneCharacter> lsChar = ctxlsChars.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.LodestoneId.Equals(Convert.ToUInt64(character.Character.ID)));
|
|
|
|
linked = lsChar.Any();
|
|
}
|
|
|
|
if(!linked)
|
|
{
|
|
string b64 =
|
|
Convert.ToBase64String(Encoding.GetEncoding(28591)
|
|
.GetBytes(character.Character.ID.ToString())) + '|' +
|
|
Convert.ToBase64String(Encoding.GetEncoding(28591)
|
|
.GetBytes(Context.Guild.Id.ToString())) + '|' +
|
|
Convert.ToBase64String(Encoding.GetEncoding(28591).GetBytes(Context.User.Id.ToString()));
|
|
|
|
if (character.Character.Bio.Contains(b64))
|
|
{
|
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
|
{
|
|
LodestoneCharacter lsChar = new LodestoneCharacter();
|
|
|
|
lsChar.LodestoneId = Convert.ToUInt64(character.Character.ID);
|
|
lsChar.DiscordUserId = Context.User.Id;
|
|
lsChar.DiscordGuildId = Context.Guild.Id;
|
|
lsChar.Name = character.Character.Name;
|
|
lsChar.Avatar = character.Character.Avatar;
|
|
|
|
await dbContext.LodestoneCharacter.Upsert(lsChar)
|
|
.On(x => new {x.DiscordGuildId, x.LodestoneId}).RunAsync();
|
|
}
|
|
|
|
embed.ImageUrl = character.Character.Avatar;
|
|
sb.AppendLine(
|
|
$"{Context.User.Mention} has been successfully linked to {character.Character.Name}");
|
|
embed.Description = sb.ToString();
|
|
|
|
IGuildUser nUser = await Context.Guild.GetUserAsync(Context.User.Id);
|
|
await nUser.ModifyAsync(x => { x.Nickname = character.Character.Name; });
|
|
|
|
await ReplyAsync(null, false, embed.Build());
|
|
}
|
|
else
|
|
{
|
|
sb.AppendLine($"Please add the following code anywhere in your bio.");
|
|
sb.AppendLine($"```{b64}```");
|
|
sb.AppendLine(
|
|
$"You can access your bio by going to https://na.finalfantasyxiv.com/lodestone/my/setting/profile/ and signing in with your Final Fantasy Lodestone account.");
|
|
sb.AppendLine();
|
|
sb.AppendLine($"Please rerun the below command in <#{Context.Channel.Id}>");
|
|
sb.AppendLine($"```{Context.Message.Content}```");
|
|
|
|
embed.Description = sb.ToString();
|
|
await Context.User.SendMessageAsync("", false, embed.Build());
|
|
|
|
await ReplyAsync($"{Context.User.Mention}, please check your Private Messages.");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
embed.ImageUrl = character.Character.Avatar;
|
|
sb.AppendLine(
|
|
$"{Context.User.Mention}, {character.Character.Name} is already linked.");
|
|
embed.Description = sb.ToString();
|
|
await ReplyAsync(null, false, embed.Build());
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
}
|
|
}
|
|
|
|
public async Task GetCharacter(string value, [Remainder] string name)
|
|
{
|
|
CharacterDetailed character;
|
|
try
|
|
{
|
|
await Context.Channel.TriggerTypingAsync();
|
|
|
|
if (name.Length == 0)
|
|
character = LodestoneManager.GetCharacter(Convert.ToInt64(value));
|
|
else
|
|
{
|
|
character = LodestoneManager.GetCharacter(value, string.Join(" ", name));
|
|
}
|
|
|
|
var embed = BuildEmbedFromCharacter(character);
|
|
|
|
/*
|
|
* Reply with the Embed created above
|
|
*/
|
|
await ReplyAsync(null, false, embed.Build());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
}
|
|
}
|
|
|
|
private static EmbedBuilder BuildEmbedFromFreeCompany(FreeCompanyDetailed freeCompany)
|
|
{
|
|
var sb = new StringBuilder();
|
|
var embed = new EmbedBuilder();
|
|
|
|
embed.WithColor(new Color(255, 255, 0));
|
|
embed.Title = $"Free Company Information";
|
|
embed.Url = $"https://na.finalfantasyxiv.com/lodestone/freecompany/{freeCompany.FreeCompany.ID}/";
|
|
embed.ImageUrl = freeCompany.FreeCompany.Crest.First();
|
|
embed.Footer = new EmbedFooterBuilder().WithText($"Lodestone ID {freeCompany.FreeCompany.ID}");
|
|
|
|
sb.AppendLine(
|
|
$"{freeCompany.FreeCompany.Name} ({freeCompany.FreeCompany.Server}, rank {freeCompany.FreeCompany.Rank})");
|
|
sb.AppendLine($"{freeCompany.FreeCompany.Slogan}");
|
|
sb.AppendLine();
|
|
sb.AppendLine($"{freeCompany.FreeCompany.ActiveMemberCount} active members.");
|
|
sb.AppendLine($"Company estate {freeCompany.FreeCompany.Estate.Plot}");
|
|
sb.AppendLine($"Pledged to Grand Company {freeCompany.FreeCompany.GrandCompany}");
|
|
sb.AppendLine();
|
|
|
|
var rankCounts = freeCompany.FreeCompanyMembers
|
|
.GroupBy(m => m.Rank)
|
|
.Select(g => new {Rank = g.Key, Count = g.Count()})
|
|
.OrderBy(x => x.Rank);
|
|
|
|
foreach (string rank in new List<string> {"Council", "Mentor", "Member", "Initiate"})
|
|
{
|
|
var rankCount = rankCounts.First(e => e.Rank == rank);
|
|
sb.AppendLine($"{rankCount.Count} members are of rank {rankCount.Rank} ");
|
|
}
|
|
|
|
/*
|
|
* Add the string to the Embed
|
|
*/
|
|
embed.Description = sb.ToString();
|
|
return embed;
|
|
}
|
|
|
|
private static EmbedBuilder BuildEmbedFromCharacter(CharacterDetailed character)
|
|
{
|
|
var sb = new StringBuilder();
|
|
var embed = new EmbedBuilder();
|
|
|
|
embed.WithColor(new Color(255, 255, 0));
|
|
embed.Title = $"Character Information";
|
|
embed.Url = $"https://na.finalfantasyxiv.com/lodestone/character/{character.Character.ID}/";
|
|
embed.ImageUrl = character.Character.Avatar;
|
|
embed.Footer = new EmbedFooterBuilder().WithText($"Lodestone ID {character.Character.ID}");
|
|
|
|
char genderIcon = character.Character.Gender == 1 ? '♂' : '♀';
|
|
sb.AppendLine($"{character.Character.Name} {genderIcon} ({character.Character.Server})");
|
|
sb.AppendLine($"Bio: {character.Character.Bio}");
|
|
sb.AppendLine();
|
|
sb.AppendLine(
|
|
$"Current Job: {LodestoneManager.Classes.GetValueOrDefault(character.Character.ActiveClassJob.UnlockedState.Name)?.Item1} {character.Character.ActiveClassJob.Level}");
|
|
sb.AppendLine();
|
|
sb.AppendLine("All Jobs:");
|
|
|
|
sb.Append("```");
|
|
List<Tuple<string, ClassType, int>> classInfoList = new List<Tuple<string, ClassType, int>>();
|
|
foreach (CharacterClassJob characterClassJob in character.Character.ClassJobs.ToArray())
|
|
{
|
|
if (characterClassJob.Level == 0) continue;
|
|
Tuple<string, ClassType> classInfo =
|
|
LodestoneManager.Classes.GetValueOrDefault(characterClassJob.UnlockedState.Name);
|
|
if (classInfo != null)
|
|
classInfoList.Add(new Tuple<string, ClassType, int>(classInfo.Item1, classInfo.Item2,
|
|
characterClassJob.Level));
|
|
}
|
|
|
|
foreach (var classTypeList in classInfoList.GroupBy(ci => ci.Item2)
|
|
.Select(group => new {ClassType = @group.Key, classList = @group.ToArray()}).OrderBy(g => g.ClassType))
|
|
{
|
|
sb.Append($"{classTypeList.ClassType.ToString()}:");
|
|
|
|
for (int i = 0; i < classTypeList.classList.Length; i++)
|
|
{
|
|
if ((i % 4) == 0)
|
|
sb.AppendLine();
|
|
|
|
Tuple<string, ClassType, int> classItem = classTypeList.classList[i];
|
|
sb.Append(String.Format(" {0,3}: {1,2}", classItem.Item1, classItem.Item3));
|
|
}
|
|
|
|
sb.AppendLine("\n");
|
|
}
|
|
|
|
sb.Append("```");
|
|
|
|
/*
|
|
* Add the string to the Embed
|
|
*/
|
|
embed.Description = sb.ToString();
|
|
return embed;
|
|
}
|
|
}
|
|
}
|