Пример #1
0
        public void SetCommand(MessageEventArgs messageEventArgs, int access)
        {
            if (messageEventArgs.Message.Type != MessageType.Text)
            {
                return;
            }
            var methods = GetMethod <CommandAttribute>();

            foreach (var method in methods) // iterate through all found methods
            {
                var attributes = method.GetCustomAttributes(false);

                var attributesAll = attributes.Select(CheckTypeCommand).ToList();
                attributesAll.RemoveAll(x => x == null);

                var attribute = attributesAll.FirstOrDefault();
                if (attribute == null)
                {
                    return;
                }

                var botCommand = new BotCommand();
                botCommand.Parse(messageEventArgs.Message.Text);

                if (attribute.Access > access)
                {
                    continue;
                }
                if (!attribute.Commands.All(x => String.Equals(x, botCommand.AllText, StringComparison.CurrentCultureIgnoreCase) ||
                                            String.Equals(x, botCommand.Command, StringComparison.CurrentCultureIgnoreCase) ||
                                            String.Equals(x, botCommand.NoCommand, StringComparison.CurrentCultureIgnoreCase)))
                {
                    continue;
                }

                Log.Information(string.Join("", new List <string>()
                {
                    $"Message Id: {messageEventArgs.Message.MessageId}, ",
                    $"Text: {messageEventArgs.Message.Text}, ",
                    $"Chat: {messageEventArgs.Message.Chat.Id}, ",
                    $"From: {messageEventArgs.Message.From}, ",
                    $"Type: {messageEventArgs.Message.Type}, ",
                    $"Date: {messageEventArgs.Message.Date}, ",
                }));

                var obj = Activator.CreateInstance(method.DeclaringType); // Instantiate the class
                method.Invoke(obj, parameters: new object[]
                {
                    new TextParams(botCommand, message: messageEventArgs.Message),
                }
                              ); // invoke the method

                break;
            }
        }
Пример #2
0
 public TextParams(BotCommand command, Message message = null)
 {
     this.Command = command;
     this.Message = message;
 }