首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java Twitch IRC机器人

Java Twitch IRC机器人
EN

Stack Overflow用户
提问于 2018-07-24 02:14:21
回答 1查看 3.4K关注 0票数 1

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

Config.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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“。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-07 06:00:20

关于Twitch,有几件事需要考虑。

  1. 您的电子邮件必须经过验证。Settings
  2. Channel -> Profile -> profile Settings
  3. Channel names必须输入,因为lowercase.
  4. Nickname是无用的,twitch are using your Profile nickname.
  5. Twitch use IRCv3 Client Capability Negotiation CAP,这意味着您也应该使用它。
  6. 您应该只尝试输入现有的频道,否则服务器将忽略您的加入TwitchBot,让他们有机会在您登录时更改您的昵称,这意味着,由TwitchBot类提供的预期nick结果可以,如果您提供的名称与您登录的配置文件昵称不同,则可能是不正确的。

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

成员资格:加入、模式、名称、部分

标签: PRIVMSG等

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

重要提示: PIRCBot,看起来不支持twitch PRIVMSG格式,这意味着onMessage回调,将不会被调用。这使得您可以通过handleLine常规回调来处理接收到的消息的解析。

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

TwitchBot.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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");
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51484971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档