Discord Developer Portal2 pola
TokenBot > Reset Token
Hasło bota do bramki Discorda. Pokazywany raz.
Privileged IntentsBot
Członkowie, obecność, treść wiadomości. Ten bot ich nie potrzebuje.
Discord Developer Portal2 pola
Application IDGeneral Information
Identyfikator aplikacji, potrzebny w linku zaproszenia.
OAuth2 URL GeneratorOAuth2
Link zaproszenia z zakresami i uprawnieniami.
bash5 poleceń
$ mkdir bot && cd bot$ python3 -m venv .venv$ source .venv/bin/activate$ pip install "discord.py==2.7.1"$ mkdir cogs
bot.pyNowy plik, 30 linii
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.pyNowy plik, 26 linii
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 polecenia
$ export DISCORD_TOKEN="token-z-zakladki-bot"$ export GUILD_ID="123456789012345678"$ python bot.py
cogs/raport.pyNowy plik, 5 linii
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.ymlNowy plik, 13 linii
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.pyZmiana w pliku: 1 linia
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)}")