Java Twitch IRC Bot [英] Java Twitch IRC Bot

查看:79
本文介绍了Java Twitch IRC Bot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm working on a basic Twitch Bot for my channel and the code is as follows:

Config.java

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

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

}

控制台显示已连接!"和成功加入频道",但是漫游器没有响应,并且不在我指定的频道中.它也不会在聊天中显示你好我是机器人".

The console displays "Connected!" and "Successfully joined channel" however the bot is unresponsive, and is not in the channel I specified. It also does not print "Hello I am a bot" in the chat.

推荐答案

有关Twitch的几点注意事项.

There are few things to consider about Twitch.

  1. 您的电子邮件必须经过验证.设置->个人资料->个人资料设置
  2. 频道名称必须以小写字母输入.
  3. 昵称没用,抽搐正在使用您的个人资料昵称.
  4. Twitch使用 IRCv3客户端功能协商 CAP ,这意味着您也应该使用它.
  5. 您应仅尝试输入现有频道,否则服务器将忽略您的JOIN频道.
  6. Twitch,让他们自己有机会在登录时更改昵称,这意味着 TwitchBot类提供的预期昵称可能而且如果提供任何名称也可能不正确与您登录的个人资料昵称不同.
  1. Your email must be validated. Settings -> Profile -> Profile Settings
  2. Channel names must be entered as lowercase.
  3. Nickname are useless, twitch are using your profile nickname.
  4. Twitch uses IRCv3 Client Capability Negotiation aka CAP, which means you should be use it as well.
  5. You should only try enter existing channels, otherwise the server will ignore your JOIN channel.
  6. Twitch, allow themselves the opportunity to change your nickname while you logged in, which means, that the expected nick results, provided by TwitchBot class, can, and probably be incorrect if you supply any name different from your logged in profile nickname.

Twitch IRC功能,可以在此处此处找到.很少..

Twitch IRC Capabilities, can be found Here, here are few..

成员身份:JOIN,MODE,NAMES,PART
标签:PRIVMSG等'

membership: JOIN, MODE, NAMES, PART
tags: PRIVMSG, etc'

您应该添加这些CAP,这是您登录的第一件事.

You should add those CAP, first thing you are logged in.

重要通知:PIRCBot似乎不支持抽调PRIVMSG格式,这意味着将不会调用onMessage回调.这样,您就可以通过handleLine常规回调来处理接收到的消息的解析.

Important Notice: PIRCBot, doesn't look to support twitch PRIVMSG format, which means onMessage callback, will not be called. which leaves you to handle the parsing of received messages, through handleLine general callback.

代码以应用于上述更改,并且应设置最终变量以使其起作用.

Code as been updated to apply to above changes, and you should set the final variables in order it to work.

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

这篇关于Java Twitch IRC Bot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