lmariscal / twitchirc

Twitch Bot Development made Easier | DEPRECATED
Other
40 stars 15 forks source link

Command returns. #17

Closed dustinduse closed 7 years ago

dustinduse commented 7 years ago

Ive tested commands, I can get "!bonus" to fire as command event. but "!bonus name" will not. couldnt figure it out.

Squidkingdom commented 7 years ago

What is the code for your full onCommmand() function?

dustinduse commented 7 years ago

String str[]; str = line.split("!"); final User msg_user = User.getUser(str[0].substring(1, str[0].length())); str = line.split(" "); Channel msg_channel; msg_channel = Channel.getChannel(str[2], this); String msg_msg = line.substring((str[0].length() + str[1].length() + str[2].length() + 4), line.length()); System.out.println("> " + msg_channel + " | " + msg_user + " >> " + msg_msg); String[] commandargs = msg_msg.split(" "); if (msg_msg.startsWith(commandTrigger)) onCommand(msg_user, msg_channel, commandargs[0], commandargs);

I found the previous way of doing it to not work as well.. This was my fix to my question earlier.

Squidkingdom commented 7 years ago

Not that :P I mean like the override portion Just use the modifier message.regionMatches(IgnoreCase, offset, other, otheroffset, read); Also go here https://github.com/cavariux/TwitchIRC/issues/16

mvarendorff commented 7 years ago

For me the String that is passed as command contains everything after the !. You should be able to split that String by spaces and treat each element of the resulting array as one argument with [0] beeing the commandname. You are probably having something like this: if (command.equals("bonus")) which will return false for command = "bonus name". Something that would eb working with the above would be something like this:

String[] commandSplit = command.split(" ")[0];
String commandName = commandSplit[0];
if (commandName.equalsIgnoreCase("bonus")) {
  //General stuff for bonus
  if (commandSplit.length > 1) {
    //Here do stuff with commandSplit[1] which would be "name"
  }
}
Squidkingdom commented 7 years ago

@geisterfurz007 I respectfully disagree, there is no need to go into arrays we can just use regionMatches() like this:

       onCommand(User user, Channel channel, String message ){
               If(message.regionMatches(false, 0, bonus, 0, 5) == true){
                String name = message.substring(6);
        //name now stores anything added after the initial bonus in the command

      }
      }

My code may be finicky (I'm on mobile) but the idea is there.

mvarendorff commented 7 years ago

I took the freedom to edit that ;) Well true, but I there is you are working with even more arguments after that, this would not work with substring. In addition to that one would have to do that for each command. When reacting to more than one command, it comes in handy to have it in one variable and then compare it using switch/case :) However I agree that for only a few commands you way works well as well.

lmariscal commented 7 years ago

First of all I can't see the names but the guy who submitted this issue you are using split in ! when in onCommand the command is passes without the ! secondly you would whant to split in every space to split arguments