PircBot API Java 字符串拆分问题

发布于 2024-11-05 02:14:09 字数 2213 浏览 0 评论 0原文

我需要一些关于管理员数组和其他东西的帮助。我正在使用这个 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 快速且易于理解。如果你能帮助我,那就太好了!

I need some help with arrays for admins and stuff. I'm using this API to make a server bot. You can find all the classes, constructors, and methods there.

Here's my current code:

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.");
            }
        }
    }
}

It compiles and runs fine. And it's still in Alpha Stages, but for some reason it won't read my command. Like:
!kick user
Yields no response.

The API is quick and easy to understand. If you CAN help me, that would be awesome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2024-11-12 02:14:09

现在在您的代码中您仅使用 equalsIgnoreCase
此方法要求两个字符串除了大小写外都完全匹配。
所以“!ban user”不起作用的原因如下。

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

但是

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

下面的这段代码将导致您能够通过输入“!禁止用户名”来禁止人们。但它会区分大小写。

     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]);
   }
}

希望这有帮助

Right now in your code you are only using equalsIgnoreCase
This method requires both strings to match perfectly apart from the case.
So the reason "!ban user" would not work is because of the following.

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

but

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

This following piece of code would result in you being able to ban people by typing "!ban username". However it will be case sensitive.

     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!");
        }
    }

If you don't want it to be just split the incoming command on a separator (usually space), and convert the first string and match them with 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]);
   }
}

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文