所以我正在为我的频道开发一个基本的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);
}
}
}
控制台显示“已连接!”和“成功加入频道”,但是漫游器没有响应,并且不在我指定的频道中。它也不会在聊天中显示“你好我是机器人”。
答案 0 :(得分:0)
关于Twitch没什么要考虑的。
Twitch IRC功能,可以找到Here,这里很少。.
成员身份:JOIN,MODE,NAMES,PART
标签:PRIVMSG等'
您应该添加那些CAP,首先要登录。
重要通知::PIRCBot似乎不支持抽调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");
}
}