Brandon Palmer
Brandon Palmer

Reputation: 313

IRC Chat Command Parsing

FIXED I'm putting the code here for anyone else who needs help with their own problem (assuming they had the problem I had.

FIXED CODE THAT WORKS

    public static bool CommandExists(String value)
    {
        string[] commands = File.ReadAllText("commands.txt")
                       .Split()
                       .Where(x => x.StartsWith(value))
                       .Distinct()
                       .ToArray();
        return commands.Contains(value);
    }
    public static string CommandParse(String value, string nick)
    {
        IEnumerable<string> lines;
        lines = File.ReadLines("commands.txt");
        IEnumerable<string> command = lines
            .Where(d => d.StartsWith(value, 
                StringComparison.CurrentCultureIgnoreCase));
        foreach (string line in command) {
            string vals = line
                .Replace("@nick", nick)
                .Replace("@upnick", nick.ToUpper())
                .Replace(value + " ", "");
            return vals;
        }
        return null;
    }

So I've been trying for a few hours and I looked around and I can't find anything related to what I'm trying to do.

I have a text file I'm reading called "commands.txt" and I'm trying to parse the text. Here's the contents:

!help Hello, current commands are: !help, !ver
!ver Testing this

Now if I pull

string x1 = File.ReadAllLines("commands.txt").ToString();
string[] x2 = x1.Split(' ');
string x3 = x2[0];
Console.WriteLine(x3);

I get 'Index outside the bounds of the array'. I have no idea what I'm doing wrong. I'm also trying to use a 'static bool' to call if the command exists and so far I got

public static bool CommandExists(String value)
{
    if (File.ReadAllLines("commands.txt").Contains(value.ToString())) {
        return true;
    }
    else
    { 
        return false; 
    }
}

and that isn't working as well.

What is causing that exception?

EDIT: CommandParse()

    public static string CommandParse(string value, string nick)
    {
        string[] commands = File.ReadAllText("commands.txt")
               .Split()
               .Where(x => x.StartsWith("!"+value.ToLower()))
               .Distinct()
               .ToArray();
        string cmd = commands[1]
            .Replace("@nick", nick)
            .Replace("@nickup", nick.ToUpper());
        return cmd;
    }

Now I know that returns True, how do I get it not to return true, but return the command itself

Upvotes: 1

Views: 1032

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

ReadAllLines returns an array of strings,you are using ToString and instead of getting lines, you get the type name of the string array which doesn't contain any while-space so Split(' ') doesn't change anything.If you want to read all text, use ReadAllText method.

string x1 = File.ReadAllText("commands.txt");

It seems all your command begins with ! so you can get all commands into an array like this:

string[] commands = File.ReadAllText("commands.txt")
                   .Split()
                   .Where(x => x.StartsWith("!"))
                   .Distinct()
                   .ToArray();

Then your method will look like this:

public static bool CommandExists(String value)
{
    string[] commands = File.ReadAllText("commands.txt")
                   .Split()
                   .Where(x => x.StartsWith("!"))
                   .Distinct()
                   .ToArray();

    return commands.Contains(value);
}

If you want to exclude the ! at the beginning add .Select(x => x.TrimStart('!')) after Where.

Upvotes: 4

Related Questions