public static void Error(string Message, LogID logID) { try { DiscordAPI.GetClient().Logger.Log(LogLevel.Error, eventIDs[logID], Message); } catch (NullReferenceException) { Console.WriteLine("[ERROR] " + Message); } }
public static void Fatal(string Message, LogID logID) { try { DiscordAPI.GetClient().Logger.Log(LogLevel.Critical, eventIDs[logID], Message); } catch (NullReferenceException) { Console.WriteLine("[CRITICAL] " + Message); } }
public static void Warn(string Message, LogID logID) { try { DiscordAPI.GetClient().Logger.Log(LogLevel.Warning, eventIDs[logID], Message); } catch (NullReferenceException) { Console.WriteLine("[WARNING] " + Message); } }
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(); }
private static async void Update() { MessageWrapper wrapper; try { wrapper = MessageWrapper.Parser.ParseDelimitedFrom(networkStream); } catch (Exception) { Logger.Error("Couldn't parse incoming packet!", LogID.Network); return; } Logger.Debug("Incoming packet: " + JsonFormatter.Default.Format(wrapper), LogID.Network); switch (wrapper.MessageCase) { case MessageWrapper.MessageOneofCase.BotActivity: DiscordAPI.SetActivity(wrapper.BotActivity.ActivityText, (ActivityType)wrapper.BotActivity.ActivityType, (UserStatus)wrapper.BotActivity.StatusType); break; case MessageWrapper.MessageOneofCase.ChatMessage: await DiscordAPI.SendMessage(wrapper.ChatMessage.ChannelID, wrapper.ChatMessage.Content); break; case MessageWrapper.MessageOneofCase.RoleQuery: DiscordAPI.GetPlayerRoles(wrapper.RoleQuery.DiscordID, wrapper.RoleQuery.SteamID); break; case MessageWrapper.MessageOneofCase.SyncRoleCommand: case MessageWrapper.MessageOneofCase.UnsyncRoleCommand: case MessageWrapper.MessageOneofCase.ConsoleCommand: case MessageWrapper.MessageOneofCase.RoleResponse: Logger.Warn("Recieved packet meant for plugin: " + JsonFormatter.Default.Format(wrapper), LogID.Network); break; default: Logger.Warn("Unknown packet received: " + JsonFormatter.Default.Format(wrapper), LogID.Network); break; } }
public async static Task Reset() { try { Logger.Log("Setting up Discord client...", LogID.Discord); instance = new DiscordAPI(); // Check if token is unset if (ConfigParser.config.bot.token == "<add-token-here>" || ConfigParser.config.bot.token == "") { Logger.Fatal("You need to set your bot token in the config and start the bot again.", LogID.Config); throw new ArgumentException("Invalid Discord bot token"); } // Checking log level if (!Enum.TryParse(ConfigParser.config.bot.logLevel, true, out LogLevel logLevel)) { Logger.Warn("Log level '" + ConfigParser.config.bot.logLevel + "' invalid, using 'Information' instead.", LogID.Config); logLevel = LogLevel.Information; } // Setting up client configuration DiscordConfiguration cfg = new DiscordConfiguration { Token = ConfigParser.config.bot.token, TokenType = TokenType.Bot, MinimumLogLevel = logLevel, AutoReconnect = true, Intents = DiscordIntents.AllUnprivileged, LogTimestampFormat = "yyyy-MM-dd HH:mm:ss" }; instance.discordClient = new DiscordClient(cfg); Logger.Log("Registering commands...", LogID.Discord); instance.commands = instance.discordClient.UseCommandsNext(new CommandsNextConfiguration { StringPrefixes = new[] { ConfigParser.config.bot.prefix } }); instance.commands.RegisterCommands <Commands.SyncRoleCommand>(); instance.commands.RegisterCommands <Commands.UnsyncRoleCommand>(); instance.commands.RegisterCommands <Commands.ServerCommand>(); instance.commands.RegisterCommands <Commands.ListCommand>(); instance.commands.RegisterCommands <Commands.KickAllCommand>(); instance.commands.RegisterCommands <Commands.KickCommand>(); instance.commands.RegisterCommands <Commands.BanCommand>(); instance.commands.RegisterCommands <Commands.UnbanCommand>(); Logger.Log("Hooking events...", LogID.Discord); instance.discordClient.Ready += instance.OnReady; instance.discordClient.GuildAvailable += instance.OnGuildAvailable; instance.discordClient.ClientErrored += instance.OnClientError; instance.discordClient.SocketErrored += instance.OnSocketError; Logger.Log("Hooking command events...", LogID.Discord); instance.commands.CommandErrored += instance.OnCommandError; ConfigParser.PrintConfig(); Logger.Log("Connecting to Discord...", LogID.Discord); await instance.discordClient.ConnectAsync(); } catch (Exception e) { Logger.Error(e.ToString(), LogID.Discord); } }
public static void Init() { shutdown = false; if (listenerSocket != null) { listenerSocket.Shutdown(SocketShutdown.Both); listenerSocket.Close(); } if (clientSocket != null) { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); } while (!ConfigParser.loaded) { Thread.Sleep(1000); } IPEndPoint listenerEndpoint; IPAddress ipAddress; if (ConfigParser.config.plugin.address == "0.0.0.0") { ipAddress = IPAddress.Any; } else if (ConfigParser.config.plugin.address == "::0") { ipAddress = IPAddress.IPv6Any; } else if (IPAddress.TryParse(ConfigParser.config.plugin.address, out IPAddress parsedIP)) { ipAddress = parsedIP; } else { IPHostEntry ipHostInfo = Dns.GetHostEntry(ConfigParser.config.plugin.address); // Use an IPv4 address if available if (ipHostInfo.AddressList.Any(ip => ip.AddressFamily == AddressFamily.InterNetwork)) { ipAddress = ipHostInfo.AddressList.First(ip => ip.AddressFamily == AddressFamily.InterNetwork); } else { ipAddress = ipHostInfo.AddressList[0]; } } listenerEndpoint = new IPEndPoint(ipAddress, ConfigParser.config.plugin.port); listenerSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listenerSocket.Bind(listenerEndpoint); listenerSocket.Listen(10); while (!shutdown) { try { if (IsConnected()) { Update(); } else { DiscordAPI.SetDisconnectedActivity(); Logger.Log("Listening on " + ipAddress.ToString() + ":" + ConfigParser.config.plugin.port, LogID.Network); clientSocket = listenerSocket.Accept(); networkStream = new NetworkStream(clientSocket, true); Logger.Log("Plugin connected.", LogID.Network); } Thread.Sleep(1000); } catch (Exception e) { Logger.Error("Network error caught, if this happens a lot try using the 'scpd_rc' command." + e, LogID.Network); } } }