Skip to content

Commit

Permalink
Implement the new DSP permission system
Browse files Browse the repository at this point in the history
  • Loading branch information
Sella-GH committed Nov 12, 2024
1 parent 39dad62 commit 2068d4f
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

### Dependencies
- Added [NReco.Logging.File](https://github.com/nreco/logging) in version 1.2.1
- Updated [DSharpPlus](https://github.com/DSharpPlus/DSharpPlus) to version 5.0.0-nightly-02407
- Updated [DSharpPlus](https://github.com/DSharpPlus/DSharpPlus) to version 5.0.0-nightly-02408
- Updated [Lavalink4NET](https://github.com/angelobreuer/Lavalink4NET) to version 4.0.26-preview.4

### Additions
Expand Down
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="CsvHelper" Version="33.0.1" />
<PackageVersion Include="DSharpPlus" Version="5.0.0-nightly-02407" />
<PackageVersion Include="DSharpPlus.Commands" Version="5.0.0-nightly-02407" />
<PackageVersion Include="DSharpPlus.Interactivity" Version="5.0.0-nightly-02407" />
<PackageVersion Include="DSharpPlus" Version="5.0.0-nightly-02408" />
<PackageVersion Include="DSharpPlus.Commands" Version="5.0.0-nightly-02408" />
<PackageVersion Include="DSharpPlus.Interactivity" Version="5.0.0-nightly-02408" />
<PackageVersion Include="EntityFrameworkCore.Exceptions.PostgreSQL" Version="8.1.3" />
<PackageVersion Include="Lavalink4NET.DSharpPlus.Nightly" Version="4.0.26-preview.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0-rc.2.24474.1" />
Expand Down
2 changes: 1 addition & 1 deletion src/AzzyBot.Bot/Commands/AdminCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace AzzyBot.Bot.Commands;
[SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "DSharpPlus best practice")]
public sealed class AdminCommands
{
[Command("admin"), RequireGuild, RequireApplicationOwner, RequirePermissions(DiscordPermissions.None, DiscordPermissions.Administrator)]
[Command("admin"), RequireGuild, RequireApplicationOwner, RequirePermissions(BotPermissions = [], UserPermissions = [DiscordPermission.Administrator])]
public sealed class AdminGroup(ILogger<AdminGroup> logger, AzzyBotSettingsRecord settings, DbActions dbActions, DiscordBotService botService)
{
private readonly ILogger<AdminGroup> _logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ValueTask<IEnumerable<DiscordAutoCompleteChoice>> AutoCompleteAsync(AutoC
ulong guildId = context.Guild.Id;
DiscordMember member = context.Member;

bool adminServer = botOwners.Any(u => u.Id == context.User.Id && member.Permissions.HasPermission(DiscordPermissions.Administrator) && guildId == _settings.ServerId);
bool adminServer = botOwners.Any(u => u.Id == context.User.Id && member.Permissions.HasPermission(DiscordPermission.Administrator) && guildId == _settings.ServerId);
bool approvedDebug = guildId == _settings.ServerId;
List<DiscordAutoCompleteChoice> results = new(25);
foreach (List<AzzyHelpRecord> kvp in AzzyHelp.GetAllCommands(context.Extension.Commands, adminServer, approvedDebug, member).Select(k => k.Value))
Expand Down
4 changes: 2 additions & 2 deletions src/AzzyBot.Bot/Commands/AzuraCastCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace AzzyBot.Bot.Commands;
[SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "DSharpPlus best practice")]
public sealed class AzuraCastCommands
{
[Command("azuracast"), RequireGuild, RequirePermissions(DiscordPermissions.None, DiscordPermissions.Administrator), ModuleActivatedCheck(AzzyModules.AzuraCast)]
[Command("azuracast"), RequireGuild, RequirePermissions(BotPermissions = [], UserPermissions = [DiscordPermission.Administrator]), ModuleActivatedCheck(AzzyModules.AzuraCast)]
public sealed class AzuraCastGroup(ILogger<AzuraCastGroup> logger, AzuraCastApiService azuraCast, AzuraChecksBackgroundTask backgroundService, DbActions dbActions, DiscordBotService botService, MusicStreamingService musicStreaming)
{
private readonly ILogger<AzuraCastGroup> _logger = logger;
Expand Down Expand Up @@ -1076,7 +1076,7 @@ public async ValueTask SearchSongAsync
await context.EditResponseAsync(embed);
}

[Command("upload-files"), Description("Upload a file to the selected station."), RequireGuild, RequirePermissions(DiscordPermissions.None, DiscordPermissions.AttachFiles), ModuleActivatedCheck(AzzyModules.AzuraCast), AzuraCastOnlineCheck, FeatureAvailableCheck(AzuraCastFeatures.FileUploading), AzuraCastDiscordChannelCheck]
[Command("upload-files"), Description("Upload a file to the selected station."), RequireGuild, RequirePermissions(BotPermissions = [], UserPermissions = [DiscordPermission.AttachFiles]), ModuleActivatedCheck(AzzyModules.AzuraCast), AzuraCastOnlineCheck, FeatureAvailableCheck(AzuraCastFeatures.FileUploading), AzuraCastDiscordChannelCheck]
public async ValueTask UploadFilesAsync
(
SlashCommandContext context,
Expand Down
2 changes: 1 addition & 1 deletion src/AzzyBot.Bot/Commands/ConfigCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace AzzyBot.Bot.Commands;
[SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "DSharpPlus best practice")]
public sealed class ConfigCommands
{
[Command("config"), RequireGuild, RequirePermissions(DiscordPermissions.None, DiscordPermissions.Administrator)]
[Command("config"), RequireGuild, RequirePermissions(BotPermissions = [], UserPermissions = [DiscordPermission.Administrator])]
public sealed class ConfigGroup(ILogger<ConfigGroup> logger, AzuraCastApiService azuraCast, AzuraChecksBackgroundTask backgroundService, DbActions dbActions, DiscordBotService botService)
{
private readonly ILogger<ConfigGroup> _logger = logger;
Expand Down
4 changes: 2 additions & 2 deletions src/AzzyBot.Bot/Commands/CoreCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public sealed class CoreGroup(ILogger<CoreGroup> logger, AzzyBotSettingsRecord s
private readonly DbActions _dbActions = dbActions;
private readonly DiscordBotService _botService = botService;

[Command("force-channel-permissions-check"), Description("Forces a check of the permissions for the bot in the necessary channel."), RequirePermissions(DiscordPermissions.None, DiscordPermissions.ManageChannels)]
[Command("force-channel-permissions-check"), Description("Forces a check of the permissions for the bot in the necessary channel."), RequirePermissions(BotPermissions = [], UserPermissions = [DiscordPermission.ManageChannels])]
public async ValueTask ForceChannelPermissionsCheckAsync(SlashCommandContext context)
{
ArgumentNullException.ThrowIfNull(context);
Expand Down Expand Up @@ -76,7 +76,7 @@ public async ValueTask HelpAsync
ulong guildId = context.Guild.Id;
DiscordMember member = context.Member;

bool adminServer = botOwners.Any(u => u.Id == context.User.Id && member.Permissions.HasPermission(DiscordPermissions.Administrator) && guildId == _settings.ServerId);
bool adminServer = botOwners.Any(u => u.Id == context.User.Id && member.Permissions.HasPermission(DiscordPermission.Administrator) && guildId == _settings.ServerId);
bool approvedDebug = guildId == _settings.ServerId;
List<DiscordEmbed> embeds = new(10);
if (string.IsNullOrWhiteSpace(command))
Expand Down
2 changes: 1 addition & 1 deletion src/AzzyBot.Bot/Commands/DebugCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace AzzyBot.Bot.Commands;
[SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "DSharpPlus best practice")]
public sealed class DebugCommands
{
[Command("debug"), RequireGuild, RequirePermissions(DiscordPermissions.None, DiscordPermissions.Administrator)]
[Command("debug"), RequireGuild, RequirePermissions(BotPermissions = [], UserPermissions = [DiscordPermission.Administrator])]
public sealed class DebugGroup(WebRequestService webRequestService, ILogger<DebugGroup> logger)
{
private readonly ILogger<DebugGroup> _logger = logger;
Expand Down
2 changes: 1 addition & 1 deletion src/AzzyBot.Bot/Commands/MusicStreamingCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace AzzyBot.Bot.Commands;
[SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "DSharpPlus best practice")]
public sealed class MusicStreamingCommands
{
[Command("player"), RequireGuild, RequirePermissions(DiscordPermissions.Speak | DiscordPermissions.UseVoice, DiscordPermissions.UseVoice)]
[Command("player"), RequireGuild, RequirePermissions(BotPermissions = [DiscordPermission.Connect, DiscordPermission.Speak], UserPermissions = [DiscordPermission.Connect])]
public sealed class PlayerGroup(ILogger<PlayerGroup> logger, AzuraCastApiService azuraCast, DbActions dbActions, MusicStreamingService musicStreaming)
{
private readonly ILogger<PlayerGroup> _logger = logger;
Expand Down
12 changes: 6 additions & 6 deletions src/AzzyBot.Bot/Services/DiscordBotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public sealed class DiscordBotService(ILogger<DiscordBotService> logger, AzzyBot
public bool CheckIfClientIsConnected
=> _client.AllShardsConnected;

public async Task<bool> CheckChannelPermissionsAsync(DiscordMember member, ulong channelId, DiscordPermissions permissions)
public async Task<bool> CheckChannelPermissionsAsync(DiscordMember member, ulong channelId, DiscordPermission[] permissions)
{
ArgumentNullException.ThrowIfNull(member);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(channelId);
Expand All @@ -50,7 +50,7 @@ public async Task<bool> CheckChannelPermissionsAsync(DiscordMember member, ulong
return false;
}

return channel.PermissionsFor(member).HasPermission(permissions);
return channel.PermissionsFor(member).HasAllPermissions(permissions);
}

public async Task CheckPermissionsAsync(DiscordGuild guild, ulong[] channelIds)
Expand All @@ -71,7 +71,7 @@ public async Task CheckPermissionsAsync(DiscordGuild guild, ulong[] channelIds)
foreach (ulong channelId in channelIds)
{
channels.Add(channelId);
if (!await CheckChannelPermissionsAsync(member, channelId, DiscordPermissions.AccessChannels | DiscordPermissions.SendMessages))
if (!await CheckChannelPermissionsAsync(member, channelId, [DiscordPermission.SendMessages, DiscordPermission.ViewChannel]))
channelNotAccessible.Add(channelId);
}

Expand Down Expand Up @@ -153,7 +153,7 @@ public async Task CheckPermissionsAsync(IAsyncEnumerable<GuildEntity> guilds)
continue;
}

if (!channel.PermissionsFor(member).HasPermission(DiscordPermissions.AccessChannels | DiscordPermissions.SendMessages))
if (!channel.PermissionsFor(member).HasAllPermissions(DiscordPermission.SendMessages, DiscordPermission.ViewChannel))
channelNotAccessible.Add(channelId);
}

Expand Down Expand Up @@ -463,7 +463,7 @@ public async Task<bool> SendMessageAsync(ulong channelId, string? content = null
return false;
}

if (!channel.PermissionsFor(dMember).HasPermission(DiscordPermissions.AccessChannels | DiscordPermissions.SendMessages))
if (!channel.PermissionsFor(dMember).HasAllPermissions(DiscordPermission.SendMessages, DiscordPermission.ViewChannel))
{
_logger.UnableToSendMessage($"Bot has no permission to send messages in channel: {channel.Name} ({channel.Id})");
return false;
Expand Down Expand Up @@ -576,7 +576,7 @@ public static DiscordUserStatus SetBotStatusUserStatus(int status)
return null;
}

return guild.Channels.FirstOrDefault(c => c.Value.Type is DiscordChannelType.Text && c.Value.PermissionsFor(member).HasPermission(DiscordPermissions.AccessChannels | DiscordPermissions.SendMessages)).Value;
return guild.Channels.FirstOrDefault(c => c.Value.Type is DiscordChannelType.Text && c.Value.PermissionsFor(member).HasAllPermissions(DiscordPermission.SendMessages, DiscordPermission.ViewChannel)).Value;
}

private static void ProcessOptions(IReadOnlyDictionary<CommandParameter, object?> paramaters, Dictionary<string, string> commandParameters)
Expand Down
2 changes: 1 addition & 1 deletion src/AzzyBot.Bot/Services/DiscordBotServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task StartAsync(CancellationToken cancellationToken)

await _client.ConnectAsync(activity, status);

string invite = _client.CurrentApplication.GenerateOAuthUri(null, DiscordPermissions.AccessChannels | DiscordPermissions.AttachFiles | DiscordPermissions.SendMessages, DiscordOAuthScope.ApplicationsCommands, DiscordOAuthScope.Bot);
string invite = _client.CurrentApplication.GenerateOAuthUri(null, new(DiscordPermission.AttachFiles, DiscordPermission.SendMessages, DiscordPermission.ViewChannel), DiscordOAuthScope.ApplicationsCommands, DiscordOAuthScope.Bot);
_logger.InviteUrl(invite);
}

Expand Down
2 changes: 1 addition & 1 deletion src/AzzyBot.Bot/Services/Modules/MusicStreamingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public sealed class MusicStreamingService(IAudioService audioService, ILogger<Mu
_logger.DiscordItemNotFound(nameof(DiscordMember), context.Guild.Id);
notConnecting = true;
}
else if (!await _botService.CheckChannelPermissionsAsync(bot, channelId, DiscordPermissions.AccessChannels | DiscordPermissions.UseVoice))
else if (!await _botService.CheckChannelPermissionsAsync(bot, channelId, [DiscordPermission.Speak, DiscordPermission.ViewChannel]))
{
notConnecting = true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/AzzyBot.Bot/Utilities/AzzyHelp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ private static bool CheckIfMemberHasPermission(bool adminServer, bool approvedDe
return command switch
{
"admin" => adminServer,
"azuracast" => permissions.HasPermission(DiscordPermissions.Administrator),
"config" => permissions.HasPermission(DiscordPermissions.Administrator),
"azuracast" => permissions.HasPermission(DiscordPermission.Administrator),
"config" => permissions.HasPermission(DiscordPermission.Administrator),
"core" => true,
"debug" => approvedDebug && permissions.HasPermission(DiscordPermissions.Administrator),
"debug" => approvedDebug && permissions.HasPermission(DiscordPermission.Administrator),
"dj" => true,
"music" => true,
"player" => true,
Expand Down

0 comments on commit 2068d4f

Please sign in to comment.