Skip to content

Commit

Permalink
feat: Extended Discord, AutoRecorder support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushaway authored Nov 1, 2023
1 parent 4952b91 commit d2a4f25
Showing 1 changed file with 90 additions and 27 deletions.
117 changes: 90 additions & 27 deletions addons/sourcemod/scripting/Lilac_Discord.sp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
#include <lilac>
#include <discordWebhookAPI>

#undef REQUIRE_PLUGIN
#tryinclude <ExtendedDiscord>
#tryinclude <AutoRecorder>
#define REQUIRE_PLUGIN

#define PLUGIN_NAME "Lilac_Discord"
#define WEBHOOK_URL_MAX_SIZE 1000
#define WEBHOOK_THREAD_NAME_MAX_SIZE 100

ConVar g_cvEnable, g_cvWebhook, g_cvWebhookRetry, g_cvAvatar, g_cvUsername, g_cvRedirectURL = null;
ConVar g_cvChannelType, g_cvThreadName, g_cvThreadID;

bool g_Plugin_ExtDiscord = false;
bool g_Plugin_AutoRecorder = false;

char g_sMap[PLATFORM_MAX_PATH];

public Plugin myinfo =
{
name = PLUGIN_NAME,
Expand All @@ -23,7 +33,7 @@ public Plugin myinfo =
public void OnPluginStart() {
/* General config */
g_cvEnable = CreateConVar("lilac_discord_enable", "1", "Toggle lilac notification system", _, true, 0.0, true, 1.0);
g_cvWebhook = CreateConVar("lilac_discord", "", "The webhook URL of your Discord channel.", FCVAR_PROTECTED);
g_cvWebhook = CreateConVar("lilac_discord_webhook", "", "The webhook URL of your Discord channel.", FCVAR_PROTECTED);
g_cvWebhookRetry = CreateConVar("lilac_discord_webhook_retry", "3", "Number of retries if webhook fails.", FCVAR_PROTECTED);
g_cvAvatar = CreateConVar("lilac_discord_avatar", "https://avatars.githubusercontent.com/u/110772618?s=200&v=4", "URL to Avatar image.");
g_cvUsername = CreateConVar("lilac_discord_username", "Little Anti-Cheat Notification", "Discord username.");
Expand All @@ -37,6 +47,33 @@ public void OnPluginStart() {
AutoExecConfig(true, PLUGIN_NAME);
}

public void OnAllPluginsLoaded()
{
g_Plugin_ExtDiscord = LibraryExists("ExtendedDiscord");
g_Plugin_AutoRecorder = LibraryExists("AutoRecorder");
}

public void OnLibraryAdded(const char[] sName)
{
if (strcmp(sName, "ExtendedDiscord", false) == 0)
g_Plugin_ExtDiscord = true;
if (strcmp(sName, "AutoRecorder", false) == 0)
g_Plugin_AutoRecorder = true;
}

public void OnLibraryRemoved(const char[] sName)
{
if (strcmp(sName, "ExtendedDiscord", false) == 0)
g_Plugin_ExtDiscord = false;
if (strcmp(sName, "AutoRecorder", false) == 0)
g_Plugin_AutoRecorder = false;
}

public void OnMapInit(const char[] mapName)
{
FormatEx(g_sMap, sizeof(g_sMap), mapName);
}

public void lilac_cheater_detected(int client, int cheat_type, char[] sLine)
{
/* Plugin Enabled ? */
Expand All @@ -52,6 +89,26 @@ public void lilac_cheater_detected(int client, int cheat_type, char[] sLine)
return;
}

#if defined _autorecorder_included
char sDemo[256];
if (g_Plugin_AutoRecorder)
{
char sDate[32];
int iCount = -1, iTick = -1, retValTime = -1;
if (AutoRecorder_IsDemoRecording())
{
iCount = AutoRecorder_GetDemoRecordCount();
iTick = AutoRecorder_GetDemoRecordingTick();
retValTime = AutoRecorder_GetDemoRecordingTime();
}
if (retValTime == -1)
sDate = "N/A";
else
FormatTime(sDate, sizeof(sDate), "%d.%m.%Y @ %H:%M", retValTime);
FormatEx(sDemo, sizeof(sDemo), "Demo: %d @ Tick: ≈ %d (Started %s)", iCount, iTick, sDate);
}
#endif

//----------------------------------------------------------------------------------------------------
/* Generate all content we will need*/
//----------------------------------------------------------------------------------------------------
Expand All @@ -64,7 +121,7 @@ public void lilac_cheater_detected(int client, int cheat_type, char[] sLine)
// Client details
char clientAuth[64], cIP[24], cDetails[240];
GetClientIP(client, cIP, sizeof(cIP));
if(!GetClientAuthId(client, AuthId_Steam2, clientAuth, sizeof(clientAuth)))
if(!GetClientAuthId(client, AuthId_Steam2, clientAuth, sizeof(clientAuth)), false)
strcopy(clientAuth, sizeof(clientAuth), "No SteamID");

FormatEx(cDetails, sizeof(cDetails), "%s \nIP: %s", clientAuth, cIP);
Expand All @@ -89,10 +146,10 @@ public void lilac_cheater_detected(int client, int cheat_type, char[] sLine)
FormatEx(connect, sizeof(connect), "[%s:%d](%s?ip=%s&port=%d)", ip, port, sURL, ip, port);

/* Send Embed message */
SendLilacDiscordMessage(client, sSuspicion, cDetails, sCheat, sLine, connect, buffer);
SendLilacDiscordMessage(client, sSuspicion, cDetails, sCheat, sLine, sDemo, connect, buffer);
}

stock void SendLilacDiscordMessage(int client, char[] sHeader, char[] sDetails, char[] sCheat, char[] sLine, char[] sConnect, char[] sWebhookURL) {
stock void SendLilacDiscordMessage(int client, char[] sHeader, char[] sDetails, char[] sCheat, char[] sLine, char[] sDemo, char[] sConnect, char[] sWebhookURL) {
//----------------------------------------------------------------------------------------------------
/* Generate the Webhook */
//----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -120,15 +177,15 @@ stock void SendLilacDiscordMessage(int client, char[] sHeader, char[] sDetails,
/* Webhook UserName */
char sName[128];
g_cvUsername.GetString(sName, sizeof(sName));
if (strlen(sName) < 1)
FormatEx(sName, sizeof(sName), "Little Anti-Cheat Notification");

/* Webhook Avatar */
char sAvatar[256];
g_cvAvatar.GetString(sAvatar, sizeof(sAvatar));

webhook.SetUsername(sName);
webhook.SetAvatarURL(sAvatar);
if (strlen(sName) > 0)
webhook.SetUsername(sName);
if (strlen(sAvatar) > 0)
webhook.SetAvatarURL(sAvatar);

Embed Embed_1 = new Embed(sHeader, sDetails);
Embed_1.SetTimeStampNow();
Expand All @@ -137,9 +194,20 @@ stock void SendLilacDiscordMessage(int client, char[] sHeader, char[] sDetails,
EmbedField Field_2 = new EmbedField("Reason", sCheat, true);
Embed_1.AddField(Field_2);

EmbedField Infos = new EmbedField("Extra Infos", sLine, false);
EmbedField Infos = new EmbedField("Details", sLine, false);
Embed_1.AddField(Infos);

EmbedField Map = new EmbedField("Map", g_sMap, false);
Embed_1.AddField(Map);

#if defined _autorecorder_included
if (g_Plugin_AutoRecorder)
{
EmbedField Demo = new EmbedField("Demo Infos", sDemo, false);
Embed_1.AddField(Demo);
}
#endif

EmbedField Connect = new EmbedField("Quick Connect", sConnect, true);
Embed_1.AddField(Connect);

Expand All @@ -163,6 +231,7 @@ stock void SendLilacDiscordMessage(int client, char[] sHeader, char[] sDetails,
pack.WriteString(sDetails);
pack.WriteString(sCheat);
pack.WriteString(sLine);
pack.WriteString(sDemo);
pack.WriteString(sConnect);
pack.WriteString(sWebhookURL);

Expand All @@ -172,7 +241,7 @@ stock void SendLilacDiscordMessage(int client, char[] sHeader, char[] sDetails,

public void OnWebHookExecuted(HTTPResponse response, DataPack pack)
{
char sHeader[192 + MAX_NAME_LENGTH], sDetails[328], sCheat[64], sLine[512], sConnect[384], sWebhookURL[WEBHOOK_URL_MAX_SIZE];
char sHeader[192 + MAX_NAME_LENGTH], sDetails[328], sCheat[64], sLine[512], sDemo[256], sConnect[384], sWebhookURL[WEBHOOK_URL_MAX_SIZE];
static int retries = 0;
pack.Reset();

Expand All @@ -183,34 +252,28 @@ public void OnWebHookExecuted(HTTPResponse response, DataPack pack)
pack.ReadString(sDetails, sizeof(sDetails));
pack.ReadString(sCheat, sizeof(sCheat));
pack.ReadString(sLine, sizeof(sLine));
pack.ReadString(sDemo, sizeof(sDemo));
pack.ReadString(sConnect, sizeof(sConnect));
pack.ReadString(sWebhookURL, sizeof(sWebhookURL));

delete pack;

if (!IsThreadReply && response.Status != HTTPStatus_OK) {
if (retries < g_cvWebhookRetry.IntValue) {
PrintToServer("[%s] Failed to send the webhook. Resending it .. (%d/%d)", PLUGIN_NAME, retries, g_cvWebhookRetry.IntValue);
SendLilacDiscordMessage(client, sHeader, sDetails, sCheat, sLine, sConnect, sWebhookURL);
retries++;
return;
} else {
LogError("[%s] Failed to send the webhook after %d retries, aborting.", PLUGIN_NAME, retries);
return;
}
}

if (IsThreadReply && response.Status != HTTPStatus_NoContent) {
if ((!IsThreadReply && response.Status != HTTPStatus_OK) || (IsThreadReply && response.Status != HTTPStatus_NoContent))
{
if (retries < g_cvWebhookRetry.IntValue) {
PrintToServer("[%s] Failed to send the webhook. Resending it .. (%d/%d)", PLUGIN_NAME, retries, g_cvWebhookRetry.IntValue);
SendLilacDiscordMessage(client, sHeader, sDetails, sCheat, sLine, sConnect, sWebhookURL);
SendLilacDiscordMessage(client, sHeader, sDetails, sCheat, sLine, sDemo, sConnect, sWebhookURL);
retries++;
return;
} else {
LogError("[%s] Failed to send the webhook after %d retries, aborting.", PLUGIN_NAME, retries);
return;
if (!g_Plugin_ExtDiscord)
LogError("[%s] Failed to send the webhook after %d retries, aborting.", PLUGIN_NAME, retries);
#if defined _extendeddiscord_included
else
ExtendedDiscord_LogError("[%s] Failed to send the webhook after %d retries, aborting.", PLUGIN_NAME, retries);
#endif
}
}

retries = 0;
}
}

0 comments on commit d2a4f25

Please sign in to comment.