public void OnRoundStart(RoundStartEvent ev) { /// <summary> /// This is the event handler for Round start events (before people are spawned in) /// </summary> plugin.SendMessage(Config.GetArray("channels.onroundstart"), "round.onroundstart"); roundHasStarted = true; }
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.SendMessage(Config.GetArray("channels.onscp914activate"), "environment.onscp914activate", variables); }
private static void Connect() { plugin.Verbose("Attempting Bot Connection..."); plugin.Verbose("Your Bot IP: " + Config.GetString("bot.ip") + ". Your Bot Port: " + Config.GetInt("bot.port") + "."); while (!IsConnected()) { try { if (socket != null && socket.IsBound) { //socket.Shutdown(SocketShutdown.Both); messageThread?.Abort(); socket.Close(); } socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(Config.GetString("bot.ip"), Config.GetInt("bot.port")); messageThread = new Thread(() => new BotListener(plugin)); messageThread.Start(); networkStream = new NetworkStream(socket); plugin.Info("Connected to Discord bot."); plugin.SendMessage(Config.GetArray("channels.statusmessages"), "botmessages.connectedtobot"); } catch (SocketException e) { plugin.VerboseError("Error occured while connecting to discord bot server: " + e.Message.Trim()); plugin.DebugError(e.ToString()); Thread.Sleep(5000); } catch (ObjectDisposedException e) { plugin.VerboseError("TCP client was unexpectedly closed."); plugin.DebugError(e.ToString()); Thread.Sleep(5000); } catch (ArgumentOutOfRangeException e) { plugin.VerboseError("Invalid port."); plugin.DebugError(e.ToString()); Thread.Sleep(5000); } catch (ArgumentNullException e) { plugin.VerboseError("IP address is null."); plugin.DebugError(e.ToString()); Thread.Sleep(5000); } } }
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.SendMessage(Config.GetArray("channels.onadminquery"), "admin.onadminquery", variables); }
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.SendMessage(Config.GetArray("channels.ondecideteamrespawnqueue"), "team.ondecideteamrespawnqueue", variables); }
/// <summary> /// Handles a ban command from Discord. /// </summary> /// <param name="channelID">ChannelID of the channel the command was used in.</param> /// <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 channelID, string steamID, string duration, string reason, string adminTag) { // Perform very basic SteamID validation. if (!IsPossibleSteamID(steamID)) { Dictionary <string, string> variables = new Dictionary <string, string> { { "steamid", steamID } }; plugin.SendMessageByID(channelID, "botresponses.invalidsteamid", variables); return; } // Create duration timestamp. string humanReadableDuration = ""; DateTime endTime; try { endTime = ParseBanDuration(duration, ref humanReadableDuration); } catch (IndexOutOfRangeException) { endTime = DateTime.MinValue; } if (endTime == DateTime.MinValue) { Dictionary <string, string> variables = new Dictionary <string, string> { { "duration", duration } }; plugin.SendMessageByID(channelID, "botresponses.invalidduration", variables); return; } string name = ""; if (!plugin.GetPlayerName(steamID, ref name)) { name = "Offline player"; } //Semicolons are separators 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(true, true) + "UserIdBans.txt", true); streamWriter.WriteLine(name + ';' + steamID + ';' + endTime.Ticks + ';' + reason + ";" + adminTag + ";" + 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 }, { "admintag", adminTag } }; plugin.SendMessage(Config.GetArray("channels.statusmessages"), "botresponses.playerbanned", banVars); }
private static void Connect(string address, int port) { if (Config.GetBool("settings.verbose")) { plugin.Info("Attempting Bot Connection..."); } while (!IsConnected()) { try { if (socket != null && socket.IsBound) { socket.Shutdown(SocketShutdown.Both); socket.Close(); } if (Config.GetBool("settings.verbose")) { plugin.Info("Your Bot IP: " + Config.GetString("bot.ip") + ". Your Bot Port: " + Config.GetInt("bot.port") + "."); } socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(Config.GetString("bot.ip"), Config.GetInt("bot.port")); plugin.Info("Connected to Discord bot."); plugin.SendMessage(Config.GetArray("channels.statusmessages"), "botmessages.connectedtobot"); } catch (SocketException e) { if (Config.GetBool("settings.verbose")) { plugin.Error("Error occured while connecting to discord bot server: " + e.Message); if (Config.GetBool("settings.verbose")) { plugin.Error(e.ToString()); } } Thread.Sleep(5000); } catch (ObjectDisposedException e) { if (Config.GetBool("settings.verbose")) { plugin.Error("TCP client was unexpectedly closed."); plugin.Error(e.ToString()); } Thread.Sleep(5000); } catch (ArgumentOutOfRangeException e) { if (Config.GetBool("settings.verbose")) { plugin.Error("Invalid port."); plugin.Error(e.ToString()); } Thread.Sleep(5000); } catch (ArgumentNullException e) { if (Config.GetBool("settings.verbose")) { plugin.Error("IP address is null."); plugin.Error(e.ToString()); } Thread.Sleep(5000); } } }
public BotListener(SCPDiscord plugin) { this.plugin = plugin; while (!plugin.shutdown) { //Listen for connections if (NetworkSystem.IsConnected()) { try { //Discord messages can be up to 2000 chars long, UTF8 chars can be up to 4 bytes long. byte[] data = new byte[1000]; int dataLength = NetworkSystem.Receive(data); string incomingData = ""; incomingData = Encoding.UTF8.GetString(data, 0, dataLength); 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; } if (Config.GetBool("settings.debug")) { plugin.Info("COMMAND: " + messages[0]); } string[] words = messages[0].Split(' '); if (words[0] == "command") { string channel = words[1]; string command = words[2]; string[] arguments = new string[0]; if (words.Length >= 4) { arguments = words.Skip(3).ToArray(); } string response = ""; Dictionary <string, string> variables = new Dictionary <string, string>(); switch (command) { case "ban": //Check if the command has enough arguments if (arguments.Length >= 2) { BanCommand(arguments[0], arguments[1], MergeString(arguments.Skip(2).ToArray())); } else { variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessage(channel, "botresponses.missingarguments", variables); } break; case "kick": //Check if the command has enough arguments if (arguments.Length >= 1) { KickCommand(arguments[0], MergeString(arguments.Skip(1).ToArray())); } else { variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessage(channel, "botresponses.missingarguments", variables); } break; case "kickall": KickallCommand(MergeString(arguments)); break; case "unban": //Check if the command has enough arguments if (arguments.Length >= 1) { UnbanCommand(arguments[0]); } else { variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessage(channel, "botresponses.missingarguments", variables); } break; case "list": var message = "```md\n# Players online (" + (plugin.Server.NumPlayers - 1) + "):\n"; foreach (Player player in plugin.Server.GetPlayers()) { message += player.Name.PadRight(35) + "<" + player.SteamId + ">" + "\n"; } message += "```"; NetworkSystem.QueueMessage(channel + message); break; case "exit": plugin.SendMessage(channel, "botresponses.exit"); break; case "help": plugin.SendMessage(channel, "botresponses.help"); break; case "hidetag": case "showtag": if (plugin.pluginManager.GetEnabledPlugin("karlofduty.toggletag") != null) { if (arguments.Length > 0) { command = "console_" + command; response = ConsoleCommand(plugin.pluginManager.Server, command, arguments); variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessage(channel, "botresponses.consolecommandfeedback", variables); } else { variables = new Dictionary <string, string> { { "command", command } }; plugin.SendMessage(channel, "botresponses.missingarguments", variables); } } else { plugin.SendMessage(channel, "botresponses.toggletag.notinstalled"); } break; case "vs_enable": case "vs_disable": case "vs_whitelist": case "vs_reload": if (plugin.pluginManager.GetEnabledPlugin("karlofduty.vpnshield") != null) { response = ConsoleCommand(plugin.pluginManager.Server, command, arguments); variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessage(channel, "botresponses.consolecommandfeedback", variables); } else { plugin.SendMessage(channel, "botresponses.vpnshield.notinstalled"); } break; case "syncrole": NetworkSystem.QueueMessage(channel + plugin.roleSync.AddPlayer(arguments[0], arguments[1])); break; case "unsyncrole": NetworkSystem.QueueMessage(channel + plugin.roleSync.RemovePlayer(arguments[0])); break; default: response = ConsoleCommand(plugin.pluginManager.Server, command, arguments); variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessage(channel, "botresponses.consolecommandfeedback", variables); break; } } else if (words[0] == "roleresponse") { plugin.roleSync.ReceiveQueryResponse(words[1], MergeString(words.Skip(2).ToArray())); } if (Config.GetBool("settings.verbose")) { plugin.Info("From discord: " + messages[0]); } messages.RemoveAt(0); } } catch (Exception ex) { if (ex is IOException) { plugin.Error("BotListener Error: " + ex); } plugin.Error("BotListener Error: " + ex); } } Thread.Sleep(1000); } }
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.SendMessage(Config.GetArray("channels.onplayerhurt.noattacker"), "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.SendMessage(Config.GetArray("channels.onplayerhurt.friendlyfire"), "player.onplayerhurt.friendlyfire", variables); return; } plugin.SendMessage(Config.GetArray("channels.onplayerhurt.default"), "player.onplayerhurt.default", variables); }