/// <summary> /// Starts a connection to discord with the given discord settings /// </summary> /// <param name="settings">Discord connection settings</param> public void Connect(DiscordSettings settings) { Settings = settings ?? throw new ArgumentNullException(nameof(settings)); Logger = new Logger(settings.LogLevel); if (string.IsNullOrEmpty(Settings.ApiToken)) { Logger.Error("API Token is null or empty!"); return; } if (!TokenValidator.IsMatch(Settings.ApiToken)) { Logger.Warning($"API Token does not appear to be a valid discord bot token: {Settings.GetHiddenToken()}. " + "Please confirm you are using the correct bot token. " + "If the token is correct and this message is showing please let the Discord Extension Developers know."); } if (!string.IsNullOrEmpty(DiscordExtension.TestVersion)) { Logger.Warning($"Using Discord Test Version: {DiscordExtension.GetExtensionVersion}"); } Logger.Debug($"{nameof(DiscordClient)}.{nameof(Connect)} GetOrCreate bot for {Owner.Name}"); Bot = BotClient.GetOrCreate(this); RegisterPluginForHooks(Owner); Interface.Call(DiscordExtHooks.OnDiscordClientConnected, Owner, this); }
public void Initialize(Plugin plugin, DiscordSettings settings) { if (plugin == null) { throw new PluginNullException(); } if (settings == null) { throw new SettingsNullException(); } if (string.IsNullOrEmpty(settings.ApiToken)) { throw new APIKeyException(); } /*if(Discord.PendingTokens.Contains(settings.ApiToken)) // Not efficient, will re-do later * { * Interface.Oxide.LogWarning($"[Discord Extension] Connection with same token in short period.. Connection delayed for {plugin.Name}"); * Timer t = new Timer() { AutoReset = false, Interval = 5000f, Enabled = true}; * t.Elapsed += (object sender, ElapsedEventArgs e) => * { * // TODO: Check if the connection still persists or cancelled * Interface.Oxide.LogWarning($"[Discord Extension] Delayed connection for {plugin.Name} is being resumed.."); * Initialize(plugin, settings); * }; * return; * }*/ RegisterPlugin(plugin); UpdatePluginReference(plugin); CallHook("DiscordSocket_Initialized"); Settings = settings; REST = new RESTHandler(Settings.ApiToken); _webSocket = new Socket(this); if (!string.IsNullOrEmpty(WSSURL)) { _webSocket.Connect(WSSURL); return; } this.GetURL(url => { UpdateWSSURL(url); _webSocket.Connect(WSSURL); }); /*Discord.PendingTokens.Add(settings.ApiToken); // Not efficient, will re-do later * Timer t2 = new Timer() { AutoReset = false, Interval = 5000f, Enabled = true }; * t2.Elapsed += (object sender, ElapsedEventArgs e) => * { * if (Discord.PendingTokens.Contains(settings.ApiToken)) * Discord.PendingTokens.Remove(settings.ApiToken); * };*/ }
/// <summary> /// Starts a connection to discord with the given apiKey and intents /// </summary> /// <param name="apiKey">API key for the connecting bot</param> /// <param name="intents">Intents the bot needs in order to function</param> public void Connect(string apiKey, GatewayIntents intents) { DiscordSettings settings = new DiscordSettings { ApiToken = apiKey, LogLevel = DiscordLogLevel.Info, Intents = intents }; Connect(settings); }
/// <summary> /// Creates a new bot client for the given settings /// </summary> /// <param name="settings"></param> public BotClient(DiscordSettings settings) { Settings = new DiscordSettings { ApiToken = settings.ApiToken, LogLevel = settings.LogLevel, Intents = settings.Intents }; Logger = new Logger(Settings.LogLevel); Initialized = true; Rest = new RestHandler(this, Logger); _webSocket = new Socket(this, Logger); }
public static void CreateClient(Plugin plugin, DiscordSettings settings) { if (plugin == null) { throw new PluginNullException(); } if (settings == null) { throw new SettingsNullException(); } if (string.IsNullOrEmpty(settings.ApiToken)) { throw new APIKeyException(); } // Find an existing DiscordClient and update it var client = Clients.FirstOrDefault(x => x.Plugins.Any(p => p.Title == plugin.Title)); if (client != null) { if (client.Settings.ApiToken != settings.ApiToken) { throw new LimitedClientException(); } var existingPlugins = client.Plugins.Where(x => x.Title == plugin.Title).ToList(); existingPlugins.ForEach(x => client.Plugins.Remove(x)); client.RegisterPlugin(plugin); client.UpdatePluginReference(plugin); client.Settings = settings; client.CallHook("DiscordSocket_Initialized", plugin); return; } // Create a new DiscordClient var newClient = new DiscordClient(); Clients.Add(newClient); newClient.Initialize(plugin, settings); }
//public static List<string> PendingTokens = new List<string>(); // Not efficient, will re-do later public static void CreateClient(Plugin plugin, string apiKey) { if (plugin == null) { throw new PluginNullException(); } if (string.IsNullOrEmpty(apiKey)) { throw new APIKeyException(); } var settings = new DiscordSettings() { ApiToken = apiKey }; CreateClient(plugin, settings); }
public void Initialize(Plugin plugin, DiscordSettings settings) { if (plugin == null) { throw new PluginNullException(); } if (settings == null) { throw new SettingsNullException(); } if (string.IsNullOrEmpty(settings.ApiToken)) { throw new APIKeyException(); } RegisterPlugin(plugin); Settings = settings; REST = new RESTHandler(Settings.ApiToken); _webSocket = new Socket(this); if (!string.IsNullOrEmpty(WSSURL)) { _webSocket.Connect(WSSURL); return; } this.GetURL(url => { WSSURL = url; _webSocket.Connect(WSSURL); }); }