Skip to content

Commit

Permalink
Add check if user is permitted to use this AzuraCast command
Browse files Browse the repository at this point in the history
  • Loading branch information
Sella-GH committed Jul 3, 2024
1 parent d3b8b59 commit 3771543
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
93 changes: 93 additions & 0 deletions AzzyBot-Next/Commands/Checks/AzuraCastDiscordPermCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using AzzyBot.Database;
using AzzyBot.Database.Entities;
using AzzyBot.Services;
using AzzyBot.Utilities.Encryption;
using DSharpPlus.Commands;
using DSharpPlus.Commands.ContextChecks;
using DSharpPlus.Commands.Processors.SlashCommands;
using DSharpPlus.Entities;

namespace AzzyBot;

public class AzuraCastDiscordPermCheck(DbActions dbActions, DiscordBotService discordBotService) : IContextCheck<AzuraCastDiscordPermCheckAttribute>
{
private readonly DbActions _dbActions = dbActions;
private readonly DiscordBotService _botService = discordBotService;

public async ValueTask<string?> ExecuteCheckAsync(AzuraCastDiscordPermCheckAttribute attribute, CommandContext context)
{
ArgumentNullException.ThrowIfNull(attribute, nameof(attribute));
ArgumentNullException.ThrowIfNull(context, nameof(context));
ArgumentNullException.ThrowIfNull(context.Guild, nameof(context.Guild));
ArgumentNullException.ThrowIfNull(context.Member, nameof(context.Member));

if (context is SlashCommandContext ctx)
{
switch (ctx.Interaction.ResponseState)
{
case DiscordInteractionResponseState.Unacknowledged:
await context.DeferResponseAsync();
return null;

case DiscordInteractionResponseState.Replied:
return "Already replied";
}
}

int stationId = Convert.ToInt32(context.Arguments.Single(o => o.Key.Name is "station_id" && o.Value is not null).Value, CultureInfo.InvariantCulture);
ulong guildId = context.Guild.Id;
GuildsEntity guild = await _dbActions.GetGuildAsync(guildId);
if (guild is null)
return "Guild is null!";

AzuraCastEntity? azuraCast = guild.AzuraCast;
if (azuraCast is null)
return "AzuraCast is null!";

AzuraCastStationEntity? station = azuraCast.Stations.FirstOrDefault(s => s.StationId == stationId);
if (station is null)
return "Station is null!";

string userId = context.User.Id.ToString(CultureInfo.InvariantCulture);
IEnumerable<DiscordRole> userRoles = context.Member.Roles;
switch (attribute.Perm)
{
case AzuraCastDiscordPerm.InstanceOwner:
if (userId == Crypto.Decrypt(azuraCast.InstanceOwner))
return null;

break;

case AzuraCastDiscordPerm.InstanceAdminGroup:
if (userRoles.Contains(_botService.GetDiscordRole(guild.UniqueId, azuraCast.InstanceAdminGroup)))
return null;

break;

case AzuraCastDiscordPerm.StationOwner:
if (userId == Crypto.Decrypt(station.StationOwner))
return null;

break;

case AzuraCastDiscordPerm.StationAdminGroup:
if (userRoles.Contains(_botService.GetDiscordRole(guild.UniqueId, station.StationAdminGroup)))
return null;

break;

case AzuraCastDiscordPerm.StationDJGroup:
if (userRoles.Contains(_botService.GetDiscordRole(guild.UniqueId, station.StationDjGroup)))
return null;

break;
}

return "No permission";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using DSharpPlus.Commands.ContextChecks;

namespace AzzyBot;

public class AzuraCastDiscordPermCheckAttribute : ContextCheckAttribute

Check warning on line 6 in AzzyBot-Next/Commands/Checks/AzuraCastDiscordPermCheckAttribute.cs

View workflow job for this annotation

GitHub Actions / Build DEBUG for linux - x64

Avoid unsealed attributes (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1813)

Check warning on line 6 in AzzyBot-Next/Commands/Checks/AzuraCastDiscordPermCheckAttribute.cs

View workflow job for this annotation

GitHub Actions / Build DEBUG for linux - arm64

Avoid unsealed attributes (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1813)

Check warning on line 6 in AzzyBot-Next/Commands/Checks/AzuraCastDiscordPermCheckAttribute.cs

View workflow job for this annotation

GitHub Actions / Build DEBUG for win - x64

Avoid unsealed attributes (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1813)

Check warning on line 6 in AzzyBot-Next/Commands/Checks/AzuraCastDiscordPermCheckAttribute.cs

View workflow job for this annotation

GitHub Actions / Build DEBUG for win - arm64

Avoid unsealed attributes (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1813)
{
public AzuraCastDiscordPerm Perm { get; set; }
}
11 changes: 11 additions & 0 deletions AzzyBot-Next/Services/DiscordBotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ public IReadOnlyDictionary<ulong, DiscordGuild> GetDiscordGuilds
return member;
}

public DiscordRole? GetDiscordRole(ulong guildId, ulong roleId)
{
DiscordGuild? guild = GetDiscordGuild(guildId);
DiscordRole? role = null;

if (guild is not null)
role = guild.GetRole(roleId);

return role;
}

public async Task<bool> LogExceptionAsync(Exception ex, DateTime timestamp, ulong guildId = 0, string? info = null)
{
ArgumentNullException.ThrowIfNull(ex, nameof(ex));
Expand Down
10 changes: 10 additions & 0 deletions AzzyBot-Next/Utilities/Enums/AzuraCastDiscordPerm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace AzzyBot;

public enum AzuraCastDiscordPerm
{
InstanceOwner,
InstanceAdminGroup,
StationOwner,
StationAdminGroup,
StationDJGroup
}

0 comments on commit 3771543

Please sign in to comment.