Exemple #1
0
 private void ResetNotificationChannel() => _notificationChannel.SetValue((_parentGuild.GetGuild().TextChannels.FirstOrDefault()?.Id).GetValueOrDefault());
Exemple #2
0
        public void LoadPlugins()
        {
            Log.Write(Log.Type.PLUGIN, "Reloading plugins for guild " + _parentHandler.GetGuild().Name);

            Filter <string> filter = new Filter <string>(x => _enabledPlugins.GetValue().Any(y => NameMatches(x, y)));

            string[] toLoad = PluginLoader.GetPlugins().Select(x => Plugin.GetFullName(x)).ToArray();
            toLoad = filter.FilterModules(toLoad).ToArray();

            Log.Write(Log.Type.PLUGIN, "Loading plugins: " + string.Join(',', toLoad));

            foreach (string name in toLoad)
            {
                Type pluginType = PluginLoader.GetPlugin(name);
                if (pluginType == null)
                {
                    Log.Write(Log.Type.WARNING, $"Attempted to instantiate unloaded/unknown plugin type {name}");
                }
                else
                {
                    IPlugin plugin = AssemblyLoader.Instantiate <IPlugin>(pluginType);
                    _activePlugins.Add(plugin);
                }
            }

            bool initError = false;

            foreach (IPlugin plugin in _activePlugins)
            {
                try
                {
                    Log.Write(Log.Type.PLUGIN, "Pre-initializing plugin " + Plugin.GetVersionedFullName(plugin.GetType()));
                    plugin.PreInitialize(_parentHandler);
                } catch (Exception exc)
                {
                    Log.Write(Log.Type.WARNING, $"Something went wrong during plugin pre-initialization of plugin '{Plugin.GetName (plugin.GetType ())}'. The plugin has been disabled and plugin initialization scheduled to be restarted.");
                    Log.Write(exc);
                    RemovePlugin(Plugin.GetFullName(plugin.GetType()));
                    initError = true;
                }
            }

            foreach (IPlugin plugin in _activePlugins)
            {
                try
                {
                    Log.Write(Log.Type.PLUGIN, "Initializing plugin " + Plugin.GetVersionedFullName(plugin.GetType()));
                    plugin.Initialize();
                }
                catch (Exception exc)
                {
                    Log.Write(Log.Type.WARNING, $"Something went wrong during plugin initialization of plugin '{Plugin.GetName(plugin.GetType())}'. The plugin has been disabled and plugin initialization scheduled to be restarted.");
                    Log.Write(exc);
                    RemovePlugin(Plugin.GetFullName(plugin.GetType()));
                    initError = true;
                }
            }

            foreach (IPlugin plugin in _activePlugins)
            {
                try
                {
                    Log.Write(Log.Type.PLUGIN, "Post-initializing plugin " + Plugin.GetVersionedFullName(plugin.GetType()));
                    plugin.PostInitialize();
                }
                catch (Exception exc)
                {
                    Log.Write(Log.Type.WARNING, $"Something went wrong during plugin post-initialization of plugin '{Plugin.GetName(plugin.GetType())}'. The plugin has been disabled and plugin initialization scheduled to be restarted.");
                    Log.Write(exc);
                    RemovePlugin(Plugin.GetFullName(plugin.GetType()));
                    initError = true;
                }
            }

            if (initError)
            {
                ReloadPlugins();
            }
        }
