1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-30 11:09:54 +03:00

Add 'ModeratorRole' guild setting (#290)

Octobot has various moderation commands such as /ban, /mute, /kick.
These commands add multiple features to Discord's built-in functions
(such as durations and logging). Some admins may want to force their
users to use Octobot's commands instead of Discord UI functions.
However, due to the current design, they can't take away the permissions
as that remove access to the respective command.

This PR adds the `ModeratorRole` option which allows anyone who has
`ManageMessages` permission and the role to perform any moderator
action.

If the role is not set, the Discord permissions are checked instead. If
the user doesn't have the role, but has the permission, they can still
run the command.

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2024-03-24 23:29:10 +05:00 committed by GitHub
parent 5e4d0a528c
commit 171cfaea1a
Signed by: GitHub
GPG key ID: B5690EEEBB952194
13 changed files with 252 additions and 163 deletions

View file

@ -28,6 +28,7 @@ namespace Octobot.Commands;
[UsedImplicitly]
public class BanCommandGroup : CommandGroup
{
private readonly AccessControlService _access;
private readonly IDiscordRestChannelAPI _channelApi;
private readonly ICommandContext _context;
private readonly IFeedbackService _feedback;
@ -36,16 +37,16 @@ public class BanCommandGroup : CommandGroup
private readonly IDiscordRestUserAPI _userApi;
private readonly Utility _utility;
public BanCommandGroup(
ICommandContext context, IDiscordRestChannelAPI channelApi, GuildDataService guildData,
IFeedbackService feedback, IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi,
Utility utility)
public BanCommandGroup(AccessControlService access, IDiscordRestChannelAPI channelApi, ICommandContext context,
IFeedbackService feedback, IDiscordRestGuildAPI guildApi, GuildDataService guildData,
IDiscordRestUserAPI userApi, Utility utility)
{
_context = context;
_access = access;
_channelApi = channelApi;
_guildData = guildData;
_context = context;
_feedback = feedback;
_guildApi = guildApi;
_guildData = guildData;
_userApi = userApi;
_utility = utility;
}
@ -65,10 +66,10 @@ public class BanCommandGroup : CommandGroup
/// </returns>
/// <seealso cref="ExecuteUnban" />
[Command("ban", "бан")]
[DiscordDefaultMemberPermissions(DiscordPermission.BanMembers)]
[DiscordDefaultMemberPermissions(DiscordPermission.ManageMessages)]
[DiscordDefaultDMPermission(false)]
[RequireContext(ChannelContext.Guild)]
[RequireDiscordPermission(DiscordPermission.BanMembers)]
[RequireDiscordPermission(DiscordPermission.ManageMessages)]
[RequireBotDiscordPermissions(DiscordPermission.BanMembers)]
[Description("Ban user")]
[UsedImplicitly]
@ -128,7 +129,8 @@ public class BanCommandGroup : CommandGroup
}
private async Task<Result> BanUserAsync(
IUser executor, IUser target, string reason, TimeSpan? duration, IGuild guild, GuildData data, Snowflake channelId,
IUser executor, IUser target, string reason, TimeSpan? duration, IGuild guild, GuildData data,
Snowflake channelId,
IUser bot, CancellationToken ct = default)
{
var existingBanResult = await _guildApi.GetGuildBanAsync(guild.ID, target.ID, ct);
@ -141,7 +143,7 @@ public class BanCommandGroup : CommandGroup
}
var interactionResult
= await _utility.CheckInteractionsAsync(guild.ID, executor.ID, target.ID, "Ban", ct);
= await _access.CheckInteractionsAsync(guild.ID, executor.ID, target.ID, "Ban", ct);
if (!interactionResult.IsSuccess)
{
return ResultExtensions.FromError(interactionResult);
@ -155,7 +157,8 @@ public class BanCommandGroup : CommandGroup
return await _feedback.SendContextualEmbedResultAsync(errorEmbed, ct: ct);
}
var builder = new StringBuilder().AppendBulletPointLine(string.Format(Messages.DescriptionActionReason, reason));
var builder =
new StringBuilder().AppendBulletPointLine(string.Format(Messages.DescriptionActionReason, reason));
if (duration is not null)
{
builder.AppendBulletPoint(
@ -221,10 +224,10 @@ public class BanCommandGroup : CommandGroup
/// <seealso cref="ExecuteBanAsync" />
/// <seealso cref="MemberUpdateService.TickMemberDataAsync" />
[Command("unban")]
[DiscordDefaultMemberPermissions(DiscordPermission.BanMembers)]
[DiscordDefaultMemberPermissions(DiscordPermission.ManageMessages)]
[DiscordDefaultDMPermission(false)]
[RequireContext(ChannelContext.Guild)]
[RequireDiscordPermission(DiscordPermission.BanMembers)]
[RequireDiscordPermission(DiscordPermission.ManageMessages)]
[RequireBotDiscordPermissions(DiscordPermission.BanMembers)]
[Description("Unban user")]
[UsedImplicitly]
@ -286,7 +289,8 @@ public class BanCommandGroup : CommandGroup
.WithColour(ColorsList.Green).Build();
var title = string.Format(Messages.UserUnbanned, target.GetTag());
var description = new StringBuilder().AppendBulletPoint(string.Format(Messages.DescriptionActionReason, reason));
var description =
new StringBuilder().AppendBulletPoint(string.Format(Messages.DescriptionActionReason, reason));
_utility.LogAction(
data.Settings, channelId, executor, title, description.ToString(), target, ColorsList.Green, ct: ct);