Permalink
Browse files

Add discord plugin

TTT-Discord send the round end logs to your discord server (to a specific channel).

This requires [sourcemod-discord](https://github.com/Deathknife/sourcemod-discord)
  • Loading branch information...
Bara committed Nov 20, 2018
1 parent 8a6e58d commit 450cfa7f78fba2a82c8e65b797d20f247b2ac261
@@ -7,5 +7,3 @@ addons/sourcemod/scripting/include/vphysics.inc
addons/sourcemod/scripting/tests/vphysics.sp
addons/sourcemod/scripting/tests/accelerator.sp
addons/sourcemod/scripting/ttt/ttt_ranking.sp
addons/sourcemod/scripting/include/ttt_discord.inc
addons/sourcemod/scripting/ttt/ttt_discord.sp
@@ -0,0 +1,24 @@
#if defined _ttt_discord_included
#endinput
#endif
#define _ttt_discord_included
native bool TTT_SendDiscordMessage(const char[] message, any ...);
public SharedPlugin __pl_ttt_discord =
{
name = "ttt_discord",
file = "ttt_discord.smx",
#if defined REQUIRE_PLUGIN
required = 1,
#else
required = 0,
#endif
};
#if !defined REQUIRE_PLUGIN
public void __pl_ttt_discord_SetNTVOptional()
{
MarkNativeAsOptional("TTT_SendDiscordMessage");
}
#endif
@@ -35,6 +35,13 @@ public void OnPluginStart()
RegAdminCmd("sm_endround", Command_EndRound, ADMFLAG_ROOT);
}
public void TTT_OnLatestVersion(const char[] version)
{
LogMessage("TTT_OnLatestVersion was called!");
LogMessage("(TTT_OnLatestVersion) version: %s", version);
TTT_CheckVersion(TTT_PLUGIN_VERSION, TTT_GetCommitsCount());
}
public Action Command_TRole(int client, int args)
{
int iRole = TTT_GetClientRole(client);
@@ -127,6 +134,11 @@ public Action Event_WeaponFire(Event event, const char[] name, bool dontBroadcas
}
}
public Action TTT_OnRoundStart_Pre()
{
LogMessage("TTT_OnRoundStart_Pre was called!");
}
public void TTT_OnRoundStart(int innocents, int traitors, int detective)
{
LogMessage("TTT_OnRoundStart was called!");
@@ -139,12 +151,24 @@ public void TTT_OnRoundStartFailed(int players, int requiredPlayers, int detecti
LogMessage("(TTT_OnRoundStartFailed) players: %d - requiredPlayers: %d - detective: %d", players, requiredPlayers, detective);
}
public void TTT_OnRoundEnd(int winner, Handle array)
{
LogMessage("TTT_OnRoundEnd was called!");
LogMessage("(TTT_OnRoundEnd) winner: %d - array: %d", winner, array);
}
public void TTT_OnClientGetRole(int client, int role)
{
LogMessage("TTT_OnClientGetRole was called!");
LogMessage("(TTT_OnClientGetRole) client: %d - role: %d", client, role);
}
public void TTT_OnTakeDamage(int victim, int attacker, float damage, int weapon, bool badAction)
{
LogMessage("TTT_OnTakeDamage was called!");
LogMessage("(TTT_OnTakeDamage) victim: %d - attacker: %d, damage: %f, weapon: %d, badAction: %d", victim, attacker, damage, weapon, badAction);
}
public void TTT_OnClientDeath(int victim, int attacker)
{
LogMessage("TTT_OnClientDeath was called!");
@@ -163,6 +187,48 @@ public void TTT_OnBodyFound(int client, int victim, const char[] deadPlayer, boo
LogMessage("(TTT_OnBodyFound) client: %d - victim: %d - deadPlayer: %s - silent: %b", client, victim, deadPlayer, silent);
}
public Action TTT_OnRulesMenu(int client, bool &show)
{
LogMessage("TTT_OnRulesMenu was called!");
LogMessage("(TTT_OnRulesMenu) client: %d, show: %d", client, show);
}
public Action TTT_OnDetectiveMenu(int client, bool &show)
{
LogMessage("TTT_OnDetectiveMenu was called!");
LogMessage("(TTT_OnDetectiveMenu) client: %d, show: %d", client, show);
}
public Action TTT_OnBodyCheck(int client, int[] ragdoll)
{
LogMessage("TTT_OnBodyCheck was called!");
LogMessage("(TTT_OnBodyCheck) client: %d, ragdoll: %d", client, ragdoll);
}
public void TTT_OnModelUpdate(int client, const char[] model)
{
LogMessage("TTT_OnModelUpdate was called!");
LogMessage("(TTT_OnModelUpdate) client: %d, model: %s", client, model);
}
public void TTT_OnPlayerRespawn(int client)
{
LogMessage("TTT_OnPlayerRespawn was called!");
LogMessage("(TTT_OnPlayerRespawn) client: %d", client);
}
public int TTT_OnButtonPress(int client, int button)
{
LogMessage("TTT_OnButtonPress was called!");
LogMessage("(TTT_OnButtonPress) client: %d, button: %s", client, button);
}
public int TTT_OnButtonRelease(int client, int button)
{
LogMessage("TTT_OnButtonRelease was called!");
LogMessage("(TTT_OnButtonRelease) client: %d, button: %s", client, button);
}
public void TTT_OnBodyScanned(int client, int victim, const char[] deadPlayer)
{
LogMessage("TTT_OnBodyScanned was called!");
@@ -175,6 +241,18 @@ public Action TTT_OnPlayerDeath(int victim, int attacker)
LogMessage("(TTT_OnPlayerDeath) victim: %d - attacker: %d", victim, attacker);
}
public void TTT_OnKarmaUpdate(int client, KarmaUpdate type, int karma)
{
LogMessage("TTT_OnKarmaUpdate was called!");
LogMessage("(TTT_OnKarmaUpdate) client: %d - type: %d - karma: %d", client, type, karma);
}
public bool TTT_OnCheckCommandAccess(int client, const char[] command, const char[] flags, bool access)
{
LogMessage("TTT_OnCheckCommandAccess was called!");
LogMessage("(TTT_OnCheckCommandAccess) client: %d - command: %d - flags: %d - access", client, command, flags, access);
}
public void TTT_OnSQLConnect(Database db)
{
LogMessage("TTT_OnSQLConnect was called!");
@@ -194,9 +272,3 @@ public Action TTT_OnGrabbing(int client, int entity)
LogMessage("TTT_OnGrabbing was called!");
LogMessage("(TTT_OnGrabbing) Client: %N - Entity: %d", client, entity);
}
public void TTT_OnLatestVersion(const char[] version)
{
LogMessage("TTT_OnLatestVersion was called!");
LogMessage("(TTT_OnLatestVersion) Version: %s", version);
}
@@ -0,0 +1,159 @@
#pragma semicolon 1
#include <sourcemod>
#include <discord>
#include <ttt>
#define PLUGIN_NAME TTT_PLUGIN_NAME ... " - Discord"
ConVar g_cBotToken = null;
ConVar g_cChannelName = null;
ConVar g_cChannelID = null;
char g_sChannelID[32];
DiscordBot g_dBot = null;
public Plugin myinfo =
{
name = PLUGIN_NAME,
author = TTT_PLUGIN_AUTHOR,
description = TTT_PLUGIN_DESCRIPTION,
version = TTT_PLUGIN_VERSION,
url = TTT_PLUGIN_URL
};
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
CreateNative("TTT_SendDiscordMessage", Native_SendDiscordMessage);
RegPluginLibrary("ttt_discord");
return APLRes_Success;
}
public void OnPluginStart()
{
TTT_StartConfig("discord");
CreateConVar("ttt2_discord_version", TTT_PLUGIN_VERSION, TTT_PLUGIN_DESCRIPTION, FCVAR_NOTIFY | FCVAR_DONTRECORD | FCVAR_REPLICATED);
g_cBotToken = AutoExecConfig_CreateConVar("discord_chat_bot_token", "bot token...", "Set the bot token", FCVAR_PROTECTED);
g_cChannelName = AutoExecConfig_CreateConVar("discord_chat_get_channel_name", "channel name...", "Channel name that will be read", FCVAR_PROTECTED);
g_cChannelID = AutoExecConfig_CreateConVar("discord_chat_get_channel_id", "channel id...", "Channel ID that will be read", FCVAR_PROTECTED);
TTT_EndConfig();
}
public void TTT_OnLatestVersion(const char[] version)
{
TTT_CheckVersion(TTT_PLUGIN_VERSION, TTT_GetCommitsCount());
}
public void OnConfigsExecuted()
{
if (g_dBot != null)
{
g_dBot.StopListening();
delete g_dBot;
}
char sToken[72];
g_cBotToken.GetString(sToken, sizeof(sToken));
g_dBot = new DiscordBot(sToken);
g_dBot.MessageCheckInterval = 1.0;
g_dBot.GetGuilds(GuildList);
}
public void GuildList(DiscordBot bot, char[] id, char[] name, char[] icon, bool owner, int permissions, any data)
{
g_dBot.GetGuildChannels(id, ChannelList, INVALID_FUNCTION, data);
}
public void ChannelList(DiscordBot bot, char[] guild, DiscordChannel Channel, any data)
{
char sChannelName[32], sChannelID[32], sCVarChannelID[32], sCVarChannelName[32];
g_cChannelName.GetString(sCVarChannelName, sizeof(sCVarChannelName));
g_cChannelID.GetString(sCVarChannelID, sizeof(sCVarChannelID));
Channel.GetID(sChannelID, sizeof(sChannelID));
Channel.GetName(sChannelName, sizeof(sChannelName));
if(Channel.IsText && (StrContains(sChannelName, sCVarChannelName, false) != -1 && StrEqual(sChannelID, sCVarChannelID, false)))
{
strcopy(g_sChannelID, sizeof(g_sChannelID), sChannelID);
}
}
public int Native_SendDiscordMessage(Handle plugin, int numParams)
{
char message[512];
int bytes;
FormatNativeString(0, 1, 2, sizeof(message), bytes, message);
g_dBot.SendMessageToChannelID(g_sChannelID, message, INVALID_FUNCTION);
return 0;
}
public void TTT_OnRoundEnd(int winner, Handle array)
{
ArrayList aLogs = view_as<ArrayList>(array);
if (g_dBot != null && aLogs.Length > 0)
{
ArrayList aMessages = new ArrayList(1998);
char sWinner[32], sMessage[1998], sDelimiter[64];
TTT_GetRoleNameByID(winner, sWinner, sizeof(sWinner));
Format(sDelimiter, sizeof(sDelimiter), "-----------------------------------------------------------\n");
Format(sMessage, sizeof(sMessage), "%sWinners: %s\n", sDelimiter, sWinner);
for (int i = 0; i < aLogs.Length; i++)
{
char sBuffer[256];
aLogs.GetString(i, sBuffer, sizeof(sBuffer));
if (strlen(sMessage) + strlen(sBuffer) >= 1999)
{
aMessages.PushString(sMessage);
strcopy(sMessage, sizeof(sMessage), "");
}
Format(sMessage, sizeof(sMessage), "%s%s\n", sMessage, sBuffer);
/* Last Message - i: 159 (+ 1), Length: 160
Push last message into the ArrayList */
if ((i + 1) == aLogs.Length)
{
aMessages.PushString(sMessage);
}
}
aMessages.PushString(sDelimiter);
for (int i = 0; i < aMessages.Length; i++)
{
char sBuffer[1998];
aMessages.GetString(i, sBuffer, sizeof(sBuffer));
DataPack pack = new DataPack();
float fTime = (2.4 * float(i + 1));
CreateTimer(fTime, Timer_SendMessage, pack);
pack.WriteString(sBuffer);
}
delete aMessages;
}
delete aLogs;
}
public Action Timer_SendMessage(Handle timer, DataPack pack)
{
pack.Reset();
char sMessage[1998];
pack.ReadString(sMessage, sizeof(sMessage));
g_dBot.SendMessageToChannelID(g_sChannelID, sMessage, INVALID_FUNCTION);
delete pack;
}

0 comments on commit 450cfa7

Please sign in to comment.