public void OnRoundStart(RoundStartEvent ev) { /// <summary> /// This is the event handler for Round start events (before people are spawned in) /// </summary> plugin.SendMessageToBot(plugin.GetConfigString("discord_channel_onroundstart"), "round.onroundstart"); roundHasStarted = true; }
public void OnDecideTeamRespawnQueue(DecideRespawnQueueEvent ev) { /// <summary> /// Called at the start, when the team respawn queue is being read. This happens BEFORE it fills it to full with filler_team_id. /// <summary> Dictionary <string, string> variables = new Dictionary <string, string> { { "teams", ev.Teams.ToString() }, }; plugin.SendMessageToBot(plugin.GetConfigString("discord_channel_ondecideteamrespawnqueue"), "team.ondecideteamrespawnqueue", variables); }
//This is a loop that keeps running and checks if the bot has been disconnected public StartConnectionWatchdog(SCPDiscordPlugin plugin) { while (true) { Thread.Sleep(2000); if (!plugin.clientSocket.Connected && plugin.hasConnectedOnce) { plugin.clientSocket.Close(); plugin.Info("Not connected, trying to reconnect"); if (plugin.GetConfigBool("discord_verbose")) { plugin.Warn("Discord bot connection issue detected, attempting reconnect..."); } try { plugin.clientSocket = new TcpClient(plugin.GetConfigString("discord_bot_ip"), plugin.GetConfigInt("discord_bot_port")); plugin.Info("Reconnected to Discord bot."); plugin.SendMessageToBot("default", "botmessages.reconnectedtobot"); } catch (SocketException e) { if (plugin.GetConfigBool("discord_verbose")) { plugin.Info("Error occured while reconnecting to discord bot server."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } catch (ObjectDisposedException e) { if (plugin.GetConfigBool("discord_verbose")) { plugin.Info("TCP client was unexpectedly closed."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } catch (ArgumentOutOfRangeException e) { if (plugin.GetConfigBool("discord_verbose")) { plugin.Info("Invalid port."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } catch (ArgumentNullException e) { if (plugin.GetConfigBool("discord_verbose")) { plugin.Info("IP address is null."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } } } }
//This is ran once on the first time connecting to the bot public ConnectToBot(SCPDiscordPlugin plugin) { Thread.Sleep(2000); while (!plugin.clientSocket.Connected) { if (plugin.GetConfigBool("discord_verbose")) { plugin.Info("Attempting Bot Connection..."); } try { if (plugin.GetConfigBool("discord_verbose")) { plugin.Info("Your Bot IP: " + plugin.GetConfigString("discord_bot_ip") + ". Your Bot Port: " + plugin.GetConfigInt("discord_bot_port") + "."); } plugin.clientSocket.Connect(plugin.GetConfigString("discord_bot_ip"), plugin.GetConfigInt("discord_bot_port")); } catch (SocketException e) { if (plugin.GetConfigBool("discord_verbose")) { plugin.Info("Error occured while connecting to discord bot server."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } catch (ObjectDisposedException e) { if (plugin.GetConfigBool("discord_verbose")) { plugin.Info("TCP client was unexpectedly closed."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } catch (ArgumentOutOfRangeException e) { if (plugin.GetConfigBool("discord_verbose")) { plugin.Info("Invalid port."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } catch (ArgumentNullException e) { if (plugin.GetConfigBool("discord_verbose")) { plugin.Info("IP address is null."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } } plugin.Info("Connected to Discord bot."); plugin.SendMessageToBot("default", "botmessages.connectedtobot"); plugin.hasConnectedOnce = true; }
//This is ran once on the first time connecting to the bot public ConnectToBot(SCPDiscordPlugin plugin) { Thread.Sleep(2000); while (!plugin.clientSocket.Connected) { if (Config.settings.verbose) { plugin.Info("Attempting Bot Connection..."); } try { if (Config.settings.verbose) { plugin.Info("Your Bot IP: " + Config.bot.ip + ". Your Bot Port: " + Config.bot.port + "."); } plugin.clientSocket.Connect(Config.bot.ip, Config.bot.port); } catch (SocketException e) { if (Config.settings.verbose) { plugin.Info("Error occured while connecting to discord bot server."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } catch (ObjectDisposedException e) { if (Config.settings.verbose) { plugin.Info("TCP client was unexpectedly closed."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } catch (ArgumentOutOfRangeException e) { if (Config.settings.verbose) { plugin.Info("Invalid port."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } catch (ArgumentNullException e) { if (Config.settings.verbose) { plugin.Info("IP address is null."); plugin.Debug(e.ToString()); } Thread.Sleep(5000); } } plugin.Info("Connected to Discord bot."); plugin.SendMessageToBot(Config.channels.onroundend, "botmessages.connectedtobot"); plugin.hasConnectedOnce = true; }
public void OnAdminQuery(AdminQueryEvent ev) { ///Triggered whenever an adming uses an admin command, both gui and commandline RA if (ev.Query == "REQUEST_DATA PLAYER_LIST SILENT") { return; } Dictionary <string, string> variables = new Dictionary <string, string> { { "ipaddress", ev.Admin.IpAddress }, { "name", ev.Admin.Name }, { "playerid", ev.Admin.PlayerId.ToString() }, { "steamid", ev.Admin.SteamId }, { "class", ev.Admin.TeamRole.Role.ToString() }, { "team", ev.Admin.TeamRole.Team.ToString() }, { "handled", ev.Handled.ToString() }, { "output", ev.Output }, { "query", ev.Query }, { "successful", ev.Successful.ToString() } }; plugin.SendMessageToBot(plugin.GetConfigString("discord_channel_onadminquery"), "admin.onadminquery", variables); }
public void OnSCP914Activate(SCP914ActivateEvent ev) { /// <summary> /// This is the event handler for when a SCP914 is activated /// </summary> Dictionary <string, string> variables = new Dictionary <string, string> { { "knobsetting", ev.KnobSetting.ToString() } }; plugin.SendMessageToBot(Config.channels.onscp914activate, "environment.onscp914activate", variables); }
/// <summary> /// Handles a ban command from Discord. /// </summary> /// <param name="steamID">SteamID of player to be banned.</param> /// <param name="duration">Duration of ban expressed as xu where x is a number and u is a character representing a unit of time.</param> /// <param name="reason">Optional reason for the ban.</param> private void BanCommand(string steamID, string duration, string reason = "") { // Perform very basic SteamID validation. if (!IsPossibleSteamID(steamID)) { Dictionary <string, string> variables = new Dictionary <string, string> { { "steamid", steamID } }; plugin.SendMessageToBot("default", "botresponses.invalidsteamid", variables); return; } // Create duration timestamp. string humanReadableDuration = ""; DateTime endTime = ParseBanDuration(duration, ref humanReadableDuration); if (endTime == DateTime.MinValue) { Dictionary <string, string> variables = new Dictionary <string, string> { { "duration", duration } }; plugin.SendMessageToBot("default", "botresponses.invalidduration", variables); return; } string name = ""; if (!plugin.GetPlayerName(steamID, ref name)) { name = "Offline player"; } //Semicolons are seperators in the ban file so cannot be part of strings name = name.Replace(";", ""); reason = reason.Replace(";", ""); if (reason == "") { reason = "No reason provided."; } // Add the player to the SteamIDBans file. StreamWriter streamWriter = new StreamWriter(FileManager.GetAppFolder() + "/SteamIdBans.txt", true); streamWriter.WriteLine(name + ';' + steamID + ';' + endTime.Ticks + ';' + reason + ";DISCORD;" + DateTime.UtcNow.Ticks); streamWriter.Dispose(); // Kicks the player if they are online. plugin.KickPlayer(steamID, "Banned for the following reason: '" + reason + "'"); Dictionary <string, string> banVars = new Dictionary <string, string> { { "name", name }, { "steamid", steamID }, { "reason", reason }, { "duration", humanReadableDuration } }; plugin.SendMessageToBot("default", "botresponses.playerbanned", banVars); }
public BotListener(SCPDiscordPlugin plugin) { this.plugin = plugin; while (true) { //Listen for connections if (plugin.clientSocket.Connected && plugin.hasConnectedOnce) { Thread.Sleep(200); try { //Discord messages can be up to 2000 chars long, UTF8 chars can be up to 4 bytes long. byte[] data = new byte[8000]; NetworkStream stream = null; try { stream = plugin.clientSocket.GetStream(); } catch (Exception ex) { if (ex is IOException) { plugin.Error("Could not get stream from socket."); } else { plugin.Error("BotListener Error: " + ex.ToString()); } } if (stream == null) { return; } string incomingData = ""; try { int lengthOfData = stream.Read(data, 0, data.Length); incomingData = Encoding.UTF8.GetString(data, 0, lengthOfData); } catch (Exception ex) { if (ex is IOException) { plugin.Error("Could not read from socket."); } else { plugin.Error("BotListener Error: " + ex.ToString()); } } List <string> messages = new List <string>(incomingData.Split('\n')); //If several messages come in at the same time, process all of them while (messages.Count > 0) { if (messages[0].Length == 0) { messages.RemoveAt(0); continue; } string[] words = messages[0].Split(' '); bool isCommand = words[0] == "command"; string command = words[1]; string[] arguments = new string[0]; if (words.Length >= 3) { arguments = words.Skip(2).ToArray(); } //A verification that message is a command and not some left over string in the socket if (isCommand) { if (command == "ban") { //Check if the command has enough arguments if (arguments.Length >= 2) { BanCommand(arguments[0], arguments[1], MergeReason(arguments.Skip(2).ToArray())); } else { Dictionary <string, string> variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageToBot("default", "botresponses.missingarguments", variables); } } else if (command == "kick") { //Check if the command has enough arguments if (arguments.Length >= 1) { KickCommand(arguments[0], MergeReason(arguments.Skip(1).ToArray())); } else { Dictionary <string, string> variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageToBot("default", "botresponses.missingarguments", variables); } } else if (command == "kickall") { KickallCommand(MergeReason(arguments)); } else if (command == "unban") { //Check if the command has enough arguments if (arguments.Length >= 1) { UnbanCommand(arguments[0]); } else { Dictionary <string, string> variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageToBot("default", "botresponses.missingarguments", variables); } } else if (command == "exit") { plugin.SendMessageToBot("default", "botresponses.exit"); } else if (command == "help") { plugin.SendMessageToBot("default", "botresponses.help"); } else if (command == "hidetag" || command == "showtag") { if (plugin.pluginManager.GetEnabledPlugin("karlofduty.toggletag") != null) { if (arguments.Length > 0) { command = "console_" + command; string[] feedback = plugin.pluginManager.CommandManager.CallCommand(plugin.pluginManager.Server, command, arguments); string response = ""; StringBuilder builder = new StringBuilder(); foreach (string line in feedback) { builder.Append(line + "\n"); } response = builder.ToString(); Dictionary <string, string> variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessageToBot("default", "botresponses.consolecommandfeedback", variables); } else { Dictionary <string, string> variables = new Dictionary <string, string> { { "command", command } }; plugin.SendMessageToBot("default", "botresponses.missingarguments", variables); } } else { plugin.SendMessageToBot("default", "botresponses.toggletag.notinstalled"); } } else { string[] feedback = plugin.pluginManager.CommandManager.CallCommand(plugin.pluginManager.Server, command, arguments); string response = ""; StringBuilder builder = new StringBuilder(); foreach (string line in feedback) { builder.Append(line + "\n"); } response = builder.ToString(); Dictionary <string, string> variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessageToBot("default", "botresponses.consolecommandfeedback", variables); } } plugin.Info("From discord: " + messages[0]); messages.RemoveAt(0); } } catch (Exception ex) { if (ex is IOException) { plugin.Error("BotListener Error: " + ex.ToString()); } plugin.Error("BotListener Error: " + ex.ToString()); } } else { Thread.Sleep(2000); } } }
public void OnPlayerHurt(PlayerHurtEvent ev) { /// <summary> /// This is called before the player is going to take damage. /// In case the attacker can't be passed, attacker will be null (fall damage etc) /// This may be broken into two events in the future /// </summary> if (ev.Player == null || ev.Player.TeamRole.Role == Smod2.API.Role.UNASSIGNED) { return; } if (ev.Attacker == null || ev.Player.PlayerId == ev.Attacker.PlayerId) { Dictionary <string, string> noAttackerVar = new Dictionary <string, string> { { "damage", ev.Damage.ToString() }, { "damagetype", ev.DamageType.ToString() }, { "playeripaddress", ev.Player.IpAddress }, { "playername", ev.Player.Name }, { "playerplayerid", ev.Player.PlayerId.ToString() }, { "playersteamid", ev.Player.SteamId }, { "playerclass", ev.Player.TeamRole.Role.ToString() }, { "playerteam", ev.Player.TeamRole.Team.ToString() } }; plugin.SendMessageToBot(plugin.GetConfigString("discord_channel_onplayerhurt"), "player.onplayerhurt.noattacker", noAttackerVar); return; } Dictionary <string, string> variables = new Dictionary <string, string> { { "damage", ev.Damage.ToString() }, { "damagetype", ev.DamageType.ToString() }, { "attackeripaddress", ev.Attacker.IpAddress }, { "attackername", ev.Attacker.Name }, { "attackerplayerid", ev.Attacker.PlayerId.ToString() }, { "attackersteamid", ev.Attacker.SteamId }, { "attackerclass", ev.Attacker.TeamRole.Role.ToString() }, { "attackerteam", ev.Attacker.TeamRole.Team.ToString() }, { "playeripaddress", ev.Player.IpAddress }, { "playername", ev.Player.Name }, { "playerplayerid", ev.Player.PlayerId.ToString() }, { "playersteamid", ev.Player.SteamId }, { "playerclass", ev.Player.TeamRole.Role.ToString() }, { "playerteam", ev.Player.TeamRole.Team.ToString() } }; if (IsTeamDamage((int)ev.Attacker.TeamRole.Team, (int)ev.Player.TeamRole.Team)) { plugin.SendMessageToBot(plugin.GetConfigString("discord_channel_onplayerhurt"), "player.onplayerhurt.friendlyfire", variables); return; } plugin.SendMessageToBot(plugin.GetConfigString("discord_channel_onplayerhurt"), "player.onplayerhurt", variables); }
public BotListener(SCPDiscordPlugin plugin) { this.plugin = plugin; while (true) { //Listen for connections if (plugin.clientSocket.Connected) { try { //Discord messages can be up to 2000 chars long, UTF8 chars can be up to 4 bytes long. byte[] data = new byte[8000]; NetworkStream stream = plugin.clientSocket.GetStream(); int lengthOfData = stream.Read(data, 0, data.Length); string incomingData = System.Text.Encoding.UTF8.GetString(data, 0, lengthOfData); List <string> messages = new List <string>(incomingData.Split('\n')); //If several messages come in at the same time, process all of them while (messages.Count > 0) { if (messages[0].Length == 0) { messages.RemoveAt(0); continue; } string[] words = messages[0].Split(' '); bool isCommand = words[0] == "command"; string command = words[1]; string[] arguments = new string[0]; if (words.Length >= 3) { arguments = words.Skip(2).ToArray(); } //A verification that message is a command and not some left over string in the socket if (isCommand) { if (command == "ban") { //Check if the command has enough arguments if (arguments.Length >= 2) { BanCommand(arguments[0], arguments[1], MergeBanReason(arguments)); } else { Dictionary <string, string> variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageToBot("default", "botresponses.missingarguments", variables); } } else if (command == "kick") { //Check if the command has enough arguments if (arguments.Length >= 1) { KickCommand(arguments[0]); } else { Dictionary <string, string> variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageToBot("default", "botresponses.missingarguments", variables); } } else if (command == "unban") { //Check if the command has enough arguments if (arguments.Length >= 1) { UnbanCommand(arguments[0]); } else { Dictionary <string, string> variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageToBot("default", "botresponses.missingarguments", variables); } } else { string[] feedback = plugin.pluginManager.CommandManager.CallCommand(plugin.pluginManager.Server, command, new string[0]); string response = ""; StringBuilder builder = new StringBuilder(); foreach (string line in feedback) { builder.Append(line + "\n"); } response = builder.ToString(); Dictionary <string, string> variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessageToBot("default", "botresponses.consolecommandfeedback", variables); } } plugin.Info("From discord: " + messages[0]); messages.RemoveAt(0); } } catch (Exception ex) { plugin.Info(ex.ToString()); plugin.clientSocket.Close(); } } else { Thread.Sleep(200); } } }
public BotListener(SCPDiscordPlugin plugin) { this.plugin = plugin; while (true) { //Listen for connections if (plugin.clientSocket.Connected && plugin.hasConnectedOnce) { Thread.Sleep(200); try { //Discord messages can be up to 2000 chars long, UTF8 chars can be up to 4 bytes long. byte[] data = new byte[8000]; NetworkStream stream = null; try { stream = plugin.clientSocket.GetStream(); } catch (Exception ex) { if (ex is IOException) { plugin.Error("Could not get stream from socket."); } else { plugin.Error("BotListener Error: " + ex.ToString()); } } if (stream == null) { return; } string incomingData = ""; try { int lengthOfData = stream.Read(data, 0, data.Length); incomingData = Encoding.UTF8.GetString(data, 0, lengthOfData); } catch (Exception ex) { if (ex is IOException) { plugin.Error("Could not read from socket."); } else { plugin.Error("BotListener Error: " + ex.ToString()); } } List <string> messages = new List <string>(incomingData.Split('\n')); //If several messages come in at the same time, process all of them while (messages.Count > 0) { if (messages[0].Length == 0) { messages.RemoveAt(0); continue; } string[] words = messages[0].Split(' '); bool isCommand = words[0] == "command"; string command = words[1]; string[] arguments = new string[0]; if (words.Length >= 3) { arguments = words.Skip(2).ToArray(); } //A verification that message is a command and not some left over string in the socket if (isCommand) { if (command == "ban") { //Check if the command has enough arguments if (arguments.Length >= 2) { BanCommand(arguments[0], arguments[1], MergeReason(arguments.Skip(2).ToArray())); } else { Dictionary <string, string> variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageToBot(Config.channels.statusmessages, "botresponses.missingarguments", variables); } } else if (command == "kick") { //Check if the command has enough arguments if (arguments.Length >= 1) { KickCommand(arguments[0], MergeReason(arguments.Skip(1).ToArray())); } else { Dictionary <string, string> variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageToBot(Config.channels.statusmessages, "botresponses.missingarguments", variables); } } else if (command == "kickall") { KickallCommand(MergeReason(arguments)); } else if (command == "unban") { //Check if the command has enough arguments if (arguments.Length >= 1) { UnbanCommand(arguments[0]); } else { Dictionary <string, string> variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageToBot(Config.channels.statusmessages, "botresponses.missingarguments", variables); } } else if (command == "list") { var message = "```md\n# Players online:\n"; foreach (Player player in plugin.Server.GetPlayers()) { string line = player.Name.PadRight(32); line += player.SteamId; line += "\n"; } message += "```"; if (plugin.clientSocket == null || !plugin.clientSocket.Connected) { if (plugin.hasConnectedOnce && Config.settings.verbose) { plugin.Warn("Error sending message '" + message + "' to bot: Not connected."); } return; } // Try to send the message to the bot try { NetworkStream serverStream = plugin.clientSocket.GetStream(); byte[] outStream = System.Text.Encoding.UTF8.GetBytes("000000000000000000" + message + '\0'); serverStream.Write(outStream, 0, outStream.Length); if (Config.settings.verbose) { plugin.Info("Sent activity '" + message + "' to bot."); } } catch (InvalidOperationException e) { plugin.Error("Error sending activity '" + message + "' to bot."); plugin.Debug(e.ToString()); } catch (ArgumentNullException e) { plugin.Error("Error sending activity '" + message + "' to bot."); plugin.Debug(e.ToString()); } } else if (command == "exit") { plugin.SendMessageToBot(Config.channels.statusmessages, "botresponses.exit"); } else if (command == "help") { plugin.SendMessageToBot(Config.channels.statusmessages, "botresponses.help"); } else if (command == "hidetag" || command == "showtag") { if (plugin.pluginManager.GetEnabledPlugin("karlofduty.toggletag") != null) { if (arguments.Length > 0) { command = "console_" + command; string response = ConsoleCommand(plugin.pluginManager.Server, command, arguments); Dictionary <string, string> variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessageToBot(Config.channels.statusmessages, "botresponses.consolecommandfeedback", variables); } else { Dictionary <string, string> variables = new Dictionary <string, string> { { "command", command } }; plugin.SendMessageToBot(Config.channels.statusmessages, "botresponses.missingarguments", variables); } } else { plugin.SendMessageToBot(Config.channels.statusmessages, "botresponses.toggletag.notinstalled"); } } else if (command == "vs_enable" || command == "vs_disable" || command == "vs_whitelist" || command == "vs_reload") { if (plugin.pluginManager.GetEnabledPlugin("karlofduty.vpnshield") != null) { string response = ConsoleCommand(plugin.pluginManager.Server, command, arguments); Dictionary <string, string> variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessageToBot(Config.channels.statusmessages, "botresponses.consolecommandfeedback", variables); } else { plugin.SendMessageToBot(Config.channels.statusmessages, "botresponses.vpnshield.notinstalled"); } } else { string response = ConsoleCommand(plugin.pluginManager.Server, command, arguments); Dictionary <string, string> variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessageToBot(Config.channels.statusmessages, "botresponses.consolecommandfeedback", variables); } } plugin.Info("From discord: " + messages[0]); messages.RemoveAt(0); } } catch (Exception ex) { if (ex is IOException) { plugin.Error("BotListener Error: " + ex.ToString()); } plugin.Error("BotListener Error: " + ex.ToString()); } } else { Thread.Sleep(2000); } } }