-
Notifications
You must be signed in to change notification settings - Fork 3
/
discord-command.inc
134 lines (103 loc) · 2.97 KB
/
discord-command.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Discord Command Processor
A simple library for discord based commands
with iZCMD-like code and functionality. Re-
quires SSCANF.
Written by AliLogic
*/
// Include guard
#if defined _discordcmd_included
#endinput
#endif
#define _discordcmd_included
#tryinclude <sscanf2>
#if !defined _INC_SSCANF
#error To use this include you need to have "https://github.com/Y-Less/sscanf" installed
#endif
#tryinclude <discord-connector>
#if !defined dcconnector_included
#error To use this include you need to have "https://github.com/maddinat0r/samp-discord-connector" installed
#endif
#if !defined isnull
#define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
#endif
// Config
#define MAX_CMD_LEN 32
#define MAX_CMD_NAME 20
// Prefix
#if !defined CMD_PREFIX
#define CMD_PREFIX '!'
#endif
// Macros
#define DISCORD:%1(%2) \
forward discord_%1(%2); \
public discord_%1(%2)
#define DC:%1(%2) \
DISCORD:%1(%2)
// Command result
#define DISCORD_SUCCESS (1)
#define DISCORD_FAILURE (0)
// Code
public DCC_OnMessageCreate(DCC_Message: message) {
new
DCC_User: author,
bool: is_bot = false,
content[256 + MAX_CMD_NAME + 2],
command[MAX_CMD_NAME],
params[256],
discord[MAX_CMD_LEN] = "discord_"
;
if (!DCC_GetMessageAuthor(message, author) || author == DCC_INVALID_USER) { // The message author is invalid
#if defined discdcmd_DCC_OnMessageCreate
return discdcmd_DCC_OnMessageCreate(DCC_Message: message);
#else
return 0;
#endif
}
DCC_IsUserBot(author, is_bot);
DCC_GetMessageContent(message, content);
if (is_bot || content[0] != CMD_PREFIX) { // Skip if the message author is a bot or is not a command
#if defined discdcmd_DCC_OnMessageCreate
return discdcmd_DCC_OnMessageCreate(DCC_Message: message);
#else
return 0;
#endif
}
if (sscanf(content, "s["#MAX_CMD_NAME"]S()[256]", command, params)) {
#if defined discdcmd_DCC_OnMessageCreate
return discdcmd_DCC_OnMessageCreate(DCC_Message: message);
#else
return 0;
#endif
}
for (new i = strlen(command) - 1; i != 0; i --) {
command[i] |= 0x20; // lower case using bitwise OR operator
}
strcat(discord, command[1]);
if (isnull(params)) {
params = "\1";
}
#if defined OnDiscordCommandPerformed
OnDiscordCommandPerformed(message, author, bool: CallLocalFunction(discord, "iis", _: message, _: author, params));
#else
CallLocalFunction(discord, "iis", _: message, _: author, params);
#endif
#if defined discdcmd_DCC_OnMessageCreate
return discdcmd_DCC_OnMessageCreate(DCC_Message: message);
#else
return 1;
#endif
}
// Hooking and functions
#if defined _ALS_DCC_OnMessageCreate
#undef DCC_OnMessageCreate
#else
#define _ALS_DCC_OnMessageCreate
#endif
#define DCC_OnMessageCreate discdcmd_DCC_OnMessageCreate
#if defined discdcmd_DCC_OnMessageCreate
forward discdcmd_DCC_OnMessageCreate(DCC_Message: message);
#endif
#if defined OnDiscordCommandPerformed
forward OnDiscordCommandPerformed(DCC_Message: message, DCC_User: author, bool: success);
#endif