Exemplo n.º 1
0
        private void EditCom(string name, UserCommand command)
        {
            List<string> newLines = new List<string>();

            string[] lines = File.ReadAllLines(settings.CommandsFile);

            foreach (string line in lines) {
                string[] s = line.Split(new[] { ' ' }, 2);

                if (s[0] != name)
                    newLines.Add(line);
                else
                    newLines.Add(name + " " + command);
            }

            File.WriteAllLines(settings.CommandsFile, newLines);
        }
Exemplo n.º 2
0
        private void SetupCommands()
        {
            commands.Add("!addcom", new BuiltinCommand(2, Command.AccessLevel.Mod, (string username, string[] args) => {
                if (args.Length == 1) {
                    Say(username + " -> No command specified.");
                    return 1;
                }

                if (!args[1].StartsWith("!")) {
                    Say(username + " -> A command must start with !.");
                    return 1;
                }

                if (args.Length == 2) {
                    Say(username + " -> A command cannot be empty.");
                    return 1;
                }

                if (commands.ContainsKey(args[1])) {
                    Say(username + " -> Command " + args[1] + " already exists.");
                    return 1;
                }

                Command.AccessLevel level = Command.AccessLevel.Regular;

                if (args[2].StartsWith("#")) {
                    string[] s = args[2].Substring(1).Split(new[] { ' ' }, 2);

                    if (s.Length == 1) {
                        Say(username + " -> A command cannot be empty.");
                        return 1;
                    }

                    if (s[0] == "regular")
                        level = Command.AccessLevel.Regular;
                    else if (s[0] == "mod")
                        level = Command.AccessLevel.Mod;
                    else if (s[0] == "owner")
                        level = Command.AccessLevel.Owner;
                    else {
                        Say(username + " -> Invalid access level #" + s[0] + ".");
                        return 1;
                    }

                    args[2] = s[1];
                }

                if (level == Command.AccessLevel.Owner && username != Channel) {
                    Say(username + " -> Mods cannot add #owner commands.");
                    return 1;
                }

                UserCommand command = new UserCommand(level, args[2]);

                commands.Add(args[1], command);

                AddCom(args[1], command);

                Say(username + " -> Command " + args[1] + " added.");
                return 0;
            }));

            commands.Add("!editcom", new BuiltinCommand(2, Command.AccessLevel.Mod, (string username, string[] args) => {
                if (args.Length == 1) {
                    Say(username + " -> No command specified.");
                    return 1;
                }

                if (!args[1].StartsWith("!")) {
                    Say(username + " -> A command must start with !.");
                    return 1;
                }

                if (args.Length == 2) {
                    Say(username + " -> A command cannot be empty.");
                    return 1;
                }

                Command.AccessLevel level = Command.AccessLevel.Regular;

                if (args[2].StartsWith("#")) {
                    string[] s = args[2].Substring(1).Split(new[] { ' ' }, 2);

                    if (s.Length == 1) {
                        Say(username + " -> A command cannot be empty.");
                        return 1;
                    }

                    if (s[0] == "regular")
                        level = Command.AccessLevel.Regular;
                    else if (s[0] == "mod")
                        level = Command.AccessLevel.Mod;
                    else if (s[0] == "owner")
                        level = Command.AccessLevel.Owner;
                    else {
                        Say(username + " -> Invalid access level #" + s[0] + ".");
                        return 1;
                    }

                    args[2] = s[1];
                }

                if (level == Command.AccessLevel.Owner && username != Channel) {
                    Say(username + " -> Mods cannot add #owner commands.");
                    return 1;
                }

                if (!commands.ContainsKey(args[1])) {
                    Say(username + " -> Command " + args[1] + " doesn't exist.");
                    return 1;
                }

                Command command;
                commands.TryGetValue(args[1], out command);

                if (command is BuiltinCommand) {
                    Say(username + " -> Cannot edit built-in commands.");
                    return 1;
                }

                if (command.Level == Command.AccessLevel.Owner && username != Channel) {
                    Say(username + " -> Mods cannot edit #owner commands.");
                    return 1;
                }

                commands.Remove(args[1]);

                UserCommand newCommand = new UserCommand(level, args[2]);

                commands.Add(args[1], newCommand);

                EditCom(args[1], newCommand);

                Say(username + " -> Command " + args[1] + " edited.");
                return 0;
            }));

            commands.Add("!delcom", new BuiltinCommand(1, Command.AccessLevel.Mod, (string username, string[] args) => {
                if (args.Length == 1) {
                    Say(username + " -> No command specified.");
                    return 1;
                }

                if (!commands.ContainsKey(args[1])) {
                    Say(username + " -> Command " + args[1] + " doesn't exist.");
                    return 1;
                }

                Command command;
                commands.TryGetValue(args[1], out command);

                if (command is BuiltinCommand) {
                    Say(username + " -> Cannot delete built-in commands.");
                    return 1;
                }

                if (command.Level == Command.AccessLevel.Owner && username != Channel) {
                    Say(username + " -> Mods cannot delete #owner commands.");
                    return 1;
                }

                commands.Remove(args[1]);

                DelCom(args[1], (UserCommand)command);

                Say(username + " -> Command " + args[1] + " deleted.");
                return 0;
            }));

            commands.Add("!quote", new BuiltinCommand(0, Command.AccessLevel.Regular, (string username, string[] args) => {
                Say(ReadRandomLine(settings.QuotesFile));
                return 0;
            }));

            commands.Add("!addquote", new BuiltinCommand(1, Command.AccessLevel.Mod, (string username, string[] args) => {
                if (args.Length == 1) {
                    Say(username + " -> A quote cannot be empty.");
                    return 1;
                }

                File.AppendAllText(settings.QuotesFile, Environment.NewLine + args[1]);

                Say(username + " -> Quote added.");
                return 0;
            }));

            commands.Add("!startraffle", new BuiltinCommand(0, Command.AccessLevel.Owner, (string username, string[] args) => {
                raffle = true;

                entrants.Clear();

                Say("The raffle has started! Type \"raffle\" to enter.");
                return 0;
            }));

            commands.Add("!closeraffle", new BuiltinCommand(0, Command.AccessLevel.Owner, (string username, string[] args) => {
                if (raffle) {
                    raffle = false;
                    Say("The raffle is closed.");
                }

                return 0;
            }));

            commands.Add("!rafflewinner", new BuiltinCommand(0, Command.AccessLevel.Owner, (string username, string[] args) => {
                if (entrants.Count == 0)
                    Say("No one entered.");
                else {
                    Say("Entrants: " + ListEntrants() + ".");
                    Say("The winner is " + entrants[random.Next() % entrants.Count] + "!");
                }

                raffle = false;

                return 0;
            }));

            commands.Add("!entrants", new BuiltinCommand(0, Command.AccessLevel.Regular, (string username, string[] args) => {
                if (!raffle)
                    return 1;

                if (entrants.Count == 0) {
                    Say("No one entered.");
                    return 0;
                }

                Say("Raffle ongoing, type \"raffle\" to enter. Entrants: " + ListEntrants() + ".");

                return 0;
            }));

            commands.Add("!raid", new BuiltinCommand(1, Command.AccessLevel.Mod, (string username, string[] args) => {
                if (args.Length == 1)
                    return 1;

                Say("Thank you so much for the raid! Please go check out their channel at http://twitch.tv/" + args[1]);
                return 0;
            }));
        }
Exemplo n.º 3
0
 private void AddCom(string name, UserCommand command)
 {
     File.AppendAllText(settings.CommandsFile, Environment.NewLine + name + " " + command);
 }