Skip to content

Commit

Permalink
Use SlashCommandContext for user context commands as workaround for p…
Browse files Browse the repository at this point in the history
…otential DSharpPlus bug
  • Loading branch information
FloatingMilkshake committed Nov 24, 2024
1 parent 8d0a063 commit 4ee23a2
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Commands/FunCmds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class FunCmds
[Command("Hug")]
[SlashCommandTypes(DiscordApplicationCommandType.UserContextMenu)]
[AllowedProcessors(typeof(UserCommandProcessor))]
public async Task Hug(UserCommandContext ctx, DiscordUser targetUser)
public async Task Hug(SlashCommandContext ctx, DiscordUser targetUser)
{
var user = targetUser;

Expand Down
2 changes: 1 addition & 1 deletion Commands/UserNoteCmds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class UserNoteCmds
[SlashCommandTypes(DiscordApplicationCommandType.UserContextMenu)]
[AllowedProcessors(typeof(UserCommandProcessor))]
[RequireHomeserverPerm(ServerPermLevel.TrialModerator), RequirePermissions(DiscordPermission.ModerateMembers)]
public async Task ShowNotes(UserCommandContext ctx, DiscordUser targetUser)
public async Task ShowNotes(SlashCommandContext ctx, DiscordUser targetUser)
{
await ctx.RespondAsync(embed: await UserNoteHelpers.GenerateUserNotesEmbedAsync(targetUser), ephemeral: true);
}
Expand Down
4 changes: 2 additions & 2 deletions Commands/UtilityCmds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class UtilityCmds
[Command("Show Avatar")]
[SlashCommandTypes(DiscordApplicationCommandType.UserContextMenu)]
[AllowedProcessors(typeof(UserCommandProcessor))]
public async Task ContextAvatar(UserCommandContext ctx, DiscordUser targetUser)
public async Task ContextAvatar(SlashCommandContext ctx, DiscordUser targetUser)
{
string avatarUrl = await LykosAvatarMethods.UserOrMemberAvatarURL(targetUser, ctx.Guild);

Expand All @@ -24,7 +24,7 @@ public async Task ContextAvatar(UserCommandContext ctx, DiscordUser targetUser)
[Command("User Information")]
[SlashCommandTypes(DiscordApplicationCommandType.UserContextMenu)]
[AllowedProcessors(typeof(UserCommandProcessor))]
public async Task ContextUserInformation(UserCommandContext ctx, DiscordUser targetUser)
public async Task ContextUserInformation(SlashCommandContext ctx, DiscordUser targetUser)
{
await ctx.RespondAsync(embed: await DiscordHelpers.GenerateUserEmbed(targetUser, ctx.Guild), ephemeral: true);
}
Expand Down
2 changes: 1 addition & 1 deletion Commands/WarningCmds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class WarningCmds
[Command("Show Warnings")]
[SlashCommandTypes(DiscordApplicationCommandType.UserContextMenu)]
[AllowedProcessors(typeof(UserCommandProcessor))]
public async Task ContextWarnings(UserCommandContext ctx, DiscordUser targetUser)
public async Task ContextWarnings(SlashCommandContext ctx, DiscordUser targetUser)
{
await ctx.RespondAsync(embed: await WarningHelpers.GenerateWarningsEmbedAsync(targetUser), ephemeral: true);
}
Expand Down
3 changes: 1 addition & 2 deletions Events/ErrorEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public static async Task CommandErrored(CommandsExtension _, CommandErroredEvent
else if (e.Context is SlashCommandContext)
{
// Interaction command error (slash, user ctx, message ctx)
if (e.Context is UserCommandContext) await InteractionEvents.ContextCommandErrored(e); // this works because UserCommandContext inherits from SlashCommandContext
else await InteractionEvents.SlashCommandErrored(e);
await InteractionEvents.SlashCommandErrored(e);
}
}

Expand Down
24 changes: 0 additions & 24 deletions Events/InteractionEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,29 +230,5 @@ await e.Context.RespondAsync(new DiscordInteractionResponseBuilder().WithContent
e.Context.Client.Logger.LogError(CliptokEventID, e.Exception, "Error during invocation of interaction command {command} by {user}", e.Context.Command.Name, $"{DiscordHelpers.UniqueUsername(e.Context.User)}");
}

public static async Task ContextCommandErrored(CommandErroredEventArgs e)
{
if (e.Exception is ChecksFailedException slex)
{
foreach (var check in slex.Errors)
if (check.ContextCheckAttribute is RequireHomeserverPermAttribute att && e.Context.Command.Name != "edit")
{
var level = (await GetPermLevelAsync(e.Context.Member));
var levelText = level.ToString();
if (level == ServerPermLevel.Nothing && rand.Next(1, 100) == 69)
levelText = $"naught but a thing, my dear human. Congratulations, you win {rand.Next(1, 10)} bonus points.";

await e.Context.RespondAsync(
new DiscordInteractionResponseBuilder().WithContent(
$"{cfgjson.Emoji.NoPermissions} Invalid permission level to use command **{e.Context.Command.Name}**!\n" +
$"Required: `{att.TargetLvl}`\n" +
$"You have: `{levelText}`")
.AsEphemeral(true)
);
}
}
e.Context.Client.Logger.LogError(CliptokEventID, e.Exception, "Error during invocation of context command {command} by {user}", e.Context.Command.Name, $"{DiscordHelpers.UniqueUsername(e.Context.User)}");
}

}
}

0 comments on commit 4ee23a2

Please sign in to comment.