PircBot API Java字符串拆分问题

时间:2011-05-03 23:42:04

标签: java api irc

我需要一些管理员和东西的数组帮助。我正在使用这个API来制作服务器机器人。您可以在那里找到所有类,构造函数和方法。

这是我目前的代码:

import org.jibble.pircbot.*;

public class MyBot extends PircBot {

    public MyBot() {
        this.setName("DevilBot");
    }

    String owner = "Evan";
    public void onMessage(String channel, String sender,
                       String login, String hostname, String message) {
        if (message.equalsIgnoreCase("!time")) {
            String time = new java.util.Date().toString();
            sendMessage(channel, sender + ": The time is now " + time);
        }
        if (message.equalsIgnoreCase("!owner")) {
            if(sender.equals(owner))
            {
                sendMessage(channel, Colors.NORMAL + "You're my owner silly!");
            }
            if (!sender.equals(owner))
            {
                sendMessage(channel, Colors.NORMAL + sender + ": " + owner + " is my owner!");
            }
        }
        if (message.equalsIgnoreCase("!ban")) {
            if(sender.equals(owner))
            {
                ban(channel, message);
                sendMessage(channel, "Banned " + message);
            }
            else
            {
                kick(channel, sender);
                sendMessage(channel, "You aren't my mother!");
            }
        }
        if (message.equalsIgnoreCase("!version")) {
            sendMessage(channel, "Version 0.1");
            sendMessage(channel, "PircBot API v1.5.0");
        }
        if (message.equalsIgnoreCase("!aelux")) {
            sendMessage(channel, "ALL HAIL AELUX!");
        }
        if (message.equalsIgnoreCase("!hates")){
            sendMessage(channel, message + ", " + sender + " hates you!");
        }
        if (message.equalsIgnoreCase("!op")){
            if(sender.equals(owner))
            {
                sendMessage(channel, "Opping " + message);
            }
            else
            {
                ban(channel, sender);
                kick(channel, sender);
                sendMessage(channel, "GTFO! Banned.");
            }
        }
    }
}

它编译并运行良好。它仍处于Alpha阶段,但由于某种原因,它不会读取我的命令。喜欢:     !踢用户 没有回应。

API易于理解。如果你可以帮助我,那就太棒了!

1 个答案:

答案 0 :(得分:0)

现在在您的代码中,您只使用equalsIgnoreCase 此方法要求两个字符串完全匹配案例。 所以“!禁用户”不起作用的原因是由于以下原因。

"!ban".equalsIgnoreCase("!bAn") == true 
"!ban".equalsIgnoreCase("!BaN") == true 

"!ban".equalsIgnoreCase("!ban username") == false
"!ban".equalsIgnoreCase("!ban ") == false 

以下代码将导致您通过键入“!ban username”来禁止用户。但是它会区分大小写。

     if (message.startsWith("!ban")) {
        if(sender.equals(owner))
        {
            String userToBan = message.split(" ")[1];
            ban(channel, userToBan);
            sendMessage(channel, "Banned " + userToBan);
        }
        else
        {
            kick(channel, sender);
            sendMessage(channel, "You aren't my mother!");
        }
    }

如果您不希望它只是在分隔符(通常是空格)上拆分传入命令,并转换第一个字符串并将它们与equalsIgnoreCase匹配。

String[] messageParts = string.split();
String command = messageParts[0];

if("!ban".equalsIgnoreCase(command){
   ban(channel,messageParts[1])
   if(messageParts[2].isEmpty()){
      sendMessage(channel, "Banned " + messageParts[1]);
   }else{
      sendMessage(channel, "Banned " + messageParts[1] + " Reason: " + messageParts[2]);
   }
}

希望这有帮助