main.py einrichten
Die main.py ist das Herzstück deines Bots. Schreib das rein:
import discord
import os
from dotenv import load_dotenv
intents = discord.Intents.default()
bot = discord.Bot(intents=intents)
if __name__ == "__main__":
for filename in os.listdir("cogs"):
if filename.endswith(".py"):
bot.load_extension(f"cogs.{filename[:-3]}")
load_dotenv()
bot.run(os.getenv("TOKEN"))
Dein erster Command!
Geh zu cogs/mycog.py und schreib das rein:
import discord
from discord.ext import commands
from discord.commands import slash_command
class MyCog(commands.Cog):
def __init__(self, bot: discord.Bot):
self.bot = bot
@slash_command()
async def hello(self, ctx: discord.ApplicationContext):
await ctx.respond(f"Hey {ctx.author.mention}")
def setup(bot: discord.Bot):
bot.add_cog(MyCog(bot))
Geh zur main.py und starte sie.
Jetzt wieder zu discord.dev und links auf "General Information" klicken. Da siehst du die "Application ID". Kopier die Nummer und gib sie hier ein.
Dein Bot sollte jetzt online sein!
Schreib /hello in den Discord Chat (falls du den Command nicht siehst, drück Strg + R in Discord und probier's nochmal.)
Hängst du fest? Komm auf unseren Discord Server!