Ejemplo n.º 1
0
            /// <summary>
            /// When a message is created on a server the bot is part of, the bot identifies commands
            /// intended for it by using the indicator character. It then goes through each of its known
            /// commands and attempts to execute them.
            /// The first command to succeed is the only one that runs, which is why sorting the command
            /// list in order of specificity is important.
            /// </summary>
            /// <param name="client">The discord client sending the message.</param>
            /// <param name="args">An object describing the context of the message sent.</param>
            /// <returns>A task to handle processing of the command.</returns>
            private async Task OnMessageCreated(DiscordClient client, MessageCreateEventArgs args)
            {
                // Only work with guild messages, not private ones.
                if (args.Guild != null)
                {
                    TConfig config = GetConfig(args.Guild.Id);

                    // Check to see if the post starts with our indicator and the author wasn't a bot.
                    if (!args.Author.IsBot && args.Message.Content.StartsWith(config.Indicator))
                    {
                        // Get the command level so we can filter out commands this user can't access.
                        CommandLevel level = await GetCommandLevelAsync(args.Guild, args.Author.Id);

                        foreach (Command <TConfig> c in Commands)
                        {
                            // Only if the command is lower or equal to the user level do we attempt to run it.
                            if (c.CommandLevel <= level)
                            {
                                // If the regex matches and the command succeeds, we can skip the rest.
                                CommandState state = await c.AttemptAsync(args, config, Instance);

                                if (state == CommandState.Handled)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        /// <summary>
        /// 解析权限数据
        /// </summary>
        /// <param name="lines"></param>
        /// <returns></returns>
        public static List <CommandLevel> ParsePermissions(List <string> lines)
        {
            List <CommandLevel> result = new List <CommandLevel>();

            CommandLevel commandLevel = null;

            int outEnd = 0;

            foreach (var item in lines)
            {
                commandLevel = new CommandLevel
                {
                    PermissionLevel = item.GetMidStr("   ", ": ", out outEnd).TrimStart(' ').ToInt(),
                    Command         = item.GetMidStr(": ", Environment.NewLine, outEnd)
                };

                result.Add(commandLevel);
            }
            return(result);
        }