[英]How can I solve authentication error in discord.py
I'm making discord bot using discord.py.我正在使用 discord.py 制作不和谐机器人。 But when I turned on the bot, it gets error discord.errors.ConnectionClosed: WebSocket connection is closed: code = 4004 (private use), reason = Authentication failed.但是当我打开机器人时,它得到错误 discord.errors.ConnectionClosed: WebSocket connection is closed: code = 4004 (private use), reason = Authentication failed. Is there any way to solve this problem?有没有办法解决这个问题?
I already tried to generate new tokens, or make new bots.我已经尝试过生成新的令牌,或者制作新的机器人。 And the code which I'm using now was run successfully before.我现在使用的代码之前运行成功。 When I run this code in other computers(Which isn't have same ip), and it works properly.当我在其他计算机上运行此代码时(没有相同的 ip),它工作正常。 How can I solve this problem?我怎么解决这个问题?
import asyncio
import discord
app = discord.Client()
def get_token(): # Get tokens from key.key
global token # This part works properly
f = open("Key.key", "r")
token = str(f.readline())
@app.event
async def on_ready(): #Login Part
print("Logining to : ")
print(app.user.name)
print(app.user.id)
print("==========")
game = discord.Game("Bot is working properly!")
await app.change_presence(status=discord.Status.online, activity=game)
@app.event
async def on_message(message):
if message.author.bot:
return None
if message.content == "!hello":
await message.channel.send("hello?")
get_token()
app.run(token)
This is my source code, and below one is traceback这是我的源代码,下面是回溯
File "d:\Code\Project\discord_bot\Koi_Bot_Discord\Main.py", line 30, in <module>
app.run(token)
File "D:\Python\lib\site-packages\discord\client.py", line 598, in run
return future.result()
File "D:\Python\lib\site-packages\discord\client.py", line 579, in runner
await self.start(*args, **kwargs)
File "D:\Python\lib\site-packages\discord\client.py", line 543, in start
await self.connect(reconnect=reconnect)
File "D:\Python\lib\site-packages\discord\client.py", line 457, in connect
await self._connect()
File "D:\Python\lib\site-packages\discord\client.py", line 421, in _connect
await self.ws.poll_event()
File "D:\Python\lib\site-packages\discord\gateway.py", line 476, in poll_event
raise ConnectionClosed(exc, shard_id=self.shard_id) from exc
discord.errors.ConnectionClosed: WebSocket connection is closed: code = 4004 (private use), reason = Authentication failed.
I would recommend using environment variables but if you insist on reading from a file, try:我建议使用环境变量,但如果您坚持从文件中读取,请尝试:
def get_token(): # Get tokens from key.key
with open("Key.key", "r") as f:
return f.readline().strip()
...
app.run(get_token())
It is likely that you are getting a newline character from your readline
, so strip
will remove that, but having the function return your token is better practice.您可能会从readline
获取换行符,因此strip
将删除它,但让函数返回您的令牌是更好的做法。
使用“python -m pip install -U discord.py”更新discord模块是一个解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.