fix bot status command

This commit is contained in:
13xforever
2025-03-15 18:23:18 +05:00
parent 0ea16cc797
commit 9fb994c14f
12 changed files with 43 additions and 45 deletions

View File

@@ -1,5 +1,4 @@
using System.Globalization;
using DSharpPlus.Commands.Processors.SlashCommands;
using org.mariuszgromada.math.mxparser;
using License = org.mariuszgromada.math.mxparser.License;
@@ -16,9 +15,10 @@ internal sealed class BotMath
[Description("Math; there you go, Juhn")]
public async ValueTask Calc(SlashCommandContext ctx, [RemainingText, Description("Math expression or `help` for syntax link")] string expression)
{
var ephemeral = !ctx.Channel.IsSpamChannel();
if (expression.Equals("help", StringComparison.OrdinalIgnoreCase))
{
await ctx.RespondAsync("Help for all the features and built-in constants and functions could be found at [mXparser website](<https://mathparser.org/mxparser-math-collection/>)", true);
await ctx.RespondAsync("Help for all the features and built-in constants and functions could be found at [mXparser website](<https://mathparser.org/mxparser-math-collection/>)", ephemeral);
return;
}
@@ -50,6 +50,6 @@ internal sealed class BotMath
{
Config.Log.Warn(e, "Math failed");
}
await ctx.RespondAsync(result, true).ConfigureAwait(false);
await ctx.RespondAsync(result, ephemeral).ConfigureAwait(false);
}
}

View File

