in spam channels add button to replace regular product code result with updates

This commit is contained in:
13xforever
2021-06-05 17:39:10 +05:00
parent f4644b2347
commit 3c93ea595e
5 changed files with 86 additions and 25 deletions

View File

@@ -96,7 +96,7 @@ namespace CompatBot.Commands
var productCodes = ProductCodeLookup.GetProductIds(ctx.Message.Content);
if (productCodes.Any())
{
await ProductCodeLookup.LookupAndPostProductCodeEmbedAsync(ctx.Client, ctx.Message, productCodes).ConfigureAwait(false);
await ProductCodeLookup.LookupAndPostProductCodeEmbedAsync(ctx.Client, ctx.Message, ctx.Channel, productCodes).ConfigureAwait(false);
return;
}
@@ -291,12 +291,12 @@ namespace CompatBot.Commands
await using var db = new BotDb();
var currentState = await db.BotState.FirstOrDefaultAsync(k => k.Key == Rpcs3UpdateStateKey).ConfigureAwait(false);
if (currentState == null)
await db.BotState.AddAsync(new BotState {Key = Rpcs3UpdateStateKey, Value = latestUpdatePr}).ConfigureAwait(false);
await db.BotState.AddAsync(new() {Key = Rpcs3UpdateStateKey, Value = latestUpdatePr}).ConfigureAwait(false);
else
currentState.Value = latestUpdatePr;
var savedLastBuild = await db.BotState.FirstOrDefaultAsync(k => k.Key == Rpcs3UpdateBuildKey).ConfigureAwait(false);
if (savedLastBuild == null)
await db.BotState.AddAsync(new BotState {Key = Rpcs3UpdateBuildKey, Value = latestUpdateBuild}).ConfigureAwait(false);
await db.BotState.AddAsync(new() {Key = Rpcs3UpdateBuildKey, Value = latestUpdateBuild}).ConfigureAwait(false);
else
savedLastBuild.Value = latestUpdateBuild;
await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
@@ -358,28 +358,28 @@ namespace CompatBot.Commands
var buildTime = masterBuildInfo?.FinishTime;
if (masterBuildInfo != null)
{
updateInfo = new UpdateInfo
updateInfo = new()
{
ReturnCode = 1,
LatestBuild = new BuildInfo
LatestBuild = new()
{
Datetime = buildTime?.ToString("yyyy-MM-dd HH:mm:ss"),
Pr = mergedPr.Number,
Windows = new BuildLink {Download = masterBuildInfo.WindowsBuildDownloadLink ?? ""},
Linux = new BuildLink {Download = masterBuildInfo.LinuxBuildDownloadLink ?? ""},
Windows = new() {Download = masterBuildInfo.WindowsBuildDownloadLink ?? ""},
Linux = new() {Download = masterBuildInfo.LinuxBuildDownloadLink ?? ""},
},
};
}
else
{
updateInfo = new UpdateInfo
updateInfo = new()
{
ReturnCode = 1,
LatestBuild = new BuildInfo
LatestBuild = new()
{
Pr = mergedPr.Number,
Windows = new BuildLink {Download = ""},
Linux = new BuildLink {Download = ""},
Windows = new() {Download = ""},
Linux = new() {Download = ""},
},
};
}
@@ -430,7 +430,7 @@ namespace CompatBot.Commands
#endif
var channel = await ctx.GetChannelForSpamAsync().ConfigureAwait(false);
if (result?.Results?.Count == 1)
await ProductCodeLookup.LookupAndPostProductCodeEmbedAsync(ctx.Client, ctx.Message, new List<string>(result.Results.Keys)).ConfigureAwait(false);
await ProductCodeLookup.LookupAndPostProductCodeEmbedAsync(ctx.Client, ctx.Message, ctx.Channel, new(result.Results.Keys)).ConfigureAwait(false);
else if (result != null)
foreach (var msg in FormatSearchResults(ctx, result))
await channel.SendAutosplitMessageAsync(msg, blockStart: "", blockEnd: "").ConfigureAwait(false);
@@ -555,7 +555,7 @@ namespace CompatBot.Commands
&& await Client.GetCompatResultAsync(RequestBuilder.Start().SetSearch(productCode), Config.Cts.Token).ConfigureAwait(false) is {} compatItemSearchResult
&& compatItemSearchResult.Results.TryGetValue(productCode, out var compatItem))
{
dbItem = (await db.Thumbnail.AddAsync(new Thumbnail
dbItem = (await db.Thumbnail.AddAsync(new()
{
ProductCode = productCode,
Name = compatItem.Title,
@@ -564,7 +564,7 @@ namespace CompatBot.Commands
if (dbItem is null)
{
Config.Log.Debug($"Missing product code {productCode} in {nameof(ThumbnailDb)}");
dbItem = new Thumbnail();
dbItem = new();
}
if (Enum.TryParse(info.Status, out CompatStatus status))
{
@@ -680,7 +680,7 @@ namespace CompatBot.Commands
{
var dbItem = await db.Thumbnail.FirstOrDefaultAsync(i => i.ProductCode == productCode).ConfigureAwait(false);
if (dbItem is null)
dbItem = (await db.Thumbnail.AddAsync(new Thumbnail
dbItem = (await db.Thumbnail.AddAsync(new()
{
ProductCode = productCode,
Name = titleInfo.Title,

View File

@@ -255,7 +255,7 @@ namespace CompatBot.Commands
return;
}
await ProductCodeLookup.LookupAndPostProductCodeEmbedAsync(ctx.Client, ctx.Message, new List<string> {productCode.ProductCode}).ConfigureAwait(false);
await ProductCodeLookup.LookupAndPostProductCodeEmbedAsync(ctx.Client, ctx.Message, ctx.Channel, new() {productCode.ProductCode}).ConfigureAwait(false);
break;
}
default:

View File

@@ -0,0 +1,54 @@
using System;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
namespace CompatBot.EventHandlers
{
internal static class GlobalButtonHandler
{
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})
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.DefferedMessageUpdate).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);
}
}
}
}
}

View File

@@ -6,6 +6,7 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CompatApiClient;
using CompatApiClient.POCOs;
using CompatBot.Commands.Attributes;
using CompatBot.Database.Providers;
using CompatBot.Utils;
using CompatBot.Utils.ResultFormatters;
@@ -50,10 +51,10 @@ namespace CompatBot.EventHandlers
if (codesToLookup.Count == 0)
return;
await LookupAndPostProductCodeEmbedAsync(c, args.Message, codesToLookup).ConfigureAwait(false);
await LookupAndPostProductCodeEmbedAsync(c, args.Message, args.Channel, codesToLookup).ConfigureAwait(false);
}
public static async Task LookupAndPostProductCodeEmbedAsync(DiscordClient client, DiscordMessage message, List<string> codesToLookup)
public static async Task LookupAndPostProductCodeEmbedAsync(DiscordClient client, DiscordMessage message, DiscordChannel channel, List<string> codesToLookup)
{
await message.ReactWithAsync(Config.Reactions.PleaseWait).ConfigureAwait(false);
try
@@ -61,11 +62,11 @@ namespace CompatBot.EventHandlers
var results = new List<(string code, Task<DiscordEmbedBuilder> task)>(codesToLookup.Count);
foreach (var code in codesToLookup)
results.Add((code, client.LookupGameInfoAsync(code)));
var formattedResults = new List<DiscordEmbedBuilder>(results.Count);
var formattedResults = new List<(string code, DiscordEmbedBuilder builder)>(results.Count);
foreach (var (code, task) in results)
try
{
formattedResults.Add(await task.ConfigureAwait(false));
formattedResults.Add((code, await task.ConfigureAwait(false)));
}
catch (Exception e)
{
@@ -73,16 +74,20 @@ namespace CompatBot.EventHandlers
}
// get only results with unique titles
formattedResults = formattedResults.GroupBy(e => e.Title).Select(g => g.First()).ToList();
formattedResults = formattedResults.DistinctBy(e => e.builder.Title).ToList();
var lookupEmoji = new DiscordComponentEmoji(DiscordEmoji.FromUnicode("🔍"));
foreach (var result in formattedResults)
try
{
await FixAfrikaAsync(client, message, result).ConfigureAwait(false);
await message.Channel.SendMessageAsync(embed: result).ConfigureAwait(false);
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));
await DiscordMessageExtensions.UpdateOrCreateMessageAsync(null, channel, messageBuilder).ConfigureAwait(false);
}
catch (Exception e)
{
Config.Log.Warn(e, $"Couldn't post result for {result.Title}");
Config.Log.Warn(e, $"Couldn't post result for {result.code} ({result.builder.Title})");
}
}
finally

View File

@@ -296,7 +296,9 @@ namespace CompatBot
Config.Log.Debug($"ComponentInteraction: type: {args.Interaction.Type}, id: {args.Interaction.Data.CustomId}, user: {args.Interaction.User}");
return Task.CompletedTask;
};
#endif
#endif
client.ComponentInteractionCreated += GlobalButtonHandler.OnComponentInteraction;
Watchdog.DisconnectTimestamps.Enqueue(DateTime.UtcNow);
try