/// <summary>
 /// Sets the user acccess.
 /// </summary>
 /// <param name="userName">Name of the user.</param>
 /// <param name="privClass">The priv class.</param>
 private void SetUserAcccess(string userName, PrivClass privClass)
 {
     this.UserAccessLevels[userName] = privClass;
 }
        /// <summary>
        /// Load settings from config file.
        /// </summary>
        public void Load()
        {
            if (File.Exists(AccessFile))
            {
                // first, save the originals
                var privs = new List<PrivClass>(PrivClasses);
                var commands = new Dictionary<string, PrivClass>(CommandAccessLevels);
                var users = new Dictionary<string, PrivClass>(UserAccessLevels);

                try
                {
                    XDocument configDoc = XDocument.Load(AccessFile);
                    var root = configDoc.Root;

                    // add priv classes
                    this.PrivClasses.Clear();
                    foreach (XElement privClassNode in root.Elements("privClass"))
                    {
                        // create priv class
                        PrivClass p = new PrivClass(
                            Convert.ToInt32(privClassNode.Attribute("id").Value),
                            privClassNode.Attribute("name").ValueOrEmpty(),
                            Convert.ToInt32(privClassNode.Attribute("order").Value));
                        this.PrivClasses.Add(p);
                    }

                    // add command data
                    this.CommandAccessLevels.Clear();
                    foreach (XElement item in root.Element("commandData").Elements("add"))
                    {
                        string command = item.Attribute("key").Value;
                        int id = Convert.ToInt32(item.Attribute("value").Value);

                        var privClass = FindPrivClassById(id);
                        this.CommandAccessLevels.Add(command, privClass);
                    }

                    // add user data
                    this.UserAccessLevels.Clear();
                    foreach (XElement item in root.Element("userData").Elements("add"))
                    {
                        string user = item.Attribute("key").Value;
                        int id = Convert.ToInt32(item.Attribute("value").Value);

                        var privClass = FindPrivClassById(id);
                        this.UserAccessLevels.Add(user, privClass);
                    }
                }
                catch (Exception ex)
                {
                    Bot.Console.Warning("Unable to read access config file. See bot logs for details.");
                    Bot.Console.Log(ex.ToString());

                    // restore originals since we're not sure of our state
                    PrivClasses = privs;
                    CommandAccessLevels = commands;
                    UserAccessLevels = users;
                }
            }
            else
            {
                Bot.Console.Notice("No access file found. Using default priv classes.");
                this.PrivClasses.AddRange(new List<PrivClass>
                {
                    PrivClass.Owner,
                    PrivClass.Operators,
                    PrivClass.Moderators,
                    PrivClass.Members,
                    PrivClass.Guests,
                    PrivClass.Banned
                });
            }

            // ensure we at least have the guest priv class since it is mandatory
            if (!this.PrivClasses.Exists(p => p.Identifier == PrivClass.Guests.Identifier))
                this.PrivClasses.Add(PrivClass.Guests);
            // ensure we have the owner and username as a user with max privileges
            if (!this.UserAccessLevels.ContainsKey(Bot.Owner))
                SetUserAcccess(Bot.Owner, PrivClass.Owner);
            if (!this.UserAccessLevels.ContainsKey(Bot.Username))
                SetUserAcccess(Bot.Username, PrivClass.Owner);
        }
 /// <summary>
 /// Sets the command access.
 /// </summary>
 /// <param name="command">The command.</param>
 /// <param name="privClass">The priv class.</param>
 private void SetCommandAccess(string command, PrivClass privClass)
 {
     this.CommandAccessLevels[command] = privClass;
 }
Exemple #4
0
 /// <summary>
 /// This method lets you tie into a command that is received from a user. There are no
 /// preset commands. Any new commands are registered with this method. Only one method
 /// can be mapped for one command.
 /// </summary>
 /// <param name="commandName">Command name to register for.</param>
 /// <param name="commandMethod">Method that will be executed when command is encountered.</param>
 /// <param name="help">Help text for command.</param>
 /// <param name="privClass">The priv class.</param>
 protected void RegisterCommand(string commandName, BotCommandEvent commandMethod, CommandHelp help, PrivClass privClass)
 {
     RegisterCommand(commandName, commandMethod, help, privClass.Name);
 }