mirror of
https://github.com/RPCS3/discord-bot.git
synced 2025-03-09 04:12:05 +00:00
Merge pull request #123 from 13xforever/vnext
New command !latest cached
This commit is contained in:
commit
d185204089
@ -25,6 +25,7 @@ namespace CompatBot.Commands
|
||||
private static SemaphoreSlim updateCheck = new SemaphoreSlim(1, 1);
|
||||
private static string lastUpdateInfo = null;
|
||||
private const string Rpcs3UpdateStateKey = "Rpcs3UpdateState";
|
||||
private static UpdateInfo CachedUpdateInfo = null;
|
||||
|
||||
static CompatList()
|
||||
{
|
||||
@ -112,47 +113,66 @@ Example usage:
|
||||
await dm.SendMessageAsync(embed: embed).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Command("latest"), Aliases("download"), TriggersTyping]
|
||||
[Group("latest"), Aliases("download"), TriggersTyping]
|
||||
[Description("Provides links to the latest RPCS3 build")]
|
||||
[Cooldown(1, 30, CooldownBucketType.Channel)]
|
||||
public Task Latest(CommandContext ctx)
|
||||
public sealed class UpdatesCheck: BaseCommandModuleCustom
|
||||
{
|
||||
return CheckForRpcs3Updates(ctx.Client, ctx.Channel);
|
||||
}
|
||||
[GroupCommand]
|
||||
public Task Latest(CommandContext ctx)
|
||||
{
|
||||
return CheckForRpcs3Updates(ctx.Client, ctx.Channel);
|
||||
}
|
||||
|
||||
public static async Task<bool> CheckForRpcs3Updates(DiscordClient discordClient, DiscordChannel channel)
|
||||
{
|
||||
var info = await client.GetUpdateAsync(Config.Cts.Token).ConfigureAwait(false);
|
||||
var embed = await info.AsEmbedAsync().ConfigureAwait(false);
|
||||
if (channel != null)
|
||||
await channel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
|
||||
var updateLinks = info?.LatestBuild?.Pr;
|
||||
if (!string.IsNullOrEmpty(updateLinks) && lastUpdateInfo != updateLinks && updateCheck.Wait(0))
|
||||
try
|
||||
[Command("cached")]
|
||||
[Description("Gets the latest known update links, without checking the API")]
|
||||
public async Task Cached(CommandContext ctx)
|
||||
{
|
||||
var tmp = CachedUpdateInfo;
|
||||
if (tmp is UpdateInfo info)
|
||||
{
|
||||
var compatChannel = await discordClient.GetChannelAsync(Config.BotChannelId).ConfigureAwait(false);
|
||||
await compatChannel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
|
||||
lastUpdateInfo = updateLinks;
|
||||
using (var db = new BotDb())
|
||||
var embed = await info.AsEmbedAsync().ConfigureAwait(false);
|
||||
await ctx.RespondAsync(embed: embed.Build()).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
await ctx.ReactWithAsync(Config.Reactions.Failure, "No update information was cached yet").ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static async Task<bool> CheckForRpcs3Updates(DiscordClient discordClient, DiscordChannel channel)
|
||||
{
|
||||
var info = await client.GetUpdateAsync(Config.Cts.Token).ConfigureAwait(false);
|
||||
var embed = await info.AsEmbedAsync().ConfigureAwait(false);
|
||||
if (channel != null)
|
||||
await channel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
|
||||
var updateLinks = info?.LatestBuild?.Pr;
|
||||
if (!string.IsNullOrEmpty(updateLinks) && lastUpdateInfo != updateLinks && updateCheck.Wait(0))
|
||||
try
|
||||
{
|
||||
var currentState = await db.BotState.FirstOrDefaultAsync(k => k.Key == Rpcs3UpdateStateKey).ConfigureAwait(false);
|
||||
if (currentState == null)
|
||||
db.BotState.Add(new BotState {Key = Rpcs3UpdateStateKey, Value = updateLinks});
|
||||
else
|
||||
currentState.Value = updateLinks;
|
||||
await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
|
||||
return true;
|
||||
var compatChannel = await discordClient.GetChannelAsync(Config.BotChannelId).ConfigureAwait(false);
|
||||
await compatChannel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
|
||||
lastUpdateInfo = updateLinks;
|
||||
CachedUpdateInfo = info;
|
||||
using (var db = new BotDb())
|
||||
{
|
||||
var currentState = await db.BotState.FirstOrDefaultAsync(k => k.Key == Rpcs3UpdateStateKey).ConfigureAwait(false);
|
||||
if (currentState == null)
|
||||
db.BotState.Add(new BotState {Key = Rpcs3UpdateStateKey, Value = updateLinks});
|
||||
else
|
||||
currentState.Value = updateLinks;
|
||||
await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Config.Log.Warn(e, "Failed to check for RPCS3 update info");
|
||||
}
|
||||
finally
|
||||
{
|
||||
updateCheck.Release(1);
|
||||
}
|
||||
return false;
|
||||
catch (Exception e)
|
||||
{
|
||||
Config.Log.Warn(e, "Failed to check for RPCS3 update info");
|
||||
}
|
||||
finally
|
||||
{
|
||||
updateCheck.Release(1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static string DicToDesc(Dictionary<char, string[]> dictionary)
|
||||
|
@ -41,7 +41,7 @@ namespace CompatBot.Commands
|
||||
await List(ctx, ctx.Message.Author).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Command("users"), RequiresBotModRole, TriggersTyping]
|
||||
[Command("users"), Aliases("top"), RequiresBotModRole, TriggersTyping]
|
||||
[Description("List users with warnings, sorted from most warned to least")]
|
||||
public async Task Users(CommandContext ctx, [Description("Optional number of items to show. Default is 10")] int number = 10)
|
||||
{
|
||||
|
@ -22,10 +22,10 @@ namespace CompatBot.EventHandlers
|
||||
return;
|
||||
|
||||
if (BuildResult.IsMatch(args.Message.Content))
|
||||
if (!await CompatList.CheckForRpcs3Updates(args.Client, null).ConfigureAwait(false))
|
||||
if (!await CompatList.UpdatesCheck.CheckForRpcs3Updates(args.Client, null).ConfigureAwait(false))
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
|
||||
await CompatList.CheckForRpcs3Updates(args.Client, null).ConfigureAwait(false);
|
||||
await CompatList.UpdatesCheck.CheckForRpcs3Updates(args.Client, null).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ namespace CompatBot
|
||||
{
|
||||
try
|
||||
{
|
||||
CompatList.CheckForRpcs3Updates((DiscordClient)client, null).ConfigureAwait(false).GetAwaiter().GetResult();
|
||||
CompatList.UpdatesCheck.CheckForRpcs3Updates((DiscordClient)client, null).ConfigureAwait(false).GetAwaiter().GetResult();
|
||||
}
|
||||
catch { }
|
||||
Task.Delay(TimeSpan.FromHours(1), Config.Cts.Token).ConfigureAwait(false).GetAwaiter().GetResult();
|
||||
|
@ -13,6 +13,7 @@ namespace CompatBot.Utils
|
||||
.GetEncoding()
|
||||
?? Encoding.ASCII;
|
||||
private static readonly Encoding Utf8 = new UTF8Encoding(false);
|
||||
|
||||
private static readonly HashSet<char> SpaceCharacters = new HashSet<char>
|
||||
{
|
||||
'\u00a0',
|
||||
@ -43,15 +44,14 @@ namespace CompatBot.Utils
|
||||
if (string.IsNullOrEmpty(str))
|
||||
return str;
|
||||
|
||||
int end = str.Length - 1;
|
||||
int start = 0;
|
||||
int start, end;
|
||||
for (start = 0; start < str.Length; start++)
|
||||
if (!char.IsWhiteSpace(str[start]) && !IsFormat(str[start]))
|
||||
break;
|
||||
|
||||
for (end = str.Length - 1; end >= start; end--)
|
||||
if (!char.IsWhiteSpace(str[end]) && !IsFormat(str[end]))
|
||||
break;
|
||||
break;
|
||||
|
||||
return CreateTrimmedString(str, start, end);
|
||||
}
|
||||
@ -99,14 +99,11 @@ namespace CompatBot.Utils
|
||||
|
||||
private static string CreateTrimmedString(string str, int start, int end)
|
||||
{
|
||||
int len = end - start + 1;
|
||||
var len = end - start + 1;
|
||||
if (len == str.Length)
|
||||
return str;
|
||||
|
||||
if (len == 0)
|
||||
return "";
|
||||
|
||||
return str.Substring(start, len);
|
||||
return len == 0 ? "" : str.Substring(start, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user