Started cleaning the .osu command
This commit is contained in:
parent
cd92577095
commit
6128703bcc
3 changed files with 95 additions and 40 deletions
|
@ -7,7 +7,7 @@ namespace EllieBot.Modules.Searches;
|
||||||
public partial class Searches
|
public partial class Searches
|
||||||
{
|
{
|
||||||
[Group]
|
[Group]
|
||||||
public partial class OsuCommands : EllieModule
|
public partial class OsuCommands : EllieModule<OsuService>
|
||||||
{
|
{
|
||||||
private readonly IBotCredentials _creds;
|
private readonly IBotCredentials _creds;
|
||||||
private readonly IHttpClientFactory _httpFactory;
|
private readonly IHttpClientFactory _httpFactory;
|
||||||
|
@ -24,9 +24,6 @@ public partial class Searches
|
||||||
if (string.IsNullOrWhiteSpace(user))
|
if (string.IsNullOrWhiteSpace(user))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
using var http = _httpFactory.CreateClient();
|
|
||||||
var modeNumber = string.IsNullOrWhiteSpace(mode) ? 0 : ResolveGameMode(mode);
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(_creds.OsuApiKey))
|
if (string.IsNullOrWhiteSpace(_creds.OsuApiKey))
|
||||||
|
@ -35,37 +32,32 @@ public partial class Searches
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var smode = ResolveGameMode(modeNumber);
|
var obj = await _service.GetOsuData(user, mode);
|
||||||
var userReq = $"https://osu.ppy.sh/api/get_user?k={_creds.OsuApiKey}&u={user}&m={modeNumber}";
|
if (obj is null)
|
||||||
var userResString = await http.GetStringAsync(userReq);
|
|
||||||
var objs = JsonConvert.DeserializeObject<List<OsuUserData>>(userResString);
|
|
||||||
|
|
||||||
if (objs.Count == 0)
|
|
||||||
{
|
{
|
||||||
await Response().Error(strs.osu_user_not_found).SendAsync();
|
await Response().Error(strs.osu_user_not_found).SendAsync();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var obj = objs[0];
|
|
||||||
var userId = obj.UserId;
|
var userId = obj.UserId;
|
||||||
|
var smode = ResolveGameMode(obj.ModeNumber);
|
||||||
|
|
||||||
await Response().Embed(_sender.CreateEmbed()
|
|
||||||
.WithOkColor()
|
await Response()
|
||||||
.WithTitle($"osu! {smode} profile for {user}")
|
.Embed(_sender.CreateEmbed()
|
||||||
.WithThumbnailUrl($"https://a.ppy.sh/{userId}")
|
.WithOkColor()
|
||||||
.WithDescription($"https://osu.ppy.sh/u/{userId}")
|
.WithTitle($"osu! {smode} profile for {user}")
|
||||||
.AddField("Official Rank", $"#{obj.PpRank}", true)
|
.WithThumbnailUrl($"https://a.ppy.sh/{userId}")
|
||||||
.AddField("Country Rank",
|
.WithDescription($"https://osu.ppy.sh/u/{userId}")
|
||||||
$"#{obj.PpCountryRank} :flag_{obj.Country.ToLower()}:",
|
.AddField("Official Rank", $"#{obj.PpRank}", true)
|
||||||
true)
|
.AddField("Country Rank",
|
||||||
.AddField("Total PP", Math.Round(obj.PpRaw, 2), true)
|
$"#{obj.PpCountryRank} :flag_{obj.Country.ToLower()}:",
|
||||||
.AddField("Accuracy", Math.Round(obj.Accuracy, 2) + "%", true)
|
true)
|
||||||
.AddField("Playcount", obj.Playcount, true)
|
.AddField("Total PP", Math.Round(obj.PpRaw, 2), true)
|
||||||
.AddField("Level", Math.Round(obj.Level), true)).SendAsync();
|
.AddField("Accuracy", Math.Round(obj.Accuracy, 2) + "%", true)
|
||||||
}
|
.AddField("Playcount", obj.Playcount, true)
|
||||||
catch (ArgumentOutOfRangeException)
|
.AddField("Level", Math.Round(obj.Level), true))
|
||||||
{
|
.SendAsync();
|
||||||
await Response().Error(strs.osu_user_not_found).SendAsync();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -96,18 +88,18 @@ public partial class Searches
|
||||||
var userStats = statsResponse.Stats;
|
var userStats = statsResponse.Stats;
|
||||||
|
|
||||||
var embed = _sender.CreateEmbed()
|
var embed = _sender.CreateEmbed()
|
||||||
.WithOkColor()
|
.WithOkColor()
|
||||||
.WithTitle($"osu!Gatari {modeStr} profile for {user}")
|
.WithTitle($"osu!Gatari {modeStr} profile for {user}")
|
||||||
.WithThumbnailUrl($"https://a.gatari.pw/{userStats.Id}")
|
.WithThumbnailUrl($"https://a.gatari.pw/{userStats.Id}")
|
||||||
.WithDescription($"https://osu.gatari.pw/u/{userStats.Id}")
|
.WithDescription($"https://osu.gatari.pw/u/{userStats.Id}")
|
||||||
.AddField("Official Rank", $"#{userStats.Rank}", true)
|
.AddField("Official Rank", $"#{userStats.Rank}", true)
|
||||||
.AddField("Country Rank",
|
.AddField("Country Rank",
|
||||||
$"#{userStats.CountryRank} :flag_{userData.Country.ToLower()}:",
|
$"#{userStats.CountryRank} :flag_{userData.Country.ToLower()}:",
|
||||||
true)
|
true)
|
||||||
.AddField("Total PP", userStats.Pp, true)
|
.AddField("Total PP", userStats.Pp, true)
|
||||||
.AddField("Accuracy", $"{Math.Round(userStats.AvgAccuracy, 2)}%", true)
|
.AddField("Accuracy", $"{Math.Round(userStats.AvgAccuracy, 2)}%", true)
|
||||||
.AddField("Playcount", userStats.Playcount, true)
|
.AddField("Playcount", userStats.Playcount, true)
|
||||||
.AddField("Level", userStats.Level, true);
|
.AddField("Level", userStats.Level, true);
|
||||||
|
|
||||||
await Response().Embed(embed).SendAsync();
|
await Response().Embed(embed).SendAsync();
|
||||||
}
|
}
|
||||||
|
|
60
src/EllieBot/Modules/Searches/OsuService.cs
Normal file
60
src/EllieBot/Modules/Searches/OsuService.cs
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
#nullable disable
|
||||||
|
using EllieBot.Modules.Searches.Common;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace EllieBot.Modules.Searches;
|
||||||
|
|
||||||
|
public sealed class OsuService : IEService
|
||||||
|
{
|
||||||
|
private readonly IHttpClientFactory _httpFactory;
|
||||||
|
private readonly IBotCredentials _creds;
|
||||||
|
|
||||||
|
public OsuService(IHttpClientFactory httpFactory, IBotCredentials creds)
|
||||||
|
{
|
||||||
|
_httpFactory = httpFactory;
|
||||||
|
_creds = creds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<OsuUserData> GetOsuData(string username, string mode)
|
||||||
|
{
|
||||||
|
using var http = _httpFactory.CreateClient();
|
||||||
|
|
||||||
|
var modeNumber = string.IsNullOrWhiteSpace(mode) ? 0 : ResolveGameMode(mode);
|
||||||
|
var userReq = $"https://osu.ppy.sh/api/get_user?k={_creds.OsuApiKey}&u={username}&m={modeNumber}";
|
||||||
|
var userResString = await http.GetStringAsync(userReq);
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(userResString))
|
||||||
|
return null;
|
||||||
|
var objs = JsonConvert.DeserializeObject<List<OsuUserData>>(userResString);
|
||||||
|
|
||||||
|
if (objs.Count == 0)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var obj = objs[0];
|
||||||
|
|
||||||
|
obj.ModeNumber = modeNumber;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int ResolveGameMode(string mode)
|
||||||
|
{
|
||||||
|
switch (mode.ToUpperInvariant())
|
||||||
|
{
|
||||||
|
case "STD":
|
||||||
|
case "STANDARD":
|
||||||
|
return 0;
|
||||||
|
case "TAIKO":
|
||||||
|
return 1;
|
||||||
|
case "CTB":
|
||||||
|
case "CATCHTHEBEAT":
|
||||||
|
return 2;
|
||||||
|
case "MANIA":
|
||||||
|
case "OSU!MANIA":
|
||||||
|
return 3;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -67,4 +67,7 @@ public class OsuUserData
|
||||||
|
|
||||||
[JsonProperty("pp_country_rank")]
|
[JsonProperty("pp_country_rank")]
|
||||||
public string PpCountryRank { get; set; }
|
public string PpCountryRank { get; set; }
|
||||||
|
|
||||||
|
[JsonIgnore]
|
||||||
|
public int ModeNumber { get; set; }
|
||||||
}
|
}
|
Loading…
Reference in a new issue