Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to DSharpPlus.Commands #243

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0c92f38
WIP: Update DSharpPlus to 5.0.0-nightly-02379
FloatingMilkshake Oct 1, 2024
78d5e65
Fix missed breaking changes
FloatingMilkshake Oct 25, 2024
3f0012a
Adjust text command names to avoid duplicate commands when registering
FloatingMilkshake Nov 14, 2024
938e6a6
Merge branch 'main' into floatingmilkshake/dsp-command-migration
FloatingMilkshake Nov 14, 2024
f7eb426
Resolve remaining TODOs
FloatingMilkshake Nov 15, 2024
427fa59
Fix (at least partially) group commands
FloatingMilkshake Nov 15, 2024
05c47ff
Error handling: Strip "textcmd" from cmd names, fix check failure han…
FloatingMilkshake Nov 15, 2024
349bb9f
Merge branch 'main' into floatingmilkshake/dsp-command-migration
FloatingMilkshake Nov 17, 2024
8dee384
Merge branch 'main' into floatingmilkshake/dsp-command-migration
FloatingMilkshake Nov 17, 2024
56e9aeb
Fix [RequirePermissions] usage
FloatingMilkshake Nov 19, 2024
e262b45
Combine commands that can be combined, move all commands to Commands …
FloatingMilkshake Nov 20, 2024
6ad00ac
Upgrade DSharpPlus to 5.0.0-nightly-02422
FloatingMilkshake Nov 20, 2024
fae85de
Merge branch 'main' into floatingmilkshake/dsp-command-migration
FloatingMilkshake Nov 21, 2024
24a56a4
Remove mistakenly-added file
FloatingMilkshake Nov 21, 2024
269b2c2
Fix incorrect namespaces
Erisa Nov 22, 2024
e88706e
Remove import of deleted namespace
Erisa Nov 22, 2024
8d0a063
Restore friendlier note in command syntax error embed
FloatingMilkshake Nov 24, 2024
4ee23a2
Use SlashCommandContext for user context commands as workaround for p…
FloatingMilkshake Nov 24, 2024
0220804
Prevent !debug overrides from being registered as a slash command
FloatingMilkshake Nov 24, 2024
1dabc79
Upgrade DSharpPlus and switch back to UserCommandContext
FloatingMilkshake Nov 24, 2024
810d9bc
Merge branch 'main' into floatingmilkshake/dsp-command-migration
FloatingMilkshake Dec 10, 2024
18b6699
Fix missed merge conflict changes / adjust for DSharpPlus.Commands
FloatingMilkshake Dec 10, 2024
08c424a
Merge branch 'main' into floatingmilkshake/dsp-command-migration
FloatingMilkshake Dec 15, 2024
1a03dee
Merge branch 'main' into floatingmilkshake/dsp-command-migration
FloatingMilkshake Dec 17, 2024
dee9be8
Implement a help command for text cmds & fix global cmd registration
FloatingMilkshake Dec 17, 2024
718023c
Help: Properly filter out args of type CommandContext
FloatingMilkshake Dec 18, 2024
34769df
Help: Show perms error when user lacks perms for requested cmd
FloatingMilkshake Dec 18, 2024
f73c93b
Fix command registration
FloatingMilkshake Dec 18, 2024
1547e69
Add missing returns to permission error messages
FloatingMilkshake Dec 18, 2024
887b069
Correctly show permission errors even on ArgumentParseException
FloatingMilkshake Dec 18, 2024
e8dc35c
Fix permission checks
FloatingMilkshake Dec 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cliptok.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
<ItemGroup>
<PackageReference Include="Abyssal.HumanDateParser" Version="2.0.0-20191113.1" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-02430" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-02430" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="5.0.0-nightly-02430" />
<PackageReference Include="DSharpPlus.Commands" Version="5.0.0-nightly-02430" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0" />
Expand Down
60 changes: 17 additions & 43 deletions CommandChecks/HomeServerPerms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static async Task<ServerPermLevel> GetPermLevelAsync(DiscordMember target
}

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class RequireHomeserverPermAttribute : CheckBaseAttribute
public class RequireHomeserverPermAttribute : ContextCheckAttribute
{
public ServerPermLevel TargetLvl { get; set; }
public bool WorkOutside { get; set; }
Expand All @@ -79,16 +79,19 @@ public RequireHomeserverPermAttribute(ServerPermLevel targetlvl, bool workOutsid
OwnerOverride = ownerOverride;
TargetLvl = targetlvl;
}
}

