0rx

xcmd.py (cellinux-bot additional)

0rx
Jun 9th, 2016
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.60 KB | None | 0 0
  1. ###########################################################################
  2. ## File        :    xcmd.py                                              ##
  3. ## Descriptions:                                                         ##
  4. ##      This is module is needed in addition to run cellinux-bot-main.py ##
  5. ## This modul is a modul to contain external commands for the main       ##
  6. ## program. -Why would you want to put all the bot's command in external ##
  7. ## module ??- The answer is quite simple. We want to be able to edit and ##
  8. ## reload some commands without restarting the bot's main process, so    ##
  9. ## this is one of our solution.                                          ##
  10. ##                                                                       ##
  11. ## -Main credit goes to: - Poeticartist1 (Shows me the idea)             ##
  12. ##                       - PIKS (Shows me the way)                       ##
  13. ###########################################################################
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. import imp
  23. import threading
  24. import random
  25. import os
  26. import re
  27. import sys
  28. import coloring
  29. import codecs
  30. import traceback
  31. import datetime
  32. import ch
  33. import release
  34. import urllib
  35. import time
  36. from xfunc import *
  37. import xfunc as exter
  38. import coloring
  39. import __future__
  40.  
  41. #############################
  42. ## urllib secondary import ##
  43. if sys.version_info[0] > 2:
  44.   import urllib.request as urlreq
  45. else:
  46.   import urllib2 as urlreq
  47. #############################
  48.  
  49. ##########################
  50. ## Reading txt database ##
  51. blacklist = dict()
  52. try:
  53.   f = open("blacklist.txt", "r")
  54.   blacklist = eval(f.read())
  55.   f.close()
  56. except:pass
  57.  
  58. ranker = dict()
  59. try:
  60.   f = open("ranker.txt", 'r')
  61.   ranker = eval(f.read())
  62.   f.close()
  63. except:pass
  64.  
  65. rooms = []
  66. file = open("rooms.txt", 'r')
  67. for name in file.readlines():
  68.   if len(name.strip()) > 0 :
  69.     rooms.append(name.strip())
  70. print("NOTICE: Room list loaded.")
  71. file.close()
  72.  
  73. locks = []
  74. file = open("locks.txt", 'r')
  75. for name in file.readlines():
  76.   if len(name.strip()) > 0 :
  77.     locks.append(name.strip())
  78. print("NOTICE: Locked Rooms loaded.")
  79. file.close()
  80.  
  81. ## finished reading database ##
  82. ###############################
  83.  
  84.  
  85.  
  86. ###############################
  87. ##         Variables         ##
  88.  
  89. botversion = release.botversion
  90.  
  91. botrelease = release.botrelease
  92.  
  93. botsys     = release.botsys
  94.  
  95. command_list = ['help','wl/register','uwl/unregister','bl/blacklist','chain','ubl/unblacklist','unchain','rank','setrank','pm','broadcast','say','reverse/rsay','find','multichat','ban','unban','join','leave','lock','unlock','rooms','save','mods','activate','restrict','lockdown','wake']
  96.  
  97. prefix = [">", "~", " "]
  98.  
  99. brutalpic = ["http://i.imgur.com/mdymM8t.jpg","http://i2.kym-cdn.com/photos/images/newsfeed/001/081/950/81b.jpg","http://s.quickmeme.com/img/f8/f8c1380e46d88b4edd02415b11f43824a1ab06c031c1dac09a162ca5384ddc33.jpg","http://ci.memecdn.com/181/9181181.jpg","http://ci.memecdn.com/925/4752925.jpg","http://stream1.gifsoup.com/view2/3452958/nathan-explosion-no-o.gif","http://ci.memecdn.com/930/8323930.jpg","http://cdn.gagbay.com/2014/10/please_prove_that_you_are_brutal-510617.jpg","http://i1.kym-cdn.com/photos/images/facebook/000/872/117/2a8.png","http://i.imgur.com/ZYrFkCw.jpg"]
  100.  
  101. nonbrutalpic = ["http://www.agrinionews.gr/wp-content/uploads/2016/01/80b.jpg","http://static4.fjcdn.com/comments/Source+is+metalocalypse+not+reddit+c+mon+_7e7ff5ab89ca355f3f891477285fec03.jpg","http://s-media-cache-ak0.pinimg.com/236x/24/10/7a/24107a5be1191a1242bc0021d7349031.jpg","http://media.giphy.com/media/scoHe0Cw79cK4/giphy.gif","http://www.quickmeme.com/img/b8/b815678dcd584fed11c7f82a8f0ce772ac0bf238dae5bcbc54b2d1c3078a7931.jpg","http://s2.quickmeme.com/img/4d/4d213474085cc2bd23479a01f8b0f893e36965d110abd595eecd1df2634fdb96.jpg"]
  102.  
  103. ##     Variable Finished     ##
  104. ###############################
  105.  
  106.  
  107.  
  108.  
  109. ###############################
  110. ##       Some Functions      ##
  111.  
  112. def saveAll(self):
  113.     room = self._Room
  114.     f = open("ranker.txt", "w")
  115.     f.write(str(ranker))
  116.     f.close()
  117.     f = open("blacklist.txt", "w")
  118.     f.write(str(blacklist))
  119.     f.close()
  120.     f = open("locks.txt", "w")
  121.     f.write("\n".join(locks))
  122.     f.close()
  123.     f = open("rooms.txt", "w")
  124.     f.write("\n".join(self.roomnames))
  125.     f.close()
  126.  
  127. def onPMMessage(self, pm, user, body):
  128.   if body != "":
  129.     pm.message(user, body)
  130.  
  131. def onMessage(self, room, user, message):
  132.   try:
  133.     if user.name == self.user.name:return
  134.     uTarget = None
  135.     global uTarget
  136.     global interval_task
  137.     def urank(target):
  138.         if target == None:
  139.             return 0
  140.         if target not in ranker:
  141.             return 0
  142.         rank = ranker[target]
  143.         if rank == 1 and room.getLevel(ch.User(target)) == 1:
  144.             rank = 2
  145.         if rank == 1 and room.getLevel(ch.User(target)) == 2:
  146.             rank = 3
  147.         return rank
  148.    
  149.     def utitle(target):
  150.         drank = urank(target)
  151.         title = "null"
  152.         if drank > 0:
  153.             if drank == 1:
  154.                 title = "player"
  155.             if drank == 2:
  156.                 title = "room moderator"
  157.             if drank == 3:
  158.                 title = "room admin"
  159.             if drank == 4:
  160.                 title = "MODERATOR"
  161.             if drank == 5:
  162.                 title = "OWNER"
  163.         return title
  164.    
  165.     myrank  = urank(user.name)
  166.     hisrank = urank(uTarget)
  167.     mytitle = utitle(user.name)
  168.    
  169.     seth = self
  170.     global seth
  171.    
  172.     saveAll(seth)
  173.  
  174.     from coloring import colorTest as ctp
  175.     from coloring import colorLite as clp
  176.     from coloring import colorBold as cbp
  177.    
  178.     def ct(col):
  179.         return ctp(seth, col)
  180.    
  181.     def cl(term):
  182.         return clp(seth, term)
  183.    
  184.     def cb(term):
  185.         return cbp(seth, term)
  186.    
  187.     global myrank
  188.     global hisrank
  189.     global mytitle
  190.    
  191.     def roomUsers():
  192.           usrs = []
  193.           gay = []
  194.           prop = 0
  195.           prop = prop + len(room._userlist) - 1
  196.           for i in room._userlist:
  197.             i = str(i)
  198.             usrs.append(i)
  199.           while prop >= 0:
  200.             j = usrs[prop].replace("<User: ", "")
  201.             i = j.replace(">", "")
  202.             gay.append(i)
  203.             prop = prop - 1
  204.           return gay
  205.      
  206.     def getParticipant(arg):
  207.           rname = self.getRoom(arg)
  208.           usrs = []
  209.           gay = []
  210.           finale = []
  211.           prop = 0
  212.           prop = prop + len(rname._userlist) - 1
  213.           for i in rname._userlist:
  214.             i = str(i)
  215.             usrs.append(i)
  216.           while prop >= 0:
  217.             j = usrs[prop].replace("<User: ", "")
  218.             i = j.replace(">", "")
  219.             gay.append(i)
  220.             prop = prop - 1
  221.           for j in gay:
  222.             if j not in finale:
  223.               finale.append(j)
  224.           return finale
  225.      
  226.     def pars(args):
  227.           args=args.lower()
  228.           userlist = roomUsers()
  229.           for name in userlist:
  230.             if args in name:return name
  231.     def roompars(args):
  232.         args = args.lower()
  233.         for name in self.roomnames:
  234.           if args in name:return name
  235.    
  236.     if message.body[0] in prefix: #prefix usage in this line (for this case I use "*" as prefix)
  237.         data = message.body[1:].split(" ", 1) #This part splits message body into [0]prefix and [1:]data ([1:] <- this means the message body's second character and after) and data will be split into 2 (cmd(data[0]), args(data[1])) which is very usefull. (Many thanks to TryHardHusky)
  238.         if len(data) == 2: #If there are more than 1 data (This is where we will get args)
  239.           cmd, args = data[0], data[1] #the first data (data[0]) will be the cmd (specified command) and the next data will be args (it doesn't matter how many word next to the cmd, It'd eventually be args)
  240.         else: #If there is only 1 data (No args)
  241.           cmd, args = data[0], "" #the arg will simply be "" (Empty)
  242.         cmd == cmd.lower()
  243.        
  244.         if cmd == "brutal":
  245.             fin = random.randint(1,2)
  246.             if fin == 1:
  247.                 pic = random.choice(["http://i.imgur.com/mdymM8t.jpg","http://i2.kym-cdn.com/photos/images/newsfeed/001/081/950/81b.jpg","http://s.quickmeme.com/img/f8/f8c1380e46d88b4edd02415b11f43824a1ab06c031c1dac09a162ca5384ddc33.jpg","http://ci.memecdn.com/181/9181181.jpg","http://ci.memecdn.com/925/4752925.jpg","http://stream1.gifsoup.com/view2/3452958/nathan-explosion-no-o.gif","http://ci.memecdn.com/930/8323930.jpg","http://cdn.gagbay.com/2014/10/please_prove_that_you_are_brutal-510617.jpg","http://i1.kym-cdn.com/photos/images/facebook/000/872/117/2a8.png","http://i.imgur.com/ZYrFkCw.jpg"])
  248.                 room.message(" "+pic)
  249.             else:
  250.                 pic = random.choice(["http://www.agrinionews.gr/wp-content/uploads/2016/01/80b.jpg","http://static4.fjcdn.com/comments/Source+is+metalocalypse+not+reddit+c+mon+_7e7ff5ab89ca355f3f891477285fec03.jpg","http://s-media-cache-ak0.pinimg.com/236x/24/10/7a/24107a5be1191a1242bc0021d7349031.jpg","http://media.giphy.com/media/scoHe0Cw79cK4/giphy.gif","http://www.quickmeme.com/img/b8/b815678dcd584fed11c7f82a8f0ce772ac0bf238dae5bcbc54b2d1c3078a7931.jpg","http://s2.quickmeme.com/img/4d/4d213474085cc2bd23479a01f8b0f893e36965d110abd595eecd1df2634fdb96.jpg"])
  251.                 room.message(" "+pic)
  252.                
  253.         if cmd == "ival":
  254.             if myrank < 3:
  255.                 room.message(cb("red!Jangan harap."), True)
  256.                 return
  257.             interval_task = room._ivalTasks
  258.             args = args.split(" ", 1)
  259.             if args[0] == "kill":
  260.                 if len(args) == 2:
  261.                     if args[1] in interval_task:
  262.                         self._tasks.remove(interval_task[args[1]][0])
  263.                         room.message("Task killed: "+str(args[1])+" - "+str(interval_task[args[1]][0]))
  264.                         interval_task.pop(args[1])
  265.             if args[0] == "tasks":
  266.                 if len(interval_task) > 0:
  267.                     tasklist = []
  268.                     for i in interval_task:
  269.                         _task = interval_task
  270.                         tasklist.append("(Name: "+str(i)+" - (every "+str(_task[i][1])+" seconds) post "+str(_task[i][2])+" ["+str(_task[i][0])+"])")
  271.                     room.message(", ".join(tasklist))
  272.             if args[0] not in ["kill", "tasks"]:
  273.                 if len(args) > 1:
  274.                     task_name = args[0]
  275.                     ivaltime, task_output = args[1].split(" ", 1)
  276.                     task_interval_time = int(ivaltime)
  277.                     task = self.setInterval(task_interval_time, room.message, str(task_output))
  278.                     interval_task.update({task_name:[task, ivaltime, task_output]})
  279.                     room.message("task created: "+str(task_name)+" ("+str(ivaltime)+" seconds interval)")
  280.            
  281.         if (cmd == "ee") and urank(user.name) == 5: #DO NOT MESS WITH THIS COMMAND IF YOU DON'T KNOW WHAT YOU'RE DOING (This is a vital command)
  282.           ret = eval(args)
  283.           if ret == None:
  284.             room.message("Done.")
  285.             return
  286.           room.message(str(ret))
  287.        
  288.            
  289.         if cmd == "setrank" and urank(user.name) == 5:
  290.           help_output = "Usage : ~setrank [usertarget] [number]<br/>Example : ~setrank gayman 1]"
  291.           if args == "":
  292.             room.message(help_output, True)
  293.           if args != "":
  294.            try:
  295.             args = args.lower()
  296.             target, rank = args.split(" ", 1)
  297.             target = str(target)
  298.             rank = int(rank)
  299.             available_rank = [-1,0,1,4,5]
  300.             if not rank in available_rank:
  301.               room.message("Type a propper rank number.")
  302.               return
  303.             if rank == 1:
  304.               if urank(target) == 1:
  305.                 room.message("Why ??? they are rank 1 you know !!")
  306.                 return
  307.               room.message(target.title()+"'s rank is set to: "+str(rank)+" [Player]")
  308.             if rank == 4:
  309.               if urank(target) == 4:
  310.                 room.message(target.title()+" is already a Moderator.")
  311.                 return
  312.               room.message(target.title()+"'s rank is set to: "+str(rank)+" [Moderator]")
  313.             if rank == 5:
  314.               if not user.name == "0rx": return #put your name there (if user.name == "yourname" and user.name == "anothername":) To prevent other Owners(in this case is Co-Owners) to set user's rank to Co-Owner.
  315.               if urank(target) == 5:
  316.                 room.message(target.title()+" is already a Co-Owner.")
  317.                 return
  318.               room.message(target.title()+"'s rank is set to: "+str(rank)+" [Owner]")
  319.             if rank == 0:
  320.               if target == "0rx": return
  321.               if target in ranker:
  322.                 ranker.pop(target)
  323.               if target in blacklist:
  324.                 blacklist.pop(target)
  325.               room.message(target.title()+"'s rank is set to: "+str(rank))
  326.               return
  327.             if rank == -1:
  328.               if target == "0rx": return
  329.               if target in blacklist:
  330.                 room.message(target.title()+" is already blacklisted.<br/>Reason: %s"% blacklist[target])
  331.                 return
  332.               blacklist.update({target:"Annoying reason."})
  333.               room.message(target.title()+"'s rank is set to: "+str(rank)+" [Fugitive]")
  334.               return
  335.             if target in blacklist:
  336.                 blacklist.pop(target)
  337.             ranker.update({target:rank})
  338.            except:
  339.              room.message(help_output, True)
  340.            
  341.            
  342.   except Exception as e:
  343.     try:
  344.           et, ev, tb = sys.exc_info()
  345.           lineno = tb.tb_lineno
  346.           fn = tb.tb_frame.f_code.co_filename
  347.           room.message("[Error] %s Line %i - %s"% (fn, lineno, str(e)))
  348.           return
  349.     except:
  350.           room.message("Undescribeable error detected !!")
  351.           return
Advertisement
Add Comment
Please, Sign In to add comment