@@ -1,6 +1,7 @@
using System.Runtime.InteropServices;
using CompatApiClient;
using CompatApiClient.Utils;
using CompatBot.Commands.Checks;
using CompatBot.Database;
using CompatBot.Database.Providers;
using CompatBot.EventHandlers;
@@ -9,12 +10,11 @@ using Microsoft.EntityFrameworkCore;
namespace CompatBot.Commands;
[Command("stats"), TextAlias("status")]
internal sealed class BotStats
internal sealed class BotStatus
{
[Command("show"), DefaultGroupCommand]
[Description("Use to look at various runtime stats")]
public async Task Show(CommandContext ctx)
[Command("status")]
[Description("Bot subsystem configuration status and various runtime stats")]
public async Task Show(SlashCommandContext ctx)
{
var latency = ctx.Client.GetConnectionLatency(Config.BotGuildId);
var embed = new DiscordEmbedBuilder
@@ -65,15 +65,9 @@ internal sealed class BotStats
#if DEBUG
embed.WithFooter("Test Instance");
#endif
var ch = await ctx.GetChannelForSpamAsync().ConfigureAwait(false);
await ch.SendMessageAsync(embed: embed).ConfigureAwait(false);
await ctx.RespondAsync(embed: embed, ephemeral: !ctx.Channel.IsSpamChannel());
}
[Command("hw"), TextAlias("hardware")]
[Description("Various hardware stats from uploaded log files")]
//[Cooldown(1, 5, CooldownBucketType.Guild)]
public Task Hardware(CommandContext ctx, [Description("Desired period in days, default is 30")] int period = 30) => Commands.Hardware.ShowStats(ctx, period);
private static string GetConfiguredApiStats()
=> $"""
{(GoogleDriveHandler.ValidateCredentials() ? "✅" : "❌")} Google Drive

View File

@@ -9,20 +9,6 @@ internal class LimitedToSpecificChannelsCheck:
IContextCheck<RequiresDmAttribute>,
IContextCheck<RequiresNotMediaAttribute>
{
internal static bool IsHelpChannel(DiscordChannel channel)
=> channel.IsPrivate
|| channel.Name.Contains("help", StringComparison.OrdinalIgnoreCase)
|| channel.Name.Equals("donors", StringComparison.OrdinalIgnoreCase);
internal static bool IsOfftopicChannel(DiscordChannel channel)
=> channel.Name.Contains("off-topic", StringComparison.InvariantCultureIgnoreCase)
|| channel.Name.Contains("offtopic", StringComparison.InvariantCultureIgnoreCase);
internal static bool IsSpamChannel(DiscordChannel channel)
=> channel.IsPrivate
|| channel.Name.Contains("spam", StringComparison.OrdinalIgnoreCase)
|| channel.Name.Equals("testers", StringComparison.OrdinalIgnoreCase);
internal static async Task<DiscordChannel?> GetHelpChannelAsync(DiscordClient client, DiscordChannel channel, DiscordUser user)
{
var guild = channel.Guild;
@@ -35,21 +21,21 @@ internal class LimitedToSpecificChannelsCheck:
public ValueTask<string?> ExecuteCheckAsync(LimitedToHelpChannelAttribute attr, CommandContext ctx)
{
if (ctx.Channel.IsPrivate || IsHelpChannel(ctx.Channel))
if (ctx.Channel.IsPrivate || ctx.Channel.IsHelpChannel())
return ValueTask.FromResult<string?>(null);
return ValueTask.FromResult("This command is limited to help channel and DMs")!;
}
public ValueTask<string?> ExecuteCheckAsync(LimitedToOfftopicChannelAttribute attr, CommandContext ctx)
{
if (IsSpamChannel(ctx.Channel) || IsOfftopicChannel(ctx.Channel))
if (ctx.Channel.IsSpamChannel() || ctx.Channel.IsOfftopicChannel())
return ValueTask.FromResult<string?>(null);
return ValueTask.FromResult("This command is limited to off-topic channels and DMs")!;
}
public async ValueTask<string?> ExecuteCheckAsync(LimitedToSpamChannelAttribute attr, CommandContext ctx)
{
if (IsSpamChannel(ctx.Channel))
if (ctx.Channel.IsSpamChannel())
return null;
var spamChannel = await ctx.Client.GetChannelAsync(Config.BotSpamId).ConfigureAwait(false);

View File

@@ -35,8 +35,8 @@ internal sealed class Vision
private static readonly Dictionary<string, string[]> Reactions = new(StringComparer.OrdinalIgnoreCase)
{
["cat"] = BotStats.GoodKot,
["dog"] = BotStats.GoodDog,
["cat"] = BotStatus.GoodKot,
["dog"] = BotStatus.GoodDog,
["hedgehog"] = ["🦔",],
["flower"] = ["🌷", "🌸", "🌹", "🌺", "🌼", "🥀", "💐", "🌻", "💮",],
["lizard"] = ["🦎",],

View File

@@ -157,8 +157,8 @@ namespace CompatBot.EventHandlers
lock (TheDoor)
{
emoji = ThankYouReactions[Rng.Next(ThankYouReactions.Length)];
thankYouMessage = LimitedToSpecificChannelsCheck.IsSpamChannel(args.Channel)
|| LimitedToSpecificChannelsCheck.IsOfftopicChannel(args.Channel)
thankYouMessage = args.Channel.IsSpamChannel()
|| args.Channel.IsOfftopicChannel()
? ThankYouMessages[Rng.Next(ThankYouMessages.Length)]
: null;
}

View File

@@ -14,7 +14,7 @@ internal static partial class LogAsTextMonitor
if (DefaultHandlerFilter.IsFluff(args.Message))
return;
if (!LimitedToSpecificChannelsCheck.IsHelpChannel(args.Channel))
if (!args.Channel.IsHelpChannel())
return;
if ((args.Message.Author as DiscordMember)?.Roles.Any() ?? false)

View File

@@ -61,8 +61,8 @@ public static class LogParsingHandler
|| message.Content.StartsWith(Config.AutoRemoveCommandPrefix)))
return Task.CompletedTask;
var isSpamChannel = LimitedToSpecificChannelsCheck.IsSpamChannel(args.Channel);
var isHelpChannel = LimitedToSpecificChannelsCheck.IsHelpChannel(args.Channel);
var isSpamChannel = args.Channel.IsSpamChannel();
var isHelpChannel = args.Channel.IsHelpChannel();
var checkExternalLinks = isHelpChannel || isSpamChannel;
OnNewLog(c, args.Channel, args.Message, checkExternalLinks: checkExternalLinks);
return Task.CompletedTask;
@@ -96,8 +96,8 @@ public static class LogParsingHandler
s?.Dispose();
}
var isSpamChannel = LimitedToSpecificChannelsCheck.IsSpamChannel(channel);
var isHelpChannel = LimitedToSpecificChannelsCheck.IsHelpChannel(channel);
var isSpamChannel = channel.IsSpamChannel();
var isHelpChannel = channel.IsHelpChannel();
if (source != null)
{
Config.Log.Debug($">>>>>>> {message.Id % 100} Parsing log '{source.FileName}' from {message.Author.Username}#{message.Author.Discriminator} ({message.Author.Id}) using {source.GetType().Name} ({source.SourceFileSize} bytes)…");

View File

@@ -27,7 +27,7 @@ internal static partial class PostLogHelpHandler
if (DefaultHandlerFilter.IsFluff(args.Message))
return;
if (!LimitedToSpecificChannelsCheck.IsHelpChannel(args.Channel))
if (!args.Channel.IsHelpChannel())
return;
if (DateTime.UtcNow - lastMention < ThrottlingThreshold)

View File

@@ -73,7 +73,7 @@ internal static partial class ProductCodeLookup
try
{
var messageBuilder = new DiscordMessageBuilder().AddEmbed(result.builder);
if (LimitedToSpecificChannelsCheck.IsSpamChannel(channel))
if (channel.IsSpamChannel())
messageBuilder.AddComponents(new DiscordButtonComponent(DiscordButtonStyle.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);
}

View File

@@ -12,9 +12,9 @@ global using CompatBot.Utils;
global using DSharpPlus;
global using DSharpPlus.Commands;
global using DSharpPlus.Commands.ArgumentModifiers;
global using DSharpPlus.Commands.Processors.SlashCommands;
global using DSharpPlus.Commands.Trees;
global using DSharpPlus.Commands.Trees.Metadata;
global using DSharpPlus.Entities;
global using DSharpPlus.EventArgs;
global using DSharpPlus.Interactivity.Extensions;

View File

@@ -0,0 +1,18 @@
namespace CompatBot.Utils;
internal static class DiscordChannelExtensions
{
internal static bool IsHelpChannel(this DiscordChannel channel)
=> channel.IsPrivate
|| channel.Name.Contains("help", StringComparison.OrdinalIgnoreCase)
|| channel.Name.Equals("donors", StringComparison.OrdinalIgnoreCase);
internal static bool IsOfftopicChannel(this DiscordChannel channel)
=> channel.Name.Contains("off-topic", StringComparison.InvariantCultureIgnoreCase)
|| channel.Name.Contains("offtopic", StringComparison.InvariantCultureIgnoreCase);
internal static bool IsSpamChannel(this DiscordChannel channel)
=> channel.IsPrivate
|| channel.Name.Contains("spam", StringComparison.OrdinalIgnoreCase)
|| channel.Name.Equals("testers", StringComparison.OrdinalIgnoreCase);
}

View File

@@ -51,7 +51,7 @@ public static partial class CommandContextExtensions
=> ctx.Channel.IsPrivate || ctx.Member is null ? ctx.Channel : await ctx.Member.CreateDmChannelAsync().ConfigureAwait(false);
public static Task<DiscordChannel> GetChannelForSpamAsync(this CommandContext ctx)
=> LimitedToSpecificChannelsCheck.IsSpamChannel(ctx.Channel) ? Task.FromResult(ctx.Channel) : ctx.CreateDmAsync();
=> ctx.Channel.IsSpamChannel() ? Task.FromResult(ctx.Channel) : ctx.CreateDmAsync();
public static Task<string> GetUserNameAsync(this CommandContext ctx, ulong userId, bool? forDmPurposes = null, string defaultName = "Unknown user")
=> ctx.Client.GetUserNameAsync(ctx.Channel, userId, forDmPurposes, defaultName);