|
| 1 | +#include <sourcemod> |
| 2 | +#include <basecomm> |
| 3 | +#include <ripext> |
| 4 | + |
| 5 | +public Plugin myinfo = |
| 6 | +{ |
| 7 | + name = "gcdr", |
| 8 | + author = "noil.lt", |
| 9 | + description = "Game Chat to Discord Relay.", |
| 10 | + version = "0.3", |
| 11 | + url = "https://noil.lt/" |
| 12 | +}; |
| 13 | + |
| 14 | +ConVar g_cvEnable; |
| 15 | +ConVar g_cvWebhook; |
| 16 | + |
| 17 | +public void OnPluginStart() |
| 18 | +{ |
| 19 | + g_cvEnable = CreateConVar("gcdr_enable", "0", "Toggle whether plugin is enabled. 1 = Enabled, 0 = Disabled"); |
| 20 | + g_cvWebhook = CreateConVar("gcdr_discord_webhook_url", "", "Discord webhook full URL", FCVAR_PROTECTED); |
| 21 | + |
| 22 | + AutoExecConfig(true, "gcdr"); |
| 23 | +} |
| 24 | + |
| 25 | +void OnRequestFinished(HTTPResponse response, any value, const char[] error) |
| 26 | +{ |
| 27 | + if (response.Status != HTTPStatus_NoContent) { |
| 28 | + LogMessage("Could not send message. Response Code: %d, Error: %s", response.Status, error); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +public void OnClientSayCommand_Post(int client, const char[] command, const char[] sArgs) |
| 33 | +{ |
| 34 | + // Do not run if either: Plugin Disabled, Player Gagged, Chat Trigger, Is Console |
| 35 | + if (!g_cvEnable.BoolValue) return; |
| 36 | + if (BaseComm_IsClientMuted(client) || IsChatTrigger()) return; |
| 37 | + if (client <= 0) return; |
| 38 | + |
| 39 | + char WebhookUrl[1024], msgContent[512], clientName[128], steamID[64]; |
| 40 | + GetConVarString(g_cvWebhook, WebhookUrl, sizeof(WebhookUrl)); |
| 41 | + GetClientName(client, clientName, sizeof(clientName)); |
| 42 | + GetClientAuthId(client, AuthId_Steam2, steamID, sizeof(steamID)); |
| 43 | + |
| 44 | + HTTPRequest request = new HTTPRequest(WebhookUrl); |
| 45 | + JSONObject payload = new JSONObject(); |
| 46 | + |
| 47 | + ReplaceString(clientName, sizeof(clientName), "@", "", false); |
| 48 | + ReplaceString(clientName, sizeof(clientName), "\\", "", false); |
| 49 | + ReplaceString(clientName, sizeof(clientName), "\\\\", "", false); |
| 50 | + ReplaceString(clientName, sizeof(clientName), "`", "", false); |
| 51 | + |
| 52 | + FormatEx(clientName, sizeof(clientName), "%s (%s)", clientName, steamID); |
| 53 | + FormatEx(msgContent, sizeof(msgContent), "%s", sArgs); |
| 54 | + |
| 55 | + payload.SetString("username", clientName); |
| 56 | + payload.SetString("content", msgContent); |
| 57 | + |
| 58 | + request.Post(view_as<JSON>(payload), OnRequestFinished); |
| 59 | + delete payload; |
| 60 | +} |
0 commit comments