Beispiel #1
0
        /// <summary>
        /// Initializes the command handlers.
        /// </summary>
        private void InitializeCommandHandlers()
        {
            foreach (var item in this.allCommandHandlers)
            {
                BotCommandHandler handler = null;

                // try to get the value
                try
                {
                    handler = item.Value;
                }
                catch (CompositionException ex)
                {
                    ReportPluginCompositionError(ex);
                    continue;
                }

                // handler is valid, process it
                var attribute = handler.GetHandlerAttribute();
                var target = PluginHandle.Get(handler);
                var attributes = new CommandDataAttributes
                {
                    CommandAttribute = attribute,
                    HelpAttribute = handler.GetHelpAttribute(),
                    RoleAttribute = handler.GetRoleDefaultAttribute()
                };
                var commandData = new CommandData(target, handler, attributes);
                this.commandHandlers.Add(commandData);

                // assign a role to this command
                authorizer.EnsureCommandRole(attribute.Commands, commandData.DefaultRole);

                // save plugin reference
                AddPluginToRunningList(target);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Executes the command handler.
        /// </summary>
        /// <param name="commandData">The command data.</param>
        /// <param name="packet">The packet.</param>
        public void ExecuteCommandHandler(CommandData commandData, CommandPacket packet)
        {
            var context = GenerateContext(packet, commandData.Target);
            var handler = commandData.CommandHandler;

            try
            {
                // Since this is a system plugin the current thread is the main processing thread. we don't
                // want to be held back processing commands while new packets are arriving. so spawn off a
                // new thread to process the incoming command and keep on trucking while that happens.
                Task.Factory
                    .StartNew(() => handler(packet, context), TaskCreationOptions.PreferFairness)
                    .ContinueWith(t =>
                    {
                        // if this runs there was an exception executing the command on the thread
                        t.Exception.Handle(ex =>
                        {
                            console.WriteLine(string.Format("Error executing the command '{0}'. See bot log for details.", packet.Command), Style.Error);
                            Logger.Error(ex.Message, ex);
                            if (context.Client != null && context.Client.Connected) context.Client.Say(packet.ChatRoom, string.Format("{0}: error occured executing the command. Notify the bot admin.", packet.Command));
                            return true;
                        });
                    }, TaskContinuationOptions.OnlyOnFaulted);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
                console.WriteLine("Error occured. See bot log for details.", Style.Error);
            }
        }