private async Task <bool> LoadScriptCollection(SocketGuild guild) { Logging.LogDebug(LogType.Bot, $"Loading scripts for guild {guild.Name}"); if (!m_scriptCollections.ContainsKey(guild.Id)) { DiscordScriptCollection scriptCollection = new DiscordScriptCollection(guild); if (!m_scriptCollections.TryAdd(guild.Id, scriptCollection)) { Logging.LogError(LogType.Bot, $"Failed to add guild {guild.Name} to script collection will be ignored"); return(false); } Logging.LogDebug(LogType.Bot, $"Starting Loading scripts for guild {guild.Name}"); await LoadScripts(DiscordData.ScriptAdminFolder, scriptCollection); await LoadScripts(DiscordData.ScriptGlobalFolder, scriptCollection); string serverScripts = Path.Combine(DiscordData.ScriptFolder, guild.Id.ToString()); await LoadScripts(serverScripts, scriptCollection); } else { DiscordScriptCollection scriptCollection; if (m_scriptCollections.TryGetValue(guild.Id, out scriptCollection)) { Logging.LogDebug(LogType.Bot, $"Starting Reloading scripts for guild {guild.Name}"); await LoadScripts(DiscordData.ScriptAdminFolder, scriptCollection); await LoadScripts(DiscordData.ScriptGlobalFolder, scriptCollection); string serverScripts = Path.Combine(DiscordData.ScriptFolder, guild.Id.ToString()); await LoadScripts(serverScripts, scriptCollection); } else { Logging.LogError(LogType.Bot, $"Failed to get script collection for guild {guild.Name} no script will be loaded"); return(false); } } Logging.LogDebug(LogType.Bot, $"Finished Loading scripts for guild {guild.Name}"); return(true); }
private Task LoadScripts(string folder, DiscordScriptCollection collection) { Logging.LogDebug(LogType.Bot, $"Loading scripts from folder '{folder}'"); try { if (!Directory.Exists(folder)) { Logging.LogDebug(LogType.Bot, $"Folder '{folder}' doesnt exist creating folder"); Directory.CreateDirectory(folder); } else { var csScripts = Directory.EnumerateFiles(folder, "*.cs", SearchOption.AllDirectories); Logging.LogDebug(LogType.Bot, $"Found '{csScripts.Count()}' scripts to compile"); foreach (string script in csScripts) { IEnumerable <IDiscordScript> scripts = null; if (TryCompileScript(script, out scripts)) { Logging.LogDebug(LogType.Bot, $"Build '{scripts.Count()}' scripts from file '{script}'"); foreach (IDiscordScript compiledScript in scripts) { Logging.LogDebug(LogType.Bot, $"Adding script '{compiledScript.Name}' to collection'"); DiscordScriptHost scriptHost = new DiscordScriptHost(m_client, compiledScript); collection.Add(scriptHost); } } } } } catch (Exception ex) { Logging.LogException(LogType.Bot, ex, "Failed to load scripts"); } Logging.LogDebug(LogType.Bot, $"Finished Loading scripts from folder '{folder}'"); return(Task.CompletedTask); }
public async Task Initalise(DiscordSocketClient client) { if (client != m_client) { Logging.LogDebug(LogType.Bot, "Client Reset"); } else { Logging.LogDebug(LogType.Bot, "Client Reset but returned the same client will rehook"); } if (client != null) { Logging.LogDebug(LogType.Bot, $"Initalising Client"); //can fail if they havent already been registered try { m_client.GuildAvailable -= client_GuildAvailable; m_client.Ready -= client_Ready; } catch { } m_client = client; m_client.GuildAvailable += client_GuildAvailable; m_client.Ready += client_Ready; if (m_adminScriptCollection != null) { m_adminScriptCollection.Dispose(); } Logging.LogDebug(LogType.Bot, $"Creating Admin script collection"); m_adminScriptCollection = new DiscordScriptCollection(); await LoadScripts(DiscordData.ScriptAdminFolder, m_adminScriptCollection); } }