PircBot在twitch聊天中不发送消息

时间:2014-05-19 15:15:33

标签: java irc twitch

您好我正在为我的抽搐流做一个聊天机器人但是我遇到了一个问题只有一个命令正在运行,这是!song命令所有其他命令因某些原因无法工作我检查了我的代码和我找不到任何错误

所以如果有人发现我的代码请求有什么问题请告诉我

import org.jibble.pircbot.*;

import java.io.*;
import java.lang.System.*;

public class HyperBotZ extends PircBot {

// Get song title from Txt file AND return it!
public String getSong() throws Exception {
    FileReader file = new FileReader ("H:/Stream_Soft/Snip/Snip.txt");
    BufferedReader reader = new BufferedReader(file);

    String song = "";
    String line = reader.readLine();
    while (line != null){
        song += line;
        line = reader.readLine();
    }

    return song;
}

// Write info to txt file

// IRC Commands_
public HyperBotZ() {
    this.setName("HyperBotZ");
}

public static String ip = "";
public static String dual = "";

public void onMessage(String channel, String sender,
                    String login, String hostname, String message) {

    String owner = "hypergainz";

    if (message.startsWith("!songrequest")) {
        sendMessage(channel, "Sorry i cant do that atm HyperGainZ need to learn me that, to see the current song use !song :D");
    }

    if (message.startsWith("!ip ")) {
        if(sender.equals(owner))
        {
            ip = message.split(" ")[1];
            sendMessage(channel, " the ip is set to " + ip);
        } 
    } else {
        if (message.equalsIgnoreCase("!ip")){
            sendMessage(channel, "HyperGainZ is currently playing on : " + ip );
        }
    }

    if (message.startsWith("!dual ")) {
        if(sender.equals(owner))
        {
            dual = message.split(" ")[1];
        } 
    } else {
        if (message.equalsIgnoreCase("!dual")){
            sendMessage(channel, "http://multitwitch.tv/hypergainz/" + dual );
        }
    }

    if (message.equalsIgnoreCase("!song")){
        String song = "";
        try {
            song = getSong();
        } catch (Exception e) { e.printStackTrace(); }
        sendMessage(channel, song);
    }
}

}

1 个答案:

答案 0 :(得分:4)

首先,您应该移动

String owner = "hypergainz";

进入类变量,因为每次收到消息时都不需要设置它。

接下来,一个好主意可能是将消息分解为String数组,以便您可以从(可能的)参数中断命令。你可以用:

来做到这一点
String[] messageArray = message.split(" ");

完成后,您可以更轻松地比较邮件的第一部分。

如果您打算使用一些命令(最多约10个),我建议您使用开关尝试保持整洁。例如:

public void onMessage(String channel, String sender,
                String login, String hostname, String message) {

    String[] messageArray = message.split(" ");
    String command = messageArray[0];

    switch(command){

    default:break;

    case: "!songrequest":
        sendMessage(channel, "Sorry i cant do that atm HyperGainZ need to learn me that, to see the current song use !song :D");
        return;

    case "!song":
        String song = "";
        try {
            song = getSong();
        } catch (Exception e) { e.printStackTrace(); }
        sendMessage(channel, song);
        return;

    }

但是,如果您打算制作一个(大)实用工具机器人,那么创建自己的分发方法可能是一个好主意,因为onMessage(...)方法很快就会填满。