2019-01-09 22:11:45 -05:00
|
|
|
import os
|
|
|
|
import discord
|
|
|
|
|
|
|
|
# Get bot token
|
|
|
|
|
|
|
|
token = ""
|
|
|
|
with open("token.cfg") as t:
|
|
|
|
token = t.read()
|
|
|
|
|
2019-03-23 13:25:44 -04:00
|
|
|
# Create bot client
|
2019-01-09 22:11:45 -05:00
|
|
|
|
2019-03-23 13:25:44 -04:00
|
|
|
client = discord.Client()
|
|
|
|
|
|
|
|
# IDs of bot admins
|
|
|
|
|
|
|
|
admins = []
|
|
|
|
|
|
|
|
# Default prefix
|
|
|
|
|
|
|
|
prefix = "r."
|
|
|
|
|
|
|
|
# Default color, to be used for embeds.
|
|
|
|
|
|
|
|
color = discord.Colour.blue()
|
2019-01-09 22:11:45 -05:00
|
|
|
|
|
|
|
# API functions
|
|
|
|
|
|
|
|
def register_command(command, function):
|
2019-03-23 13:25:44 -04:00
|
|
|
|
2019-01-09 22:11:45 -05:00
|
|
|
commands[command] = function
|
|
|
|
|
2019-03-23 13:25:44 -04:00
|
|
|
def register_admin_command(command, function):
|
|
|
|
|
|
|
|
admin_commands[command] = function
|
|
|
|
|
|
|
|
def get_commands():
|
|
|
|
return commands
|
|
|
|
|
|
|
|
def get_admin_commands():
|
|
|
|
return admin_commands
|
|
|
|
|
|
|
|
def get_bot_prefix():
|
|
|
|
return prefix
|
|
|
|
|
|
|
|
def get_embed_color():
|
|
|
|
return color
|
2019-01-09 22:11:45 -05:00
|
|
|
|
|
|
|
def command_exists(command):
|
2019-03-23 13:25:44 -04:00
|
|
|
|
2019-01-09 22:11:45 -05:00
|
|
|
if command in commands:
|
2019-03-23 13:25:44 -04:00
|
|
|
return True
|
|
|
|
|
|
|
|
if command in admin_commands:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2019-01-09 22:11:45 -05:00
|
|
|
|
2019-03-23 13:25:44 -04:00
|
|
|
def is_admin(id):
|
2019-01-09 22:11:45 -05:00
|
|
|
|
2019-03-23 13:25:44 -04:00
|
|
|
return id in admins
|
|
|
|
|
|
|
|
async def restart():
|
2019-03-23 13:39:10 -04:00
|
|
|
os.system('./restart.sh')
|
2019-03-23 13:25:44 -04:00
|
|
|
# Exit cleanly
|
|
|
|
exit()
|
2019-01-09 22:11:45 -05:00
|
|
|
|
2019-03-23 13:25:44 -04:00
|
|
|
# Initializing the command dictionaries
|
2019-01-09 22:11:45 -05:00
|
|
|
|
2019-03-23 13:25:44 -04:00
|
|
|
# Commands anyone can use!
|
|
|
|
commands = {}
|
2019-01-09 22:11:45 -05:00
|
|
|
|
2019-03-23 13:25:44 -04:00
|
|
|
# Commands only accessible to admins
|
|
|
|
admin_commands = {}
|
|
|
|
|
|
|
|
# Load commands
|
2019-03-23 14:00:16 -04:00
|
|
|
|
|
|
|
os.chdir('commands')
|
|
|
|
|
2019-01-16 17:28:53 -05:00
|
|
|
for filename in os.listdir():
|
2019-01-09 22:11:45 -05:00
|
|
|
|
2019-03-23 13:25:44 -04:00
|
|
|
# Make sure it isn't this source file. Needs to be abstracted in the future.
|
|
|
|
if filename != 'bot.py':
|
2019-01-09 22:11:45 -05:00
|
|
|
|
2019-03-23 13:25:44 -04:00
|
|
|
# Open the file.
|
|
|
|
with open(filename) as file:
|
|
|
|
|
|
|
|
# Run the file in the current scope.
|
|
|
|
exec(file.read())
|
2019-01-09 22:11:45 -05:00
|
|
|
|
|
|
|
# When message detected
|
|
|
|
|
|
|
|
@client.event
|
|
|
|
async def on_message(message):
|
2019-03-23 13:25:44 -04:00
|
|
|
|
|
|
|
# If it looks like a command...
|
|
|
|
if message.content.startswith(prefix):
|
|
|
|
|
|
|
|
for command in commands:
|
|
|
|
|
|
|
|
if message.content.startswith(prefix+command):
|
|
|
|
|
|
|
|
try:
|
2019-03-23 14:07:53 -04:00
|
|
|
args = message.content[len(message.content.split()[0])+1:]
|
2019-03-23 13:25:44 -04:00
|
|
|
await commands[command](message, args)
|
|
|
|
|
|
|
|
except Exception as e:
|
2019-03-23 14:00:16 -04:00
|
|
|
msg = "**Exception in command `" + command + "`:**\n" + str(e)
|
2019-03-23 14:06:12 -04:00
|
|
|
await message.channel.send(msg)
|
2019-03-23 13:25:44 -04:00
|
|
|
|
|
|
|
# Checking for admin commands as well! But only if the author is an admin.
|
|
|
|
if is_admin(str(message.author.id)):
|
|
|
|
|
|
|
|
for command in admin_commands:
|
|
|
|
|
|
|
|
if message.content.startswith(prefix+command):
|
|
|
|
|
|
|
|
try:
|
2019-03-23 14:07:53 -04:00
|
|
|
args = message.content[len(message.content.split()[0])+1:]
|
2019-03-23 13:25:44 -04:00
|
|
|
await commands[command](message, args)
|
|
|
|
|
|
|
|
except Exception as e:
|
2019-03-23 14:00:16 -04:00
|
|
|
msg = "**Exception in command `" + command + "`:**\n" + str(e)
|
2019-03-23 14:06:12 -04:00
|
|
|
await message.channel.send(msg)
|
2019-03-23 13:25:44 -04:00
|
|
|
|
2019-01-09 22:11:45 -05:00
|
|
|
|
|
|
|
# When logged in
|
|
|
|
|
|
|
|
@client.event
|
|
|
|
async def on_ready():
|
|
|
|
print("Logged in as\n" + client.user.name + "\n" + str(client.user.id) + "\n---")
|
|
|
|
|
|
|
|
# Log in
|
|
|
|
|
|
|
|
client.run(token)
|