public string[] OnCall(ICommandSender sender, string[] args) { if (NetworkSystem.IsConnected()) { NetworkSystem.Disconnect(); return(new string[] { "Connection closed, reconnecting will begin shortly." }); } else { return(new string[] { "Connection was already closed, reconnecting is in progress." }); } }
public bool SendString(IEnumerable <string> channelAliases, string message) { foreach (string channel in channelAliases) { if (Config.GetDict("aliases").ContainsKey(channel)) { NetworkSystem.QueueMessage(Config.GetDict("aliases")[channel] + message); } } return(true); }
public async void Reload() { ConfigParser.loaded = false; NetworkSystem.ShutDown(); Logger.Log("Loading config \"" + Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "config.yml\"", LogID.Config); ConfigParser.LoadConfig(); await DiscordAPI.Reset(); new Thread(() => new StartNetworkSystem()).Start(); }
public string[] OnCall(ICommandSender sender, string[] args) { plugin.Info("Reloading plugin..."); Config.Reload(plugin); plugin.Info("Successfully loaded config '" + plugin.GetConfigString("scpdiscord_config") + "'."); Language.Reload(); plugin.roleSync.Reload(); if (NetworkSystem.IsConnected()) { NetworkSystem.Disconnect(); } return(new string[] { "Reload complete." }); }
public bool SendStringByID(ulong channelID, string message) { MessageWrapper wrapper = new MessageWrapper { ChatMessage = new ChatMessage { ChannelID = channelID, Content = message } }; NetworkSystem.QueueMessage(wrapper); return(true); }
public void SendRoleQuery(Player player) { if (player.UserIDType != UserIdType.STEAM || !syncedPlayers.ContainsKey(player.UserID)) { return; } MessageWrapper message = new MessageWrapper { RoleQuery = new RoleQuery { SteamID = player.UserID, DiscordID = syncedPlayers[player.UserID] } }; NetworkSystem.QueueMessage(message); }
public bool SendString(IEnumerable <string> channelAliases, string message) { foreach (string channel in channelAliases) { if (Config.GetDict("aliases").ContainsKey(channel)) { MessageWrapper wrapper = new MessageWrapper { ChatMessage = new ChatMessage { ChannelID = Config.GetDict("aliases")[channel], Content = message } }; NetworkSystem.QueueMessage(wrapper); } } return(true); }
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 = 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; } plugin.Debug("Incoming command from discord: " + messages[0]); string[] words = messages[0].Split(' '); if (words[0] == "command") { string channel = words[1]; string discordTag = words[2].Replace('_', ' '); string command = words[3]; string[] arguments = new string[0]; if (words.Length >= 5) { arguments = words.Skip(4).ToArray(); } string response; Dictionary <string, string> variables; switch (command) { case "ban": //Check if the command has enough arguments if (arguments.Length >= 2) { BanCommand(channel, arguments[0], arguments[1], MergeString(arguments.Skip(2).ToArray()), discordTag); } else { variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageByID(channel, "botresponses.missingarguments", variables); } break; case "kick": //Check if the command has enough arguments if (arguments.Length >= 1) { KickCommand(channel, arguments[0], MergeString(arguments.Skip(1).ToArray()), discordTag); } else { variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageByID(channel, "botresponses.missingarguments", variables); } break; case "kickall": KickallCommand(channel, MergeString(arguments), discordTag); break; case "unban": //Check if the command has enough arguments if (arguments.Length >= 1) { UnbanCommand(channel, arguments[0]); } else { variables = new Dictionary <string, string> { { "command", messages[0] } }; plugin.SendMessageByID(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.UserId + ">" + "\n"; } message += "```"; NetworkSystem.QueueMessage(channel + message); break; case "exit": plugin.SendMessageByID(channel, "botresponses.exit"); break; case "help": plugin.SendMessageByID(channel, "botresponses.help"); break; case "hidetag": case "showtag": if (plugin.PluginManager.GetEnabledPlugin("karlofduty.toggletag") != null) { if (arguments.Length > 0) { command = "console_" + command; response = plugin.ConsoleCommand(plugin.PluginManager.Server, command, arguments); variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessageByID(channel, "botresponses.consolecommandfeedback", variables); } else { variables = new Dictionary <string, string> { { "command", command } }; plugin.SendMessageByID(channel, "botresponses.missingarguments", variables); } } else { plugin.SendMessageByID(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 = plugin.ConsoleCommand(plugin.PluginManager.Server, command, arguments); variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessageByID(channel, "botresponses.consolecommandfeedback", variables); } else { plugin.SendMessageByID(channel, "botresponses.vpnshield.notinstalled"); } break; case "scperms_reload": case "scperms_giverank": case "scperms_removerank": case "scperms_verbose": case "scperms_debug": case "scpermissions_reload": case "scpermissions_giverank": case "scpermissions_removerank": case "scpermissions_verbose": case "scpermissions_debug": if (plugin.PluginManager.GetEnabledPlugin("karlofduty.scpermissions") != null) { response = plugin.ConsoleCommand(plugin.PluginManager.Server, command, arguments); variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessageByID(channel, "botresponses.consolecommandfeedback", variables); } else { plugin.SendMessageByID(channel, "botresponses.scpermissions.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 = plugin.ConsoleCommand(plugin.PluginManager.Server, command, arguments); variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessageByID(channel, "botresponses.consolecommandfeedback", variables); break; } } else if (words[0] == "roleresponse") { plugin.roleSync.ReceiveQueryResponse(words[1] + "@steam", MergeString(words.Skip(2).ToArray())); } plugin.Verbose("From discord: " + messages[0]); messages.RemoveAt(0); } } catch (Exception ex) { plugin.Error("BotListener Error: " + ex); } } Thread.Sleep(1000); } }
public ProcessMessageAsync(string channelID, string messagePath, Dictionary <string, string> variables) { NetworkSystem.ProcessMessage(channelID, messagePath, variables); }
public StartNetworkSystem(SCPDiscord plugin) { NetworkSystem.Run(plugin); }
public BotListener(SCPDiscord plugin) { this.plugin = plugin; while (true) { try { //Listen for connections if (NetworkSystem.IsConnected()) { MessageWrapper data; try { data = MessageWrapper.Parser.ParseDelimitedFrom(NetworkSystem.networkStream); } catch (Exception e) { if (e is IOException) { plugin.Error("Connection to bot lost."); } else { plugin.Error("Couldnt parse incoming packet!\n" + e.ToString()); } return; } plugin.Debug("Incoming packet: " + Google.Protobuf.JsonFormatter.Default.Format(data)); switch (data.MessageCase) { case MessageWrapper.MessageOneofCase.SyncRoleCommand: plugin.SendStringByID(data.SyncRoleCommand.ChannelID, plugin.roleSync.AddPlayer(data.SyncRoleCommand.SteamID.ToString(), data.SyncRoleCommand.DiscordID)); break; case MessageWrapper.MessageOneofCase.UnsyncRoleCommand: plugin.SendStringByID(data.UnsyncRoleCommand.ChannelID, plugin.roleSync.RemovePlayer(data.UnsyncRoleCommand.DiscordID)); break; case MessageWrapper.MessageOneofCase.ConsoleCommand: string[] words = data.ConsoleCommand.Command.Split(' '); string response = plugin.ConsoleCommand(plugin.PluginManager.Server, words[0], words.Skip(1).ToArray()); Dictionary <string, string> variables = new Dictionary <string, string> { { "feedback", response } }; plugin.SendMessageByID(data.ConsoleCommand.ChannelID, "botresponses.consolecommandfeedback", variables); break; case MessageWrapper.MessageOneofCase.RoleResponse: plugin.roleSync.ReceiveQueryResponse(data.RoleResponse.SteamID, data.RoleResponse.RoleIDs.ToList()); break; case MessageWrapper.MessageOneofCase.BanCommand: BanCommand(data.BanCommand.ChannelID, data.BanCommand.SteamID.ToString(), data.BanCommand.Duration, data.BanCommand.Reason, data.BanCommand.AdminTag); break; case MessageWrapper.MessageOneofCase.UnbanCommand: UnbanCommand(data.UnbanCommand.ChannelID, data.UnbanCommand.SteamIDOrIP); break; case MessageWrapper.MessageOneofCase.KickCommand: KickCommand(data.KickCommand.ChannelID, data.KickCommand.SteamID.ToString(), data.KickCommand.Reason, data.KickCommand.AdminTag); break; case MessageWrapper.MessageOneofCase.KickallCommand: KickallCommand(data.KickallCommand.ChannelID, data.KickallCommand.Reason, data.KickallCommand.AdminTag); break; case MessageWrapper.MessageOneofCase.ListCommand: var reply = "```md\n# Players online (" + (plugin.Server.NumPlayers - 1) + "):\n"; foreach (Player player in plugin.Server.GetPlayers()) { reply += player.Name.PadRight(35) + "<" + player.UserID + ">" + "\n"; } reply += "```"; plugin.SendStringByID(data.ListCommand.ChannelID, reply); break; case MessageWrapper.MessageOneofCase.BotActivity: case MessageWrapper.MessageOneofCase.ChatMessage: case MessageWrapper.MessageOneofCase.RoleQuery: plugin.Warn("Recieved packet meant for bot: " + Google.Protobuf.JsonFormatter.Default.Format(data)); break; default: plugin.Warn("Unknown packet received: " + Google.Protobuf.JsonFormatter.Default.Format(data)); break; } } Thread.Sleep(1000); } catch (Exception ex) { plugin.Error("BotListener Error: " + ex); } } }
public bool SendStringByID(string channelID, string message) { NetworkSystem.QueueMessage(channelID + message); return(true); }
public override void OnDisable() { shutdown = true; NetworkSystem.Disconnect(); this.Info("SCPDiscord disabled."); }
public StartNetworkSystem() { NetworkSystem.Init(); }