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
import discord
from discord.ext import commands
from configs.config import MODS, TOKEN, PREFIX
from libs.logger import logger
PREFIX = '>'
TOKEN = 'TOKEN'
MODS = ['ParentCog']
class MyBot(commands.Bot):
def __init__(self) -> None:
super().__init__(command_prefix=PREFIX, intents=discord.Intents.all(), help_command=None)
async def setup_hook(self) -> None:
for mod in MODS:
logger.info(f'Loading {mod}')
await self.load_extension(f'cogs.{mod}') # name of module
async def close(self) -> None:
await super().close()
bot = MyBot()
bot.run(TOKEN)ParentCog.py
import discord
from discord.ext import commands
from configs.config import GUILD_ID
from cogs.ChildGroup import ChildGroup
from cogs.ParentGroup import ParentGroup
GUILD_ID = 000000000000000000
class ParentCog(ParentGroup,
ChildGroup,
name="parent", description='parent commands'):
"""All commands and subcommands for the parent command group """
async def setup(bot: commands.Bot) -> None:
await bot.add_cog(ParentCog(bot), guilds=[discord.Object(id=GUILD_ID)])ParentGroup.py
import discord
from discord import app_commands
from discord.ext import commands
class ParentGroup(commands.GroupCog, name="someparent"):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
@app_commands.command(name="parent_command-1")
async def my_sub_command_1(self, interaction: discord.Interaction) -> None:
await interaction.response.send_message("Hello from parent_command 1", ephemeral=True)ChildGroup.py
import discord
from discord import app_commands
from discord.ext import commands
class ChildGroup(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")
async def my_child_command_1(self, interaction: discord.Interaction) -> None:
await interaction.response.send_message("Hello from sub command 1", ephemeral=True)