-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathshavit-bash2-discord.sp
184 lines (150 loc) · 4.65 KB
/
shavit-bash2-discord.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
#include <sourcemod>
#include <bash2>
#include <ripext>
#pragma newdecls required
#pragma semicolon 1
ConVar gCV_Webhook;
ConVar gCV_OnlyBans;
ConVar gCV_IgnoreNulls;
ConVar gCV_UseEmbeds;
public Plugin myinfo =
{
name = "[BASH] Discord",
author = "Eric",
description = "",
version = "1.0.0",
url = "https://github.com/hermansimensen/bash2"
};
public void OnPluginStart()
{
gCV_Webhook = CreateConVar("bash_discord_webhook", "", "Discord webhook.", FCVAR_PROTECTED);
gCV_OnlyBans = CreateConVar("bash_discord_only_bans", "0", "Only send ban messages and no logs.", _, true, 0.0, true, 1.0);
gCV_IgnoreNulls = CreateConVar("bash_discord_ignore_nulls", "1", "Don't send null logs.", _, true, 0.0, true, 1.0);
gCV_UseEmbeds = CreateConVar("bash_discord_use_embeds", "1", "Send embed messages.", _, true, 0.0, true, 1.0);
AutoExecConfig(true, "bash-discord", "sourcemod");
}
public void Bash_OnDetection(int client, char[] buffer)
{
if (gCV_OnlyBans.BoolValue)
{
return;
}
if (gCV_IgnoreNulls.BoolValue && StrContains(buffer, "nullPct") != -1)
{
return;
}
if (gCV_UseEmbeds.BoolValue)
{
FormatEmbedMessage(client, buffer);
}
else
{
FormatMessage(client, buffer);
}
}
public void Bash_OnClientBanned(int client)
{
if (gCV_UseEmbeds.BoolValue)
{
FormatEmbedMessage(client, "Banned for cheating.");
}
else
{
FormatMessage(client, "Banned for cheating.");
}
}
void FormatEmbedMessage(int client, char[] buffer)
{
char hostname[128];
FindConVar("hostname").GetString(hostname, sizeof(hostname));
char steamId[32];
GetClientAuthId(client, AuthId_SteamID64, steamId, sizeof(steamId));
char name[MAX_NAME_LENGTH];
GetClientName(client, name, sizeof(name));
SanitizeName(name);
char player[512];
Format(player, sizeof(player), "[%s](http://www.steamcommunity.com/profiles/%s)", name, steamId);
// https://discord.com/developers/docs/resources/channel#embed-object
// https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure
// https://discord.com/developers/docs/resources/webhook#webhook-object-jsonform-params
JSONObject playerField = new JSONObject();
playerField.SetString("name", "Player");
playerField.SetString("value", player);
playerField.SetBool("inline", true);
JSONObject eventField = new JSONObject();
eventField.SetString("name", "Event");
eventField.SetString("value", buffer);
eventField.SetBool("inline", true);
JSONArray fields = new JSONArray();
fields.Push(playerField);
fields.Push(eventField);
JSONObject embed = new JSONObject();
embed.SetString("title", hostname);
embed.SetString("color", "16720418");
embed.Set("fields", fields);
JSONArray embeds = new JSONArray();
embeds.Push(embed);
JSONObject json = new JSONObject();
json.SetString("username", "BASH 2.0");
json.Set("embeds", embeds);
SendMessage(json);
delete playerField;
delete eventField;
delete fields;
delete embed;
delete embeds;
delete json;
}
void FormatMessage(int client, char[] buffer)
{
char hostname[128];
FindConVar("hostname").GetString(hostname, sizeof(hostname));
char steamId[32];
GetClientAuthId(client, AuthId_SteamID64, steamId, sizeof(steamId));
char name[MAX_NAME_LENGTH];
GetClientName(client, name, sizeof(name));
SanitizeName(name);
char content[1024];
Format(content, sizeof(content), "[%s](http://www.steamcommunity.com/profiles/%s) %s", name, steamId, buffer);
// Suppress Discord mentions and embeds.
// https://discord.com/developers/docs/resources/channel#allowed-mentions-object
// https://discord.com/developers/docs/resources/channel#message-object-message-flags
JSONArray parse = new JSONArray();
JSONObject allowedMentions = new JSONObject();
allowedMentions.Set("parse", parse);
JSONObject json = new JSONObject();
json.SetString("username", hostname);
json.SetString("content", content);
json.Set("allowed_mentions", allowedMentions);
json.SetInt("flags", 4);
SendMessage(json);
delete parse;
delete allowedMentions;
delete json;
}
void SendMessage(JSONObject json)
{
char webhook[256];
gCV_Webhook.GetString(webhook, sizeof(webhook));
if (webhook[0] == '\0')
{
LogError("Discord webhook is not set.");
return;
}
HTTPRequest request = new HTTPRequest(webhook);
request.Post(json, OnMessageSent);
}
public void OnMessageSent(HTTPResponse response, any value, const char[] error)
{
if (response.Status != HTTPStatus_NoContent)
{
LogError("Failed to send message to Discord. Response status: %d.", response.Status);
}
}
void SanitizeName(char[] name)
{
ReplaceString(name, MAX_NAME_LENGTH, "(", "", false);
ReplaceString(name, MAX_NAME_LENGTH, ")", "", false);
ReplaceString(name, MAX_NAME_LENGTH, "]", "", false);
ReplaceString(name, MAX_NAME_LENGTH, "[", "", false);
}