1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

Merge remote-tracking branch 'origin/master' into warn

This commit is contained in:
Macintxsh 2024-03-26 19:42:41 +03:00
commit 2564277070
Signed by: mctaylors
GPG key ID: 7181BEBE676903C1

View file

@ -2,8 +2,6 @@
using Octobot.Extensions; using Octobot.Extensions;
using Remora.Discord.API.Abstractions.Objects; using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest; using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.Commands.Conditions;
using Remora.Discord.Commands.Results;
using Remora.Rest.Core; using Remora.Rest.Core;
using Remora.Results; using Remora.Results;
@ -13,32 +11,30 @@ public sealed class AccessControlService
{ {
private readonly GuildDataService _data; private readonly GuildDataService _data;
private readonly IDiscordRestGuildAPI _guildApi; private readonly IDiscordRestGuildAPI _guildApi;
private readonly RequireDiscordPermissionCondition _permission;
private readonly IDiscordRestUserAPI _userApi; private readonly IDiscordRestUserAPI _userApi;
public AccessControlService(GuildDataService data, IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi, public AccessControlService(GuildDataService data, IDiscordRestGuildAPI guildApi, IDiscordRestUserAPI userApi)
RequireDiscordPermissionCondition permission)
{ {
_data = data; _data = data;
_guildApi = guildApi; _guildApi = guildApi;
_userApi = userApi; _userApi = userApi;
_permission = permission;
} }
private async Task<Result<bool>> CheckPermissionAsync(GuildData data, Snowflake memberId, IGuildMember member, private static bool CheckPermission(IEnumerable<IRole> roles, GuildData data, Snowflake memberId,
DiscordPermission permission, CancellationToken ct = default) IGuildMember member,
DiscordPermission permission)
{ {
var moderatorRole = GuildSettings.ModeratorRole.Get(data.Settings); var moderatorRole = GuildSettings.ModeratorRole.Get(data.Settings);
var result = await _permission.CheckAsync(new RequireDiscordPermissionAttribute([permission]), member, ct); if (!moderatorRole.Empty() && data.GetOrCreateMemberData(memberId).Roles.Contains(moderatorRole.Value))
if (result.Error is not null and not PermissionDeniedError)
{ {
return Result<bool>.FromError(result); return true;
} }
var hasPermission = result.IsSuccess; return roles
return hasPermission || (!moderatorRole.Empty() && .Where(r => member.Roles.Contains(r.ID))
data.GetOrCreateMemberData(memberId).Roles.Contains(moderatorRole.Value)); .Any(r =>
r.Permissions.HasPermission(permission)
);
} }
/// <summary> /// <summary>
@ -67,30 +63,35 @@ public sealed class AccessControlService
return Result<string?>.FromSuccess($"UserCannot{action}Themselves".Localized()); return Result<string?>.FromSuccess($"UserCannot{action}Themselves".Localized());
} }
var botResult = await _userApi.GetCurrentUserAsync(ct);
if (!botResult.IsDefined(out var bot))
{
return Result<string?>.FromError(botResult);
}
var guildResult = await _guildApi.GetGuildAsync(guildId, ct: ct); var guildResult = await _guildApi.GetGuildAsync(guildId, ct: ct);
if (!guildResult.IsDefined(out var guild)) if (!guildResult.IsDefined(out var guild))
{ {
return Result<string?>.FromError(guildResult); return Result<string?>.FromError(guildResult);
} }
var targetMemberResult = await _guildApi.GetGuildMemberAsync(guildId, targetId, ct); if (interacterId == guild.OwnerID)
if (!targetMemberResult.IsDefined(out var targetMember))
{ {
return Result<string?>.FromSuccess(null); return Result<string?>.FromSuccess(null);
} }
var botResult = await _userApi.GetCurrentUserAsync(ct);
if (!botResult.IsDefined(out var bot))
{
return Result<string?>.FromError(botResult);
}
var botMemberResult = await _guildApi.GetGuildMemberAsync(guildId, bot.ID, ct); var botMemberResult = await _guildApi.GetGuildMemberAsync(guildId, bot.ID, ct);
if (!botMemberResult.IsDefined(out var botMember)) if (!botMemberResult.IsDefined(out var botMember))
{ {
return Result<string?>.FromError(botMemberResult); return Result<string?>.FromError(botMemberResult);
} }
var targetMemberResult = await _guildApi.GetGuildMemberAsync(guildId, targetId, ct);
if (!targetMemberResult.IsDefined(out var targetMember))
{
return Result<string?>.FromSuccess(null);
}
var rolesResult = await _guildApi.GetGuildRolesAsync(guildId, ct); var rolesResult = await _guildApi.GetGuildRolesAsync(guildId, ct);
if (!rolesResult.IsDefined(out var roles)) if (!rolesResult.IsDefined(out var roles))
{ {
@ -110,18 +111,14 @@ public sealed class AccessControlService
var data = await _data.GetData(guildId, ct); var data = await _data.GetData(guildId, ct);
var permissionResult = await CheckPermissionAsync(data, interacterId.Value, interacter, var hasPermission = CheckPermission(roles, data, interacterId.Value, interacter,
action switch action switch
{ {
"Ban" => DiscordPermission.BanMembers, "Ban" => DiscordPermission.BanMembers,
"Kick" => DiscordPermission.KickMembers, "Kick" => DiscordPermission.KickMembers,
"Mute" or "Unmute" => DiscordPermission.ModerateMembers, "Mute" or "Unmute" => DiscordPermission.ModerateMembers,
_ => throw new Exception() _ => throw new Exception()
}, ct); });
if (!permissionResult.IsDefined(out var hasPermission))
{
return Result<string?>.FromError(permissionResult);
}
return hasPermission return hasPermission
? CheckInteractions(action, guild, roles, targetMember, botMember, interacter) ? CheckInteractions(action, guild, roles, targetMember, botMember, interacter)
@ -137,11 +134,6 @@ public sealed class AccessControlService
return new ArgumentNullError(nameof(targetMember.User)); return new ArgumentNullError(nameof(targetMember.User));
} }
if (!interacter.User.IsDefined(out var interacterUser))
{
return new ArgumentNullError(nameof(interacter.User));
}
if (botMember.User == targetMember.User) if (botMember.User == targetMember.User)
{ {
return Result<string?>.FromSuccess($"UserCannot{action}Bot".Localized()); return Result<string?>.FromSuccess($"UserCannot{action}Bot".Localized());
@ -161,11 +153,6 @@ public sealed class AccessControlService
return Result<string?>.FromSuccess($"BotCannot{action}Target".Localized()); return Result<string?>.FromSuccess($"BotCannot{action}Target".Localized());
} }
if (interacterUser.ID == guild.OwnerID)
{
return Result<string?>.FromSuccess(null);
}
var interacterRoles = roles.Where(r => interacter.Roles.Contains(r.ID)); var interacterRoles = roles.Where(r => interacter.Roles.Contains(r.ID));
var targetInteracterRoleDiff var targetInteracterRoleDiff
= targetRoles.MaxOrDefault(r => r.Position) - interacterRoles.MaxOrDefault(r => r.Position); = targetRoles.MaxOrDefault(r => r.Position) - interacterRoles.MaxOrDefault(r => r.Position);