Exemplo n.º 1
0
        public void CommandReceivedHandler(object sender, CommandReceivedEventArgs e)
        {
            if (sender is IChatClient chatClient)
            {
                string userDisplayName = e.ChatUser.DisplayName;

                _usageTracker.PurgeExpiredUserCommandCooldowns(DateTimeOffset.Now);

                var previousUsage = _usageTracker.GetByUserDisplayName(userDisplayName);
                if (previousUsage != null)
                {
                    if (!previousUsage.WasUserWarned)
                    {
                        chatClient.SendMessage($"Whoa {userDisplayName}! Slow down there cowboy!");
                        previousUsage.WasUserWarned = true;
                    }

                    return;
                }

                IBotCommand botCommand = _commandMessages.FirstOrDefault(c => c.ShouldExecute(e.CommandWord));
                if (botCommand != null)
                {
                    AttemptToRunCommand(e, botCommand, chatClient);
                    _usageTracker.RecordUsage(new CommandUsage(userDisplayName, DateTimeOffset.Now, false));
                }
            }
        }
Exemplo n.º 2
0
        public void CommandReceivedHandler(object sender, CommandReceivedEventArgs e)
        {
            if (!(sender is IChatClient chatClient))
            {
                return;
            }

            IBotCommand botCommand = _commandMessages.FirstOrDefault(c => c.ShouldExecute(e.CommandWord));

            if (botCommand == null)
            {
                return;
            }

            var cooldown = _usageTracker.GetActiveCooldown(e.ChatUser, botCommand);

            switch (cooldown)
            {
            case NoCooldown none:
                ProcessTheCommand(e, chatClient, botCommand);
                break;

            case UserCooldown userCooldown:
                chatClient.SendDirectMessage(e.ChatUser.DisplayName, userCooldown.Message);
                break;

            case UserCommandCooldown userCommandCooldown:
                chatClient.SendDirectMessage(e.ChatUser.DisplayName, userCommandCooldown.Message);
                break;

            case CommandCooldown commandCooldown:
                chatClient.SendMessage(commandCooldown.Message);
                break;
            }
        }
Exemplo n.º 3
0
        private CommandUsage AttemptToRunCommand(CommandReceivedEventArgs e,
                                                 IBotCommand botCommand, IChatClient chatClient1, IList <string> args)
        {
            try
            {
                _logger.LogInformation($"{e.ChatUser.DisplayName} is running the {botCommand.GetType().Name} command.");

                if (e.ChatUser.CanRunCommand(botCommand))
                {
                    if (args.Any())
                    {
                        e.Arguments.Clear();
                        foreach (string arg in args)
                        {
                            e.Arguments.Add(arg);
                        }
                    }
                    return(botCommand.Process(chatClient1, e));
                }

                chatClient1.SendMessage(
                    $"Sorry, {e.ChatUser.DisplayName}! You don't have permission to use the !{e.CommandWord} command.");
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Failed to run a command.");
            }

            return(new CommandUsage(e.ChatUser.DisplayName, DateTimeOffset.UtcNow, botCommand));
        }
Exemplo n.º 4
0
 private void CommandReceivedHandler(object sender, CommandReceivedEventArgs e)
 {
     if (sender is IChatClient chatClient)
     {
         ICommandMessage commandMessage = _commandMessages.FirstOrDefault(c => c.CommandText == e.CommandWord);
         commandMessage?.Process(chatClient, e);
     }
 }
Exemplo n.º 5
0
        private void DoTheThing(CommandReceivedEventArgs e, IChatClient chatClient, IBotCommand botCommand)
        {
            CommandUsage commandUsage       = AttemptToRunCommand(e, botCommand, chatClient);
            var          commandUsageEntity = new CommandUsageEntity(e.CommandWord, botCommand.GetType().FullName,
                                                                     e.ChatUser.UserId, e.ChatUser.DisplayName, chatClient.GetType().Name);

            _repository.Create(commandUsageEntity);
            _usageTracker.RecordUsage(commandUsage);
        }
Exemplo n.º 6
0
        public void CommandReceivedHandler(object sender, CommandReceivedEventArgs e)
        {
            if (!(sender is IChatClient chatClient))
            {
                return;
            }

            string userDisplayName = e.ChatUser.DisplayName;


            //List<CommandUsage> globalCooldownUsages = _usageTracker.GetUsagesByUserSubjectToGlobalCooldown(userDisplayName, DateTimeOffset.UtcNow);
            //if (globalCooldownUsages != null && globalCooldownUsages.Any() && !e.ChatUser.IsInThisRoleOrHigher(UserRole.Mod))
            //{
            //    if (!globalCooldownUsages.Any(x => x.WasUserWarned))
            //    {
            //        chatClient.SendMessage($"Whoa {userDisplayName}! Slow down there cowboy!");
            //        globalCooldownUsages.ForEach(x => x.WasUserWarned = true);
            //    }

            //    return;
            //}

            IBotCommand botCommand = _commandMessages.FirstOrDefault(c => c.ShouldExecute(e.CommandWord));

            if (botCommand == null)
            {
                return;
            }
            var cooldown = _usageTracker.GetActiveCooldown(e.ChatUser, botCommand);

            chatClient.SendMessage(cooldown.Message);
            // TODO: prevent running the command if there was a cooldown

            switch (cooldown)
            {
            case NoCooldown none:
                break;

            case UserCooldown userCooldown:
                chatClient.SendDirectMessage(e.ChatUser.DisplayName, userCooldown.Message);
                break;

            case UserCommandCooldown userCommandCooldown:
                chatClient.SendDirectMessage(e.ChatUser.DisplayName, userCommandCooldown.Message);
                break;

            case CommandCooldown commandCooldown:
                chatClient.SendMessage(commandCooldown.Message);
                break;

            default:
                break;
            }

            DoTheThing(e, chatClient, botCommand);
        }
