Last active
March 5, 2024 22:17
-
-
Save JakeStanger/2f6842ddcaa637210bc076bfe2580ffd to your computer and use it in GitHub Desktop.
Base code for discord bot - put commands in json file, create command functions in `commands.py` of same name, everything else is done for you.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import discord | |
import asyncio | |
import json | |
import commands as bot_commands | |
client = discord.Client() | |
commands = {} | |
aliases = {} | |
def generate_help(): | |
print(''.join(commands[command].get_help() for command in commands)) | |
return ''.join(commands[command].get_help() for command in commands) | |
class Command: | |
def __init__(self, name: str, command_aliases: list, description: str): | |
self._name = name | |
self._aliases = command_aliases | |
self._description = description | |
if command != 'help': | |
self._function = getattr(bot_commands, self.get_name()) | |
else: self._function = generate_help | |
def get_name(self): | |
return self._name | |
def get_aliases(self): | |
return self._aliases | |
def get_description(self): | |
return self._description | |
def get_help(self): | |
return "\n\n**%s** - %s\n__Aliases:__ *%s*" % (self.get_name(), self.get_description(), | |
'*, *'.join(alias for alias in self.get_aliases())) | |
def run(self): | |
return self._function() | |
def register_command(command: Command): | |
if command.get_name() in commands: | |
raise ValueError("A command with this name is already registered.") | |
commands[command.get_name()] = command | |
for alias in command.get_aliases(): | |
aliases[alias] = command.get_name() | |
with open('settings.json', 'r') as f: | |
settings = json.loads(f.read()) | |
TOKEN = settings['token'] | |
PREFIX = settings['prefix'] | |
for command in settings['commands']: | |
c = settings['commands'][command] | |
register_command(Command(command, c['aliases'], c['description'])) | |
def get_command_by_name(name: str) -> Command: | |
if name in commands: | |
return commands[name] | |
elif name in aliases: | |
return commands[aliases[name]] | |
else: | |
return None | |
@client.event | |
async def on_ready(): | |
print('Logged in as') | |
print(client.user.name) | |
print(client.user.id) | |
print('------') | |
@client.event | |
async def on_message(message): | |
if message.content.startswith(PREFIX): | |
command_name = message.content.split(PREFIX)[1] | |
command = get_command_by_name(command_name) | |
if command: | |
return_message = command.run() | |
await client.send_message(message.channel, return_message) | |
client.run(TOKEN) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"token": "", | |
"prefix": "+", | |
"commands": { | |
"help": { | |
"aliases": ["assist"], | |
"description": "Lists commands and their function." | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment