From 60fbe3d3fa3d9e66c984666ba619d8bb0e0ac02b Mon Sep 17 00:00:00 2001 From: Runebaas Date: Fri, 13 Oct 2017 23:40:39 +0200 Subject: [PATCH] Super basic overwatch command --- Geekbot.net/Commands/BattleTag.cs | 2 +- Geekbot.net/Commands/Overwatch.cs | 132 ++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 Geekbot.net/Commands/Overwatch.cs diff --git a/Geekbot.net/Commands/BattleTag.cs b/Geekbot.net/Commands/BattleTag.cs index 65a9444..e466abb 100644 --- a/Geekbot.net/Commands/BattleTag.cs +++ b/Geekbot.net/Commands/BattleTag.cs @@ -67,7 +67,7 @@ namespace Geekbot.net.Commands } } - private bool isValidTag(string tag) + public static bool isValidTag(string tag) { var splited = tag.Split("#"); if (splited.Length != 2) return false; diff --git a/Geekbot.net/Commands/Overwatch.cs b/Geekbot.net/Commands/Overwatch.cs new file mode 100644 index 0000000..3527cdf --- /dev/null +++ b/Geekbot.net/Commands/Overwatch.cs @@ -0,0 +1,132 @@ +using System; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; +using Google.Apis.Util; +using OverwatchAPI; +using OverwatchAPI.Config; +using Serilog; +using StackExchange.Redis; + +namespace Geekbot.net.Commands +{ + [Group("ow")] + public class Overwatch : ModuleBase + { + private readonly IErrorHandler _errorHandler; + private readonly ILogger _logger; + private readonly IUserRepository _userRepository; + + public Overwatch(IErrorHandler errorHandler, ILogger logger, IDatabase redis, IUserRepository userRepository) + { + _errorHandler = errorHandler; + _logger = logger; + _userRepository = userRepository; + } + + [Command("profile", RunMode = RunMode.Async)] + [Summary("Get someones overwatch profile. EU on PC only. Default battletag is your own (if set).")] + [Remarks(CommandCategories.Games)] + public async Task owProfile() + { + try + { + var tag = _userRepository.getUserSetting(Context.User.Id, "BattleTag"); + if (string.IsNullOrEmpty(tag)) + { + await ReplyAsync("You have no battle Tag saved, use `!battletag`"); + return; + } + var profile = await createProfile(tag); + if (profile == null) + { + await ReplyAsync("That player doesn't seem to exist"); + return; + } + await ReplyAsync("", false, profile.Build()); + + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + [Command("profile", RunMode = RunMode.Async)] + [Summary("Get someones overwatch profile. EU on PC only. Default battletag is your own (if set).")] + [Remarks(CommandCategories.Games)] + public async Task owProfile([Summary("BattleTag")] string tag) + { + try + { + if (!BattleTag.isValidTag(tag)) + { + await ReplyAsync("That doesn't seem to be a valid battletag..."); + return; + } + var profile = await createProfile(tag); + if (profile == null) + { + await ReplyAsync("That player doesn't seem to exist"); + return; + } + await ReplyAsync("", false, profile.Build()); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + [Command("profile", RunMode = RunMode.Async)] + [Summary("Get someones overwatch profile. EU on PC only.")] + [Remarks(CommandCategories.Games)] + public async Task owProfile([Summary("@someone")] IUser user) + { + try + { + var tag = _userRepository.getUserSetting(user.Id, "BattleTag"); + if (string.IsNullOrEmpty(tag)) + { + await ReplyAsync("This user didn't set a battletag"); + return; + } + var profile = await createProfile(tag); + if (profile == null) + { + await ReplyAsync("That player doesn't seem to exist"); + return; + } + await ReplyAsync("", false, profile.Build()); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + private async Task createProfile(string battletag) + { + var owConfig = new OverwatchConfig.Builder().WithRegions(Region.Eu).WithPlatforms(Platform.Pc); + using (var owClient = new OverwatchClient(owConfig)) + { + var player = await owClient.GetPlayerAsync(battletag); + if (player.Username == null) + { + return null; + } + _logger.Debug($"[OW] Username = {player.Username}"); + var eb = new EmbedBuilder(); + eb.WithAuthor(new EmbedAuthorBuilder() + .WithIconUrl(player.ProfilePortraitUrl) + .WithName(player.Username)); + eb.Url = player.ProfileUrl; + eb.AddInlineField("Level", player.PlayerLevel); + eb.AddInlineField("Current Rank", player.CompetitiveRank > 0 ? player.CompetitiveRank.ToString() : "Unranked"); + + return eb; + } + } + } +}