Exemplo n.º 7
0
 private void CommandReceivedHandler(object sender, CommandReceivedEventArgs e)
 {
     if (sender is IChatClient chatClient)
     {
         IBotCommand botCommand = _commandMessages.FirstOrDefault(c => c.CommandText.ToLowerInvariant() == e.CommandWord.ToLowerInvariant());
         if (botCommand != null)
         {
             AttemptToRunCommand(e, botCommand, chatClient);
         }
     }
 }
Exemplo n.º 8
0
 private CommandUsage AttemptToRunCommand(CommandReceivedEventArgs e, IBotCommand botCommand, IChatClient chatClient1)
 {
     try
     {
         if (e.ChatUser.CanRunCommand(botCommand))
         {
             return(botCommand.Process(chatClient1, e));
         }
         chatClient1.SendMessage(
             $"Sorry, {e.ChatUser.DisplayName}! You don't have permission to use the !{e.CommandWord} command.");
     }
     catch (Exception exception)
     {
         Console.WriteLine(exception);
     }
     return(null);
 }
Exemplo n.º 9
0
 private void AttemptToRunCommand(CommandReceivedEventArgs e, IBotCommand botCommand, IChatClient chatClient1)
 {
     try
     {
         if (CanUserRunCommand(e.ChatUser, botCommand))
         {
             botCommand.Process(chatClient1, e);
         }
         else
         {
             chatClient1.SendMessage(
                 $"Sorry, {e.ChatUser.DisplayName}! You don't have permission to use the !{e.CommandWord} command.");
         }
     }
     catch (Exception exception)
     {
         Console.WriteLine(exception);
     }
 }
Exemplo n.º 10
0
        public void CommandReceivedHandler(object sender, CommandReceivedEventArgs e)
        {
            if (!(sender is IChatClient chatClient))
            {
                return;
            }

            IList <string> args       = new List <string>();
            IBotCommand    botCommand = _commandList.FirstOrDefault(c => c.ShouldExecute(e.CommandWord, out args));

            if (botCommand == null)
            {
                return;
            }

            // TODO:Remove this soon and replace with smarter code
            if (botCommand is RefreshCommandListCommand refreshCommand &&
                refreshCommand.NeedsInitializing)
            {
                refreshCommand.Initialize(_commandList);
            }

            var cooldown = _usageTracker.GetActiveCooldown(e.ChatUser, botCommand);

            switch (cooldown)
            {
            case NoCooldown none:
                ProcessTheCommand(e, chatClient, botCommand, args);
                break;

            case UserCooldown userCooldown:
                chatClient.SendDirectMessage(e.ChatUser.DisplayName, userCooldown.Message);
                break;

            case UserCommandCooldown userCommandCooldown:
                chatClient.SendDirectMessage(e.ChatUser.DisplayName, userCommandCooldown.Message);
                break;

            case CommandCooldown commandCooldown:
                chatClient.SendMessage(commandCooldown.Message);
                break;
            }
        }
Exemplo n.º 11
0
        private CommandUsage AttemptToRunCommand(CommandReceivedEventArgs e, IBotCommand botCommand,
                                                 IChatClient chatClient1)
        {
            try
            {
                _logger.LogInformation($"{e.ChatUser.DisplayName} is running the {botCommand.GetType().Name} command.");

                if (e.ChatUser.CanRunCommand(botCommand))
                {
                    return(botCommand.Process(chatClient1, e));
                }

                chatClient1.SendMessage(
                    $"Sorry, {e.ChatUser.DisplayName}! You don't have permission to use the !{e.CommandWord} command.");
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Failed to run a command.");
            }

            return(null);
        }
Exemplo n.º 12
0
        public void CommandReceivedHandler(object sender, CommandReceivedEventArgs e)
        {
            if (!(sender is IChatClient chatClient))
            {
                return;
            }

            IList <string> args       = new List <string>();
            IBotCommand    botCommand = _commandList.FindCommandByKeyword(e.CommandWord, out args);

            if (botCommand == null)
            {
                return;
            }

            var cooldown = _usageTracker.GetActiveCooldown(e.ChatUser, botCommand);

            switch (cooldown)
            {
            case NoCooldown none:
                ProcessTheCommand(e, chatClient, botCommand, args);
                break;

            case UserCooldown userCooldown:
                chatClient.SendDirectMessage(e.ChatUser.DisplayName, userCooldown.Message);
                break;

            case UserCommandCooldown userCommandCooldown:
                chatClient.SendDirectMessage(e.ChatUser.DisplayName, userCommandCooldown.Message);
                break;

            case CommandCooldown commandCooldown:
                chatClient.SendMessage(commandCooldown.Message);
                break;
            }
        }