Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.38 KB | None | 0 0
  1. package AoSBot;
  2.  
  3. import java.util.*;
  4. import org.jibble.pircbot.*;
  5.  
  6. public class MyBot extends PircBot {
  7.     /*
  8.     public String name;
  9.     public String time;
  10.     public String address;
  11.     public String players;
  12.     public Boolean on = false;
  13.      */
  14.         private HashMap<String,String> triggers = new HashMap<String,String>();
  15.         private HashMap<String,String> muted = new HashMap<String,String>();
  16.         private HashMap<String,String> users = new HashMap<String,String>();
  17.         private Set<String> mutedKeys = new HashSet<String>(muted.keySet());
  18.         private Set<String> triggersKeys = new HashSet<String>(triggers.keySet());
  19.         private String msgsaftermute = "2";
  20.         private String currentUSERHOST = "";
  21.         boolean finished = false;
  22.         //private boolean awaitingServerResponse = true;
  23.         private String channel = "#AoS";
  24.    
  25.     public MyBot(){
  26.         this.setName("AoS-MuteBot");
  27.         this.setAutoNickChange(true);
  28.     }
  29. /*
  30.     public void add(String name){
  31.         this.players = this.players+", " + name;
  32.     }
  33.  
  34.     public String show(){
  35.     return name + " at " + time + " server: " + address;
  36.     }
  37. */
  38.     @Override
  39.     public void onJoin(String channel, String sender, String login, String hostname){
  40.         /*if(!(users.containsKey(hostname))){
  41.             users.put(hostname, sender);
  42.         } else {
  43.  
  44.         }*/
  45.         /*String result;
  46.         User users[] = getUsers(channel);
  47.         for(int i=0; i<users.length; i++){
  48.             if(users[i].getNick().equals(sender)){
  49.                 users[i].getHost();
  50.             }
  51.         }*/
  52.         //sendMessage(channel, "Hostname: "+hostname);
  53.         currentUSERHOST = "";
  54.         System.out.println("current"+currentUSERHOST);
  55.         sendRawLine("userhost "+sender);
  56.         System.out.println("current"+currentUSERHOST);
  57.         System.out.println("SENT RAW LINE");
  58.         System.out.println("current"+currentUSERHOST);
  59.         while(!finished){ /* wait */}
  60.         System.out.println("FINISHED WAITING");
  61.         currentUSERHOST = "";
  62.         System.out.println("current"+currentUSERHOST);
  63.        
  64.         System.out.println("RESET");
  65.  
  66.  
  67.         //Check if person joined is muted, and if so, issue a warning
  68.         for (String key: muted.keySet()) {
  69.             if(sender.toUpperCase().equals(key)){
  70.                 sendNotice(sender, "You are muted on this channel.");
  71.             }
  72.         }
  73.     }
  74.     @Override
  75.     public void onServerResponse(int code, String response){
  76.         if(response.lastIndexOf("@") != -1){
  77.             //presume a userhost
  78.             System.out.println("Response: "+response);
  79.             currentUSERHOST = response;
  80.             finished = true;
  81.         }
  82.     }
  83.  
  84.     public String getHost(String nickname){
  85.         String result = "";
  86.         System.out.println("current: "+currentUSERHOST);
  87.         System.out.println("SENT RAW LINE (getHost)");
  88.         sendRawLine("userhost "+nickname);
  89.        
  90.         int i = 0;
  91.         while(currentUSERHOST.equals("")){ i++; System.out.println(i); }
  92.         System.out.println("FINISHED WAITING (getHost)");
  93.         String temp[] = currentUSERHOST.split("@");
  94.         if(temp.length == 2) {
  95.             result = temp[1];
  96.         }
  97.         System.out.println("temp1: "+temp[1]);
  98.         currentUSERHOST = "";
  99.         System.out.println("RESET (getHost)");
  100.         return result;
  101.     }
  102.  
  103.     @Override
  104.     public void onNickChange(String oldNick, String login, String hostname, String newNick){
  105.         //Check if person joined is muted, and if so, issue a warning
  106.         for (String key: muted.keySet()) {
  107.             if(oldNick.toUpperCase().equals(key)){
  108.                 addToMuted(newNick.toUpperCase(), "1");
  109.                 sendNotice(newNick, "For changing names you cannot speak again without a kick.");
  110.             }
  111.         }
  112.     }
  113.    
  114.     @Override
  115.     public void onPrivateMessage(String sender, String login, String hostname, String message){
  116.         if(isOperator(channel, getUserIndex(sender, channel))){
  117.             //msg bot <command> <trigger> <(line)>
  118.  
  119.             //Addtrig command, should have 3 parts
  120.             if(message.startsWith("addtrigger") || message.startsWith("addtrig")){
  121.                 String temp[] = message.split(" ", 3);
  122.                 if (temp.length == 3){
  123.                     String trig = temp[1];
  124.                     String msg = temp[2];
  125.  
  126.                     String currenttrig = triggers.get(trig);
  127.                     if(currenttrig == null){
  128.                         //there is no trigger set under this name
  129.                         addToTriggers(trig, msg);
  130.                         sendNotice(sender, "Added the trigger \""+trig+"\" -> "+msg);
  131.                     } else {
  132.                         //there is a trigger set for this name, so add a \n and add it on
  133.                         String val = currenttrig + "\n" + msg;
  134.                         addToTriggers(trig, val);
  135.                         sendNotice(sender, "Trigger already exists. Added new line to \""+trig+"\":\""+msg);
  136.                     }
  137.                 } else {
  138.                     sendNotice(sender, "Improper command, please retry");
  139.                 }
  140.             }
  141.  
  142.             //Modify command
  143.             if(message.startsWith("modifytrigger") || message.startsWith("modtrig")){
  144.                 //modtrig !trig edit 1 blah
  145.                 String temp[] = message.split(" ");
  146.                 if(temp.length == 5){
  147.                     String value = "";
  148.                     for (String key: triggers.keySet()) {
  149.                         if(temp[1].equals(key)){
  150.                             value = triggers.get(key);
  151.                             String linevals[] = value.split("\n");
  152.                             int line = Integer.parseInt(temp[3]);
  153.  
  154.                             if(temp[2].equals("add")){
  155.                                 if(line == 1){
  156.                                     value = temp[4] + "\n" + value;
  157.                                 }
  158.                             }
  159.                             if(linevals.length == 2){
  160.  
  161.                             }
  162.                         }
  163.                     }
  164.                     if(value.equals("")) {
  165.                         sendNotice(sender, "Trigger not found");
  166.                     }
  167.                 }else{
  168.                     sendNotice(sender, "Wrong number of parameters: modtrig <trigger> <add/edit/delete> <line number> <msg>");
  169.                 }
  170.  
  171.             }
  172.  
  173.             //Delete command
  174.             if(message.startsWith("deletetrigger") || message.startsWith("deltrig")){
  175.                 //deletetrigger !trigger
  176.                 String temp[] = message.split(" ");
  177.                 String trig = temp[1];
  178.                 String currenttrig = triggers.get(trig);
  179.                 if(currenttrig == null){
  180.                     sendNotice(sender, "There is no trigger called \""+trig+"\"..");
  181.                 } else {
  182.                     triggers.remove(trig);
  183.                     sendNotice(sender, "Success. \""+trig+"\" has been removed.");
  184.                 }
  185.             }
  186.  
  187.             //change num messages before kicking
  188.             if(message.startsWith("mutelimit")){
  189.                 String temp[] = message.split(" ");
  190.                 if(temp.length != 2){
  191.                     sendNotice(sender, "Improper command, please retry");
  192.                 } else {
  193.                     setMsgsAfterMute(temp[1]);
  194.                     sendNotice(sender, "Success. # of messages before kicking when muted is now "+temp[1]);
  195.                 }
  196.             }
  197.         } else {
  198.             sendNotice(sender, "Sorry, you do not have necessary rights to perform said action.");
  199.         }
  200.     }
  201.  
  202.     @Override
  203.     public void onMessage(String channel, String sender, String login, String hostname, String message) {
  204.         if(message.startsWith("!host")){
  205.             String temp[] = message.split(" ", 2);
  206.             sendMessage(channel, getHost(temp[1]));
  207.         }
  208.  
  209.         //Checks if the message has a trigger in it
  210.         for (String key: triggers.keySet()) {
  211.             if(message.startsWith(key)){
  212.                 System.out.println(triggers.get(key));
  213.                 String value = triggers.get(key);
  214.                 String temp[] = value.split("\n");
  215.  
  216.                 //for multi-line triggers, cycle through
  217.                 for (int i = 0; i<temp.length; i++){
  218.                     sendMessage(channel, temp[i]);
  219.                 }
  220.             }
  221.         }
  222.  
  223.         //testing
  224.         if(message.startsWith("!triggers")){
  225.             sendMessage(channel, "Displaying triggers in console.");
  226.             System.out.println(triggers);
  227.         }
  228.         //Kick command
  229.         if (message.startsWith("!kick")){
  230.             String temp[] = message.split(" ", 3);
  231.             String reason = "Requested by " + sender;
  232.             if(temp.length == 3){
  233.                 if(!(temp[2].equals(""))){
  234.                     reason += ", for reason(s):"+temp[2];
  235.                 }
  236.             }
  237.             int userindex = getUserIndex(sender, channel);
  238.             if(userindex == -1){
  239.                 sendNotice(sender, "Unable to find user "+temp[1]+" on this channel.");
  240.             } else {
  241.                 if(isOperator(channel, userindex)){
  242.                     kick(channel, temp[1], reason);
  243.                 } else {
  244.                     sendNotice(sender, "Sorry, you do not have necessary rights to perform said action.");
  245.                 }
  246.             }
  247.         }
  248.  
  249.         //Mute command
  250.         if (message.startsWith("!mute")){
  251.             //!mute <username> <reason>
  252.             String temp[] = message.split(" ", 3);
  253.             String reason = "Requested by " + sender;
  254.             if(temp.length == 3){
  255.                 if(!(temp[2].equals(""))){
  256.                     reason += ", for reason(s):"+temp[2];
  257.                 }
  258.             }
  259.             int userindex = getUserIndex(sender, channel);
  260.             //user index will be returned as -1 if not found, so:
  261.             if(userindex == -1){
  262.                 sendNotice(sender, "Unable to find user "+temp[1]+" on this channel.");
  263.             } else {
  264.                 if(isOperator(channel, userindex)){
  265.                     sendNotice(temp[1], "You have been muted. "+reason+". This means you are limited in the number of lines you can say before being kicked, so keep quiet.");
  266.                     sendMessage(channel, temp[1]+" has been muted by "+sender);
  267.                     addToMuted(temp[1].toUpperCase(), msgsaftermute);
  268.                 } else {
  269.                     sendNotice(sender, "Sorry, you do not have necessary rights to perform said action.");
  270.                 }
  271.             }
  272.         }
  273.  
  274.         //Unmute command
  275.         if (message.startsWith("!unmute")){
  276.             String temp[] = message.split(" ", 3);
  277.             removeFromMuted(temp[1].toUpperCase());
  278.             sendNotice(temp[1], "You have been unmuted on #AoS.");
  279.         }
  280.  
  281.         //See if user sending message is muted
  282.         checkMuting(hostname, channel, sender);
  283.     }
  284.        
  285.     public void checkMuting(String host, String channel, String sender){
  286.         System.out.println(mutedKeys);
  287.         int i = 0,j = 0;
  288.         for (String key: muted.keySet()) {
  289.             System.out.println("k: "+i);
  290.             i++;
  291.             System.out.println("Sender: "+sender+"/"+host+", Key: "+key);
  292.             String checksender = sender.toUpperCase();
  293.             if(checksender.equals(key)){
  294.                 int left = Integer.parseInt(muted.get(key));
  295.                 System.out.println("Left: "+left);
  296.                 left--;
  297.                 //Decrement amount of lines before kick in the hashmap
  298.                 addToMuted(key, Integer.toString(left));
  299.                 if(left < 1){
  300.                     //Message & kick
  301.                     sendNotice(sender, "Goodbye, "+sender+".");
  302.                     kick(channel, sender, "For failure to follow the rules and keeping your mouth shut");
  303.                 }else{
  304.                     //Warn
  305.                     sendMessage(channel, sender+": "+Integer.toString(left)+" left.");
  306.                     sendNotice(sender, "You have been muted. If you do not stop talking, you shall be kicked after "+left+" line(s)");
  307.                 }
  308.             }
  309.         }
  310.     }
  311.    
  312.     boolean isOperator(String channel, int userindex){
  313.         User users[] = getUsers(channel);
  314.         if(users[userindex].isOp()){
  315.             return true;
  316.         }
  317.         return false;
  318.     }
  319.     boolean isVoiced(String user, String channel){
  320.         User users[] = getUsers(channel);
  321.         if(users[getUserIndex(user, channel)].hasVoice()){
  322.             return true;
  323.         }
  324.         return false;
  325.     }
  326.     int getUserIndex(String user, String channel){
  327.         int result = -1;
  328.         User users[] = getUsers(channel);
  329.         for(int i=0; i<users.length; i++){
  330.             if(users[i].getNick().equals(user)){
  331.                 result = i;
  332.             }
  333.         }
  334.         return result;
  335.     }
  336.  
  337.  
  338.    
  339.     public void addToTriggers(String key, String value){
  340.         triggers.put(key, value);
  341.     }
  342.     public void removeFromTriggers(String key){
  343.         triggers.remove(key);
  344.     }
  345.     public void addToMuted(String key, String value){
  346.         muted.put(key, value);
  347.     }
  348.     public void removeFromMuted(String key){
  349.         muted.remove(key);
  350.     }
  351.     public void setMsgsAfterMute(String num) {
  352.         msgsaftermute = num;
  353.     }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement