1

I am making an IRC Chat bot for my stream. I found a few basic connectivity examples using C# so I decided to give it a try.

So far I love it

But i am stuck on this one part.

I want to store the bot commands inside an array of a structure type.

public delegate void cmdHandler(string[]);

struct botCommand
{
    string name;
    cmdHandler chandler;
    bool isAdmin = false;
    string help = "Nothing here.";
}

Is currently what I have, and then I want to beable to do this:

botCommand[]commands =
{
    { "TestCommand", testCommand(), 0, "Help for this" },
    { "TestCommand2", testCommand2(), 0 "..." },
    ......
};

So how do I link a generic function in that array?

or am I going about this all the wrong way?

Basically instead of having a giant Switch() statement to check for which command was used I want to loop through an array and see if the command is in there. If it is then call the function associated with that command.

EDIT:

This is exactly what I have now so you can see what I am trying to do

    public delegate void cmdHandler(string[] ex);

    struct botCommand
    {
        string name;
        cmdHandler chandler;
        bool isAdmin = false;
        string help = "Nothing here.";
    }

    botCommand[] commands =
    {
        {"test",  new cmdHandler(testf), 0, ""  }
    };

    public void testf(string[] ex) {
        return;
    }

Steps of logic:

  1. user enters the test command
  2. Loop through all botCommands to see if we find the test command
  3. Test command is found
  4. Call the function associated with the test command and pass on an argument (the rest of the command)
7
  • You don't need the parenthesis (), unless that function returns a delegate, but I doubt you're doing that. Commented Jul 21, 2013 at 22:59
  • oh, I've tried it every which way from Sunday. I need to know how to link that function from inside the array declaration. I need the syntax! I have searched google for hours. But I am new to C# Commented Jul 21, 2013 at 23:01
  • testCommand and testCommand2 is a function that accepts a string array? Commented Jul 21, 2013 at 23:02
  • No. I want a static array that has a list of commands and then pointers to functions for those commands. So if someone uses the command "TestCommand" I simply call the "chandler" delegate from the structure. Does that make sense? Commented Jul 21, 2013 at 23:04
  • What is your testf defined as? Commented Jul 21, 2013 at 23:08

1 Answer 1

1

To me it seems like you're mixing C/C++ concepts with C# (using struct instead of class, 0 for false, object initializers, etc...).

To solve your individual problem, you must instantiate your struct differently.

botCommand[] commands = new []
{
    new botCommand {
        name = "Test",
        chandler = new cmdHandler(MyMethod),
        isAdmin = false,
        help = "No help for you..."
    }
};

where MyMethod is defined as.

public static void MyMethod(string[] myArgs)
{
    //... do something ...
}

However, I think a better approach would be to have an abstract class / interface for an individual command, list or dictionary of your commands.

public interface IBotCommand
{
    string Name { get; }
    bool IsAdmin { get; }
    void Process(string[] args);
    string HelpText { get; }
}

public class MyCommand : IBotCommand
{
    string Name 
    {
        get
        {
            return "NameOfTheCommand";
        }
    }

    bool IsAdmin 
    { 
        get 
        { 
            return false; 
        } 
    }

    void Process(string[] args)
    {
        // bla bla, im processing stuff
    }

    string HelpText 
    { 
        get 
        { 
            return "This is the help text"; 
        } 
    }
}

And then using it.

List<IBotCommand> commands = new List<IBotCommand>();

commands.Add(new MyCommand());

// to find a command and execute it
IBotCommand cmdToExecute = commands.SingleOrDefault(c => c.Name == "NameOfTheCommand");

if (cmdToExecute != null)
{
    cmdToExecute.Process(args); // where-ever args comes from
}
else 
{
    // unknown command "NameOfTheCommand"
}
Sign up to request clarification or add additional context in comments.

4 Comments

Ugh, I can't give you an upvote but this is the solution! Thank you so much!
Ok that is great! I have one other question.When you add a new command to the stack with commands.Add(new MyCommand()) how are you setting the values in that new command? More specifically how are you referencing a function to call? Thank you so much for your time!
oh wait, so with this example do I have a new class for every function and I am simply adding each class (that inherits from the public interface ibotcommand to the stack?
Each type of command would be a new class that inherits from IBotCommand. Each command has a Process method which accepts a list of arguments. Since each commands interface is the same, you don't have to write special code to call each command, the command itself takes care of that concern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.