I need some help with arrays for admins and stuff. I'm using this API to make a server bot. You can find all the classes, constructors, and methods there.
Here's my current code:
import org.jibble.pircbot.*;
public class MyBot extends PircBot {
public MyBot() {
this.setName("DevilBot");
}
String owner = "Evan";
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);
}
if (message.equalsIgnoreCase("!owner")) {
if(sender.equals(owner))
{
sendMessage(channel, Colors.NORMAL + "You're my owner silly!");
}
if (!sender.equals(owner))
{
sendMessage(channel, Colors.NORMAL + sender + ": " + owner + " is my owner!");
}
}
if (message.equalsIgnoreCase("!ban")) {
if(sender.equals(owner))
{
ban(channel, message);
sendMessage(channel, "Banned " + message);
}
else
{
kick(channel, sender);
sendMessage(channel, "You aren't my mother!");
}
}
if (message.equalsIgnoreCase("!version")) {
sendMessage(channel, "Version 0.1");
sendMessage(channel, "PircBot API v1.5.0");
}
if (message.equalsIgnoreCase("!aelux")) {
sendMessage(channel, "ALL HAIL AELUX!");
}
if (message.equalsIgnoreCase("!hates")){
sendMessage(channel, message + ", " + sender + " hates you!");
}
if (message.equalsIgnoreCase("!op")){
if(sender.equals(owner))
{
sendMessage(channel, "Opping " + message);
}
else
{
ban(channel, sender);
kick(channel, sender);
sendMessage(channel, "GTFO! Banned.");
}
}
}
}
It compiles and runs fine. And it's still in Alpha Stages, but for some reason it won't read my command. Like: !kick user Yields no response.
The API is quick and easy to understand. If you CAN help me, that would be awesome!
Right now in your code you are only using equalsIgnoreCase This method requires both strings to match perfectly apart from the case. So the reason "!ban user" would not work is because of the following.
"!ban".equalsIgnoreCase("!bAn") == true
"!ban".equalsIgnoreCase("!BaN") == true
but
"!ban".equalsIgnoreCase("!ban username") == false
"!ban".equalsIgnoreCase("!ban ") == false
This following piece of code would result in you being able to ban people by typing "!ban username". However it will be case sensitive.
if (message.startsWith("!ban")) {
if(sender.equals(owner))
{
String userToBan = message.split(" ")[1];
ban(channel, userToBan);
sendMessage(channel, "Banned " + userToBan);
}
else
{
kick(channel, sender);
sendMessage(channel, "You aren't my mother!");
}
}
If you don't want it to be just split the incoming command on a separator (usually space), and convert the first string and match them with equalsIgnoreCase.
String[] messageParts = string.split();
String command = messageParts[0];
if("!ban".equalsIgnoreCase(command){
ban(channel,messageParts[1])
if(messageParts[2].isEmpty()){
sendMessage(channel, "Banned " + messageParts[1]);
}else{
sendMessage(channel, "Banned " + messageParts[1] + " Reason: " + messageParts[2]);
}
}
Hope this helps