Exemple #1
0
        public void WriteLine(LogLevel level, string format, Color?color = null, params object[] bindings)
        {
            IRocketSettingsProvider rocketSettings = container.Resolve <IRocketSettingsProvider>();
            Color orgCol = ConsoleLogger.GetForegroundColor();

            SetForegroundColor(Color.White);
            System.Console.Write("[");

            SetForegroundColor(BaseLogger.GetLogLevelColor(level));
            System.Console.Write(BaseLogger.GetLogLevelPrefix(level));

            SetForegroundColor(Color.White);
            System.Console.Write("] ");

            if (rocketSettings?.Settings.Logging.IncludeMethods ?? true)
            {
                SetForegroundColor(Color.White);
                System.Console.Write("[");

                SetForegroundColor(Color.DarkGray);
                System.Console.Write(GetLoggerCallingMethod().GetDebugName());

                SetForegroundColor(Color.White);
                System.Console.Write("] ");
            }

            SetForegroundColor(color ?? Color.White);

            string line = string.Format(format, bindings);

            System.Console.WriteLine(line);

            SetForegroundColor(orgCol);
        }
        public async Task <bool> HandleCommandAsync(IUser user, string commandLine, string prefix)
        {
            GuardUser(user);

            commandLine = commandLine.Trim();
            string[] args = commandLine.Split(' ');

            IDependencyContainer    contextContainer = container.CreateChildContainer();
            IRocketSettingsProvider settings         = contextContainer.Resolve <IRocketSettingsProvider>();

            CommandContext context = new CommandContext(contextContainer,
                                                        user, prefix, null,
                                                        args[0], args.Skip(1).ToArray(), null, null);

            ICommand target = context.Container.Resolve <ICommandProvider>()
                              .Commands.GetCommand(context.CommandAlias, user);

            if (target == null)
            {
                return(false); // only return false when the command was not found
            }
            context.Command = target;

            List <ICommand> tree = new List <ICommand> {
                context.Command
            };

            context = GetChild(context, context, tree);

            var permission = GetPermission(context);

            try
            {
                IPermissionProvider provider = container.Resolve <IPermissionProvider>();

                if (await provider.CheckPermissionAsync(user, permission) != PermissionResult.Grant)
                {
                    var logger = container.Resolve <ILogger>();
                    logger.LogInformation($"User \"{user.UserName}\" does not have permission to execute: \"{commandLine}\"");
                    throw new NotEnoughPermissionsException(user, permission);
                }

                if (settings.Settings.Logging.EnableCommandExecutionsLogs)
                {
                    contextContainer.Resolve <ILogger>().LogInformation($"{user.UserName} has executed command: \"{commandLine}\"");
                }
                await context.Command.ExecuteAsync(context);
            }
            catch (Exception e)
            {
                if (e is ICommandFriendlyException exception)
                {
                    await exception.SendErrorMessageAsync(context);

                    return(true);
                }

                await context.User.SendMessageAsync("An internal error occured.", Color.DarkRed);

                throw new Exception($"Command \"{commandLine}\" of user \"{user.UserName}\" caused an exception: ", e);
            }

            return(true);
        }