diff --git a/README.md b/README.md index fcbfab1..9418dde 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ To use RiftBot, you need a Discord application configured as a bot. To get one, Then, download the RiftBot files in the directory of your choice by cloning the URL: `git clone https://github.com/archfan7411/riftbot.git` -Then, replace the contents of token.cfg with the token you have saved. To run the bot, simply run `python bot.py &`. + +**Make a file, token.cfg, with the token you have saved, and run `chmod a+x restart.sh`**. To run the bot, do `python bot.py &`. # Inviting your bot instance To generate an invite link, go back to the developer dashboard (https://discordapp.com/developers/applications/me), select your application, and go to the OAuth2 tab. Scroll down and edit the Scopes as required; a URL will then be generated below them. Copy and paste this URL into your browser and follow the instructions from then on. diff --git a/bot.py b/bot.py index 7bb4b2f..abb636c 100644 --- a/bot.py +++ b/bot.py @@ -7,72 +7,121 @@ 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.system('./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 +# Load commands os.chdir('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 +for filename in os.listdir(): -client = discord.Client() + # Make sure it isn't this source file. Needs to be abstracted in the future. + if filename != 'bot.py': + + # 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()[0])+1:] + await commands[command](message, args) + + except Exception as e: + msg = "**Exception in command `" + command + "`:**\n" + str(e) + await 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()[0])+1:] + await commands[command](message, args) + + except Exception as e: + msg = "**Exception in command `" + command + "`:**\n" + str(e) + await message.channel.send(msg) + # When logged in diff --git a/commands/cmd_8ball.py b/commands/cmd_8ball.py index ea303a9..5875d8f 100644 --- a/commands/cmd_8ball.py +++ b/commands/cmd_8ball.py @@ -1,7 +1,17 @@ import random -async def cmd_8ball(message): - channel = message.channel + +async def cmd_8ball(message, args): + responses = ["Yep!", "Of course.", "Absolutely!", "Eh, ask again.", "Unsure", "What? No!", "I don't think so.", "Probably a bad idea."] + response = random.choice(responses) - await send(channel, response) + + random_embed = discord.Embed( + title = "**8ball**", + description = response, + colour = get_embed_color() + ) + + await message.channel.send(embed = random_embed) + register_command("8ball", cmd_8ball) diff --git a/commands/cmd_debug.py b/commands/cmd_debug.py deleted file mode 100644 index 4c8eda2..0000000 --- a/commands/cmd_debug.py +++ /dev/null @@ -1,5 +0,0 @@ -async def cmd_debug(message): - channel = message.channel - content = message.content - await send(channel, ("You said " + content)) -register_command("debug", cmd_debug) diff --git a/commands/cmd_help.py b/commands/cmd_help.py new file mode 100644 index 0000000..8e07830 --- /dev/null +++ b/commands/cmd_help.py @@ -0,0 +1,26 @@ +# The Help command *only* displays commands available to normal users. +# It is assumed that admins already know the admin commands :) +# (though an admin help command is easily done!) + +# Registering the command function. +# With latest RiftBot, the new command argument, 'args', is here. +# It contains the message text *after* the command. +async def cmd_help(message, args): + + help_msg = "Available commands:" + + # Iterating through available commands. + for command in get_commands(): + + help_msg += "\n" + get_bot_prefix() + command + + help_embed = discord.Embed( + title = "**Help**", + description = help_msg, + colour = get_embed_color() + ) + + # Send the help embed. + await message.channel.send(embed = help_embed) + +register_command("help", cmd_help) diff --git a/commands/cmd_restart.py b/commands/cmd_restart.py new file mode 100644 index 0000000..a426b9d --- /dev/null +++ b/commands/cmd_restart.py @@ -0,0 +1,4 @@ +async def cmd_restart(message, args): + await message.channel.send("Restarting!") + restart() +register_admin_command("restart", cmd_restart) diff --git a/restart.sh b/restart.sh new file mode 100644 index 0000000..5a3ea13 --- /dev/null +++ b/restart.sh @@ -0,0 +1,2 @@ +sleep 3 +python bot.py & \ No newline at end of file diff --git a/token.cfg b/token.cfg deleted file mode 100644 index 42ca352..0000000 --- a/token.cfg +++ /dev/null @@ -1 +0,0 @@ -Haha lol use your own token. \ No newline at end of file