Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement block check within a precondition
With more preconditions in use, command logging has been modified to also be better able to respond to users in the event of an error. As a result, the bot is now able to respond to users and notify them properly if they fail any preconditions.
- Loading branch information
Showing
10 changed files
with
93 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using BirthdayBot.Data; | ||
using Discord.Interactions; | ||
|
||
namespace BirthdayBot.ApplicationCommands; | ||
|
||
/// <summary> | ||
/// Only users not on the blocklist or affected by moderator mode may use the command.<br/> | ||
/// This is used in the <see cref="BotModuleBase"/> base class. Manually using it anywhere else is unnecessary. | ||
/// </summary> | ||
class EnforceBlockingAttribute : PreconditionAttribute { | ||
public const string FailModerated = "Guild has moderator mode enabled."; | ||
public const string FailBlocked = "User is in the guild's block list."; | ||
public const string ReplyModerated = ":x: This bot is in moderated mode, preventing you from using any bot commands in this server."; | ||
public const string ReplyBlocked = ":x: You have been blocked from using bot commands in this server."; | ||
|
||
public override async Task<PreconditionResult> CheckRequirementsAsync( | ||
IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) { | ||
// Not in guild context, unaffected by blocking | ||
if (context.Guild is not SocketGuild guild) return PreconditionResult.FromSuccess(); | ||
|
||
// Manage Guild permission overrides any blocks | ||
var user = (SocketGuildUser)context.User; | ||
if (user.GuildPermissions.ManageGuild) return PreconditionResult.FromSuccess(); | ||
|
||
var gconf = await guild.GetConfigAsync().ConfigureAwait(false); | ||
|
||
// Bot moderators override any blocks | ||
if (gconf.ModeratorRole.HasValue && user.Roles.Any(r => r.Id == gconf.ModeratorRole.Value)) return PreconditionResult.FromSuccess(); | ||
|
||
// Moderated mode check | ||
if (gconf.IsModerated) return PreconditionResult.FromError(FailModerated); | ||
|
||
// Block list check | ||
if (await gconf.IsUserInBlocklistAsync(user.Id)) return PreconditionResult.FromError(FailBlocked); | ||
|
||
return PreconditionResult.FromSuccess(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Discord.Interactions; | ||
|
||
namespace BirthdayBot.ApplicationCommands; | ||
|
||
/// <summary> | ||
/// Implements the included precondition from Discord.Net, requiring a guild context while using our custom error message.<br/><br/> | ||
/// Combining this with <see cref="RequireBotModeratorAttribute"/> is redundant. If possible, only use the latter instead. | ||
/// </summary> | ||
class RequireGuildContextAttribute : RequireContextAttribute { | ||
public const string Error = "Command not received within a guild context."; | ||
public const string Reply = ":x: This command is only available within a server."; | ||
|
||
public override string ErrorMessage => Error; | ||
|
||
public RequireGuildContextAttribute() : base(ContextType.Guild) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters