Merge pull request #2 from archfan7411/unstable
Merge unstable branch into master
This commit is contained in:
commit
780ab9e21e
@ -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.
|
||||
|
135
bot.py
135
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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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)
|
26
commands/cmd_help.py
Normal file
26
commands/cmd_help.py
Normal file
@ -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)
|
4
commands/cmd_restart.py
Normal file
4
commands/cmd_restart.py
Normal file
@ -0,0 +1,4 @@
|
||||
async def cmd_restart(message, args):
|
||||
await message.channel.send("Restarting!")
|
||||
restart()
|
||||
register_admin_command("restart", cmd_restart)
|
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