Skip to content

Commit e776fd5

Browse files
authored
Reimplement message parsing as a toggleable feature (#338)
#!components: grid-bot, grid-bot-recovery #!deployable-components: grid-bot * Initial whitespace changes * #337: LuaVM changes ~ Drop all type annotations to support #337 ~ Update branches to properly handle LuaVMEnabledForAdmins * #336: Script Logger ~ Reorganize code to support both interaction and command context ~ Add Discord.Net.Commands dependency to Shared.Utility * Update BacktraceUtility No longer log trace back, as this feature is not needed. * Update SocketInteractionExtensions.cs Add method to get the channel, and ingest it via GetChannelAsString * #336: Extension methods ~ Create IUserMessageExtensions to add a method to reply with a file. ~ Create ModuleBaseExtensions to add functionality to reply with reference, reply with file, and reply with multiple files easily within module base. * Update CommandsSettings.cs ~ EnableLockdownCommands: Determines if lockdown commands are enabled, if false then any commands marked with LockdownAttribute will just be passed straight on to the next precondition attribute. ~ LockdownGuildId: The guild permitted to respond to lockdown commands. ~ TextCommandsDisabledWarningText: Warning text to respond with when text commands are disabled, otherwise it will default to a generic message. ~ EnableTextCommands: Determines if text commands should be enabled. ~ ShouldWarnWhenCommandsAreDisabled: Determines if a warning should be responded with when the user tries to use a text command. * Update ScriptLogger.cs Drop dependency on client directly and use the context instead. * Update OnSlashCommandExecuted.cs ~ Drop dependency on discord client directly and use the context client instead. ~ Catch-all on Discord.Net.HttpException * #336: OnCommandExecutedEvent ~ Text-commands version of on interaction executed * Update OnLogMessage.cs Do not handle InteractionException or CommandException as OnInteractionExecuted and OnCommandExecuted events handles it themselves. * #336: OnMessage ~ Add references to CommandService, DiscordClient and ServiceProvider ~ Update _allowedCommandRegex to support underscores, as well as match only on one line to improve performance. ~ Add hashset to store all command aliases to filter out responses and calls to commands service ~ Update all metrics to remove the idea of deprecation against text commands. ~ Add metric for command processing time. ~ Add initialization method called within on ready event finished that will update the command aliases hashset with the currently registered modules. ~ Move around logger instantiation after command alias validation to introduce better performance when commands are not found. ~ Drop log on the users ingesting "previous phase commands" ~ Add branches to check if commands are enabled or disabled, debating on whether or not to put this before maintenance checks. ~ Call on commands service. * Update OnInteraction.cs ~ Drop branch out of interaction service execution to a different thread and just run within the current thread (report back later when it causes problems) * #336: Update Runner.cs ~ Add command service to the container. ~ Add on command executed event. * #336: OnReady ~ Add references to commands service and on command executed event. ~ Add text commands modules. ~ Initialize the on message event after modules are added. * #336: Lock Down Attribute An attribute only allows the command to be executed within the DMs of users within the specified bot role, or within the specified guild (if configured). * #336: RequireBotRoleAttribute Drop the interaction one for the commands one * #336: Private Modules Move all private modules to text commands to help lock them down to specific guilds etc so that they don't flood out command lists. * #336: Modules Port slash commands to text commands. * Local Testing: Noop Job Manager Implement a NOOP job manager to allow local debugging for users who are not on infra networks, or do not have software tools available to run grid servers. * Administration: Bot roles ~ Move around the bot role enum to Shared.Utility. ~ Add a method in admin utility to wrap the individual methods to detect if users are owners, admins or privilaged by using the bot role enum. * Account for change to BotRole enum. Update RequireCommandBotRoleAttribute, ClientSettings and Maintenance modules to account for the movement of the enum to Shared.Utility. * #336: Help Command Add the help command for text based commands. This will cache the embeds for each indivual command on the first usage of the command. * Rewrap on interaction + command service calls Apparently this will cause thread locks. * Update on command and interaction executed. Change case for CommandException * Bump all mfdlabs packages LTS changes, all versions are in sync now to better guage future changes * Reflect package upgrades All loggers now take in a function getter for the log level instead of hard coding the log level. Trace no longer exists and has been changed to Verbose * #336: Text commands clean ups ~ Rename references of interactions to commands. Drop need for summary attribute on command arguments as it is unused within the stack. ~ Clean up constructors of some handlers removing uneeded arguments. * #336: Settings Text command. Settings.cs: ~ This command allows you to: 1. Get environment information about the current application configuration. 2. List all available settings provider names. 3. List all values within a settings provider (cached, can optionally be refreshed just before fetch) -- For now it only functions in Vault-based environments. 4. Get the value of a specific setting in a provider, supported on both Vault and Env var based environments. 5. Support for setting a specific setting value in a provider, for now only supported by Vault providers as base class is only Vault and the base class does not fall back to EnvVar provider when Vault refresh is not enabled within set commands. 6. Support for refreshing all registered auto refresh providers immediately, or in turn refreshing a specific provider (will possibly refresh twice if the refresh thread hits is around the same time) * #336: Minor fix with Help command Correctly fetch the required permission attribute * Update recovery to reflect logger changes * Update recovery log message adaptor * #336: Settings Command List Account for the fact that cached values is a dictionary of strings and JsonElements, 1.1.0 of Configuration should address this issue and it will be removed in a future release * Update Help.cs Signed-off-by: Nikita Petko <[email protected]> * Update ClientSettings.cs Signed-off-by: Nikita Petko <[email protected]> * Update OnLogMessage.cs Signed-off-by: Nikita Petko <[email protected]> * Update OnLogMessage.cs Signed-off-by: Nikita Petko <[email protected]> * Update ScriptLogger.cs Signed-off-by: Nikita Petko <[email protected]> * Update IScriptLogger.cs Signed-off-by: Nikita Petko <[email protected]> --------- Signed-off-by: Nikita Petko <[email protected]>
1 parent d8bf48d commit e776fd5

File tree

135 files changed

+2673
-742
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+2673
-742
lines changed

.editorconfig

100644100755
File mode changed.

.gitattributes

100644100755
File mode changed.

.github/ISSUE_TEMPLATE/blacklist_appeal.yml

100644100755
File mode changed.

.github/ISSUE_TEMPLATE/bug_report.yml

100644100755
File mode changed.

.github/ISSUE_TEMPLATE/config.yml

100644100755
File mode changed.

.github/ISSUE_TEMPLATE/feature_request.yml

100644100755
File mode changed.

.github/ISSUE_TEMPLATE/security_vulnerabilty.yml

100644100755
File mode changed.

.github/workflows/build.yml

100644100755
File mode changed.

.github/workflows/deploy.yml

100644100755
File mode changed.

.github/workflows/docs.yml

100644100755
File mode changed.

.gitignore

100644100755
File mode changed.

.gitmodules

100644100755
File mode changed.

CODEOWNERS

100644100755
File mode changed.

CONTRIBUTING.md

100644100755
File mode changed.

LICENSE

100644100755
File mode changed.

README.md

100644100755
File mode changed.

SECURITY.md

100644100755
File mode changed.

docs/CNAME

100644100755
File mode changed.

docs/assets/logo.svg

100644100755
File mode changed.

docs/assets/theme.css

100644100755
File mode changed.

docs/index.md

100644100755
File mode changed.

docs/legal/index.md

100644100755
File mode changed.

docs/legal/privacy-policy.md

100644100755
File mode changed.

docs/legal/tos.md

100644100755
File mode changed.

docs/overrides/assets/stylesheets/main.css

100644100755
File mode changed.

docs/overrides/main.html

100644100755
File mode changed.

docs/request-data-deletion.md

100644100755
File mode changed.

mkdocs.yml

100644100755
File mode changed.

proto/grid_bot.proto

100644100755
File mode changed.

scripts/clean.sh

100644100755
File mode changed.

services/Directory.Build.props

100644100755
File mode changed.

services/Directory.Build.targets

100644100755
File mode changed.

services/grid-bot/.component.yaml

100644100755
File mode changed.

services/grid-bot/.dockerignore

100644100755
File mode changed.

services/grid-bot/Dockerfile

100644100755
File mode changed.

services/grid-bot/README.md

100644100755
File mode changed.

services/grid-bot/grid-bot-bare.sln

100644100755
File mode changed.

services/grid-bot/grid-bot.sln

100644100755
File mode changed.

services/grid-bot/lib/Directory.Build.props

100644100755
File mode changed.

services/grid-bot/lib/Directory.Build.targets

100644100755
File mode changed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace Grid.Bot.Commands;
2+
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
using Discord.Commands;
9+
10+
using Utility;
11+
12+
/// <summary>
13+
/// An attribute only allows the command to be executed within the DMs of
14+
/// users within the specified bot role, or within the specified guild (if configured).
15+
/// </summary>
16+
/// <remarks>
17+
/// Construct a new instance of <see cref="LockDownCommandAttribute"/>.
18+
/// </remarks>
19+
/// <param name="botRole">The <see cref="BotRole"/>.</param>
20+
public class LockDownCommandAttribute(BotRole botRole = BotRole.Administrator) : PreconditionAttribute
21+
{
22+
private readonly BotRole _botRole = botRole;
23+
24+
/// <summary>
25+
/// The marker to indicate that the command should not respond.
26+
/// </summary>
27+
public const string MarkerDoNotRespond = "___||DO_NOT_RESPOND||___";
28+
29+
/// <inheritdoc cref="PreconditionAttribute.CheckPermissionsAsync(ICommandContext, CommandInfo, IServiceProvider)"/>
30+
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo commandInfo, IServiceProvider services)
31+
{
32+
var commandsSettings = services.GetRequiredService<CommandsSettings>();
33+
if (!commandsSettings.EnableLockdownCommands)
34+
return Task.FromResult(PreconditionResult.FromSuccess());
35+
36+
if (context.Guild is not null)
37+
return context.Guild.Id == commandsSettings.LockdownGuildId
38+
? Task.FromResult(PreconditionResult.FromSuccess())
39+
: Task.FromResult(PreconditionResult.FromError(MarkerDoNotRespond));
40+
41+
var adminUtility = services.GetRequiredService<IAdminUtility>();
42+
43+
var isInRole = _botRole switch
44+
{
45+
BotRole.Privileged => adminUtility.UserIsPrivilaged(context.User),
46+
BotRole.Administrator => adminUtility.UserIsPrivilaged(context.User),
47+
BotRole.Owner => adminUtility.UserIsOwner(context.User),
48+
_ => throw new ArgumentOutOfRangeException(nameof(_botRole), _botRole, null),
49+
};
50+
51+
return isInRole
52+
? Task.FromResult(PreconditionResult.FromSuccess())
53+
: Task.FromResult(PreconditionResult.FromError(MarkerDoNotRespond));
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace Grid.Bot.Commands;
2+
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
using Discord.Commands;
9+
10+
using Utility;
11+
12+
/// <summary>
13+
/// An attribute that validates if the user has the required bot role.
14+
/// </summary>
15+
/// <remarks>
16+
/// Construct a new instance of <see cref="RequireBotRoleAttribute"/>.
17+
/// </remarks>
18+
/// <param name="botRole">The <see cref="Utility.BotRole"/>.</param>
19+
public class RequireBotRoleAttribute(BotRole botRole = BotRole.Privileged) : PreconditionAttribute
20+
{
21+
/// <summary>
22+
/// The role.
23+
/// </summary>
24+
public BotRole BotRole { get; } = botRole;
25+
26+
private const string _permissionDeniedText = "You lack permission to execute this command.";
27+
28+
29+
/// <inheritdoc cref="PreconditionAttribute.CheckPermissionsAsync(ICommandContext, CommandInfo, IServiceProvider)"/>
30+
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo commandInfo, IServiceProvider services)
31+
{
32+
var adminUtility = services.GetRequiredService<IAdminUtility>();
33+
34+
return BotRole switch
35+
{
36+
BotRole.Privileged => Task.FromResult(!adminUtility.UserIsPrivilaged(context.User)
37+
? PreconditionResult.FromError(_permissionDeniedText)
38+
: PreconditionResult.FromSuccess()),
39+
BotRole.Administrator => Task.FromResult(!adminUtility.UserIsPrivilaged(context.User)
40+
? PreconditionResult.FromError(_permissionDeniedText)
41+
: PreconditionResult.FromSuccess()),
42+
BotRole.Owner => Task.FromResult(!adminUtility.UserIsOwner(context.User)
43+
? PreconditionResult.FromError(_permissionDeniedText)
44+
: PreconditionResult.FromSuccess()),
45+
_ => throw new ArgumentOutOfRangeException(nameof(BotRole), BotRole, null),
46+
};
47+
48+
}
49+
}

services/grid-bot/lib/commands/Attributes/RequireInteractionBotRoleAttribute.cs

-51
This file was deleted.

0 commit comments

Comments
 (0)