Discord Developer Portal2 fields
TokenBot > Reset Token
The bot's password to the Discord gateway. Shown once.
Privileged IntentsBot
Members, presence, message content. This bot does not need them.
Discord Developer Portal2 fields
Application IDGeneral Information
The application ID, needed in the invite link.
OAuth2 URL GeneratorOAuth2
The invite link with scopes and permissions.
bash5 commands
$ mkdir bot && cd bot$ python3 -m venv .venv$ source .venv/bin/activate$ pip install "discord.py==2.7.1"$ mkdir cogs
bot.pyNew file, 30 lines
1import os2 3import discord4from discord.ext import commands5 6GUILD_ID = int(os.environ["GUILD_ID"])7 8 9class Bot(commands.Bot):10 def __init__(self) -> None:11 super().__init__(command_prefix="!", intents=discord.Intents.default())12 13 async def setup_hook(self) -> None:14 await self.load_extension("cogs.serwer")15 guild = discord.Object(id=GUILD_ID)16 self.tree.copy_global_to(guild=guild)17 synced = await self.tree.sync(guild=guild)18 print(f"Zsynchronizowane komendy: {len(synced)}")19 20 21bot = Bot()22 23 24@bot.event25async def on_ready() -> None:26 print(f"Zalogowano jako {bot.user} (ID {bot.user.id})")27 28 29if __name__ == "__main__":30 bot.run(os.environ["DISCORD_TOKEN"])
cogs/serwer.pyNew file, 26 lines
1import discord2from discord import app_commands3from discord.ext import commands4 5 6class Serwer(commands.Cog):7 def __init__(self, bot: commands.Bot) -> None:8 self.bot = bot9 10 @app_commands.command(name="ping", description="Sprawdza, czy bot odpowiada")11 async def ping(self, interaction: discord.Interaction) -> None:12 delay = round(self.bot.latency * 1000)13 await interaction.response.send_message(f"Pong, {delay} ms", ephemeral=True)14 15 @app_commands.command(name="serwer", description="Podstawowe informacje o serwerze")16 @app_commands.guild_only()17 async def serwer(self, interaction: discord.Interaction) -> None:18 guild = interaction.guild19 embed = discord.Embed(title=guild.name, color=0x1679FA)20 embed.add_field(name="Członkowie", value=str(guild.member_count))21 embed.add_field(name="Założony", value=discord.utils.format_dt(guild.created_at, "D"))22 await interaction.response.send_message(embed=embed)23 24 25async def setup(bot: commands.Bot) -> None:26 await bot.add_cog(Serwer(bot))
bash3 commands
$ export DISCORD_TOKEN="token-z-zakladki-bot"$ export GUILD_ID="123456789012345678"$ python bot.py
cogs/raport.pyNew file, 5 lines
1@app_commands.command(name="raport", description="Raport, który liczy się dłużej")2async def raport(self, interaction: discord.Interaction) -> None:3 await interaction.response.defer(thinking=True) 4 wynik = await policz_raport() 5 await interaction.followup.send(wynik)
Dockerfile, docker-compose.ymlNew file, 13 lines
12FROM python:3.12-slim3WORKDIR /app4RUN pip install --no-cache-dir "discord.py==2.7.1"5COPY . .6CMD ["python", "-u", "bot.py"]7 89services:10 bot:11 build: .12 restart: always13 env_file: .env
bot.pyChanged in the file: 1 line
1async def setup_hook(self) -> None:2 await self.load_extension("cogs.serwer")3 synced = await self.tree.sync() 4 print(f"Zsynchronizowane komendy: {len(synced)}")