Skip to content

Instantly share code, notes, and snippets.

@bl4ckscor3
Last active September 21, 2015 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bl4ckscor3/3492bef5f2d2759eb963 to your computer and use it in GitHub Desktop.
Save bl4ckscor3/3492bef5f2d2759eb963 to your computer and use it in GitHub Desktop.
A simple IRC bot to keep the #litterbox clean.
package bl4ckscor3.bot.janitor.core;
import java.io.IOException;
import java.util.Random;
import org.pircbotx.Configuration;
import org.pircbotx.PircBotX;
import org.pircbotx.UtilSSLSocketFactory;
import org.pircbotx.exception.IrcException;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.events.MessageEvent;
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 bl4ckscor3
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
public class Core
{
private static PircBotX bot;
public static void main(String args[]) throws IOException, IrcException
{
Configuration<PircBotX> config;
config = new Configuration.Builder<PircBotX>()
.setVersion("v1.4")
.setName("Janitor")
.setLogin("Jane")
.setServer("irc.esper.net", 6697)
.setSocketFactory(new UtilSSLSocketFactory().trustAllCertificates())
.setAutoNickChange(true)
.addAutoJoinChannel("#litterbox")
.setMessageDelay(0)
.addListener(new Core.JanitorListener())
.buildConfiguration();
bot = new PircBotX(config);
bot.startBot();
}
public static class JanitorListener extends ListenerAdapter<PircBotX>
{
private static int clean = 9; //10 messages until cleanup
private static boolean cleaning = false;
private static boolean hasCleanedRightBefore = true;
@Override
public void onMessage(MessageEvent<PircBotX> event) throws Exception
{
if(!event.getChannel().getName().equals("#litterbox"))
return;
if(event.getMessage().startsWith("+"))
{
if(event.getMessage().startsWith("+version"))
event.getChannel().send().message(Core.bot.getConfiguration().getVersion());
else if(event.getMessage().startsWith("+changelog"))
event.getChannel().send().message("https://www.dropbox.com/s/rjspywk6qq4d8s0/Janitor.txt?dl=0&s=sl");
else if(event.getMessage().startsWith("+source"))
event.getChannel().send().message("https://gist.github.com/bl4ckscor3/3492bef5f2d2759eb963");
clean--;
hasCleanedRightBefore = false;
return;
}
if(!cleaning && (clean == 0 || (contains(event.getMessage().toLowerCase(), "litterbox", "clean" , "janitor") && clean != 0)))
{
if(hasCleanedRightBefore)
{
event.respond("There is no litter to collect right now...");
return;
}
cleaning = true;
Thread.sleep(2000 + new Random().nextInt(4) * 1000);
clean = 9;
cleaning = false;
hasCleanedRightBefore = true;
Core.bot.sendIRC().action("#litterbox", "cleans up the #litterbox.");
}
clean--;
hasCleanedRightBefore = false;
}
/**
* Checks wether some strings are contained in a message
* @param message The message to check
* @param checks The substrings to check
* @return True if all the substrings are contained in the message
*/
private boolean contains(String message, String... checks)
{
int ok = 0;
for(String s : checks)
{
if(message.contains(s))
ok++;
}
return ok == checks.length;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment