I have a txt file with a few names of channel operators in it and I want to use it for my bot, so that if a command is ordered, first it checks if that person is op.
The txt file simply has each name on a seperate line. If I run the isOp() method in main outside of anything, it works fine. I can print out the names, check them, do whatever I want. But for some reason when using it in the bots class, it won't work.
It is supposed to return true when the name is found but the check in line 51 (isOp(sender)) doesn't seem to work. My name is in the file, but yet it does not enter the if statement. Am I doing something terribly wrong here?
package bot;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import org.jibble.pircbot.PircBot;
public class HelpBot extends PircBot{
public HelpBot(){
this.setName("AmA");
}
public void onJoin(String channel, String sender, String login, String hostname, String message){
if (!(sender.equalsIgnoreCase(this.getNick()))){
sendMessage(channel, "Hello "+sender+"! Enjoy your stay here :)");
}else{
sendMessage(channel, sender+" is up and running again :)");
}
}
public void onMessage(String channel, String sender, String login, String hostname, String message){
//Operator commands. Settings etc.
if (message.startsWith("!")){
//Owner only commands (me :P)
if (sender.equals("@tikker") || sender.equals("tikker")){
if (message.equalsIgnoreCase("!stop")){
System.out.println("Stopping AmA :(");
sendMessage(channel, "Out of fuel, all systems shutting down :(");
this.disconnect();
System.exit(0);
}
if (message.equalsIgnoreCase("!test")){
sendMessage(channel, "====== Running tests now ======");
sendMessage(channel, "====== Starting message test...");
sendMessage(channel, "Hello World!");
sendMessage(channel, "====== Starting PM test...");
sendMessage("Franz24", "Hi I'm AmA! Don't be scared, this is a test. Not some sort of attack :)");
sendMessage("Gorok", "Hi I'm AmA! Don't be scared, this is a test. Not some sort of attack :)");
sendMessage("Agent321", "Hi I'm AmA! Don't be scared, this is a test. Not some sort of attack :)");
sendMessage("tousent", "Hi I'm AmA! Don't be scared, this is a test. Not some sort of attack :)");
sendMessage("tikker", "Hi I'm AmA! Don't be scared, this is a test. Not some sort of attack :)");
sendMessage(channel, " ");
sendMessage(channel, "Tests succesfully ran");
sendMessage(channel, "====== Stopping tests ======");
}
}
//Regular OP commands
if (message.contains("!nick")){
System.out.println("Person "+sender+" tried to order !nick.");
try {
if (isOp(sender) == true){
System.out.println(sender+" changed AmA's nick.");
String newNick = message.substring(6);
this.changeNick(newNick);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
if (message.equalsIgnoreCase("!topic")){
try {
if (isOp(sender)){
String newTopic = message.substring(8);
this.setTopic(channel, newTopic);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
//User commands. Questions etc.
if (message.startsWith(">")){
if (message.equalsIgnoreCase(">AmA")){
sendMessage(sender, "I'm "+this.getNick()+" the question bot. I'm here to help you with questions about Galaxy55 :) I'm created by tikker. Should there be any trouble, contact him.");
}
if (message.equalsIgnoreCase(">wiki")){
sendMessage(sender, "The wiki can be found at http://www.galaxy55wiki.com");
}
}
}
public void onPing(String sourceNick, String sourceLogin, String sourceHostname, String target, String pingValue) {
sendMessage(sourceNick, "PONG");
System.out.println("Received PING from "+sourceNick+" sent PONG");
}
/*
* User defined methods
*/
//Checks if a name is listed in ops.txt
public static boolean isOp(String name) throws FileNotFoundException{
File opsFile = new File(System.getProperty("user.dir")+"\\src\\ops.txt");
Scanner scan = new Scanner(opsFile);
String line = scan.nextLine();
while (scan.hasNextLine() && line != name){
line = scan.nextLine();
System.out.println(line);
if (name == line){
System.out.println(line);
System.out.println(line == name);
return true;
}
}
return false;
}
}