You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DoUserCheckAttribute.cs 1.7 KiB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Discord;
  2. using Discord.Interactions;
  3. using Discord.WebSocket;
  4. using System;
  5. using System.Threading.Tasks;
  6. namespace InteractionFramework.Attributes
  7. {
  8. internal class DoUserCheck : PreconditionAttribute
  9. {
  10. public override Task<PreconditionResult> CheckRequirementsAsync(IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services)
  11. {
  12. // Check if the component matches the target properly.
  13. if (context.Interaction is not SocketMessageComponent componentContext)
  14. return Task.FromResult(PreconditionResult.FromError("Context unrecognized as component context."));
  15. else
  16. {
  17. // The approach here entirely depends on how you construct your custom ID. In this case, the format is:
  18. // unique-name:*,*
  19. // here the name and wildcards are split by ':'
  20. var param = componentContext.Data.CustomId.Split(':');
  21. // here we determine that we should always check for the first ',' present.
  22. // This will deal with additional wildcards by always selecting the first wildcard present.
  23. if (param.Length > 1 && ulong.TryParse(param[1].Split(',')[0], out ulong id))
  24. return (context.User.Id == id)
  25. // If the user ID
  26. ? Task.FromResult(PreconditionResult.FromSuccess())
  27. : Task.FromResult(PreconditionResult.FromError("User ID does not match component ID!"));
  28. else return Task.FromResult(PreconditionResult.FromError("Parse cannot be done if no userID exists."));
  29. }
  30. }
  31. }
  32. }