lilggamegenuis

Fozruci

Jun 22nd, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 32.92 KB | None | 0 0
  1. package com.mkyong.reflection;
  2. import org.jibble.pircbot.*;
  3.  
  4. import com.rmtheis.yandtran.*;
  5. import com.rmtheis.yandtran.detect.Detect;
  6. import com.rmtheis.yandtran.translate.Translate;
  7.  
  8. import java.io.*;
  9. import java.text.*;
  10. import java.nio.charset.Charset;
  11. import java.util.*;
  12. import java.util.concurrent.TimeUnit;
  13. import java.util.Arrays;
  14. import java.util.regex.*;
  15. import com.google.code.chatterbotapi.*;
  16.  
  17. import com.fathzer.soft.javaluator.*;
  18.  
  19.  
  20. public class MyBot extends PircBot {
  21.     String prefix = "!";
  22.     boolean jokeCommands = true;
  23.     boolean chngCMDRan = false;
  24.     ArrayList<String> playerList = new ArrayList<>();
  25.     //boolean spinStarted = false;
  26.     String[] dictionary = {": i don't know what \"%s\" is, do i look like a dictionary?", ": Go look it up yourself.", ": Why not use your computer and look \"%s\" up", ": Google it", "Nope.", "Get someone else to do it", "Why not get that " + Colors.RED + " Other bot" + Colors.NORMAL + " to do it?"};
  27.     String[] commands = {"HelpMe", " time", " Hello", " RandomNum", " version", " Spin", " StringToBytes", " temp", " chat",  " Joke: Splatoon", "Joke: Attempt", " Joke: Stfu", " Joke: potato-chan", " Joke: whatIs?", "Joke: getFinger"};
  28.     String currentNick = "Lil-G";
  29.     String currentUsername = "lilggamegenui";
  30.     byte[] bytes = {-snip-};
  31.     final String PASSWORD = new String(bytes);
  32.     final int BLOCKS = 128;
  33.     final int BLOCKSMB = 8*BLOCKS;
  34.     final int BLOCKSGB = 8192*BLOCKS;
  35.     int i = 0;
  36.     ChatterBotFactory factory = new ChatterBotFactory();
  37.     ChatterBot cleverBot;
  38.     ChatterBotSession cleverBotsession;
  39.     boolean cleverBotInt;
  40.     ChatterBot pandoraBot;
  41.     ChatterBotSession pandoraBotsession;
  42.     boolean pandoraBotInt;
  43.     String currChannel = null;
  44.     boolean debugMode = false;
  45.  
  46.     DoubleEvaluator calc = new DoubleEvaluator();
  47.     StaticVariableSet<Double> variables = new StaticVariableSet<>();
  48.  
  49.    
  50.  
  51. // yandex api trnsl.1.1.20150924T011621Z.e06050bb431b7175.e5452b78ee8d11e4b736035e5f99f2831a57d0e2
  52.  
  53.  
  54.     public MyBot(String name, String userName, boolean debugMode) {
  55.         this.setName(name);
  56.         this.setLogin(userName);
  57.         this.setMessageDelay(1000);
  58.         this.setDebugMode(debugMode);
  59.     }
  60.    
  61.     public void onConnect(){
  62.         identify(PASSWORD);
  63.         sendRawLine("MODE Lil-G|bot +Bx");
  64.         joinChannel("#Lil-G|bot");
  65.         if(!debugMode) {
  66.             joinChannel("#pokemon");
  67.             joinChannel("#retro");
  68.             joinChannel("#retrotech");
  69.             //joinChannel("#ssrg");
  70.             joinChannel("#SSB");
  71.             //joinChannel("#tcrf");
  72.         }
  73.         sendRawLine("cs op #Lil-G|bot " + getNick());
  74.         sendRawLine("cs op #Lil-G|bot Lil-G");
  75.         sendRawLine("cs op #SSB " + getNick());
  76.         sendRawLine("cs op #SSB Lil-G");
  77.     }
  78.  
  79.     public void onMessage(String channel, String sender, String login, String hostname, String message) {
  80.         currChannel = channel;
  81.         List<String> argLog = splitMessage(message);
  82.         log(argLog.toString());
  83.         String[] arg = argLog.toArray(new String[argLog.size()]);
  84.  
  85. // !helpme
  86.         if (arg[0].equalsIgnoreCase(prefix + "helpme")){
  87.  
  88.             if (message.equalsIgnoreCase(prefix + "helpme")){
  89.                 sendMessage(channel,"List of commands so far.");
  90.                 ArrayList<String> cmdhelp = new ArrayList<>(Arrays.asList(commands));
  91.                 sendMessage(channel, cmdhelp.toString());
  92.             }
  93.  
  94.             if (arg[1].equalsIgnoreCase("Helpme")){
  95.                     sendMessage(channel, sender + " , Really? " + "ಠ_ಠ");
  96.             }
  97.  
  98.             if (arg[1].equalsIgnoreCase("time")){
  99.                 sendMessage(channel,"Displays info from the Date class");
  100.             }
  101.  
  102.             if (arg[1].equalsIgnoreCase("Hello")){
  103.                 sendMessage(channel,"Just your average \"hello world!\" program");
  104.             }
  105.  
  106.             if (arg[1].equalsIgnoreCase("Attempt")){
  107.                 sendMessage(channel,"Its a inside-joke to my friends in school. If i'm not away, ask me and i'll tell you about it.");
  108.             }
  109.  
  110.             if (arg[1].equalsIgnoreCase("RandomNum")){
  111.                 sendMessage(channel,"Creates a random number between the 2 integers");
  112.                 sendMessage(channel,"Usage: first number sets the minimum number, second sets the maximum");
  113.             }
  114.  
  115.             if (arg[1].equalsIgnoreCase("version")){
  116.                 sendMessage(channel,"Displays the version of the bot");
  117.             }
  118.  
  119.             if (arg[1].equalsIgnoreCase("Spin")){
  120.                 sendMessage(channel,"Russian Roulette on irc. For more info do " + prefix + "spin help");
  121.             }
  122.  
  123.             if (arg[1].equalsIgnoreCase("StringToBytes")){
  124.                 sendMessage(channel,"Converts a String into a Byte array");
  125.             }
  126.  
  127.             if (arg[1].equalsIgnoreCase("temp")){
  128.                 sendMessage(channel,"Converts a temperature unit to another unit.");
  129.                 sendMessage(channel,"Usage: First parameter is the unit its in. Second parameter is the unit to convert to. Third parameter is the number to convert to.");
  130.             }
  131.  
  132.             if (arg[1].equalsIgnoreCase("chat")){
  133.                 sendMessage(channel,"This command functions like ELIZA. Talk to it and it talks back.");
  134.                 sendMessage(channel,"Usage: First parameter defines what service to use. it supports CleverBot, PandoraBot, and JabberWacky (JabberWacky not yet implemented). Second parameter is the message to send. Could also be the special param \"\\setup\" to actually start the bot.");
  135.             }
  136.  
  137.         }
  138.  
  139. // !Time - Tell the time
  140.         if (message.equalsIgnoreCase(prefix + "time")) {
  141.             String time = new java.util.Date().toString();
  142.             sendMessage(channel, sender + ": The time is now " + time);
  143.         }
  144.  
  145. // !calcj - calculate a expression
  146.         if (arg[0].equalsIgnoreCase(prefix + "calcJ")) {
  147.             double eval = 0;
  148.             if(arg.length > 3) {
  149.                 double x = 0;
  150.                 eval = calc.evaluate(arg[1]);
  151.             }
  152.             sendMessage(channel, sender + ": " + eval);
  153.         }
  154.  
  155. // !Time - Tell the time
  156.         if ((message.contains("hi") || message.contains("hey") || message.contains("hello")) && message.contains(getNick())) {
  157.             sendMessage(channel, sender + ": I'm a bot");
  158.         }
  159.  
  160. // !StringToBytes - convert a String into a Byte array
  161.         if (arg[0].equalsIgnoreCase(prefix + "StringToBytes")) {
  162.             sendMessage(channel, sender + ": " + getBytes(arg[1]));
  163.         }
  164.  
  165. // !chat - chat's with a internet conversation bot
  166.         if (arg[0].equalsIgnoreCase(prefix + "chat")) {
  167.             if (arg[1].equalsIgnoreCase("clever")){
  168.                 if (arg[2].equalsIgnoreCase("\\setup")){
  169.                     try {
  170.                         cleverBot = factory.create(ChatterBotType.CLEVERBOT);
  171.                         cleverBotsession = cleverBot.createSession();
  172.                         cleverBotInt = true;
  173.                         sendNotice(sender, "CleverBot started");
  174.                     } catch(Exception e){
  175.                         sendMessage(channel, "Error: Could not create clever bot session. Error was: " + e);
  176.                     }
  177.                 }
  178.                 else{
  179.                     if (cleverBotInt) {
  180.                         try {
  181.                             sendMessage(channel, sender + ": " + botTalk("clever", arg[2]));
  182.                         } catch (Exception e) {
  183.                             sendMessage(channel, "Error: Problem with bot. Error was: " + e);
  184.                         }
  185.                     }
  186.                     else {
  187.                         sendMessage(channel, sender + ": You have to start CleverBot before you can talk to it. star it with \\setup");
  188.                     }
  189.                 }
  190.  
  191.             } else if (arg[1].equalsIgnoreCase("pandora")) {
  192.                 if (arg[2].equalsIgnoreCase("\\setup")) {
  193.                     try {
  194.                         pandoraBot = factory.create(ChatterBotType.PANDORABOTS, "b0dafd24ee35a477");
  195.                         pandoraBotsession = pandoraBot.createSession();
  196.                         pandoraBotInt = true;
  197.                         sendNotice(sender, "PandoraBot started");
  198.                     } catch(Exception e){
  199.                         sendMessage(channel, "Error: Could not create pandora bot session. Error was: " + e);
  200.                     }
  201.                 } else {
  202.                     if (pandoraBotInt) {
  203.                         try {
  204.                             sendMessage(channel, sender + ": " + botTalk("pandora", arg[2]));
  205.                         } catch (Exception e) {
  206.                             sendMessage(channel, "Error: Problem with bot. Error was: " + e);
  207.                         }
  208.                     }
  209.                     else {
  210.                         sendMessage(channel, sender + ": You have to start PandoraBot before you can talk to it. start it with \\setup");
  211.                     }
  212.                 }
  213.  
  214.  
  215.             }
  216.         }
  217.  
  218. // !temp - Converts a unit of temperature to another
  219.         if (arg[0].equalsIgnoreCase(prefix + "temp")) {
  220.             int temp = Integer.parseInt(arg[3]);
  221.             double ans = 0;
  222.             String unit = "err";
  223.             if(arg[1].equalsIgnoreCase("F")){
  224.                 if(arg[2].equalsIgnoreCase("C")){
  225.                     ans = (temp - 32)*5/9;
  226.                     unit = "C";
  227.                 }
  228.                 else if(arg[2].equalsIgnoreCase("K")){
  229.                     ans = (temp - 32) * 5/9 + 273.15;
  230.                     unit = "K";
  231.                 }
  232.             }
  233.             else if(arg[1].equalsIgnoreCase("C")){
  234.                 if(arg[2].equalsIgnoreCase("F")){
  235.                     ans = (temp * 9/5) + 32;
  236.                     unit = "F";
  237.                 }
  238.                 else if(arg[2].equalsIgnoreCase("K") && temp < 0){
  239.                     ans = temp + 273.15;
  240.                     unit = "K";
  241.                 }
  242.             }
  243.             else if(arg[1].equalsIgnoreCase("K")){
  244.                 if(arg[2].equalsIgnoreCase("F")){
  245.                     ans = (temp - 273.15) * 9/5 + 32;
  246.                     unit = "F";
  247.                 }
  248.                 else if(arg[2].equalsIgnoreCase("C")){
  249.                     ans = temp - 273.15;
  250.                     unit = "C";
  251.                 }
  252.             }
  253.             sendMessage(channel, sender + ": " + ans + unit);
  254.         }
  255.  
  256.  
  257. // !blockconv - Converts blocks to bytes
  258.         if (arg[0].equalsIgnoreCase(prefix + "blockconv")) {
  259.             int data = Integer.parseInt(arg[3]);
  260.             double ans = 0;
  261.             String unit = "err";
  262.             if(arg[1].equalsIgnoreCase("blocks")){
  263.                 if(arg[2].equalsIgnoreCase("kb")){
  264.                     ans = BLOCKS*data;
  265.                     unit = "KB";
  266.                 }
  267.             }
  268.             else if(arg[1].equalsIgnoreCase("kb")){
  269.                 if(arg[2].equalsIgnoreCase("blocks")) {
  270.                     ans = data/BLOCKS;
  271.                     unit = "Blocks";
  272.                 }
  273.             }
  274.             else if(arg[1].equalsIgnoreCase("mb")){
  275.                 if(arg[2].equalsIgnoreCase("blocks")){
  276.                     ans = data/BLOCKSMB;
  277.                     unit = "Blocks";
  278.                 }
  279.             }
  280.             else if(arg[1].equalsIgnoreCase("gb")){
  281.                 if(arg[2].equalsIgnoreCase("blocks")){
  282.                     ans = data/BLOCKSGB;
  283.                     unit = "Blocks";
  284.                 }
  285.             }
  286.             sendMessage(channel, sender + ": " + ans + unit);
  287.             sendMessage(channel, "NOTICE: this command currently doesn't work like it should. The only conversion that works is blocks to kb and kb to blocks");
  288.         }
  289.  
  290. // !Hello - Standard "Hello world" command
  291.         if (message.equalsIgnoreCase(prefix + "hello")){
  292.             sendMessage(channel, sender + ": Hello World!");
  293.         }
  294.  
  295. // !Bot - Explains that "yes this is a bot"
  296.         if (message.equalsIgnoreCase(prefix + "bot")){
  297.             sendMessage(channel, sender + ": Yes, this is "+ currentNick + "'s bot.");
  298.         }
  299.  
  300. // !getname - gets the name of the bot
  301.         if (message.equalsIgnoreCase(prefix + "getname")){
  302.             sendMessage(channel, getName());
  303.         }
  304.  
  305. // !login - attempts to login to nickserv
  306.         if (message.equalsIgnoreCase(prefix + "login")){
  307.             sendRawLine("ns identify " + PASSWORD);
  308.             sendRawLine("cs op #Lil-G|bot " + getNick());
  309.             sendRawLine("cs op #Lil-G|bot Lil-G");
  310.             sendRawLine("cs op #SSB " + getNick());
  311.             sendRawLine("cs op #SSB Lil-G");
  312.         }
  313.  
  314. // !getLogin - gets the login of the bot
  315.         if (message.equalsIgnoreCase(prefix + "getLogin")){
  316.             sendMessage(channel, getLogin());
  317.         }
  318.  
  319. // !getID - gets the ID of the user
  320.         if (arg[0].equalsIgnoreCase(prefix + "getID")){
  321.             sendMessage(channel, "You are :" + findIn(getUsers(channel), arg[1]));
  322.         }
  323.  
  324. // !RandomNum - Gives the user a random Number
  325.         if (arg[0].equalsIgnoreCase(prefix + "randomnum")){
  326.             long num1, num2;
  327.             if(arg[1].contains("0x")){
  328.                 num1 = Long.parseLong(arg[1], 16);
  329.             }
  330.             else{
  331.                 num1 = Long.parseLong(arg[1], 10);
  332.             }
  333.             if(arg[2].contains("0x")){
  334.                 num2 = Long.parseLong(arg[2], 16);
  335.             }
  336.             else{
  337.                 num2 = Long.parseLong(arg[2], 10);
  338.             }
  339.  
  340.             sendMessage(channel, sender + ": " + randInt((int) num1, (int) num2));
  341.         }
  342.  
  343. // !version - Displays what version the bot is on
  344.         if (message.equalsIgnoreCase(prefix + "version")){
  345.             sendMessage(channel, sender + ": " + getVersion());
  346.             try
  347.             {
  348.                 Process p=Runtime.getRuntime().exec("java -version");
  349.                 p.waitFor();
  350.                 BufferedReader reader=new BufferedReader(
  351.                         new InputStreamReader(p.getInputStream()));
  352.                 String line;
  353.                 while((line = reader.readLine()) != null)
  354.                 {
  355.                     sendMessage(channel, line);
  356.                 }
  357.  
  358.             }
  359.             catch(Exception e1) {
  360.                 sendMessage(channel, "Error: " + e1);
  361.             }
  362.  
  363.         }
  364.  
  365. // !ChngCMD - Changes the command prefix when it isn't the standard "!"
  366.         if (arg[0].equalsIgnoreCase("!chngcmd") && !prefix.equals("!")){
  367.             if(checkPerm(sender, hostname, login)){
  368.                 prefix = arg[1];
  369.                 sendMessage(channel, sender + ": Command variable is now \"" + prefix + "\"");
  370.                 chngCMDRan = true;
  371.             }
  372.             else {
  373.                 permError(sender, "can change the command character");
  374.             }
  375.         }
  376.  
  377. // !ChngCMD - Changes the command prefix
  378.         if (arg[0].equalsIgnoreCase(prefix + "chngcmd") && !chngCMDRan){
  379.             if(checkPerm(sender, hostname, login)){
  380.                 prefix = arg[1];
  381.                 sendMessage(channel, sender + ": Command variable is now \"" + prefix + "\"");
  382.             }
  383.             else {
  384.                 permError(sender, "can change the command character");
  385.             }
  386.         }
  387.         chngCMDRan = false;
  388.  
  389. // !Saythis - Tells the bot to say someting
  390.         if (arg[0].equalsIgnoreCase(prefix + "saythis")){
  391.             if(checkPerm(sender, hostname, login)){
  392.                 sendMessage(channel, arg[1]);
  393.             }
  394.             else {
  395.                 permErrorchn(channel, sender, "can use this command");
  396.             }
  397.         }
  398.  
  399. // !ToSciNo - converts a number to scientific notation
  400.         if (arg[0].equalsIgnoreCase(prefix + "ToSciNo")){
  401.             NumberFormat formatter = new DecimalFormat("0.######E0");
  402.  
  403.             long num = Long.parseLong(arg[1]);
  404.                 try {
  405.                     sendMessage(channel, formatter.format(num));
  406.                 } catch (Exception e) {
  407.                     sendError(channel, e);
  408.                     log(e.toString());
  409.                 }
  410.         }
  411.  
  412. // !Trans - Translate from 1 language to another
  413.         if (arg[0].equalsIgnoreCase(prefix + "trans")){
  414.             String text = null;
  415.             YandexTranslatorAPI.setKey("trnsl.1.1.20150924T011621Z.e06050bb431b7175.e5452b78ee8d11e4b736035e5f99f2831a57d0e2");
  416.             try {
  417.                 text = Translate.execute(arg[3], Detect.execute(arg[1]), Detect.execute(arg[2]));
  418.             } catch (Exception e) {
  419.                 sendError(channel, e);
  420.                 log(e + "");
  421.             }
  422.             sendMessage(channel, text);
  423.         }
  424.  
  425. // !debugvar - changes a variable to the value
  426.         if (arg[0].equalsIgnoreCase(prefix + "debugvar")){
  427.             if(checkPerm(sender, hostname, login)){
  428.                 switch(arg[1]){
  429.                 case "i":   i = Integer.parseInt(arg[2]);
  430.                             sendMessage(channel, "DEBUG: Var \"i\" is now \"" + i + "\"");
  431.                 }
  432.             }
  433.             else {
  434.                 permErrorchn(channel, sender, "can use this command");
  435.             }
  436.         }
  437.  
  438. // !runcmd - Tells the bot to say run a OS command
  439.         if (arg[0].equalsIgnoreCase(prefix + "runcmd")){
  440.             if(checkPerm(sender, hostname, login)){
  441.                 String console;
  442.                 if(arg[1].equalsIgnoreCase("cmd") || arg[1].equalsIgnoreCase("command"))
  443.                     console = "cmd.exe";
  444.                 else if(arg[1].equalsIgnoreCase("term") || arg[1].equalsIgnoreCase("terminal"))
  445.                     console = "mintty.exe";
  446.                 else
  447.                     console = arg[1];
  448.  
  449.                 ProcessBuilder builder = new ProcessBuilder(console);
  450.                 Process p=null;
  451.                 try {
  452.                     p = builder.start();
  453.                 }
  454.                 catch (IOException e) {
  455.                     sendError(channel, e);
  456.                 }
  457.                 //get stdin of shell
  458.                 BufferedWriter p_stdin = null;
  459.                 try {
  460.                     p_stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
  461.                 } catch (NullPointerException e) {
  462.                     sendMessage(channel, "error: " + e);
  463.                 }
  464.  
  465.                 try {
  466.                     //single execution
  467.                     try {
  468.                         p_stdin.write(arg[2]);
  469.                         p_stdin.newLine();
  470.                         p_stdin.flush();
  471.                     } catch (NullPointerException e) {
  472.                         sendError(channel, e);
  473.                     }
  474.  
  475.                 }
  476.                 catch (IOException e) {
  477.                     sendError(channel, e);
  478.                 }
  479.                 // finally close the shell by execution exit command
  480.                 try {
  481.                     p_stdin.write("exit");
  482.                     p_stdin.newLine();
  483.                     p_stdin.flush();
  484.                 }
  485.                 catch (IOException e) {
  486.                     sendError(channel, e);
  487.                 }
  488.  
  489.                 // write stdout of shell (=output of all commands)
  490.                 Scanner s = null;
  491.                 try {
  492.                     s = new Scanner(p.getInputStream());
  493.                 } catch (Exception e) {
  494.                     sendMessage(channel, "error: " + e);
  495.                 }
  496.                 String output;
  497.                 int loop = i;
  498.                 if(console.equals("cmd.exe")){
  499.                     while(loop>0){
  500.                         try {
  501.                             s.nextLine();
  502.                         } catch (Exception e) {
  503.                             sendMessage(channel, "error: " + e);
  504.                         }
  505.                         loop--;
  506.                     }
  507.                 }
  508.                 while (s.hasNextLine())
  509.                 {
  510.                     output = s.nextLine();
  511.                     if(!output.contains("C:\\Users\\ggonz\\workspace\\MyBot")
  512.                             && !output.equals("Microsoft Windows [Version 10.0.10240]")
  513.                             && !output.equals("(c) 2015 Microsoft Corporation. All rights reserved.")
  514.                             && !output.contains("Directory of")
  515.                             && !output.contains("<DIR>          .")
  516.                             && !output.contains("bytes free")
  517.                             && !output.contains("Volume in drive C is OS")
  518.                             && !output.contains("Volume Serial Number is")
  519.                             && !output.contains(">exit")){
  520.                         sendMessage(channel, output);
  521.                     }
  522.                 }
  523.                 s.close();
  524.             }
  525.             else {
  526.                 permError(sender, "can use this command");
  527.             }
  528.         }
  529.  
  530. // !SayRaw - Tells the bot to send a raw line
  531.         if (arg[0].equalsIgnoreCase(prefix + "SayRaw")){
  532.             if(checkPerm(sender, hostname, login)){
  533.                 sendRawLineViaQueue(arg[1]);
  534.             }
  535.             else {
  536.                 permErrorchn(channel, sender, "can use this command");
  537.             }
  538.         }
  539.  
  540. // !SayNotice - Tells the bot to send a notice
  541.         if (arg[0].equalsIgnoreCase(prefix + "SayNotice")){
  542.             if(checkPerm(sender, hostname, login)){
  543.                 sendNotice(arg[1], arg[2]);
  544.             }
  545.             else {
  546.                 permErrorchn(channel, sender, "can use this command");
  547.             }
  548.         }
  549.  
  550. // !SayCTCPCommand - Tells the bot to send a CTCP Command
  551.         if (arg[0].equalsIgnoreCase(prefix + "SayCTCPCommand")){
  552.             if(checkPerm(sender, hostname, login)){
  553.                 sendCTCPCommand(arg[2], arg[1]);
  554.             }
  555.             else {
  556.                 permErrorchn(channel, sender, "can use this command");
  557.             }
  558.         }
  559.  
  560. //// !SayMethod - Tells the bot to run a method
  561. //      if (arguments[0].equalsIgnoreCase(prefix + "sayMethod")){
  562. //          if(checkPerm(sender, hostname, login)){
  563. //              sendRawLineViaQueue(arguments[1]);
  564. //          }
  565. //          else {
  566. //              permErrorchn(channel, sender, "can use this command");
  567. //          }
  568. //      }
  569.  
  570. // !leave - Tells the bot to leave the current channel
  571.         if (arg[0].equalsIgnoreCase(prefix + "leave")){
  572.             if(checkPerm(sender, hostname, login)){
  573.                 partChannel(channel, "Successfully parted from " + channel);
  574.             }
  575.             else {
  576.                 permErrorchn(channel, sender, "can use this command");
  577.             }
  578.         }
  579.  
  580. // !changenick - Changes the nick of the bot
  581.         if (arg[0].equalsIgnoreCase(prefix + "changeNick")){
  582.             if(checkPerm(sender, hostname, login)){
  583.                 changeNick(arg[1]);
  584.                 //sendMessage(channel, "Succsefully changed nick to " + arguments[1]);
  585.             }
  586.             else{
  587.                 permErrorchn(channel, sender , "can change the Nick of the bot");
  588.             }
  589.         }
  590.  
  591. // !SayAction - Makes the bot do a action
  592.         if (arg[0].equalsIgnoreCase(prefix + "SayAction")){
  593.             if(checkPerm(sender, hostname, login)){
  594.                 sendAction(channel, arg[1]);
  595.             }
  596.             else{
  597.                 permErrorchn(channel, sender , "can make the bot do actions");
  598.             }
  599.         }
  600.  
  601. // !QuitServ - Tells the bot to disconnect from server
  602.         if (message.contentEquals(prefix + "quitserv")){
  603.             if(checkPerm(sender, hostname, login)){
  604.                 sendNotice(sender, "Disconnecting from server");
  605.                 disconnect();
  606.                 System.exit(0);
  607.             }
  608.             else{
  609.                 permErrorchn(channel, sender , "can make the bot do actions");
  610.             }
  611.         }
  612.  
  613. // !Spin has all of the Spin related sub commands
  614.         if (arg[0].equalsIgnoreCase(prefix + "spin")){
  615.  
  616.             if (message.equalsIgnoreCase(prefix + "spin")){
  617.                 //sendMessage(channel,"DEBUG : Spin");
  618.             }
  619.  
  620.             if (arg[1].equalsIgnoreCase("join")){
  621.                 if(playerList.contains(sender)){
  622.                     sendMessage(channel, sender + " :You are already in Spin.");
  623.                 }
  624.                 else{
  625.                     playerList.add(sender);
  626.                     sendMessage(channel, sender + " has joined spin");
  627.                 }
  628.             }
  629.  
  630.             if (arg[1].equalsIgnoreCase("list")){
  631.                 sendMessage(channel,"Spin list is \"" + playerList.toString() + "\"");
  632.             }
  633.  
  634.             if (arg[1].equalsIgnoreCase("clear")){
  635.                 playerList.clear();
  636.                 sendMessage(channel,"The Spin list has been cleared!");
  637.             }
  638.  
  639.             if (arg[1].equalsIgnoreCase("help")){
  640.                 sendMessage(channel,"Spin is pretty much Russian Roulette. just join spin by typing in " + prefix + "spin join");
  641.             }
  642.  
  643.             if (arg[1].equalsIgnoreCase("test")){
  644.                 sendMessage(channel,"DEBUG : Spin test");
  645.             }
  646.  
  647.         }
  648.  
  649.  
  650.  
  651. // !admin - Does admin commands
  652.         if(arg[0].equalsIgnoreCase(prefix + "admin")){
  653.             if(checkPerm(sender, hostname, login)){
  654.                 switch(arg[1].toLowerCase()){
  655.                     case "kick":
  656.                         if(arg.length <= 3){
  657.                         kick(channel, arg[2]);
  658.                     }
  659.                         else if(arg.length >= 3){
  660.                         kick(channel, arg[2], arg[3]);
  661.                     }
  662.                         break;
  663.                     case "op":
  664.                         op(channel, arg[2]);
  665.                         break;
  666.                     case "voice":
  667.                         voice(channel, arg[2]);
  668.                         break;
  669.                     case "deop":
  670.                         deOp(channel, arg[2]);
  671.                         break;
  672.                     case "devoice":
  673.                         deVoice(channel, arg[2]);
  674.                         break;
  675.                 }
  676.             }
  677.             else{
  678.                 permErrorchn(channel, sender, "can do admin commands with the bot");
  679.             }
  680.         }
  681.  
  682. // !JokeCommands - toggle joke commands
  683.         if (arg[0].equalsIgnoreCase(prefix + "JokeCommands")){
  684.             if (message.equalsIgnoreCase(prefix + "JokeCommands")){
  685.                 if (jokeCommands){
  686.                     sendMessage(channel, "Joke commands are currently enabled");
  687.                 }
  688.                 else{
  689.                     sendMessage(channel, "Joke commands are currently disabled");
  690.                 }
  691.             }
  692.             if(arg[1].equalsIgnoreCase("toggle")){
  693.                 if(checkPerm(sender, hostname, login)){
  694.                     jokeCommands = !jokeCommands;
  695.                     if (jokeCommands){
  696.                         sendMessage(channel, "Joke commands are now enabled");
  697.                     }
  698.                     else{
  699.                         sendMessage(channel, "Joke commands are now disabled");
  700.                     }
  701.                 }
  702.                 else{
  703.                     permErrorchn(channel, sender , "can enable or disable the use of joke commands");
  704.                 }
  705.             }
  706.         }
  707.  
  708.  
  709. // !Splatoon - Joke command - ask the splatoon question
  710.         if (message.equalsIgnoreCase(prefix + "Splatoon")){
  711.             if(jokeCommands)
  712.                 sendMessage(channel, sender + ": YOU'RE A KID YOU'RE A SQUID");
  713.             else
  714.                 sendMessage(channel, sender + ": Sorry, Joke commands are disabled");
  715.         }
  716.  
  717. // !attempt - Joke command - NOT ATTEMPTED
  718.         if (message.equalsIgnoreCase(prefix + "attempt")){
  719.             if(jokeCommands)
  720.                 sendMessage(channel, sender + ": NOT ATTEMPTED");
  721.             else
  722.                 sendMessage(channel, sender + ": Sorry, Joke commands are disabled");
  723.         }
  724.  
  725. //// !stfu - Joke command - say "no u"
  726. //      if (message.equalsIgnoreCase(prefix + "stfu")){
  727. //          if(jokeCommands)
  728. //              sendMessage(channel, sender + ": " + prefix + "no u");
  729. //      }
  730.  
  731. // !eatabowlofdicks - Joke command - joke help command
  732.         if (message.equalsIgnoreCase(prefix + "eatabowlofdicks")){
  733.             if(jokeCommands)
  734.                 sendMessage(channel, sender + ": " + "no u");
  735.         }
  736.  
  737. // !potato - Joke command - say "i am potato" in Japanese
  738.         if (message.equalsIgnoreCase(prefix + "potato")){
  739.             if(jokeCommands){
  740.                 byte[] bytes = "わたしわポタトデス".getBytes( Charset.forName("UTF-8"));
  741.                 String v = new String( bytes, Charset.forName("UTF-8") );
  742.                 sendMessage(channel, v);
  743.             }
  744.             else
  745.                 sendMessage(channel, sender + ": Sorry, Joke commands are disabled");
  746.         }
  747.  
  748. // !whatis? - Joke command -
  749.         if (arg[0].equalsIgnoreCase(prefix + "whatis?")){
  750.             if(jokeCommands){
  751.                 int num = randInt(0, dictionary.length - 1);
  752.                 String comeback = String.format(dictionary[num], arg[1]);
  753.                 sendMessage(channel, sender + comeback);
  754.             }
  755.             else
  756.                 sendMessage(channel, sender + ": Sorry, Joke commands are disabled");
  757.         }
  758. // !getFinger - gets the Finger of the bot
  759.         if (message.equalsIgnoreCase(prefix + "getFinger")){
  760.             if(jokeCommands){
  761.                 sendMessage(channel, getFinger());
  762.             }
  763.             else
  764.                 sendMessage(channel, sender + ": Sorry, Joke commands are disabled");
  765.         }
  766.     }
  767.  
  768.     public void onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice){
  769.         List<String> argLog = splitMessage(notice);
  770.         log(argLog.toString());
  771.         String[] arg = argLog.toArray(new String[argLog.size()]);
  772.  
  773.         if(target.equals(getNick()))
  774.             sendNotice(currentNick, "Notice from " + sourceNick + " in " + sourceNick + ". Notice was :" + notice);
  775.     }
  776.  
  777. //  public void onJoin(String channel, String sender, String login, String hostname){
  778. //      sendNotice(sender, "\u0001AVATAR http://puu.sh/kA75A.jpg 19117\u0001");
  779. //  }
  780.  
  781.     public void onPrivateMessage(String sender, String login, String hostname, String message)
  782.     {
  783.         List<String> argLog = splitMessage(message);
  784.         log(argLog.toString());
  785.         String[] arg = argLog.toArray(new String[argLog.size()]);
  786.  
  787.         if (message.equals("\u0001AVATAR\u0001")){
  788.             sendNotice(sender, "\u0001AVATAR http://puu.sh/kA75A.jpg 19117\u0001");
  789.         }
  790.         if (message.equals("\u0001")){
  791.             sendNotice(sender, "\u0001AVATAR http://puu.sh/kA75A.jpg 19117\u0001");
  792.         }
  793.  
  794. // !login - Sets the authed named to the new name ...if the password is right
  795.         if (arg[0].equalsIgnoreCase(prefix + "login")){
  796.             if(arg[1].equals(PASSWORD)){
  797.                 currentNick = sender;
  798.                 currentUsername = login;
  799.  
  800.             }
  801.             else
  802.                 sendMessage(sender, "password is incorrect.");
  803.         }
  804.     //Only allow me (Lil-G) to use PM commands except for the login command
  805.         else if(checkPerm(sender, hostname, login) && !arg[0].equalsIgnoreCase(prefix + "login") && arg[0].contains(prefix)){
  806.  
  807.  
  808. // !SendTo - Tells the bot to say someting on a channel
  809.             if (arg[0].equalsIgnoreCase(prefix + "sendto")){
  810.                     sendMessage(arg[1], arg[2]);
  811.             }
  812.  
  813. // !SayAction - Tells the bot to make a action on a channel
  814.             if (arg[0].equalsIgnoreCase(prefix + "SayAction")){
  815.                 sendAction(arg[1], arg[2]);
  816.             }
  817.  
  818. // !changenick- Changes the nick of the bot
  819.             if (arg[0].equalsIgnoreCase(prefix + "changeNick")){
  820.                 changeNick(arg[1]);
  821.                 sendNotice(sender, "Succsefully changed nick to " + arg[1]);
  822.             }
  823.  
  824.  
  825. // !Connect - Tells the bot to connect to specified channel
  826.             if (arg[0].equalsIgnoreCase(prefix + "connect")){
  827.                 joinChannel(arg[1]);
  828.                 sendNotice(sender, "Succsefully connected to " + arg[1]);
  829.             }
  830.  
  831. // !Exit - Tells the bot to disconnect from specified channel
  832.             if (arg[0].equalsIgnoreCase(prefix + "exit")){
  833.                 partChannel(arg[1], "Successfully parted from " + arg[1]);
  834.             }
  835.  
  836. // !QuitServ - Tells the bot to disconnect from server
  837.             if (message.equalsIgnoreCase(prefix + "quitserv")){
  838.                 sendNotice(sender, "Disconnecting from server");
  839.                 disconnect();
  840.                 System.exit(0);
  841.             }
  842.         }
  843.         else if(arg[0].startsWith(prefix)) {
  844.             permError(sender, "can use PM commands");
  845.             sendMessage(currentNick, "Attempted use of PM commands by " + sender + ". The command used was \"" + message + "\"");
  846.         }
  847.     }
  848.  
  849.     public void onOp(String channel, String sourceNick, String sourceLogin, String sourceHostname, String recipient)
  850.     {
  851.         if(recipient.equals(getNick()))
  852.             sendNotice(currentNick, "i have been given OP from " + sourceNick + " in " + channel);
  853.     }
  854.  
  855.     public void onVoice(String channel, String sourceNick, String sourceLogin, String sourceHostname, String recipient)
  856.     {
  857.         if(recipient.equals(getNick()))
  858.             sendNotice(currentNick, "i have been given Voice from " + sourceNick + " in " + channel);
  859.     }
  860.  
  861.     public void onPing(String channel, String sourceNick, String sourceLogin, String sourceHostname, String recipient)
  862.     {
  863.         if(recipient.equals(getNick()))
  864.             sendNotice(currentNick, "Ping from " + sourceNick + " in " + channel);
  865.     }
  866.  
  867.     public void onDisconnect(){
  868.         while (!isConnected()) {
  869.             try {
  870.                 TimeUnit.SECONDS.sleep(3);
  871.                 reconnect();
  872.             }
  873.             catch (Exception e) {
  874.                 // Couldn't reconnect!
  875.                 // Pause for a short while...?
  876.             }
  877.         }
  878.     }
  879.     public void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason){
  880.         if (recipientNick.equalsIgnoreCase(getNick())) {
  881.             try {
  882.                 TimeUnit.SECONDS.sleep(3);
  883.             } catch (InterruptedException e) {
  884.                 System.out.print(e);
  885.             }
  886.             joinChannel(channel);
  887.         }
  888.     }
  889.  
  890.     public void onNickChange(String oldNick, String login, String hostname, String newNick){
  891.         if(newNick.equalsIgnoreCase(currentNick)){
  892.             currentNick = null;
  893.         }
  894.  
  895.         if(oldNick.equalsIgnoreCase(currentNick)){
  896.             currentNick = newNick;
  897.         }
  898.     }
  899.  
  900.     /**
  901.      * Returns a pseudo-random number between min and max, inclusive.
  902.      * The difference between min and max can be at most
  903.      * <code>Integer.MAX_VALUE - 1</code>.
  904.      *
  905.      * @param min Minimum value
  906.      * @param max Maximum value.  Must be greater than min.
  907.      * @return Integer between min and max, inclusive.
  908.      * @see java.util.Random#nextInt(int)
  909.      */
  910.     public static int randInt(int min, int max) {
  911.  
  912.         // NOTE: Usually this should be a field rather than a method
  913.         // variable so that it is not re-seeded every call.
  914.         Random rand = new Random();
  915.  
  916.         // nextInt is normally exclusive of the top value,
  917.         // so add 1 to make it inclusive
  918.  
  919.         return rand.nextInt((max - min) + 1) + min;
  920.     }
  921.  
  922.     /**
  923.      * tells the user they don't have permission to use the command
  924.      *
  925.      * @param sender User trying to use command
  926.      * @param e String to send back
  927.      */
  928.     public void permError(String sender, String e){
  929.         sendNotice(sender, ": Sorry, only Lil-G " + e);
  930.     }
  931.  
  932.     /**
  933.      * same as permError() except to be used in channels
  934.      *
  935.      * @param channel Channel that the user used the command in
  936.      * @param sender User trying to use command
  937.      * @param e String to send back
  938.      */
  939.     public void permErrorchn(String channel, String sender, String e){ sendMessage(channel, sender + ": Sorry, only Lil-G " + e); }
  940.  
  941.     /**
  942.      * Checks if the user attempting to use the command is allowed
  943.      *
  944.      * @param sender User trying to use command
  945.      * @param hostname hostname of user trying to use command
  946.      * @return Boolean true if allowed, false if not
  947.      */
  948.     public boolean checkPerm(String sender, String hostname, String login){
  949.         log("attempted use of command by "+ sender + "!" + login + "@" + hostname);
  950.         if(sender.equalsIgnoreCase(currentNick) &&  login.equalsIgnoreCase(currentUsername)){
  951.  
  952.             return true;
  953.         }
  954.         else{
  955.             log("user " + sender + " was denied");
  956.             return false;
  957.  
  958.         }
  959.     }
  960.  
  961.     private List<String> splitMessage(String s) {
  962.         if (s == null)
  963.             return Collections.emptyList();
  964.  
  965.         List<String> list = new ArrayList<String>();
  966.         Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(s);
  967.         while (m.find())
  968.             list.add(m.group(1)); // Add .replace("\"", "") to remove surrounding quotes.
  969.         int i = 0;
  970.         while(list.size() > i){
  971.             list.set(i, list.get(i).replaceAll("\"", ""));
  972.             i++;
  973.         }
  974.         return list;
  975.     }
  976.  
  977.     public static String getBytes(String bytes){
  978.         byte[] Bytes = bytes.getBytes();
  979.         return Arrays.toString(Bytes);
  980.     }
  981.  
  982.     public static String formatFileSize(long size) {
  983.         String hrSize = null;
  984.  
  985.         double b = size;
  986.         double k = size/1024.0;
  987.         double m = ((size/1024.0)/1024.0);
  988.         double g = (((size/1024.0)/1024.0)/1024.0);
  989.         double t = ((((size/1024.0)/1024.0)/1024.0)/1024.0);
  990.  
  991.         DecimalFormat dec = new DecimalFormat("0.00");
  992.  
  993.         if ( t>1 ) {
  994.             hrSize = dec.format(t).concat(" TB");
  995.         } else if ( g>1 ) {
  996.             hrSize = dec.format(g).concat(" GB");
  997.         } else if ( m>1 ) {
  998.             hrSize = dec.format(m).concat(" MB");
  999.         } else if ( k>1 ) {
  1000.             hrSize = dec.format(k).concat(" KB");
  1001.         } else {
  1002.             hrSize = dec.format(b).concat(" Bytes");
  1003.         }
  1004.  
  1005.         return hrSize;
  1006.     }
  1007.  
  1008.     public String botTalk(String bot, String message) throws Exception{
  1009.         if(bot.equalsIgnoreCase("clever")){
  1010.             return cleverBotsession.think(message);
  1011.         }
  1012.         else if(bot.equalsIgnoreCase("pandora")){
  1013.             return pandoraBotsession.think(message);
  1014.         }
  1015.         else{
  1016.             return "Error, not a valid bot";
  1017.         }
  1018.     }
  1019.  
  1020.     public int findIn(String[] list, String nick){
  1021.         int index = 0;
  1022.         int max = list.length;
  1023.         boolean found = false;
  1024.         while(!found && index == max){
  1025.             index++;
  1026.             if(list[index].equalsIgnoreCase(nick)){
  1027.                 found = true;
  1028.             }
  1029.         }
  1030.         return index;
  1031.     }
  1032.  
  1033.     public int findIn(User[] list, String nick){
  1034.         int index = 0;
  1035.         int max = list.length;
  1036.         boolean found = false;
  1037.         while(!found && index == max){
  1038.             index++;
  1039.             if(list[index].equals(nick.toLowerCase())){
  1040.                 found = true;
  1041.             }
  1042.         }
  1043.         return index;
  1044.     }
  1045.  
  1046.     public void log(String line) {
  1047.         if (getVerbose()) {
  1048.             System.out.println(System.currentTimeMillis() + " " + line);
  1049.         }
  1050.         if (line.startsWith("@## ")){
  1051.             String str = line.substring(4);
  1052.             if (!(str.equalsIgnoreCase("java.util.NoSuchElementException") || str.equalsIgnoreCase("\tat java.util.StringTokenizer.nextToken(StringTokenizer.java:349)") || str.equalsIgnoreCase("\tat org.jibble.pircbot.PircBot.handleLine(PircBot.java:891)") || str.equalsIgnoreCase("\tat org.jibble.pircbot.InputThread.run(InputThread.java:92)"))) {
  1053.                 sendNotice(currentNick, str);
  1054.             }
  1055.         }
  1056.     }
  1057.  
  1058.     public void sendError(String Channel, Exception e){
  1059.         sendMessage(Channel, "Error: " + e);
  1060.     }
  1061.  
  1062.     public void setDebugMode(boolean debugMode) {
  1063.         this.debugMode = debugMode;
  1064.     }
  1065.  
  1066. }
Add Comment
Please, Sign In to add comment