new command to show last known update information

This commit is contained in:
13xforever 2018-11-08 22:05:07 +05:00
parent fee6ad6ed5
commit f39d5e7431
3 changed files with 57 additions and 37 deletions

View File

@ -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)

View File

@ -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);
}
}
}

View File

@ -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();