Esempio n. 1
0
        public async Task <bool> CommandHandler(PrivateMessageEventArgs e)
        {
            if (e.PrivateMessage.User.Hostmask == IrcSelf.Hostmask)
            {
                return(false);
            }

            string message      = e.PrivateMessage.Message;
            string messageLower = message.ToLower();

            string[]     args = message.SplitMessage();
            IRespondable respondTo;

            if (e.PrivateMessage.IsChannelMessage)
            {
                respondTo = (LinkedIrcChannel)e.PrivateMessage.Channel;
            }
            else
            {
                respondTo = (LinkedIrcUser)e.PrivateMessage.User;
            }

            string first           = args[0].ToLower();
            bool   commandByName   = first.StartsWith(IrcSelf.Nick.ToLower());
            bool   commandByPrefix = messageLower.StartsWithAny(Config.CommandPrefixes);

            if (commandByName || commandByPrefix)
            {
                string command = null;
                int    offset  = 1;
                if (commandByName)
                {
                    if (args.Length < 2)
                    {
                        return(false);
                    }

                    command = args[1];
                    offset  = 2;
                }

                if (commandByPrefix)
                {
                    foreach (string prefix in Config.CommandPrefixes)
                    {
                        if (messageLower.StartsWith(prefix))
                        {
                            if (prefix.EndsWith(" "))
                            {
                                if (args.Length < 2)
                                {
                                    return(false);
                                }

                                command = args[1];
                                offset  = 2;
                                break;
                            }

                            command = args[0].Substring(prefix.Length);
                            break;
                        }
                    }
                }

                if (command == null)
                {
                    return(false);
                }

                if (Program.CommandList.ContainsCommand(command))
                {
                    LinkedIrcMessage linkedMessage = e;
                    if (!LilGUtil.CheckPermission(command, linkedMessage))
                    {
                        await respondTo.respond($"Sorry, you don't have the permission to run {command}");

                        return(true);
                    }

                    ICommand icommand             = Program.CommandList[command];
                    ArraySegment <string> segment = new ArraySegment <string>(args, offset, args.Length - offset);
                    try{ await icommand.HandleCommand(this, respondTo, segment, (LinkedIrcMessage)e); } catch (Exception ex) {
                        Logger.Error(ex, "Problem processing command: \n{0}", ex.StackTrace);
                        await respondTo.respond($"Sorry there was a problem processing the command: {ex.Message}");

                        return(true);
                    }

                    return(true);
                }
            }

            return(false);
        }
Esempio n. 2
0
        public async Task <bool> CommandHandler(MessageCreateEventArgs e)
        {
            if (e.Author == e.Client.CurrentUser)
            {
                return(false);
            }

            string message = e.Message.Content;

            string[]     args = message.SplitMessage();
            IRespondable respondTo;

            if (e.Channel != null)
            {
                respondTo = (LinkedDiscordChannel)e.Channel;
            }
            else
            {
                respondTo = (LinkedDiscordUser)e.Author;
            }

            bool commandByName = args[0].StartsWith(e.Client.CurrentUser.Mention) ||
                                 args[0].StartsWith(e.Guild.CurrentMember.DisplayName) ||
                                 args[0].StartsWith(e.Client.CurrentUser.Username);
            bool commandByPrefix = message.StartsWithAny(Config.CommandPrefixes);

            if (commandByName || commandByPrefix)
            {
                string command = null;
                int    offset  = 1;
                if (commandByName)
                {
                    if (args.Length < 2)
                    {
                        return(false);
                    }

                    command = args[1];
                    offset  = 2;
                }

                if (commandByPrefix)
                {
                    foreach (string prefix in Config.CommandPrefixes)
                    {
                        if (message.StartsWith(prefix))
                        {
                            if (prefix.EndsWith(" "))
                            {
                                if (args.Length < 2)
                                {
                                    return(false);
                                }

                                command = args[1];
                                offset  = 2;
                                break;
                            }

                            command = args[0].Substring(prefix.Length);
                            break;
                        }
                    }
                }

                if (command == null)
                {
                    return(false);
                }

                if (Program.CommandList.ContainsCommand(command))
                {
                    LinkedDiscordMessage linkedMessage = e;
                    if (!LilGUtil.CheckPermission(command, linkedMessage))
                    {
                        await respondTo.respond($"Sorry, you don't have the permission to run {command}");

                        return(true);
                    }

                    ArraySegment <string> segment = new ArraySegment <string>(args, offset, args.Length - offset);
                    try{
                        await Program.CommandList[command].HandleCommand(this, respondTo, segment, (LinkedDiscordMessage)e);
                    }
                    catch (Exception ex) {
                        Logger.Error(ex, "Problem processing command: \n{0}", ex);
                        await respondTo.respond($"Sorry there was a problem processing the command: {ex.Message}");

                        return(true);
                    }

                    return(true);
                }
            }

            return(false);
        }