public override async Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help)
public class RequireHomeserverPermCheck : IContextCheck<RequireHomeserverPermAttribute>
{
public async ValueTask<string?> ExecuteCheckAsync(RequireHomeserverPermAttribute attribute, CommandContext ctx)
{
// If the command is supposed to stay within the server and its being used outside, fail silently
if (!WorkOutside && (ctx.Channel.IsPrivate || ctx.Guild.Id != Program.cfgjson.ServerID))
return false;
if (!attribute.WorkOutside && (ctx.Channel.IsPrivate || ctx.Guild.Id != Program.cfgjson.ServerID))
return "This command must be used in the home server, but was executed outside of it.";

// bot owners can bypass perm checks ONLY if the command allows it.
if (OwnerOverride && Program.cfgjson.BotOwners.Contains(ctx.User.Id))
return true;
if (attribute.OwnerOverride && Program.cfgjson.BotOwners.Contains(ctx.User.Id))
return null;

DiscordMember member;
if (ctx.Channel.IsPrivate || ctx.Guild.Id != Program.cfgjson.ServerID)
Expand All @@ -100,7 +103,7 @@ public override async Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help
}
catch (DSharpPlus.Exceptions.NotFoundException)
{
return false;
return "The invoking user must be a member of the home server; they are not.";
}
}
else
Expand All @@ -109,50 +112,21 @@ public override async Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help
}

var level = await GetPermLevelAsync(member);
if (level >= TargetLvl)
return true;

else if (!help && ctx.Command.QualifiedName != "edit")
{
var levelText = level.ToString();
if (level == ServerPermLevel.Nothing && Program.rand.Next(1, 100) == 69)
levelText = $"naught but a thing, my dear human. Congratulations, you win {Program.rand.Next(1, 10)} bonus points.";
if (level >= attribute.TargetLvl)
return null;

await ctx.RespondAsync(
$"{Program.cfgjson.Emoji.NoPermissions} Invalid permissions to use command **{ctx.Command.Name}**!\n" +
$"Required: `{TargetLvl}`\nYou have: `{levelText}`");
}
return false;
return "The invoking user does not have permission to use this command.";
}
}

public class HomeServerAttribute : CheckBaseAttribute
{
public override async Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help)
{
return !ctx.Channel.IsPrivate && ctx.Guild.Id == Program.cfgjson.ServerID;
}
}
public class HomeServerAttribute : ContextCheckAttribute;

public class SlashRequireHomeserverPermAttribute : SlashCheckBaseAttribute
public class HomeServerCheck : IContextCheck<HomeServerAttribute>
{
public ServerPermLevel TargetLvl;

public SlashRequireHomeserverPermAttribute(ServerPermLevel targetlvl)
=> TargetLvl = targetlvl;

public override async Task<bool> ExecuteChecksAsync(InteractionContext ctx)
public async ValueTask<string?> ExecuteCheckAsync(HomeServerAttribute attribute, CommandContext ctx)
{
if (ctx.Guild.Id != Program.cfgjson.ServerID)
return false;

var level = await GetPermLevelAsync(ctx.Member);
if (level >= TargetLvl)
return true;
else
return false;
return !ctx.Channel.IsPrivate && ctx.Guild.Id == Program.cfgjson.ServerID ? null : "This command must be used in the home server, but was executed outside of it.";
}
}

}
}
14 changes: 6 additions & 8 deletions CommandChecks/OwnerChecks.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
namespace Cliptok.CommandChecks
{
public class IsBotOwnerAttribute : CheckBaseAttribute
public class IsBotOwnerAttribute : ContextCheckAttribute;

public class IsBotOwnerCheck : IContextCheck<IsBotOwnerAttribute>
{
public override async Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help)
public async ValueTask<string?> ExecuteCheckAsync(IsBotOwnerAttribute attribute, CommandContext ctx)
{
if (Program.cfgjson.BotOwners.Contains(ctx.User.Id))
{
return true;
return null;
}
else
{
if (!help)
{
await ctx.RespondAsync($"{Program.cfgjson.Emoji.NoPermissions} This command is only accessible to bot owners.");
}
return false;
return "Bot owner-only command was executed by a non-owner.";
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions CommandChecks/UserRoleChecks.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace Cliptok.CommandChecks
{
public class UserRolesPresentAttribute : CheckBaseAttribute
public class UserRolesPresentAttribute : ContextCheckAttribute;

public class UserRolesPresentCheck : IContextCheck<UserRolesPresentAttribute>
{
public override async Task<bool> ExecuteCheckAsync(CommandContext ctx, bool help)
public async ValueTask<string?> ExecuteCheckAsync(UserRolesPresentAttribute attribute, CommandContext ctx)
{
return Program.cfgjson.UserRoles is not null;
return Program.cfgjson.UserRoles is null ? "A user role command was executed, but user roles are not configured in config.json." : null;
}
}
}
Loading
Loading