use of pcl.lc.irc.entryClasses.Command in project LanteaBot by PC-Logix.
the class Weather method initHook.
@Override
protected void initHook() {
local_command = new Command("weather", new CommandArgumentParser(1, new CommandArgument(ArgumentTypes.STRING, "Location"))) {
@Override
public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
Helper.sendMessage(target, getWeather(this.argumentParser.getArgument("Location")));
return new CommandChainStateObject();
}
};
local_command.registerAlias("w");
local_command.setHelpText("Returns weather data for the supplied postal code, or Place name");
IRCBot.registerCommand(local_command);
}
use of pcl.lc.irc.entryClasses.Command in project LanteaBot by PC-Logix.
the class WhoPinged method initHook.
@Override
protected void initHook() {
Database.addStatement("CREATE TABLE IF NOT EXISTS Pings(id INTEGER PRIMARY KEY, whowaspinged, whopinged, message, time, channel)");
Database.addPreparedStatement("addPing", "INSERT INTO Pings(whowaspinged, whopinged, message, time, channel) VALUES (?,?,?,?,?);");
Database.addPreparedStatement("getPings", "SELECT id, whopinged, message, time, channel FROM Pings WHERE LOWER(whowaspinged) = ?;");
Database.addPreparedStatement("getAllPings", "SELECT id, time FROM Pings;");
Database.addPreparedStatement("delPings", "DELETE FROM Pings WHERE id = ?;");
command_WhoPinged = new Command("whopinged") {
@Override
public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) {
try {
PreparedStatement statement = Database.getPreparedStatement("getPings");
statement.setString(1, nick.toLowerCase());
ResultSet results = statement.executeQuery();
if (Config.httpdEnable.equals("true") && results.next()) {
Helper.sendMessage(target, "Who Pinged you: " + httpd.getBaseDomain() + "/whopinged?nick=" + nick, nick);
} else if (!results.next()) {
Helper.sendMessage(target, "No pings! :(");
}
} catch (Exception e) {
Helper.sendMessage(target, "Error: " + e.getClass() + " " + e.getMessage());
e.printStackTrace();
}
return new CommandChainStateObject();
}
};
command_WhoPinged.setHelpText("Shows you the last pings you had.");
command_ClearPings = new Command("clearpings") {
@Override
public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) {
try {
PreparedStatement statement = Database.getPreparedStatement("delPings");
statement.setString(1, nick);
statement.execute();
Helper.sendMessage(target, "Ok");
} catch (Exception e) {
Helper.sendMessage(target, "Error: " + e.getClass() + " " + e.getMessage());
e.printStackTrace();
}
return new CommandChainStateObject();
}
};
command_ClearPings.setHelpText("Clears your pings from the DB");
IRCBot.registerCommand(command_WhoPinged);
IRCBot.registerCommand(command_ClearPings);
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
executor = ses.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
long epoch = System.currentTimeMillis();
PreparedStatement getPings = Database.getPreparedStatement("getAllPings");
ResultSet results = getPings.executeQuery();
while (results.next()) {
if ((results.getLong(2) + 259200000) <= epoch) {
PreparedStatement delPings = Database.getPreparedStatement("delPings");
delPings.setLong(1, results.getLong(1));
delPings.execute();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 1, TimeUnit.SECONDS);
httpd.registerContext("/whopinged", new WhoPingedHandler(), "WhoPinged");
}
use of pcl.lc.irc.entryClasses.Command in project LanteaBot by PC-Logix.
the class Exclamation method initCommands.
private void initCommands() {
CommandRateLimit limit = new CommandRateLimit(0, 5, 0, true, false);
command_curse_word = new Command("curseword", limit) {
@Override
public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) {
Helper.sendMessage(target, getRandomExpression(TypeFilter.CURSE_WORD) + TablesOfRandomThings.getRandomExclamations(true, false), nick);
return new CommandChainStateObject();
}
};
command_curse_word.registerAlias("curses");
command_curse_word.registerAlias("cursewd");
command_curse_word.registerAlias("cursew");
command_curse_word.registerAlias("swear");
command_curse_word.registerAlias("swearword");
command_curse_word.setHelpText("Holy manbats Batman!");
command_exclamation = new Command("exclamation", limit) {
@Override
public CommandChainStateObject onExecuteSuccess(Command command, String nick, String target, GenericMessageEvent event, String params) {
Helper.sendMessage(target, getRandomExpression(TypeFilter.ALL) + TablesOfRandomThings.getRandomExclamations(true, false), nick);
return new CommandChainStateObject();
}
};
command_exclamation.registerAlias("excl");
command_exclamation.setHelpText("Wot in tarnation?!");
}
use of pcl.lc.irc.entryClasses.Command in project LanteaBot by PC-Logix.
the class GenericEventListener method onGenericMessage.
@Override
public void onGenericMessage(final GenericMessageEvent event) {
if (event.getUser() == null) {
return;
}
for (String str : Config.ignoreMessagesEndingWith) {
if (event.getMessage().endsWith(str)) {
System.out.println("Ignored '" + event.getMessage() + "' because it ends with '" + str + "'");
return;
}
}
String user;
String callingRelay = null;
String bracketsPre = "";
String bracketsPost = "";
for (String brackets : Config.overBridgeUsernameBrackets) {
bracketsPre += "\\" + brackets.substring(0, 1);
bracketsPost += "\\" + brackets.substring(1, 2);
}
Pattern pattern = Pattern.compile("[" + bracketsPre + "](.*)[" + bracketsPost + "] ");
Matcher matcher = pattern.matcher(event.getMessage());
if (Config.parseBridgeCommandsFromUsers.contains(event.getUser().getNick()) && matcher.find()) {
user = Helper.cleanNick(matcher.group(1));
callingRelay = event.getUser().getNick();
} else {
user = event.getUser().getNick();
}
ArrayList<ArrayList<String>> commands = CommandHelper.findCommandInString(event.getMessage());
for (ArrayList<String> thisCmd : commands) {
try {
String command = thisCmd.get(0).toLowerCase();
String actualCommand = command.replaceFirst("\\" + Config.commandprefix, "");
;
String[] params = new String[] {};
if (thisCmd.size() > 1)
params = thisCmd.subList(1, thisCmd.size()).toArray(new String[] {});
// System.out.println("CMD: " + command + ", params: " + String.join(";", params));
if (IRCBot.commands.containsKey(actualCommand)) {
Command cmd = IRCBot.commands.get(actualCommand);
cmd.callingRelay = callingRelay;
String target = Helper.getTarget(event);
CommandChainStateObject stateObj = cmd.tryExecute(command, user, target, event, params);
System.out.println("Executed command '" + cmd.getCommand() + "': " + stateObj.state + (stateObj.msg != null ? " => '" + stateObj.msg + "'" : ""));
DbStatCounter.Increment("commands", cmd.getCommand());
} else if (IRCBot.dynamicCommands.containsKey(actualCommand)) {
DynamicCommand cmd = IRCBot.dynamicCommands.get(actualCommand);
cmd.callingRelay = callingRelay;
String target = Helper.getTarget(event);
CommandChainStateObject stateObj = cmd.tryExecute(command, user, target, event, params);
System.out.println("Executed dynamic command '" + cmd.getCommand() + "': " + stateObj.state + (stateObj.msg != null ? " => '" + stateObj.msg + "'" : ""));
DbStatCounter.Increment("commands_dynamic", cmd.getCommand());
} else {
System.out.println("No command '" + actualCommand + "'");
}
} catch (Exception e) {
System.out.println("Command exception!");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String pasteURL = PasteUtils.paste(sw.toString(), PasteUtils.Formats.NONE);
Helper.sendMessage(Helper.getTarget(event), "I had an exception... ow. Here's the stacktrace: " + pasteURL);
e.printStackTrace();
}
}
super.onGenericMessage(event);
if (!(event instanceof MessageEvent))
IRCBot.log.info("<-- Query: " + event.getUser().getNick() + ": " + event.getMessage());
}
use of pcl.lc.irc.entryClasses.Command in project LanteaBot by PC-Logix.
the class Help method getHelpRows.
public static String getHelpRows(String permFilter) {
String items = "";
try {
items = "";
includedCommands.clear();
for (Map.Entry<String, Command> entry : IRCBot.commands.entrySet()) {
Command command = entry.getValue();
String item = getHelpRow(command, permFilter);
if (!item.equals(""))
includedCommands.add(command.getCommand());
items += item;
}
if (IRCBot.dynamicCommands.size() > 0)
items += "<tr><th colspan='3'>Dynamic commands</th></tr>";
for (Map.Entry<String, DynamicCommand> entry : IRCBot.dynamicCommands.entrySet()) {
DynamicCommand command = entry.getValue();
String item = getHelpRow(command, permFilter);
if (!item.equals(""))
includedCommands.add(command.getCommand());
items += item;
}
items = StringUtils.strip(items, "\n");
} catch (Exception e) {
e.printStackTrace();
}
return items;
}
Aggregations