【问题标题】:Improper token passed传递了不正确的令牌
【发布时间】:2019-01-07 05:09:15
【问题描述】:

我在 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")

【问题讨论】:

  • 确切的错误信息是什么?
  • @PatrickHaugh discord.errors.LoginFailure:通过了不正确的令牌。
  • 尝试重新生成机器人令牌。确保复制/粘贴没有向字符串添加任何空格。如果不查看令牌(您不应该分享),我看不出任何人可以提供帮助。
  • client.run("token is here", bot=True)client.run("token is here", bot=False)

标签: python token discord discord.py


【解决方案1】:
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")

【讨论】:

    【解决方案2】:

    确保您从 Discord 开发站点的“Bot”页面获取“Token”,而不是“General Information”页面中的“Secret”。

    我遇到了同样的问题。通过使用 Discord 应用页面中的正确令牌解决了我的问题。我使用的是“一般信息”页面中的“秘密”(它为我在原始帖子中生成了错误),而不是“机器人”页面中的“令牌”。

    正如 sheneb 在对此的评论中所说,这个答案(可能)不会帮助 OP(因为现在的问题是“在有人问之前,是的,我已经放入了 bot 令牌,而不是 id 或秘密”) .但是,我在搜索答案时发现了这个问题/页面,我的问题通过这些信息得到了解决。

    【讨论】:

      【解决方案3】:

      试试这个:

      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 ;)")
      

      【讨论】:

      • 解释一下。例如,想法/要点是什么?请通过editing (changing) your answer 回复,而不是在 cmets 中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来像是今天写的)。
      【解决方案4】:

      对我来说,我的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}
      

      【讨论】:

      • 我同意@zabop。假设您遵循与我相同的示例,系统会指示您将“占位符”替换为实际值。您不会被告知占位符包含大括号。删除大括号后,问题就解决了。
      • 奇怪的是,我无法通过绕过 .env 文件来让该示例正常工作。我只是将令牌作为字符串放入脚本中。而且它不喜欢它,因为它没有字符串属性或类似 strip() 的方法。
      【解决方案5】:

      您应该能够通过这样做来使其工作:

      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 is here", bot=True)
      

      我有时会这样做,但也请查看开发人员页面上的“机器人”选项卡,以确保您已完全创建了机器人。

      我还修复了您的消息发送线路。它已经过时了;)。

      【讨论】:

      • Re “你应该可以通过这样做来让它工作”:解释一下。例如,想法/要点是什么?什么是操作理论?为什么它会起作用?请通过editing (changing) your answer 回复,而不是在 cmets 中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来像是今天写的)。
      【解决方案6】:

      同样的事情发生在我身上,但是当我去开发者页面并刷新令牌时它开始工作了。然后我只是将新令牌放入代码中,它就起作用了!

      也许同样适用于你...?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-10
        • 1970-01-01
        • 1970-01-01
        • 2022-08-24
        • 2021-11-22
        • 2014-10-12
        • 1970-01-01
        相关资源
        最近更新 更多