discord-cmd.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. ________________________________________________________________________________
  3. discord-command.inc
  4. Command processor for SA-MP Discord Connector
  5. @Author : Akshay Mohan - https://github.com/AkshayMohan
  6. @Version : v1.0.2
  7. @Github : https://github.com/AkshayMohan/pawn-discord-cmd
  8. Last update: 7th of January, 2021
  9. Changelogs:
  10. v1.0.2 - 7th of January, 2021
  11. - Fixed case sensitivity.
  12. v1.0.1 - 6th of January, 2021
  13. - Added function DCMD_GetCommandMessageId();
  14. v1.0.0 - 3rd of January, 2021
  15. - Initial release.
  16. MIT License
  17. Copyright (c) 2021 Akshay Mohan
  18. Permission is hereby granted, free of charge, to any person obtaining a copy
  19. of this software and associated documentation files (the "Software"), to deal
  20. in the Software without restriction, including without limitation the rights
  21. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22. copies of the Software, and to permit persons to whom the Software is
  23. furnished to do so, subject to the following conditions:
  24. The above copyright notice and this permission notice shall be included in all
  25. copies or substantial portions of the Software.
  26. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. SOFTWARE.
  33. native DCC_Message:DCMD_GetCommandMessageId();
  34. ________________________________________________________________________________ */
  35. #if defined _included_discord_command
  36. #endinput
  37. #endif
  38. #define _included_discord_command
  39. #if !defined DCMD_PREFIX
  40. #define DCMD_PREFIX '.'
  41. #endif
  42. #define DCMD:%0(%1,%2,%3) \
  43. forward DCMD_%0(DCC_User:%1,DCC_Channel:%2,%3); \
  44. public DCMD_%0(DCC_User:%1,DCC_Channel:%2,%3)
  45. #if !defined isnull
  46. #define isnull(%0) (!(%0[0]) || (%0[0] == '\1' && !(%0[1])))
  47. #endif
  48. #tryinclude <discord-connector>
  49. #if defined DCMD_STRICT_CASE
  50. static const DCMD_MAX_CMD_LEN = 32 - 5;
  51. #endif
  52. const DCMD_TOTAL_CMD_SIZE = 256;
  53. #if defined OnDiscordCommandAttempt
  54. forward OnDiscordCommandAttempt(DCC_User:user, DCC_Channel:channel, cmdtext[]);
  55. #endif
  56. #if defined OnDiscordCommandPerformed
  57. forward OnDiscordCommandPerformed(DCC_User:user, DCC_Channel:channel,
  58. cmdtext[], success);
  59. #endif
  60. static DCC_Message:g_DCMDMessageID = DCC_Message:0;
  61. stock DCC_Message:DCMD_GetCommandMessageId() {
  62. return g_DCMDMessageID;
  63. }
  64. public DCC_OnMessageCreate(DCC_Message:message) {
  65. new
  66. DCC_User:user,
  67. DCC_Channel:channel,
  68. cmdtext[DCMD_TOTAL_CMD_SIZE],
  69. bool:hasParams = false,
  70. bool:isBot;
  71. DCC_GetMessageContent(message, cmdtext, sizeof(cmdtext));
  72. if(cmdtext[0] == DCMD_PREFIX) {
  73. DCC_GetMessageAuthor(message, user);
  74. DCC_GetMessageChannel(message, channel);
  75. DCC_IsUserBot(user, isBot);
  76. #if !defined DCMD_ALLOW_BOTS
  77. if(isBot)
  78. return 0;
  79. #endif
  80. g_DCMDMessageID = message;
  81. #if defined OnDiscordCommandAttempt
  82. if(!OnDiscordCommandAttempt(user, channel, cmdtext[1]))
  83. return 0;
  84. #endif
  85. static command[32] = "DCMD_", success = 0;
  86. new pos;
  87. #if defined DCMD_STRICT_CASE
  88. pos = strfind(cmdtext, " ", false, 1);
  89. command[5] = '\0';
  90. if(pos != -1) {
  91. strcat(command, cmdtext[1], pos + 5);
  92. while(cmdtext[pos] == ' ') ++pos;
  93. if(cmdtext[pos]) {
  94. hasParams = true;
  95. }
  96. } else {
  97. strcat(command, cmdtext[1], DCMD_MAX_CMD_LEN);
  98. }
  99. #else
  100. new idx = 5;
  101. pos = 1;
  102. while(idx < 31 && cmdtext[pos] > ' ') {
  103. command[idx++] = (
  104. (cmdtext[pos] >= 'A' && cmdtext[pos] <= 'Z')
  105. ? (cmdtext[pos] | 0x20) : cmdtext[pos]
  106. );
  107. pos++;
  108. }
  109. command[idx] = '\0';
  110. while(cmdtext[pos] == ' ') ++pos;
  111. if(cmdtext[pos]) {
  112. hasParams = true;
  113. }
  114. #endif
  115. if(hasParams) {
  116. success = CallLocalFunction(command, "iis",
  117. _:user, _:channel, cmdtext[pos]);
  118. } else {
  119. success = CallLocalFunction(command, "iis",
  120. _:user, _:channel, "\1");
  121. }
  122. #if defined OnDiscordCommandPerformed
  123. return OnDiscordCommandPerformed(
  124. user, channel, cmdtext[1], success);
  125. #else
  126. return 1;
  127. #endif
  128. #pragma unused success
  129. }
  130. #if defined DCMD_OnMessageCreate
  131. return DCMD_OnMessageCreate(message);
  132. #else
  133. return 1;
  134. #endif
  135. }
  136. #if defined _ALS_OnMessageCreate
  137. #undef DCC_OnMessageCreate
  138. #else
  139. #define _ALS_OnMessageCreate
  140. #endif
  141. #define DCC_OnMessageCreate DCMD_OnMessageCreate
  142. #if defined DCMD_OnMessageCreate
  143. forward DCMD_OnMessageCreate(DCC_Message:message);
  144. #endif