From 9003d6249eb62e32302c4b38cf35b42905674cfb Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 28 Jul 2020 22:14:59 +0200 Subject: [PATCH] Add !mmr to get League of Legends MMR numbers --- .../Commands/Integrations/LolMmr/LolMmr.cs | 61 +++++++++++++++++++ .../Commands/Integrations/LolMmr/LolMmrDto.cs | 9 +++ .../Integrations/LolMmr/LolMrrInfoDto.cs | 11 ++++ 3 files changed, 81 insertions(+) create mode 100644 Geekbot.net/Commands/Integrations/LolMmr/LolMmr.cs create mode 100644 Geekbot.net/Commands/Integrations/LolMmr/LolMmrDto.cs create mode 100644 Geekbot.net/Commands/Integrations/LolMmr/LolMrrInfoDto.cs diff --git a/Geekbot.net/Commands/Integrations/LolMmr/LolMmr.cs b/Geekbot.net/Commands/Integrations/LolMmr/LolMmr.cs new file mode 100644 index 0000000..5de7d63 --- /dev/null +++ b/Geekbot.net/Commands/Integrations/LolMmr/LolMmr.cs @@ -0,0 +1,61 @@ +using System; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Web; +using Discord.Commands; +using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; + +namespace Geekbot.net.Commands.Integrations.LolMmr +{ + public class LolMmr : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public LolMmr(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("mmr", RunMode = RunMode.Async)] + [Summary("Get the League of Legends MMR for a specified summoner")] + public async Task GetMmr([Remainder] [Summary("summoner")] string summonerName) + { + try + { + LolMmrDto data; + try + { + var name = HttpUtility.UrlEncode(summonerName.ToLower()); + var httpClient = HttpAbstractions.CreateDefaultClient(); + // setting the user agent in accordance with the whatismymmr.com api rules + httpClient.DefaultRequestHeaders.Remove("User-Agent"); + httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Linux:rocks.pizzaandcoffee.geekbot:v0.0.0"); + data = await HttpAbstractions.Get(new Uri($"https://euw.whatismymmr.com/api/v1/summoner?name={name}"), httpClient); + } + catch (HttpRequestException e) + { + if (e.StatusCode != HttpStatusCode.NotFound) throw e; + + await Context.Channel.SendMessageAsync("Player not found"); + return; + + } + + var sb = new StringBuilder(); + sb.AppendLine($"**MMR for {summonerName}**"); + sb.AppendLine($"Normal: {data.Normal.Avg}"); + sb.AppendLine($"Ranked: {data.Ranked.Avg}"); + sb.AppendLine($"ARAM: {data.ARAM.Avg}"); + + await Context.Channel.SendMessageAsync(sb.ToString()); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/LolMmr/LolMmrDto.cs b/Geekbot.net/Commands/Integrations/LolMmr/LolMmrDto.cs new file mode 100644 index 0000000..4a9887d --- /dev/null +++ b/Geekbot.net/Commands/Integrations/LolMmr/LolMmrDto.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.Commands.Integrations.LolMmr +{ + public class LolMmrDto + { + public LolMrrInfoDto Ranked { get; set; } + public LolMrrInfoDto Normal { get; set; } + public LolMrrInfoDto ARAM { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/LolMmr/LolMrrInfoDto.cs b/Geekbot.net/Commands/Integrations/LolMmr/LolMrrInfoDto.cs new file mode 100644 index 0000000..55804fd --- /dev/null +++ b/Geekbot.net/Commands/Integrations/LolMmr/LolMrrInfoDto.cs @@ -0,0 +1,11 @@ +using System; +using Newtonsoft.Json; + +namespace Geekbot.net.Commands.Integrations.LolMmr +{ + public class LolMrrInfoDto + { + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + public decimal Avg { get; set; } = 0; + } +} \ No newline at end of file