Slides:
https://docs.google.com/presentation/d/1xCEXvK5QcWgNO2mXJpSJKhHBGgcWbjrS-784hCxUAzI/edit?usp=sharing
https://github.com/HackMiami/Discordbot-presentation To use this on your server update TOKEN, GUILD_ID and ROLE_ID in config/config.py
Enjoy and happy programming
Joing the discord.py server https://discord.gg/dpy
docker compose build
docker compose up -d
docker compose logs
docker compose exec -it bot bash
docker build -f Docker/Dockerfile -t discord-bot:latest .
docker run --rm -v logs:/var/logs discord-bot
python -m ensurepip
python -m venv .venv
source pyenv/bin/activate
pip install -r opt/requirements.txt
python main.py
How break up large goupcogs or add group cog from other py files?
/parent child child_cmd-1
app/
├── cogs/
├── ChildGroup.py
├── ParentGroup.py
├── ParentCog.py
└── main.py
main.py
importdiscordfromdiscord.extimportcommandsfromconfigs.configimportMODS, TOKEN, PREFIXfromlibs.loggerimportloggerPREFIX='>'TOKEN='TOKEN'MODS= ['ParentCog']
classMyBot(commands.Bot):
def__init__(self) ->None:
super().__init__(command_prefix=PREFIX, intents=discord.Intents.all(), help_command=None)
asyncdefsetup_hook(self) ->None:
formodinMODS:
logger.info(f'Loading {mod}')
awaitself.load_extension(f'cogs.{mod}') # name of moduleasyncdefclose(self) ->None:
awaitsuper().close()
bot=MyBot()
bot.run(TOKEN)ParentCog.py
importdiscordfromdiscord.extimportcommandsfromconfigs.configimportGUILD_IDfromcogs.ChildGroupimportChildGroupfromcogs.ParentGroupimportParentGroupGUILD_ID=000000000000000000classParentCog(ParentGroup,
ChildGroup,
name="parent", description='parent commands'):
"""All commands and subcommands for the parent command group """asyncdefsetup(bot: commands.Bot) ->None:
awaitbot.add_cog(ParentCog(bot), guilds=[discord.Object(id=GUILD_ID)])ParentGroup.py
importdiscordfromdiscordimportapp_commandsfromdiscord.extimportcommandsclassParentGroup(commands.GroupCog, name="someparent"):
def__init__(self, bot: commands.Bot) ->None:
self.bot=bot@app_commands.command(name="parent_command-1")asyncdefmy_sub_command_1(self, interaction: discord.Interaction) ->None:
awaitinteraction.response.send_message("Hello from parent_command 1", ephemeral=True)ChildGroup.py
importdiscordfromdiscordimportapp_commandsfromdiscord.extimportcommandsclassChildGroup(commands.GroupCog):
child=app_commands.Group(name='child', description='child commands')
def__init__(self, bot: commands.Bot) ->None:
self.bot=bot@child.command(name="child_command-1")asyncdefmy_child_command_1(self, interaction: discord.Interaction) ->None:
awaitinteraction.response.send_message("Hello from sub command 1", ephemeral=True)