2019-02-14 18:44:26 +00:00
|
|
|
|
using System.Linq;
|
2019-02-08 00:23:44 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-02-08 16:55:57 +00:00
|
|
|
|
using CompatApiClient.Utils;
|
2019-02-08 00:23:44 +00:00
|
|
|
|
using CompatBot.Commands;
|
2019-02-11 13:19:55 +00:00
|
|
|
|
using CompatBot.Database.Providers;
|
2019-02-08 00:23:44 +00:00
|
|
|
|
using CompatBot.Utils;
|
2019-03-20 12:08:47 +00:00
|
|
|
|
using CompatBot.Utils.Extensions;
|
2019-02-11 13:19:55 +00:00
|
|
|
|
using CompatBot.Utils.ResultFormatters;
|
2019-02-08 00:23:44 +00:00
|
|
|
|
using DSharpPlus.CommandsNext;
|
|
|
|
|
using DSharpPlus.CommandsNext.Exceptions;
|
2019-02-14 18:44:26 +00:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2019-02-08 00:23:44 +00:00
|
|
|
|
|
|
|
|
|
namespace CompatBot.EventHandlers
|
|
|
|
|
{
|
|
|
|
|
internal static class UnknownCommandHandler
|
|
|
|
|
{
|
|
|
|
|
public static async Task OnError(CommandErrorEventArgs e)
|
|
|
|
|
{
|
2019-03-20 12:08:47 +00:00
|
|
|
|
if (e.Context.User.IsBotSafeCheck())
|
2019-02-24 21:05:29 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2019-02-08 00:23:44 +00:00
|
|
|
|
if (!(e.Exception is CommandNotFoundException cnfe))
|
|
|
|
|
{
|
|
|
|
|
Config.Log.Error(e.Exception);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(cnfe.CommandName))
|
|
|
|
|
return;
|
|
|
|
|
|
2019-02-17 18:49:52 +00:00
|
|
|
|
if (e.Context.Prefix != Config.CommandPrefix
|
2019-02-24 21:05:29 +00:00
|
|
|
|
&& e.Context.Prefix != Config.AutoRemoveCommandPrefix
|
2019-02-22 10:32:36 +00:00
|
|
|
|
&& (e.Context.Message.Content?.EndsWith("?") ?? false)
|
2019-02-17 18:49:52 +00:00
|
|
|
|
&& e.Context.CommandsNext.RegisteredCommands.TryGetValue("8ball", out var cmd))
|
2019-02-08 00:23:44 +00:00
|
|
|
|
{
|
|
|
|
|
var updatedContext = e.Context.CommandsNext.CreateContext(
|
|
|
|
|
e.Context.Message,
|
|
|
|
|
e.Context.Prefix,
|
|
|
|
|
cmd,
|
2019-11-05 22:45:31 +00:00
|
|
|
|
e.Context.Message.Content[(e.Context.Prefix.Length)..].Trim()
|
2019-02-08 00:23:44 +00:00
|
|
|
|
);
|
|
|
|
|
try { await cmd.ExecuteAsync(updatedContext).ConfigureAwait(false); } catch { }
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-18 15:00:12 +00:00
|
|
|
|
if (cnfe.CommandName.Length < 3)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-02-22 10:24:22 +00:00
|
|
|
|
#if !DEBUG
|
|
|
|
|
if (e.Context.User.IsSmartlisted(e.Context.Client, e.Context.Guild))
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-06-05 19:06:10 +00:00
|
|
|
|
var pos = e.Context.Message?.Content?.IndexOf(cnfe.CommandName) ?? -1;
|
2019-02-08 00:23:44 +00:00
|
|
|
|
if (pos < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-11-05 22:45:31 +00:00
|
|
|
|
var gameTitle = e.Context.Message.Content[pos..].TrimEager().Trim(40);
|
2019-02-17 18:49:52 +00:00
|
|
|
|
if (string.IsNullOrEmpty(gameTitle) || char.IsPunctuation(gameTitle[0]))
|
|
|
|
|
return;
|
|
|
|
|
|
2019-02-11 13:19:55 +00:00
|
|
|
|
var term = gameTitle.ToLowerInvariant();
|
2019-06-05 19:06:10 +00:00
|
|
|
|
var (explanation, fuzzyMatch, score) = await Explain.LookupTerm(term).ConfigureAwait(false);
|
|
|
|
|
if (score > 0.5 && explanation != null)
|
2019-02-08 00:23:44 +00:00
|
|
|
|
{
|
2019-06-05 19:06:10 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(fuzzyMatch))
|
2019-02-11 13:19:55 +00:00
|
|
|
|
{
|
2019-06-05 19:06:10 +00:00
|
|
|
|
var fuzzyNotice = $"Showing explanation for `{fuzzyMatch}`:";
|
2019-02-11 13:19:55 +00:00
|
|
|
|
#if DEBUG
|
2019-06-05 19:06:10 +00:00
|
|
|
|
fuzzyNotice = $"Showing explanation for `{fuzzyMatch}` ({score:0.######}):";
|
2019-02-11 13:19:55 +00:00
|
|
|
|
#endif
|
|
|
|
|
await e.Context.RespondAsync(fuzzyNotice).ConfigureAwait(false);
|
|
|
|
|
}
|
2019-06-05 19:06:10 +00:00
|
|
|
|
StatsStorage.ExplainStatCache.TryGetValue(explanation.Keyword, out int stat);
|
|
|
|
|
StatsStorage.ExplainStatCache.Set(explanation.Keyword, ++stat, StatsStorage.CacheTime);
|
|
|
|
|
await e.Context.Channel.SendMessageAsync(explanation.Text, explanation.Attachment, explanation.AttachmentFilename).ConfigureAwait(false);
|
2019-02-08 00:23:44 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 13:19:55 +00:00
|
|
|
|
gameTitle = CompatList.FixGameTitleSearch(gameTitle);
|
|
|
|
|
var productCodes = ProductCodeLookup.GetProductIds(gameTitle);
|
|
|
|
|
if (productCodes.Any())
|
2019-02-08 00:23:44 +00:00
|
|
|
|
{
|
2019-02-11 13:19:55 +00:00
|
|
|
|
await ProductCodeLookup.LookupAndPostProductCodeEmbedAsync(e.Context.Client, e.Context.Message, productCodes).ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (productCode, titleInfo) = await IsTheGamePlayableHandler.LookupGameAsync(e.Context.Channel, e.Context.Message, gameTitle).ConfigureAwait(false);
|
|
|
|
|
if (titleInfo != null)
|
|
|
|
|
{
|
|
|
|
|
var thumbUrl = await e.Context.Client.GetThumbnailUrlAsync(productCode).ConfigureAwait(false);
|
|
|
|
|
var embed = titleInfo.AsEmbed(productCode, thumbnailUrl: thumbUrl);
|
2019-02-11 13:31:38 +00:00
|
|
|
|
await ProductCodeLookup.FixAfrikaAsync(e.Context.Client, e.Context.Message, embed).ConfigureAwait(false);
|
2019-02-11 13:19:55 +00:00
|
|
|
|
await e.Context.Channel.SendMessageAsync(embed: embed).ConfigureAwait(false);
|
|
|
|
|
return;
|
2019-02-08 00:23:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 13:19:55 +00:00
|
|
|
|
var ch = await e.Context.GetChannelForSpamAsync().ConfigureAwait(false);
|
|
|
|
|
await ch.SendMessageAsync(
|
|
|
|
|
$"I am not sure what you wanted me to do, please use one of the following commands:\n" +
|
2019-06-06 05:16:24 +00:00
|
|
|
|
$"`{Config.CommandPrefix}c {term.Sanitize(replaceBackTicks: true)}` to check the game status\n" +
|
2019-02-11 13:19:55 +00:00
|
|
|
|
$"`{Config.CommandPrefix}explain list` to look at the list of available explanations\n" +
|
|
|
|
|
$"`{Config.CommandPrefix}help` to look at available bot commands\n"
|
|
|
|
|
).ConfigureAwait(false);
|
2019-02-08 00:23:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|