Compare commits

...

2 commits

4 changed files with 103 additions and 46 deletions

View file

@ -7,7 +7,7 @@ namespace EllieBot.Modules.Searches;
public partial class Searches
{
[Group]
public partial class OsuCommands : EllieModule
public partial class OsuCommands : EllieModule<OsuService>
{
private readonly IBotCredentials _creds;
private readonly IHttpClientFactory _httpFactory;
@ -24,9 +24,6 @@ public partial class Searches
if (string.IsNullOrWhiteSpace(user))
return;
using var http = _httpFactory.CreateClient();
var modeNumber = string.IsNullOrWhiteSpace(mode) ? 0 : ResolveGameMode(mode);
try
{
if (string.IsNullOrWhiteSpace(_creds.OsuApiKey))
@ -35,21 +32,19 @@ public partial class Searches
return;
}
var smode = ResolveGameMode(modeNumber);
var userReq = $"https://osu.ppy.sh/api/get_user?k={_creds.OsuApiKey}&u={user}&m={modeNumber}";
var userResString = await http.GetStringAsync(userReq);
var objs = JsonConvert.DeserializeObject<List<OsuUserData>>(userResString);
if (objs.Count == 0)
var obj = await _service.GetOsuData(user, mode);
if (obj is null)
{
await Response().Error(strs.osu_user_not_found).SendAsync();
return;
}
var obj = objs[0];
var userId = obj.UserId;
var smode = ResolveGameMode(obj.ModeNumber);
await Response().Embed(_sender.CreateEmbed()
await Response()
.Embed(_sender.CreateEmbed()
.WithOkColor()
.WithTitle($"osu! {smode} profile for {user}")
.WithThumbnailUrl($"https://a.ppy.sh/{userId}")
@ -61,11 +56,8 @@ public partial class Searches
.AddField("Total PP", Math.Round(obj.PpRaw, 2), true)
.AddField("Accuracy", Math.Round(obj.Accuracy, 2) + "%", true)
.AddField("Playcount", obj.Playcount, true)
.AddField("Level", Math.Round(obj.Level), true)).SendAsync();
}
catch (ArgumentOutOfRangeException)
{
await Response().Error(strs.osu_user_not_found).SendAsync();
.AddField("Level", Math.Round(obj.Level), true))
.SendAsync();
}
catch (Exception ex)
{

View 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;
}
}
}

View file

@ -67,4 +67,7 @@ public class OsuUserData
[JsonProperty("pp_country_rank")]
public string PpCountryRank { get; set; }
[JsonIgnore]
public int ModeNumber { get; set; }
}

View file

@ -178,8 +178,9 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
{
foreach (var user in globalToAdd)
{
var amount = user.Value.XpAmount * conf.CurrencyPerXp;
await _cs.AddAsync(user.Key, (long)(amount), null);
var amount = (long)(user.Value.XpAmount * conf.CurrencyPerXp);
if (amount > 0)
await _cs.AddAsync(user.Key, amount, null);
}
}
@ -770,7 +771,8 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
{
var channelId = channel.Id;
if (_excludedChannels.TryGetValue(user.Guild.Id, out var chans) && (chans.Contains(channelId)
if (_excludedChannels.TryGetValue(user.Guild.Id, out var chans)
&& (chans.Contains(channelId)
|| (channel is SocketThreadChannel tc && chans.Contains(tc.ParentChannel.Id))))
return false;