picapi_

Untitled

Dec 30th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.64 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import discord
  3. import datetime
  4. import time
  5. import os
  6. import sys
  7.  
  8. #Config
  9. version = 1.0 #Version number, change as needed. Try to never lower the version number.
  10. logging = True #True or False, enables/disables logging.
  11. #Logging saves to file Discord.log every time someone uses a command.
  12. configfile = "login.cfg" #Contains any details needed, in the format
  13. #"______=_______". (No "=" signs other than the one in the middle
  14. #allowed right now.)
  15. welcomenewusers = True # True/False; creates a welcome message for new users.
  16. AdminRoles = ["Bot","Owner"] #Type the name of roles that should have admin rights here.
  17.  
  18. #Code
  19. commands = {}
  20. def AddCommand(command,reply):
  21.     global commands
  22.     commands[command] = reply
  23.     return
  24. def AddListCommand(command,reply,inplist):
  25.     global commands
  26.     print(inplist)
  27.     for x in inplist:
  28.         reply += x+", "
  29.     commands[command] = reply
  30.     return
  31. def AddCustomCommand(command,function): #Runs the specified function on command,
  32.     #with words after the first being passed in a list called params, and the
  33.     #original message being passed as message.
  34.     global commands
  35.     commands[command] = function
  36.    
  37.    
  38. def GetKeys(dic): #Returns a list of commands.
  39.     keys = []
  40.     for k,v in dic.items():
  41.         keys.append(k)
  42.     return keys
  43. def AddToLog(entry):
  44.     logentry = "["+str(datetime.datetime.now())+"] " + entry
  45.     file = open("Discord.log","a")
  46.     file.write(logentry)
  47.     file.write("\n")
  48.     file.close()
  49.     print(logentry)
  50. def GetFromConfig(value):
  51.     global configfile
  52.     if os.path.exists(configfile):
  53.         file = open(configfile,"r")
  54.         resultdict = {}
  55.         for x in file:
  56.             y = x.split("=")
  57.             resultdict[y[0].rstrip('\n')] = y[1].rstrip('\n')
  58.         print(resultdict)
  59.         file.close()
  60.         if value in resultdict:
  61.             return resultdict[value]
  62.         else:
  63.             print("ERROR: Could not find "+value+" in dictonary.")
  64.             return None
  65.     else:
  66.         print("ERROR: Config file does not exist. Creating.")
  67.         open(configfile,"w")
  68.         return None
  69.  
  70.  
  71.  
  72. #Custom Command Executions
  73. def Announce(params,message):
  74.     server = message.server
  75.     content = " ".join(params[1:])
  76.     for x in message.author.roles:
  77.         if x.name in AdminRoles:
  78.             client.send_message(server.get_default_channel(),"@everyone ANNOUNCEMENT: "+content,True,True)
  79.  
  80. def NextComicUpdate(params,message):
  81.     now = datetime.datetime.now()
  82.     updatetime = datetime.datetime(now.year,now.month,now.day,18, 0, 0, 0)
  83.     if updatetime < now:
  84.         updatetime = updatetime + datetime.timedelta(days=1)
  85.     time = updatetime - now
  86.     print(time)
  87.     client.send_message(message.channel, "The next comic update should be in around "+ str(time).split(".")[0])
  88.    
  89.    
  90.    
  91.  
  92.        
  93.  
  94.  
  95.  
  96. starttime = time.time()
  97. client = discord.Client()
  98. client.login(GetFromConfig("DiscordUser"),GetFromConfig("DiscordPass"))
  99. @client.event
  100. def on_message(message):
  101.     global commands
  102.     logentry = ""
  103.     for k,v in commands.items():
  104.         if message.content.lower().split(" ")[0] == k:
  105.             if logging == True:
  106.                 if message.server == None:
  107.                     AddToLog("User "+message.author.name+" just executed command "+k+" via PM.")
  108.                 else:
  109.                     AddToLog("User "+message.author.name+" just executed command "+k+" in channel "+message.channel.name+".")
  110.             if callable(v)== True:
  111.                 message1 = message.content.split("!")[1:]
  112.                 message1str = "!".join(message1)
  113.                 params = message1str.split(" ")
  114.                 v(params,message)
  115.             else:
  116.                 client.send_message(message.channel, v)
  117.            
  118. @client.event
  119. def on_member_join(member):
  120.     if welcomenewusers == True:
  121.         client.send_message(member.server.get_default_channel(),"Welcome to "+member.server.name+", "+member.mention()+" !",True)
  122.     if logging == True:
  123.         AddToLog("User "+member.name+" joined server "+member.server.name+"!")
  124.        
  125. @client.event
  126. def on_member_remove(member):
  127.     if logging == True:
  128.         AddToLog("User "+member.name+" left server "+member.server.name+".")
  129.  
  130. @client.event
  131.  
  132.  
  133. @client.event
  134. def on_ready():
  135.     print('Logged in as')
  136.     print(client.user.name)
  137.     print(client.user.id)
  138.     print('------')
  139.     AddCustomCommand("!comicupdate",NextComicUpdate)
  140.     #Keep at end!
  141.     AddListCommand('!help',"Current commands: !help, ",GetKeys(commands))
  142.     if logging == True:
  143.         AddToLog("Bot loaded with "+str(len(commands))+" commands!")
  144.    
  145.  
  146. client.run()
Advertisement
Add Comment
Please, Sign In to add comment