Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import discord
- import datetime
- import time
- import os
- import sys
- #Config
- version = 1.0 #Version number, change as needed. Try to never lower the version number.
- logging = True #True or False, enables/disables logging.
- #Logging saves to file Discord.log every time someone uses a command.
- configfile = "login.cfg" #Contains any details needed, in the format
- #"______=_______". (No "=" signs other than the one in the middle
- #allowed right now.)
- welcomenewusers = True # True/False; creates a welcome message for new users.
- AdminRoles = ["Bot","Owner"] #Type the name of roles that should have admin rights here.
- #Code
- commands = {}
- def AddCommand(command,reply):
- global commands
- commands[command] = reply
- return
- def AddListCommand(command,reply,inplist):
- global commands
- print(inplist)
- for x in inplist:
- reply += x+", "
- commands[command] = reply
- return
- def AddCustomCommand(command,function): #Runs the specified function on command,
- #with words after the first being passed in a list called params, and the
- #original message being passed as message.
- global commands
- commands[command] = function
- def GetKeys(dic): #Returns a list of commands.
- keys = []
- for k,v in dic.items():
- keys.append(k)
- return keys
- def AddToLog(entry):
- logentry = "["+str(datetime.datetime.now())+"] " + entry
- file = open("Discord.log","a")
- file.write(logentry)
- file.write("\n")
- file.close()
- print(logentry)
- def GetFromConfig(value):
- global configfile
- if os.path.exists(configfile):
- file = open(configfile,"r")
- resultdict = {}
- for x in file:
- y = x.split("=")
- resultdict[y[0].rstrip('\n')] = y[1].rstrip('\n')
- print(resultdict)
- file.close()
- if value in resultdict:
- return resultdict[value]
- else:
- print("ERROR: Could not find "+value+" in dictonary.")
- return None
- else:
- print("ERROR: Config file does not exist. Creating.")
- open(configfile,"w")
- return None
- #Custom Command Executions
- def Announce(params,message):
- server = message.server
- content = " ".join(params[1:])
- for x in message.author.roles:
- if x.name in AdminRoles:
- client.send_message(server.get_default_channel(),"@everyone ANNOUNCEMENT: "+content,True,True)
- def NextComicUpdate(params,message):
- now = datetime.datetime.now()
- updatetime = datetime.datetime(now.year,now.month,now.day,18, 0, 0, 0)
- if updatetime < now:
- updatetime = updatetime + datetime.timedelta(days=1)
- time = updatetime - now
- print(time)
- client.send_message(message.channel, "The next comic update should be in around "+ str(time).split(".")[0])
- starttime = time.time()
- client = discord.Client()
- client.login(GetFromConfig("DiscordUser"),GetFromConfig("DiscordPass"))
- @client.event
- def on_message(message):
- global commands
- logentry = ""
- for k,v in commands.items():
- if message.content.lower().split(" ")[0] == k:
- if logging == True:
- if message.server == None:
- AddToLog("User "+message.author.name+" just executed command "+k+" via PM.")
- else:
- AddToLog("User "+message.author.name+" just executed command "+k+" in channel "+message.channel.name+".")
- if callable(v)== True:
- message1 = message.content.split("!")[1:]
- message1str = "!".join(message1)
- params = message1str.split(" ")
- v(params,message)
- else:
- client.send_message(message.channel, v)
- @client.event
- def on_member_join(member):
- if welcomenewusers == True:
- client.send_message(member.server.get_default_channel(),"Welcome to "+member.server.name+", "+member.mention()+" !",True)
- if logging == True:
- AddToLog("User "+member.name+" joined server "+member.server.name+"!")
- @client.event
- def on_member_remove(member):
- if logging == True:
- AddToLog("User "+member.name+" left server "+member.server.name+".")
- @client.event
- @client.event
- def on_ready():
- print('Logged in as')
- print(client.user.name)
- print(client.user.id)
- print('------')
- AddCustomCommand("!comicupdate",NextComicUpdate)
- #Keep at end!
- AddListCommand('!help',"Current commands: !help, ",GetKeys(commands))
- if logging == True:
- AddToLog("Bot loaded with "+str(len(commands))+" commands!")
- client.run()
Advertisement
Add Comment
Please, Sign In to add comment