Exemple #3
0
        public GuildNotifier(GuildHandler parentGuild)
        {
            _parentGuild         = parentGuild;
            _notificationChannel = new CachedValue <ulong>(
                new DoubleKeyJsonRepository("pluginconfig"), _parentGuild.GuildId, "NotificationChannel", () => (_parentGuild.GetGuild().TextChannels.FirstOrDefault()?.Id).GetValueOrDefault());
            _allowNotifications = new CachedValue <bool>(
                new DoubleKeyJsonRepository("pluginconfig"), _parentGuild.GuildId, "AllowNotifications", () => true);

            _parentGuild.Config.Add("Set Notification Channel", "Set channel", "Universal Settings",
                                    new Action <SocketTextChannel>(x => _notificationChannel.SetValue(x.Id)),
                                    new Func <SocketTextChannel, string>(x => $"Set notification channel to {x.Mention}"),
                                    "Channel");
            _parentGuild.Config.Add("Set Notification Channel", "Set channel", "Universal Settings", new Action(() => { }), new Func <string>(
                                        () => $"Current notification channel is {GetNotificationChannelName()}"));
            _parentGuild.Config.Add("Toggle Notifications", "Toggle notications", "Universal Settings", new Action(() => _allowNotifications.SetValue(!_allowNotifications.GetValue())), new Func <string>(
                                        () => _allowNotifications.GetValue() ? "You have opted in to bot notifications." : "You have opted out of bot notifications."));
        }
Exemple #4
0
        public void LoadPlugins()
        {
            MakeSuperSureCriticalPluginsAreEnabled();
            PurgeDuplicateEnabledPlugins();

            Log.Write(Log.Type.PLUGIN, "Reloading plugins for guild " + _parentHandler.GetGuild().Name);
            OnPrePluginsLoaded?.Invoke();

            Filter <string> filter = new Filter <string>(x => _enabledPlugins.GetValue().Any(y => NameMatches(x, y)), x => ContainsDependencies(_enabledPlugins.GetValue(), x));

            string[] toLoad = PluginLoader.GetPlugins().Select(x => Plugin.GetFullName(x)).ToArray();
            toLoad = filter.FilterModules(toLoad).ToArray();

            Log.Write(Log.Type.PLUGIN, "Loading plugins: " + string.Join(',', toLoad));

            foreach (string name in toLoad)
            {
                Type pluginType = PluginLoader.GetPlugin(name);
                if (pluginType == null)
                {
                    Log.Write(Log.Type.WARNING, $"Attempted to instantiate unloaded/unknown plugin type {name}");
                }
                else
                {
                    Log.Plugin($"Instantiating plugin '{Plugin.GetVersionedFullName(pluginType)}'.");
                    IPlugin plugin = AssemblyLoader.Instantiate <IPlugin>(pluginType);
                    _activePlugins.Add(plugin);
                }
            }

            bool initError = false;

            foreach (IPlugin plugin in _activePlugins)
            {
                try
                {
                    Log.Write(Log.Type.PLUGIN, "Pre-initializing plugin " + Plugin.GetVersionedFullName(plugin.GetType()));
                    plugin.PreInitialize(_parentHandler);
                } catch (Exception exc)
                {
                    ReportInitError("pre-initialization", new PluginInitializationException(Plugin.GetName(plugin.GetType()), exc), plugin, ref initError);
                }
            }

            foreach (IPlugin plugin in _activePlugins)
            {
                try
                {
                    Log.Write(Log.Type.PLUGIN, "Initializing plugin " + Plugin.GetVersionedFullName(plugin.GetType()));
                    plugin.Initialize();
                }
                catch (Exception exc)
                {
                    ReportInitError("initialization", new PluginInitializationException(Plugin.GetName(plugin.GetType()), exc), plugin, ref initError);
                }
            }

            foreach (IPlugin plugin in _activePlugins)
            {
                try
                {
                    Log.Write(Log.Type.PLUGIN, "Post-initializing plugin " + Plugin.GetVersionedFullName(plugin.GetType()));
                    plugin.PostInitialize();
                }
                catch (Exception exc)
                {
                    ReportInitError("post-initialization", new PluginInitializationException(Plugin.GetName(plugin.GetType()), exc), plugin, ref initError);
                }

                OnPluginLoaded?.Invoke(plugin);
            }

            if (initError)
            {
                ReloadPlugins();
            }

            OnPluginsLoaded?.Invoke();
        }