예제 #1
0
        void RegisterCommand(ICommand cmd)
        {
            string cmdName = cmd.Command.ToLower();

            if (_commands.ContainsKey(cmdName))
            {
                DClient.WriteLine($"Discord: Command {cmdName} already exists, skipping...");
                return;
            }

            for (int i = 0; i < cmdName.Length; i++)
            {
                if (cmdName[i] == ' ')
                {
                    DClient.WriteLine($"Discord: Command {cmdName} contains illegal character \" \", skipping...");
                    return;
                }
                else if (!char.IsLetterOrDigit(cmdName[i]))
                {
                    DClient.WriteLine($"Discord: Command {cmdName} contains illegal character \"{cmdName[i]}\", skipping...");
                    return;
                }
            }

            _commands.Add(cmdName, cmd);
            DClient.WriteLine($"Discord: Registered command {cmdName}");
        }
예제 #2
0
        public void InitCommands()
        {
            if (_commands.Count > 0)
            {
                _commands.Clear();
            }

            Assembly ass = typeof(CommandHandler).Assembly;

            Type[] types = ass.GetTypes();

            for (int i = 0; i < types.Length; i++)
            {
                if (types[i].GetCustomAttribute <CommandAttribute>() == null)
                {
                    continue;
                }

                ICommand cmd;
                try
                {
                    cmd = Activator.CreateInstance(types[i]) as ICommand;
                }
                catch (Exception ex)
                {
                    DClient.Error("Command Initialization", ex.ToString());
                    continue;
                }

                if (cmd == null)
                {
                    continue;
                }

                RegisterCommand(cmd);
            }

            DClient.WriteLine($"Registered a total of {_commands.Count} commands");
        }