【问题标题】:PircBot Not sending Message in twitch chatPircBot 未在 twitch 聊天中发送消息
【发布时间】:2014-05-19 15:15:33
【问题描述】:

您好,我目前正在为我的 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);
    }
}

}

【问题讨论】:

    标签: java irc twitch


    【解决方案1】:

    首先,你应该移动你的

    String owner = "hypergainz";
    

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

    接下来,一个好主意可能是将消息分解为字符串数组,以便您可以将命令与(可能的)参数分解。你可以这样做:

    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(...) 方法很快就会被填满。

    【讨论】:

    • ok id 照你说的做了,但唯一有效的命令是“!song”,这是我的代码:pastebin.com/vxzrRK2P
    • 我刚刚测试了您的代码,它按预期工作...如果您想要的不仅仅是 !song 和 !songrequest 命令,则需要向开关添加命令。你期待什么输出?
    • 它可以在普通的 irc 服务器上运行,但不能在 twitch 服务器上运行
    • 您是否收到从服务器返回的任何错误消息?如果您将 PircBot 设置为详细模式,您应该会在终端中看到打印的所有交互
    • 嗯,不确定。你能用工作消息和非工作消息的终端输出更新你原来的问题吗?
    猜你喜欢
    • 2015-05-26
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 2018-08-20
    • 2021-04-14
    • 2016-04-13
    • 2014-07-26
    • 2019-08-24
    相关资源
    最近更新 更多