/
discord_test.sp
209 lines (170 loc) · 5.74 KB
/
discord_test.sp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#pragma semicolon 1
#define PLUGIN_VERSION "1.10"
#include <sourcemod>
#include <discord>
#define BOT_TOKEN ""
#define WEBHOOK ""
public Plugin myinfo =
{
name = "Discord Test",
author = "Deathknife",
description = "",
version = PLUGIN_VERSION,
url = ""
};
DiscordBot gBot;
public void OnPluginStart() {
RegConsoleCmd("sm_getguilds", Cmd_GetGuilds);
RegConsoleCmd("sm_recreatebot", Cmd_RecreateBot);
RegConsoleCmd("sm_webhooktest", Cmd_Webhook);
RegConsoleCmd("sm_sendmsg", Cmd_SendMsg);
RegConsoleCmd("sm_getroles", Cmd_GetRoles);
}
public void OnAllPluginsLoaded() {
gBot = new DiscordBot(BOT_TOKEN);
}
public Action Cmd_Webhook(int client, int argc) {
DiscordWebHook hook = new DiscordWebHook(WEBHOOK);
hook.SlackMode = true;
hook.SetContent("@here");
hook.SetUsername("Server");
MessageEmbed Embed = new MessageEmbed();
Embed.SetColor("#ff2222");
Embed.SetTitle("Testing WebHook");
Embed.AddField("Field1", "Test1", true);
Embed.AddField("abc def", "deef", true);
hook.Embed(Embed);
hook.Send();
delete hook;
hook = new DiscordWebHook("");
hook.SetUsername("Testing");
hook.SlackMode = false;
hook.SetContent("Testing 1 2 3");
hook.Send();
delete hook;
}
public Action Cmd_GetRoles(int client, int argc) {
if(client == 0)
{
ReplyToCommand(client, "[SM] This command cannot be used from console.");
return Plugin_Handled;
}
gBot.GetGuilds(GuildListGetRoles, _, GetClientUserId(client));
ReplyToCommand(client, "Trying!");
return Plugin_Handled;
}
public void GuildListGetRoles(DiscordBot bot, char[] id, char[] name, char[] icon, bool owner, int permissions, any data) {
int client = GetClientOfUserId(data);
if(client > 0 && IsClientConnected(client) && IsClientInGame(client)) {
bot.GetGuildRoles(id, OnGetRoles, data);
}
}
public void OnGetRoles(DiscordBot bot, char[] guild, RoleList roles, any data) {
PrintToChatAll("%i a", data);
int client = GetClientOfUserId(data);
if(client > 0 && IsClientConnected(client) && IsClientInGame(client)) {
PrintToConsole(client, "Roles for guild %s", guild);
for(int i = 0; i < roles.Size; i++) {
Role role = roles.Get(i);
char id[64];
char name[64];
role.GetID(id, sizeof(id));
role.GetName(name, sizeof(name));
PrintToConsole(client, "Role %s %s", id, name);
}
}
}
public Action Cmd_GetGuilds(int client, int argc) {
if(client == 0)
{
ReplyToCommand(client, "[SM] This command cannot be used from console.");
return Plugin_Handled;
}
gBot.GetGuilds(GuildList, GuildListAll, GetClientUserId(client));
ReplyToCommand(client, "Trying!");
return Plugin_Handled;
}
public Action Cmd_RecreateBot(int client, int argc) {
if(gBot != null) {
gBot.StopListening();
delete gBot;
}
gBot = new DiscordBot(BOT_TOKEN);
ReplyToCommand(client, "Recreated");
}
public void GuildList(DiscordBot bot, char[] id, char[] name, char[] icon, bool owner, int permissions, any data) {
int client = GetClientOfUserId(data);
if(client > 0 && IsClientConnected(client) && IsClientInGame(client)) {
PrintToConsole(client, "Guild [%s] [%s] [%s] [%i] [%i]", id, name, icon, owner, permissions);
gBot.GetGuildChannels(id, ChannelList, INVALID_FUNCTION, data);
}
}
public void ChannelList(DiscordBot bot, char[] guild, DiscordChannel Channel, any data) {
int client = GetClientOfUserId(data);
if(client > 0 && IsClientConnected(client) && IsClientInGame(client)) {
char name[32];
char id[32];
Channel.GetID(id, sizeof(id));
Channel.GetName(name, sizeof(name));
PrintToConsole(client, "Channel for Guild(%s) - [%s] [%s]", guild, id, name);
if(Channel.IsText) {
//Send a message with all ways
//gBot.SendMessage(Channel, "Sending message with DiscordBot.SendMessage");
//gBot.SendMessageToChannelID(id, "Sending message with DiscordBot.SendMessageToChannelID");
//Channel.SendMessage(gBot, "Sending message with DiscordChannel.SendMessage");
gBot.StartListeningToChannel(Channel, OnMessage);
}
}
}
public void OnMessage(DiscordBot Bot, DiscordChannel Channel, DiscordMessage message) {
char sMessage[2048];
message.GetContent(sMessage, sizeof(sMessage));
char sAuthor[128];
message.GetAuthor().GetUsername(sAuthor, sizeof(sAuthor));
PrintToChatAll("[DISCORD] %s: %s", sAuthor, sMessage);
if(StrEqual(sMessage, "Ping", false)) {
gBot.SendMessage(Channel, "Pong!");
}
}
public void GuildListAll(DiscordBot bot, ArrayList Alid, ArrayList Alname, ArrayList Alicon, ArrayList Alowner, ArrayList Alpermissions, any data) {
int client = GetClientOfUserId(data);
if(client > 0 && IsClientConnected(client) && IsClientInGame(client)) {
char id[32];
char name[64];
char icon[128];
bool owner;
int permissions;
PrintToConsole(client, "Dumping Guilds from arraylist");
for(int i = 0; i < Alid.Length; i++) {
GetArrayString(Alid, i, id, sizeof(id));
GetArrayString(Alname, i, name, sizeof(name));
GetArrayString(Alicon, i, icon, sizeof(icon));
owner = GetArrayCell(Alowner, i);
permissions = GetArrayCell(Alpermissions, i);
PrintToConsole(client, "Guild: [%s] [%s] [%s] [%i] [%i]", id, name, icon, owner, permissions);
}
}
}
public Action Cmd_SendMsg(int client, int argc) {
if(client == 0)
{
ReplyToCommand(client, "[SM] This command cannot be used from console.");
return Plugin_Handled;
}
if(argc != 2)
{
ReplyToCommand(client, "[SM] Usage: sm_sendmsg <channelid> <message>.");
return Plugin_Handled;
}
char channelid[64];
GetCmdArg(1, channelid, sizeof(channelid));
char message[256];
GetCmdArg(2, message, sizeof(message));
gBot.SendMessageToChannelID(channelid, message, OnMessageSent, GetClientUserId(client));
return Plugin_Handled;
}
public void OnMessageSent(DiscordBot bot, char[] channel, DiscordMessage message, any data)
{
int client = GetClientOfUserId(data);
ReplyToCommand(client, "Message sent!");
}