Merge pull request #883 from 13xforever/vnext

Try to fix random stuff
This commit is contained in:
clienthax 2022-07-05 17:05:01 +01:00 committed by GitHub
commit 54c47b7f11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 82 deletions

View File

@ -16,6 +16,7 @@ using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Interactivity.Extensions;
using PsnClient.POCOs;
@ -134,9 +135,6 @@ internal sealed partial class Psn
var sqvat = ctx.Client.GetEmoji(":sqvat:", Config.Reactions.No)!;
await ctx.Message.ReactWithAsync(sqvat).ConfigureAwait(false);
}
if (embeds.Count > 1 || embeds[0].Fields.Count > 0)
embeds[^1] = embeds.Last().WithFooter("Note that you need to install ALL listed updates, one by one");
var resultMsgBuilder = new DiscordMessageBuilder()
.WithEmbed(embeds[0])
.WithReply(ctx.Message.Id);
@ -221,5 +219,38 @@ internal sealed partial class Psn
await Task.Delay(TimeSpan.FromHours(1), cancellationToken).ConfigureAwait(false);
}
}
internal static async Task OnCheckUpdatesButtonClick(DiscordClient client, ComponentInteractionCreateEventArgs e)
{
var btnId = e.Interaction.Data.CustomId;
var parts = btnId.Split(':');
if (parts.Length != 4)
{
Config.Log.Warn("Invalid interaction id: " + btnId);
return;
}
try
{
var authorId = ulong.Parse(parts[1]);
var refMsgId = ulong.Parse(parts[2]);
var productCode = parts[3];
if (e.User.Id != authorId)
return;
e.Handled = true;
await e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate).ConfigureAwait(false);
await e.Message.DeleteAsync().ConfigureAwait(false);
var refMsg = await e.Channel.GetMessageAsync(refMsgId).ConfigureAwait(false);
var cne = client.GetCommandsNext();
var cmd = cne.FindCommand("psn check updates", out _);
var context = cne.CreateContext(refMsg, Config.CommandPrefix, cmd, productCode);
await cne.ExecuteCommandAsync(context).ConfigureAwait(false);
}
catch (Exception ex)
{
Config.Log.Warn(ex);
}
}
}
}

View File

@ -1,52 +1,23 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CompatBot.Commands;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.EventArgs;
namespace CompatBot.EventHandlers;
internal static class GlobalButtonHandler
{
private const string ReplaceWithUpdatesPrefix = "replace with game updates:";
public static async Task OnComponentInteraction(DiscordClient sender, ComponentInteractionCreateEventArgs e)
{
if (e.Interaction.Type != InteractionType.Component
|| e.Interaction.Data.ComponentType != ComponentType.Button
|| e.Interaction.Data.CustomId is not {Length: >0})
if (e.Interaction is not { Type: InteractionType.Component }
or not { Data.ComponentType: ComponentType.Button }
or not { Data.CustomId.Length: > 0 })
return;
const string replaceWithUpdatesPrefix = "replace with game updates:";
var btnId = e.Interaction.Data.CustomId;
if (btnId.StartsWith(replaceWithUpdatesPrefix))
{
var parts = btnId.Split(':');
if (parts.Length != 4)
{
Config.Log.Warn("Invalid interaction id: " + btnId);
return;
}
try
{
var authorId = ulong.Parse(parts[1]);
var refMsgId = ulong.Parse(parts[2]);
var productCode = parts[3];
if (e.User.Id != authorId)
return;
e.Handled = true;
await e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate).ConfigureAwait(false);
await e.Message.DeleteAsync().ConfigureAwait(false);
var refMsg = await e.Channel.GetMessageAsync(refMsgId).ConfigureAwait(false);
var cne = sender.GetCommandsNext();
var cmd = cne.FindCommand("psn check updates", out _);
var context = cne.CreateContext(refMsg, Config.CommandPrefix, cmd, productCode);
await cne.ExecuteCommandAsync(context).ConfigureAwait(false);
}
catch (Exception ex)
{
Config.Log.Warn(ex);
}
}
if (btnId.StartsWith(ReplaceWithUpdatesPrefix))
await Psn.Check.OnCheckUpdatesButtonClick(sender, e);
}
}

View File

@ -13,7 +13,6 @@ using CompatBot.Utils.ResultFormatters;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using Microsoft.Extensions.Caching.Memory;
namespace CompatBot.EventHandlers;
@ -52,6 +51,8 @@ internal static class IsTheGamePlayableHandler
if (!matches.Any())
return;
try
{
var gameTitle = matches.Select(m => m.Groups["game_title_1"].Value)
.Concat(matches.Select(m => m.Groups["game_title_2"].Value))
.Concat(matches.Select(m => m.Groups["game_title_3"].Value))
@ -91,6 +92,11 @@ internal static class IsTheGamePlayableHandler
await args.Channel.SendMessageAsync(msg).ConfigureAwait(false);
CooldownBuckets[args.Channel.Id] = DateTime.UtcNow;
}
catch (Exception e)
{
Config.Log.Error(e);
}
}
public static async Task<(string? productCode, TitleInfo? info)> LookupGameAsync(DiscordChannel channel, DiscordMessage message, string gameTitle)
{

View File

@ -82,7 +82,7 @@ internal static class ProductCodeLookup
await FixAfrikaAsync(client, message, result.builder).ConfigureAwait(false);
var messageBuilder = new DiscordMessageBuilder().WithEmbed(result.builder);
if (LimitedToSpamChannel.IsSpamChannel(channel))
messageBuilder.AddComponents(new DiscordButtonComponent(ButtonStyle.Secondary, $"replace with game updates:{message.Author.Id}:{message.Id}:{result.code}", "Check game updates instead", emoji: lookupEmoji));
messageBuilder.AddComponents(new DiscordButtonComponent(ButtonStyle.Secondary, $"replace with game updates:{message.Author.Id}:{message.Id}:{result.code}", "Check for updates", emoji: lookupEmoji));
await DiscordMessageExtensions.UpdateOrCreateMessageAsync(null, channel, messageBuilder).ConfigureAwait(false);
}
catch (Exception e)

View File

@ -33,9 +33,9 @@ internal static class TitlePatchFormatter
var pages = pkgs.Length / EmbedPager.MaxFields + (pkgs.Length % EmbedPager.MaxFields == 0 ? 0 : 1);
if (pages > 1)
embedBuilder.Title = $"{title} [Part 1 of {pages}]".Trim(EmbedPager.MaxFieldTitleLength);
var desc = $"Total download size of all {pkgs.Length} packages is {pkgs.Sum(p => p.Size).AsStorageUnit()}";
if (patch?.OfflineCacheTimestamp is DateTime cacheTimestamp)
desc = $"{desc}\n(Offline cache, last updated {(DateTime.UtcNow - cacheTimestamp).AsTimeDeltaDescription()} ago)";
var desc = $" Total download size of all {pkgs.Length} packages is {pkgs.Sum(p => p.Size).AsStorageUnit()}.\n" +
"\n" +
"⚠️ You **must** install updates starting with the first. You **can not** install only the latest update.";
embedBuilder.Description = desc;
var i = 0;
do
@ -64,6 +64,8 @@ internal static class TitlePatchFormatter
embedBuilder.Description = "No update information available";
if (!result.Any() || embedBuilder.Fields.Any())
result.Add(embedBuilder);
if (patch?.OfflineCacheTimestamp is DateTime cacheTimestamp)
result[^1].WithFooter($"Offline cache, last updated {(DateTime.UtcNow - cacheTimestamp).AsTimeDeltaDescription()} ago");
return result;
}