我正在 YouTube 上关注 Python Discord 机器人的基本教程,我的代码在下面。它说:
discord.errors.LoginFailure:传递了不正确的令牌。
在有人问之前,是的,我已经输入了机器人令牌,而不是 ID 或秘密。
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot(command_prefix = "!")
@client.event
async def on_ready():
print("Bot is ready!")
@client.event
async def on_message(message):
if message.content == "cookie":
await client.send_message(message.channel, ":cookie:")
client.run("token is here")
对我来说,我的
bot.py
文件如下所示:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
由于我使用了 env(环境)模块,因此我在文件夹中的同一路径中创建了一个名称为空且扩展名为 .env 的新文件。 env文件只有这行代码:
DISCORD_TOKEN=DFHKJAhka7fdsKHJASDFk1jhaf5afd.HASDFafd23FHdfa_adfahHJKADF32W
所以对我来说问题是我在令牌代码周围使用括号,删除它后,它起作用了!
我的代码有括号时:
DISCORD_TOKEN={DFHKJAhka7fdsKHJASDFk1jhaf5afd.HASDFafd23FHdfa_adfahHJKADF32W}
确保您从 Discord 开发网站的“机器人”页面获取“令牌”,而不是从“一般信息”页面获取“秘密”。
我也遇到了同样的问题。我的问题已通过使用 Discord 应用页面中的正确令牌得到解决。我使用的是“一般信息”页面中的“秘密”(它为我生成了原始帖子中的错误),而不是“机器人”页面中的“令牌”。
正如 sheneb 在对此的评论中所说,这个答案(可能)不会对 OP 有帮助(因为现在的问题是“在任何人询问之前,是的,我已经输入了机器人令牌,而不是 id 或秘密”)。但是,我在搜索答案时发现了这个问题/页面,并且我的问题通过这些信息得到了解决。
同样的事情也发生在我身上,但是当我进入开发者页面并刷新令牌时它开始工作。然后我只需将新令牌放入代码中就可以了!
也许同样对你有用......?
您应该能够通过这样做让它工作:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
bot = commands.Bot(command_prefix = "!")
@bot.event
async def on_ready():
print("Bot is ready!")
@bot.event
async def on_message(message):
if message.content == "cookie":
await message.channel.send(":cookie:")
bot.run("token is here")
我有时会这样做,但也会通过查看开发者页面上的“机器人”选项卡来检查以确保您已完全创建机器人。
我还修复了您的消息发送行、机器人声明,并删除了您从未实际使用过的
discord.Client()
实例。
试试这个:
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot(command_prefix = "!")
@client.event
async def on_ready():
print("Bot is ready!")
@client.event
async def on_message(message):
if message.content == "cookie":
await message.client.send(":cookie:")
client.run("TOKEN HERE. PUT YOUR TOKEN HERE ;)")
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot(command_prefix = "!")
@client.event
async def on_ready():
print("Bot is ready!")
@client.event
async def on_message(message):
if message.content == "cookie":
await message.client.send(":cookie:")
client.run("PUT YOUR TOKEN HERE")