Discord Bot funktioniert nicht

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
user.py
User
Beiträge: 2
Registriert: Sonntag 28. Mai 2023, 11:16

Moin moin,
Ich bin neu in Python und meine Tochter wollte ein Discord Bot programmieren!
Der funktioniert aber nicht so wie er soll! Genauer gesagt, der Bot geht gar nicht erst online!
Das ist der Code zum Bot!
##############################################################################

import discord
from discord.ext import commands
import os

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='>',intents=intents)

@bot.command()
async def ping(ctx):
await ctx.send('pong')

TOKEN = os.environ['TOKEN']

bot.run("TOKEN")

###############################################################################

Wenn ich den Code ausführe erhalte ich diese Ausgabe:

INFO discord.client logging in using static token
Traceback (most recent call last):
File "/home/runner/dcBot/venv/lib/python3.10/site-packages/discord/http.py", line 801, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "/home/runner/dcBot/venv/lib/python3.10/site-packages/discord/http.py", line 744, in request
raise HTTPException(response, data)
discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "main.py", line 15, in <module>
bot.run("TOKEN")
File "/home/runner/dcBot/venv/lib/python3.10/site-packages/discord/client.py", line 860, in run
asyncio.run(runner())
File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
return future.result()
File "/home/runner/dcBot/venv/lib/python3.10/site-packages/discord/client.py", line 849, in runner
await self.start(token, reconnect=reconnect)
File "/home/runner/dcBot/venv/lib/python3.10/site-packages/discord/client.py", line 777, in start
await self.login(token)
File "/home/runner/dcBot/venv/lib/python3.10/site-packages/discord/client.py", line 612, in login
data = await self.http.static_login(token)
File "/home/runner/dcBot/venv/lib/python3.10/site-packages/discord/http.py", line 805, in static_login
raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.

################################################################################################
Kann mir jemand sagen was ich Falsch gemacht habe?
PS: Ich habe Replit benutzt!
Sirius3
User
Beiträge: 17712
Registriert: Sonntag 21. Oktober 2012, 17:20

Ihr solltet den TOKEN übergeben und nicht den String "TOKEN":

Code: Alles auswählen

import discord
from discord.ext import commands
import os

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='>', intents=intents)

@bot.command()
async def ping(ctx):
    await ctx.send('pong')

TOKEN = os.environ['TOKEN']
bot.run(TOKEN)
user.py
User
Beiträge: 2
Registriert: Sonntag 28. Mai 2023, 11:16

Danke für die schnelle Antwort!
neuer Code:
import discord
from discord.ext import commands
import os

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='>',intents=intents)

@bot.command()
async def ping(ctx):
await ctx.send('pong')

DiscordToken = os.environ['DiscordToken']

bot.run(DiscordToken)

##############################################################################
Jetzt bekomme ich diese Meldung

Traceback (most recent call last):
File "/home/runner/MyDiscordBot/venv/lib/python3.10/site-packages/discord/http.py", line 801, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "/home/runner/MyDiscordBot/venv/lib/python3.10/site-packages/discord/http.py", line 744, in request
raise HTTPException(response, data)
discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "main.py", line 15, in <module>
bot.run(DiscordToken)
File "/home/runner/MyDiscordBot/venv/lib/python3.10/site-packages/discord/client.py", line 860, in run
asyncio.run(runner())
File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
return future.result()
File "/home/runner/MyDiscordBot/venv/lib/python3.10/site-packages/discord/client.py", line 849, in runner
await self.start(token, reconnect=reconnect)
File "/home/runner/MyDiscordBot/venv/lib/python3.10/site-packages/discord/client.py", line 777, in start
await self.login(token)
File "/home/runner/MyDiscordBot/venv/lib/python3.10/site-packages/discord/client.py", line 612, in login
data = await self.http.static_login(token)
File "/home/runner/MyDiscordBot/venv/lib/python3.10/site-packages/discord/http.py", line 805, in static_login
raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.
Sirius3
User
Beiträge: 17712
Registriert: Sonntag 21. Oktober 2012, 17:20

Die Fehlermeldung ist ja immer noch die selbe, der Token DiscordToken ist nicht gültig. Nur dass diesmal der Token tatsächlich aus der Umgebungsvariable stammt, so dass jetzt nicht mehr am Python-Code liegt. Also mußt Du prüfen, ob der Token auch wirklich korrekt ist.
Antworten