您好我正在为Twich工作一个Irc Bot,我想添加一个从txt文件获取文本并在命令中使用它的命令
EX:用户类型"!song"在聊天中 - >机器人从txt文件中获取歌曲标题,并在聊天歌曲标题中说出来。
我得到了从txt文件中获取数据的部分,但我无法在命令中获取该数据。
import org.jibble.pircbot.*;
import java.io.BufferedReader;
import java.io.FileReader;
public class MprovBot extends PircBot
{
// Get song title from Txt file
public static void main(String[] args) throws Exception {
FileReader file = new FileReader ("song.txt");
BufferedReader reader = new BufferedReader(file);
String song = "";
String line = reader.readLine();
while (line != null){
song += line;
line = reader.readLine();
}
System.out.println(song);
}
// IRC Commands_
public MprovBot() {
this.setName("MprovBot");
}
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
if (message.equalsIgnoreCase("!test")) {
sendMessage (channel, "Test Done");
}
if (message.equalsIgnoreCase("!Command")) {
sendMessage (channel, "This are the commands you can do.");
}
if (message.equalsIgnoreCase("!song")){
sendMessage (channel, song);
}
}
}
当我尝试编译代码时,我得到了这个
MprovBot.java:40: error: cannot find symbol
sendMessage (channel, song);
^
symbol: variable song
location: class MprovBot
1 error
答案 0 :(得分:0)
song
方法中未定义变量onMessage()
。所以编译器找不到变量。您需要做的是将main方法更改为具有返回类型的普通方法,并在接收!song
命令时调用此方法。
public class MprovBot extends PircBot {
// Get song title from Txt file AND return it!
public String getSong() throws Exception {
FileReader file = new FileReader ("song.txt");
BufferedReader reader = new BufferedReader(file);
String song = "";
String line = reader.readLine();
while (line != null){
song += line;
line = reader.readLine();
}
return song;
}
// IRC Commands_
public MprovBot() {
this.setName("MprovBot");
}
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
if (message.equalsIgnoreCase("!test")) {
sendMessage (channel, "Test Done");
}
if (message.equalsIgnoreCase("!Command")) {
sendMessage (channel, "This are the commands you can do.");
}
if (message.equalsIgnoreCase("!song")){
String song = "";
try {
song = getSong();
} catch(Exception e) { e.printStackTrace(); }
sendMessage (channel, song);
}
}
}