Skip to content

Instantly share code, notes, and snippets.

@Bert-Macklin
Last active March 25, 2018 05:08
Show Gist options
  • Save Bert-Macklin/0f0776ffa46c99a24ba8632c1563299e to your computer and use it in GitHub Desktop.
Save Bert-Macklin/0f0776ffa46c99a24ba8632c1563299e to your computer and use it in GitHub Desktop.
Making a Discord Bot in Python 3
# command handler class
class CommandHandler:
# on object creation take in the client variable
def __init__(self, client):
self.client = client
# create an array to store commands
self.commands = []
def add_command(self, command):
# add a command to the commands array
self.commands.append(command)
def command_handler(self, message):
# loop through command array
for command in self.commands:
# if the message starts with a command trigger
if message.content.startswith(command['trigger']):
args = message.content.split(' ')
if args[0] == command['trigger']:
args.pop(0)
if command['args_num'] == 0:
#returns the results of the function
return self.client.send_message(message.channel,str(command['function'](message, self.client, args)))
break
else:
if len(args) >= command['args_num']:
# return the results of the function
return self.client.send_message(message.channel,str(command['function'](message, self.client, args)))
break
else:
# return argument error
return self.client.send_message(message.channel, 'command "{}" requires {} argument(s) "{}"'.format(command['trigger'], command['args_num'], ', '.join(command['args_name'])))
break
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment