【发布时间】:2022-10-23 00:52:58
【问题描述】:
我想做一个简单的不和谐机器人。除了我希望它在特定时间发送消息这一事实之外,它的作用并不太重要。下面的代码非常基础,不是成品。
# bot.py
import os
import discord
from dotenv import load_dotenv
intents = discord.Intents().all()
intents.messages = True
load_dotenv()
TOKEN = os.getenv("FAKETOKENBLAHBLAHBLAH")
client = discord.Client(command_prefix=',', intents=discord.Intents().all())
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
难道我做错了什么?如果是这样,请告诉我。我已经被难住了好几个小时了,我唯一可以参考的是这个错误:
TypeError: expected token to be a str, received <class 'NoneType'> instead
我了解回溯中对模块和行号的其他引用,但我就是不明白哪里出错了,因为我没有编写 discord.py 模块。
我已经阅读了很多关于它的文章,但似乎没有一篇有效。我读的第一篇文章告诉我将“discord.Client()”的参数完全留空,这给了我这个错误:
TypeError: Client.__init__() missing 1 required keyword-only argument: 'intents'
那来自链接:https://realpython.com/how-to-make-a-discord-bot-python/#creating-a-discord-account
我不明白,但我绝对知道最大的问题是关于 discord.Client() 的参数以及与意图有关的事情。
我觉得我还应该附上我的 .env 代码:
#.env
DISCORD_TOKEN={FAKETOKENBLAHBLAHBLAH}
更新: 我修复了这段代码:
TOKEN = os.getenv("DISCORD_TOKEN")
但是,现在它给了我这个例外:
[2022-08-22 01:20:03] [INFO ] discord.client: logging in using static token
Traceback (most recent call last):
File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\http.py", line 801, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\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 "C:\Users\domin\AppData\Local\Programs\Python\Python310\Projects\Discord Bot Text Game\bot.py", line 19, in <module>
client.run(TOKEN)
File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 828, in run
asyncio.run(runner())
File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete
return future.result()
File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 817, in runner
await self.start(token, reconnect=reconnect)
File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 745, in start
await self.login(token)
File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 580, in login
data = await self.http.static_login(token)
File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\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.
我该如何解决这个问题?我需要生成新令牌吗?
【问题讨论】:
-
欢迎来到堆栈溢出。请阅读How to Ask 和ericlippert.com/2014/03/05/how-to-debug-small-programs。你有没有试过检查会发生什么
TOKEN = os.getenv("FAKETOKENBLAHBLAHBLAH")何时运行?TOKEN的结果值是多少?这是你所期望的吗? “我所参考的只是这个错误:”这不是真的。至少会有一个追溯- 以Traceback (most recent call last):开头的那一行告诉你在哪里在程序中发生错误。 -
“我已经阅读了很多关于它的文章,但似乎没有一篇有效!”告诉我们这些是没有用的。相反,请告诉我们:您具体阅读了哪些文章?读完之后,你从他们那里了解到了什么?所以,你试图做出什么改变到代码?发生了什么因此?
-
请关闭此问题,您自己找到了答案。对于剩下的问题,请提出一个新问题。
标签: python discord.py bots