Created 05_Python-Basic-Discord-Bot.md - First basic Bot
This commit is contained in:
93
Einführung/05_Python-Basic-Discord-Bot.md
Normal file
93
Einführung/05_Python-Basic-Discord-Bot.md
Normal file
@@ -0,0 +1,93 @@
|
||||
# Python Basics - Discord Bot
|
||||
|
||||
Die Bibliothek welche wir für dieses Beisiel verwenden lautet [`py-cord`](https://pycord.dev/).
|
||||
Du kannst pycord installieren indem du folgenden Befehl im Terminal ausführst: `python3 -m pip install py-cord`
|
||||
|
||||
## Aufbau des Codes
|
||||
|
||||
### Imports
|
||||
An dieser Stelle importiert man alle, für das Projekt benötigten, Bibliotheken. Z.B.:
|
||||
> ```
|
||||
> import discord
|
||||
> from discord.ext import commands
|
||||
> ```
|
||||
|
||||
### Intents
|
||||
Als Nächstes gibt man an, wie der Bot Nutzerdaten verarbeitet. In diesem Beispiel reichen die `defaults`:
|
||||
|
||||
> ```
|
||||
> intents = discord.Intents.default()
|
||||
> intents.members = True
|
||||
> intents.message_content = True
|
||||
> ```
|
||||
|
||||
### Bot Variable deklarieren
|
||||
Hier wird die bot-Variable deklariert.
|
||||
Außerdem wird noch der Prefix festgelegt (!), die Botbeschreibung wird hinzugefügt und die zuvor eingestellten intents werden übergeben.
|
||||
> ```
|
||||
> bot = commands.Bot(
|
||||
> command_prefix=commands.when_mentioned_or("!"),
|
||||
> description=description,
|
||||
> intents=intents)
|
||||
> ```
|
||||
|
||||
### Bot Events (@)
|
||||
Sogenannte Events werden mit dem `@bot.event` Dekorator versehen. Dieser "Dekoriert" sozusagen den darunterliegenden Code.
|
||||
Ein Beispiel für ein Bot-Event, wenn der Bot eingeloggt und bereit ist:
|
||||
|
||||
> ```
|
||||
> @bot.event
|
||||
> async def on_ready():
|
||||
> print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
||||
> print("------")
|
||||
> ```
|
||||
|
||||
### Bot Commands
|
||||
Um ein Command zu erstellen gibt es auch wieder einen Dekorator. Commands werden mit dem `@bot.command()` Dekorator versehen.
|
||||
Ein Beispiel für einen Command um zwei Strings miteinander zu verbinden:
|
||||
|
||||
> ```
|
||||
> bot.command()
|
||||
> async def add(ctx: commands.Context, left: int, right: int):
|
||||
> """Adds two numbers together."""
|
||||
> await ctx.send(str(left + right))
|
||||
> ```
|
||||
|
||||
### Last but not least `bot.run()`
|
||||
`bot.run("TOKEN")` kommt **immer** an das Ende deines Codes.
|
||||
Dieser Codeschnipsel bildet den Abschluss.
|
||||
|
||||
|
||||
### Das Beispiel einmal zusammengesetzt
|
||||
```
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
description = """
|
||||
An example bot to showcase the discord.ext.commands extension module.
|
||||
There are a number of utility commands being showcased here.
|
||||
"""
|
||||
|
||||
intents = discord.Intents.default()
|
||||
intents.members = True
|
||||
intents.message_content = True
|
||||
|
||||
bot = commands.Bot(
|
||||
command_prefix=commands.when_mentioned_or("!"),
|
||||
description=description,
|
||||
intents=intents)
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
||||
print("------")
|
||||
|
||||
|
||||
@bot.command()
|
||||
async def add(ctx: commands.Context, left: int, right: int):
|
||||
"""Adds two numbers together."""
|
||||
await ctx.send(str(left + right))
|
||||
|
||||
bot.run("TOKEN")
|
||||
```
|
||||
Reference in New Issue
Block a user