【问题标题】:Java Twitch IRC BotJava Twitch IRC 机器人
【发布时间】:2018-12-31 06:29:20
【问题描述】:

所以我正在为我的频道开发一个基本的 Twitch Bot,代码如下:

Config.java

import java.io.IOException;

import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.NickAlreadyInUseException;

public class Config {

private static final String OAUTH = "MYOAUTHHERE";
private static final String ADRESS = "irc.chat.twitch.tv.";
private static final int PORT = 6667;
private static final String channelName = "#MYCHANNELNAMEHERE";

public static void main(String[] args) throws NickAlreadyInUseException, IOException, IrcException {

    TwitchBot bot = new TwitchBot();

    bot.setVerbose(true);

    bot.connect(ADRESS, PORT, OAUTH);
    // bot.onMessage(channelName, "Bot", channelName, channelName, channelName);
    System.out.println("Connected!");
    bot.joinChannel(channelName);
    System.out.println("Successfully joined channel!");

    bot.sendMessage(channelName, "Hello, I am a bot");

    }
}

TwitchBot.java

import org.jibble.pircbot.*;

public class TwitchBot extends PircBot {

private static final String channelName = "#MYCHANNELNAME";
private final String botName = "THEBOTNAME";

public TwitchBot() {
    this.setName(botName);
    this.setLogin(botName);

}

public String getchannelName() {
    return channelName;
}

@Override
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);
        }
    }

}

控制台显示“已连接!”和“成功加入频道”,但是机器人没有响应,并且不在我指定的频道中。它也不会在聊天中打印“Hello I am a bot”。

【问题讨论】:

  • 这不是一个真正的答案,但您可能想查看 pircbotx,您会发现 API 更适合与它一起使用
  • 我假设你的程序在连接并加入频道后会退出,因为在那之后没有任何东西让它继续运行
  • Sami 不是这样,pircbot 认为运行可以保持程序运行
  • 对我来说很好用.. 可能是登录问题,您应该发布完整的输出结果以获得进一步的帮助。
  • 这里是完整的输出 dumptext.com/VQxnNT8a

标签: java bots irc twitch


【解决方案1】:

关于 Twitch 需要考虑的事情很少。

  1. 您的电子邮件必须经过验证。设置 -> 个人资料 -> 个人资料设置
  2. 频道名称必须以小写形式输入。
  3. 昵称没用,twitch 正在使用您的个人资料昵称。
  4. Twitch 使用 IRCv3 客户端能力协商 又名 CAP,这意味着您也应该使用它。
  5. 您应该只尝试输入现有频道,否则服务器将忽略您的 JOIN 频道。
  6. Twitch,让他们有机会在您登录时更改您的昵称,这意味着,如果您提供任何名称,TwitchBot 类 提供的预期昵称结果可能并且可能不正确与您登录的个人资料昵称不同。

Twitch IRC 功能,可以在Here 找到,这里有几个..

成员资格:JOIN、MODE、NAMES、PART
标签:PRIVMSG等'

您应该添加这些 CAP,首先要登录。

重要提示: PIRCBot,看起来不支持 twitch PRIVMSG 格式,即onMessage 回调,不会被调用。这让您可以通过handleLine 通用回调处理收到的消息的解析。

代码已更新以适用于上述更改,您应该设置最终变量以使其工作。

TwitchBot.java

import org.jibble.pircbot.*;

public class TwitchBot extends PircBot {

    private final String requestedNick;

    private String realNick;
    private String realServer;

    public TwitchBot(String nick) {
        this.requestedNick = nick;

        setName(this.requestedNick);
        setLogin(this.requestedNick);
    }

    @Override
    protected void onConnect() {
        super.onConnect();
        System.out.println("Connected!");

        // Sending special capabilities.
        sendRawLine("CAP REQ :twitch.tv/membership");
        sendRawLine("CAP REQ :twitch.tv/commands");
        sendRawLine("CAP REQ :twitch.tv/tags");
    }

    @Override
    protected void handleLine(String line) {
        super.handleLine(line);

        if (line.startsWith(":")) {
            String[] recvLines = line.split(" ");

            // First message is 001, extract logged in information.
            if (recvLines[1].equals("001")) {
                this.realServer = recvLines[0].substring(1);
                this.realNick = recvLines[2];
                System.out.println("realServer: " + this.realServer);
                System.out.println("realNick: " + this.realNick);
            }
        }
    }

    @Override
    protected void onJoin(String channel, String sender, String login, String hostname) {
        super.onJoin(channel, sender, login, hostname);
        if (sender.equals(this.realNick)){
            System.out.println("Successfully joined: " + channel);
        }
    }

    @Override
    protected 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);
        }
    }
}

goFile.java

import java.io.IOException;

import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.NickAlreadyInUseException;

public class goFile {

    private static final String OAUTH = "MYOAUTHHERE";
    private static final String ADDRESS = "irc.twitch.tv.";
    private static final int PORT = 6667;
    private static final String Nick = "MYNICKHERE";
    private static final String Channel = "#MYCHANNELHERE";

    public static void main(String[] args) throws NickAlreadyInUseException, IOException, IrcException {

        TwitchBot bot = new TwitchBot(Nick);
        bot.setVerbose(true);

        bot.connect(ADDRESS, PORT, OAUTH);
        bot.joinChannel(Channel);
        bot.sendMessage(Channel, "Hello, I am a bot");
    }
}

【讨论】:

  • 看来问题是我的频道名称不是全小写的。这成功了。非常感谢。
猜你喜欢
  • 2014-09-18
  • 2015-02-09
  • 2015-02-21
  • 2012-05-29
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
相关资源
最近更新 更多