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.

MessageExtensions.cs 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. namespace Discord.Commands
  3. {
  4. public static class MessageExtensions
  5. {
  6. public static bool HasCharPrefix(this IUserMessage msg, char c, ref int argPos)
  7. {
  8. var text = msg.Content;
  9. if (text.Length > 0 && text[0] == c)
  10. {
  11. argPos = 1;
  12. return true;
  13. }
  14. return false;
  15. }
  16. public static bool HasStringPrefix(this IUserMessage msg, string str, ref int argPos, StringComparison comparisonType = StringComparison.Ordinal)
  17. {
  18. var text = msg.Content;
  19. if (text.StartsWith(str, comparisonType))
  20. {
  21. argPos = str.Length;
  22. return true;
  23. }
  24. return false;
  25. }
  26. public static bool HasMentionPrefix(this IUserMessage msg, IUser user, ref int argPos)
  27. {
  28. var text = msg.Content;
  29. if (text.Length <= 3 || text[0] != '<' || text[1] != '@') return false;
  30. int endPos = text.IndexOf('>');
  31. if (endPos == -1) return false;
  32. if (text.Length < endPos + 2 || text[endPos + 1] != ' ') return false; //Must end in "> "
  33. ulong userId;
  34. if (!MentionUtils.TryParseUser(text.Substring(0, endPos + 1), out userId)) return false;
  35. if (userId == user.Id)
  36. {
  37. argPos = endPos + 2;
  38. return true;
  39. }
  40. return false;
  41. }
  42. public static string GetJumpUrl(this IMessage msg)
  43. {
  44. var channel = msg.Channel;
  45. return $"https://discordapp.com/channels/{(channel is IDMChannel ? "@me" : $"{(channel as IGuildChannel).GuildId}")}/{channel.Id}/{msg.Id}";
  46. }
  47. }
  48. }