Skip to content

Commit

Permalink
fix command permission checks in DMs
Browse files Browse the repository at this point in the history
  • Loading branch information
Erisa committed Jan 12, 2025
1 parent 67bf5b9 commit bc1e2ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CommandChecks/HomeServerPerms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public enum ServerPermLevel

public static async Task<ServerPermLevel> GetPermLevelAsync(DiscordMember target)
{
if (target.Guild.Id != Program.cfgjson.ServerID)
if (target is null || target.Guild is null || target.Guild.Id != Program.cfgjson.ServerID)
return ServerPermLevel.Nothing;

// Torch approved of this.
Expand Down
25 changes: 22 additions & 3 deletions Commands/GlobalCmds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,25 @@ private async Task<IEnumerable<ContextCheckAttribute>> CheckPermissionsAsync(Com
var contextChecks = cmd.Attributes.Where(x => x is ContextCheckAttribute);
var failedChecks = new List<ContextCheckAttribute>();

// similar to home server perm check logic
DiscordMember member = null;
if (ctx.Channel.IsPrivate || ctx.Guild.Id != Program.cfgjson.ServerID)
{
var guild = await ctx.Client.GetGuildAsync(Program.cfgjson.ServerID);
try
{
member = await guild.GetMemberAsync(ctx.User.Id);
}
catch (DSharpPlus.Exceptions.NotFoundException)
{
// member is null, remember this for later
}
}
else
{
member = ctx.Member;
}

foreach (var check in contextChecks)
{
if (check is HomeServerAttribute homeServerAttribute)
Expand All @@ -329,13 +348,13 @@ private async Task<IEnumerable<ContextCheckAttribute>> CheckPermissionsAsync(Com
if (check is RequireHomeserverPermAttribute requireHomeserverPermAttribute)
{
// Fail if guild member is null but this cmd does not work outside of the home server
if (ctx.Member is null && !requireHomeserverPermAttribute.WorkOutside)
if (member is null && !requireHomeserverPermAttribute.WorkOutside)
{
failedChecks.Add(requireHomeserverPermAttribute);
}
else
{
var level = await GetPermLevelAsync(ctx.Member);
var level = await GetPermLevelAsync(member);
if (level < requireHomeserverPermAttribute.TargetLvl)
{
if (requireHomeserverPermAttribute.OwnerOverride && !Program.cfgjson.BotOwners.Contains(ctx.User.Id)
Expand All @@ -349,7 +368,7 @@ private async Task<IEnumerable<ContextCheckAttribute>> CheckPermissionsAsync(Com

if (check is RequirePermissionsAttribute requirePermissionsAttribute)
{
if (ctx.Member is null || ctx.Guild is null
if (member is null || ctx.Guild is null
|| !ctx.Channel.PermissionsFor(ctx.Member).HasAllPermissions(requirePermissionsAttribute.UserPermissions)
|| !ctx.Channel.PermissionsFor(ctx.Guild.CurrentMember).HasAllPermissions(requirePermissionsAttribute.BotPermissions))
{
Expand Down

0 comments on commit bc1e2ca

Please sign in to comment.