Initial rewrite commit
This commit is contained in:
parent
8cbb3a6270
commit
8a0af02407
135
bot.py
135
bot.py
@ -7,72 +7,119 @@ token = ""
|
||||
with open("token.cfg") as t:
|
||||
token = t.read()
|
||||
|
||||
# Default prefixes
|
||||
# Create bot client
|
||||
|
||||
prefix = "/"
|
||||
lib_prefix = "lib_"
|
||||
preload_prefix = "preload_"
|
||||
client = discord.Client()
|
||||
|
||||
# IDs of bot admins
|
||||
|
||||
admins = []
|
||||
|
||||
# Default prefix
|
||||
|
||||
prefix = "r."
|
||||
|
||||
# Default color, to be used for embeds.
|
||||
|
||||
color = discord.Colour.blue()
|
||||
|
||||
# API functions
|
||||
|
||||
def register_command(command, function):
|
||||
|
||||
commands[command] = function
|
||||
|
||||
def set_prefix(newPrefix):
|
||||
prefix = newPrefix
|
||||
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
|
||||
|
||||
def command_exists(command):
|
||||
|
||||
if command in commands:
|
||||
return true
|
||||
else:
|
||||
return false
|
||||
|
||||
async def send(channel, response):
|
||||
await channel.send(response)
|
||||
|
||||
# Initialized with no commands
|
||||
return True
|
||||
|
||||
if command in admin_commands:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def is_admin(id):
|
||||
|
||||
return id in admins
|
||||
|
||||
async def restart():
|
||||
os.execute("restart.sh")
|
||||
# Exit cleanly
|
||||
exit()
|
||||
|
||||
# Initializing the command dictionaries
|
||||
|
||||
# Commands anyone can use!
|
||||
commands = {}
|
||||
|
||||
# Default commands should be registered here.
|
||||
# Commands only accessible to admins
|
||||
admin_commands = {}
|
||||
|
||||
async def cmd_help(message):
|
||||
response = "Available commands:"
|
||||
for command in commands:
|
||||
response += "\n" + prefix + command
|
||||
await send(message.channel, response)
|
||||
register_command("help", cmd_help)
|
||||
|
||||
# End default command registration
|
||||
|
||||
# Load external commands
|
||||
|
||||
os.chdir('commands')
|
||||
# Load commands
|
||||
|
||||
for filename in os.listdir():
|
||||
if filename.startswith(preload_prefix):
|
||||
with open(filename) as file:
|
||||
exec(file.read())
|
||||
|
||||
for filename in os.listdir():
|
||||
if filename != 'bot.py' and filename.startswith(lib_prefix) == False and filename.startswith(preload_prefix) == False:
|
||||
with open(filename) as file:
|
||||
exec(file.read())
|
||||
|
||||
# Create bot client object
|
||||
# Make sure it isn't this source file. Needs to be abstracted in the future.
|
||||
if filename != 'bot.py':
|
||||
|
||||
client = discord.Client()
|
||||
# Open the file.
|
||||
with open(filename) as file:
|
||||
|
||||
# Run the file in the current scope.
|
||||
exec(file.read())
|
||||
|
||||
# When message detected
|
||||
|
||||
@client.event
|
||||
async def on_message(message):
|
||||
for command in commands:
|
||||
if message.content.startswith(prefix+command):
|
||||
try:
|
||||
await commands[command](message)
|
||||
except Exception as e:
|
||||
msg = "**Exception in command `"+command+"`:**\n"+e
|
||||
message.channel.send(msg)
|
||||
|
||||
# If it looks like a command...
|
||||
if message.content.startswith(prefix):
|
||||
|
||||
for command in commands:
|
||||
|
||||
if message.content.startswith(prefix+command):
|
||||
|
||||
try:
|
||||
args = message.content[len(message.content.split()[1])+1]
|
||||
await commands[command](message, args)
|
||||
|
||||
except Exception as e:
|
||||
msg = "**Exception in command `" + command + "`:**\n" + e
|
||||
message.channel.send(msg)
|
||||
|
||||
# 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:
|
||||
args = message.content[len(message.content.split()[1])+1]
|
||||
await commands[command](message, args)
|
||||
|
||||
except Exception as e:
|
||||
msg = "**Exception in command `" + command + "`:**\n" + e
|
||||
message.channel.send(msg)
|
||||
|
||||
|
||||
# When logged in
|
||||
|
||||
|
2
restart.sh
Normal file
2
restart.sh
Normal file
@ -0,0 +1,2 @@
|
||||
sleep 3
|
||||
python bot.py &
|
Loading…
x
Reference in New Issue
Block a user