Merge pull request #127 from 13xforever/vnext

Reduce spam for !explain when users refuse to read
This commit is contained in:
Ilya 2018-11-12 14:39:58 +05:00 committed by GitHub
commit 38a5211286
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 8 deletions

View File

@ -1,7 +1,10 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using CompatBot.Utils;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
namespace CompatBot.Commands.Attributes
{
@ -10,14 +13,27 @@ namespace CompatBot.Commands.Attributes
{
public override async Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help)
{
if (ctx.Channel.IsPrivate || help)
if (help || IsSpamChannel(ctx.Channel))
return true;
if (ctx.Channel.Name.Contains("spam", StringComparison.InvariantCultureIgnoreCase))
return true;
try
{
var msgList = await ctx.Channel.GetMessagesAsync(10).ConfigureAwait(false);
if (msgList.Any(m => m.Author.IsCurrent && m.Content is string s && s.Contains("explain list")))
{
await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);
return false; // we just explained to use #bot-spam or DMs, can't help if people can't read
}
}
catch {}
await ctx.RespondAsync($"`{Config.CommandPrefix}{ctx.Command.QualifiedName}` is limited to bot spam channel and DMs").ConfigureAwait(false);
return false;
}
internal static bool IsSpamChannel(DiscordChannel channel)
{
return channel.IsPrivate || channel.Name.Contains("spam", StringComparison.InvariantCultureIgnoreCase);
}
}
}

View File

@ -22,10 +22,15 @@ namespace CompatBot.Commands
public async Task ShowExplanation(CommandContext ctx, [RemainingText, Description("Term to explain")] string term)
{
await ctx.TriggerTypingAsync().ConfigureAwait(false);
if (string.IsNullOrEmpty(term))
string inSpecificLocation = null;
if (!LimitedToSpamChannel.IsSpamChannel(ctx.Channel))
{
var spamChannel = await ctx.Client.GetChannelAsync(Config.BotSpamId).ConfigureAwait(false);
await ctx.RespondAsync($"You may want to look at available terms by using `{Config.CommandPrefix}explain list` in {spamChannel.Mention} or bot DMs").ConfigureAwait(false);
inSpecificLocation = $" in {spamChannel.Mention} or bot DMs";
}
if (string.IsNullOrEmpty(term))
{
await ctx.RespondAsync($"You may want to look at available terms by using `{Config.CommandPrefix}explain list`{inSpecificLocation}").ConfigureAwait(false);
return;
}
@ -67,8 +72,8 @@ namespace CompatBot.Commands
}
}
var spamCh = await ctx.Client.GetChannelAsync(Config.BotSpamId).ConfigureAwait(false);
await ctx.RespondAsync($"Unknown term `{term.Sanitize()}`. Use `!explain list` to look at defined terms in {spamCh.Mention} or bot DMs").ConfigureAwait(false);
var msg = $"Unknown term `{term.Sanitize()}`. Use `{Config.CommandPrefix}explain list` to look at defined terms{inSpecificLocation}";
await ctx.RespondAsync(msg).ConfigureAwait(false);
}
[Command("add"), RequiresBotModRole]
@ -151,10 +156,11 @@ namespace CompatBot.Commands
await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);
}
[Command("list"), LimitedToSpamChannel, TriggersTyping]
[Command("list"), LimitedToSpamChannel]
[Description("List all known terms that could be used for !explain command")]
public async Task List(CommandContext ctx)
{
await ctx.TriggerTypingAsync().ConfigureAwait(false);
using (var db = new BotDb())
{
var keywords = await db.Explanation.Select(e => e.Keyword).OrderBy(t => t).ToListAsync().ConfigureAwait(false);