Esempio n. 1
0
        public async Task MessageReceived(IMessageObject message)
        {
            foreach (Prefix p in _prefixes)
            {
                if (message.Content.StartsWith(p.Value))
                {
                    string command = message.Content
                                     .Substring(p.Value.Length)
                                     .Split(' ')[0]
                                     .ToLower();

                    if (cachedCommands.ContainsKey(command))
                    {
                        Command commandObject = cachedCommands[command];

                        if (OnPreCommandExecute != null)
                        {
                            if (!await OnPreCommandExecute.Invoke(commandObject))
                            {
                                if (OnPostCommandExecute != null)
                                {
                                    await OnPreCommandFailure.Invoke(commandObject);
                                }
                                return;
                            }
                        }

                        Stopwatch timeTaken = Stopwatch.StartNew();
                        try
                        {
                            await cachedCommands[command].ProcessCommand(message);
                            timeTaken.Stop();

                            if (OnPostCommandExecute != null)
                            {
                                await OnPostCommandExecute(commandObject, timeTaken.ElapsedMilliseconds, true);
                            }
                        }
                        catch (Exception e)
                        {
                            timeTaken.Stop();
                            if (OnPostCommandExecute != null)
                            {
                                await OnPostCommandExecute(commandObject, timeTaken.ElapsedMilliseconds, false);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Message receive event, handles commands
        /// </summary>
        /// <param name="message">IMessage object from any IBotProvider</param>
        public async Task MessageReceived(IMessage message)
        {
            try
            {
                if (message.Author.IsBot && config.IgnoreBots && !message.Author.IsSelf)
                {
                    return;
                }

                if (message.Author.IsSelf && config.IgnoreSelf)
                {
                    return;
                }

                foreach (Prefix p in prefixes)
                {
                    if (await p.MatchesAsync(message))
                    {
                        string prefix = await p.GetPrefixAsync(message);

                        string content = message.Content
                                         .Substring(prefix.Length)
                                         .TrimStart(' ');

                        string command = content
                                         .Split(' ')[0]
                                         .ToLowerInvariant();

                        string arguments = content
                                           .Substring(command.Length)
                                           .TrimStart(' ');

                        if (cachedCommands.ContainsKey(command))
                        {
                            Command commandObject = cachedCommands[command];

                            if (OnPreCommandExecute != null)
                            {
                                if (!await OnPreCommandExecute.Invoke(commandObject))
                                {
                                    if (OnPostCommandExecute != null)
                                    {
                                        await OnPreCommandFailure.Invoke(commandObject);
                                    }
                                    return;
                                }
                            }

                            Stopwatch timeTaken = Stopwatch.StartNew();
                            bool      success   = false;

                            CommandEventArgs args = new CommandEventArgs
                            {
                                Message    = message,
                                Arguments  = arguments,
                                Processor  = this,
                                PrefixUsed = p
                            };

                            try
                            {
                                if (await cachedCommands[command].CanBeUsedAsync(message))
                                {
                                    await cachedCommands[command].ProcessAsync(args);
                                    success = true;
                                    Log.PrintLine($"[{DateTime.Now.ToShortTimeString()}][cmd]: {message.Author.Name.PadRight(10)} called the command {command.PadRight(10)} in {timeTaken.ElapsedMilliseconds}ms");
                                }
                            }
                            catch (Exception e)
                            {
                                Log.PrintLine(e.Message + "\n" + e.StackTrace);
                            }
                            finally
                            {
                                timeTaken.Stop();
                                await OnPostCommandExecute?.Invoke(commandObject, timeTaken.ElapsedMilliseconds, success);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.PrintLine(e.Message + e.StackTrace);
            }
        }