Java Pircbot将文本从txt文件获取到命令中
你好,我正在为Twich开发一个Irc机器人,我想添加一个命令,从txt文件中获取文本,并在命令中使用它 例如:用户在聊天->机器人中键入“!song”从txt文件中获取歌曲标题,并在聊天歌曲标题中键入 我得到了从txt文件中获取数据的部件,但我无法在命令中获取该数据Java Pircbot将文本从txt文件获取到命令中,java,readfile,Java,Readfile,你好,我正在为Twich开发一个Irc机器人,我想添加一个命令,从txt文件中获取文本,并在命令中使用它 例如:用户在聊天->机器人中键入“!song”从txt文件中获取歌曲标题,并在聊天歌曲标题中键入 我得到了从txt文件中获取数据的部件,但我无法在命令中获取该数据 import org.jibble.pircbot.*; import java.io.BufferedReader; import java.io.FileReader; public class MprovBot exten
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
在
onMessage()
方法中未定义变量song
。因此编译器找不到变量。您需要做的是将main方法更改为带有返回类型的普通方法,并在收到时调用此方法!歌曲
命令
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);
}
}
}
@Nicolai编辑了答案,出现了一个错误,因为我做了
try
,它给了我一个新的错误:MprovBot.java:83:error:';'应为}catch(异常e){e.printStackTrace()}^1错误,所以我这样做了,并再次尝试编译,我得到了与“Variable song”未定义相同的旧错误,我这样做了,但现在是sendMessage中的字符串song(channle,song);未知变量:S@Nicolai你有没有看到弦乐曲=”代码>在之前尝试?