英文:
getting 400 code while joining discord servers with discord API using requests python
问题
我的经验:
> 在开发一个使用Python和Requests模块加入Discord服务器的功能时,我一直收到错误代码:400。
我的代码:
def joiner():
invite_code = input("输入邀请码:")
with open('tokens.json', 'r') as file:
tokens = json.load(file)
tokens = tokens.get('token', [])
for token in tokens:
headers = {
"Authorization": f"{token}"
}
url = f"https://discord.com/api/v9/invites/{invite_code}"
if "discord.gg/" in url:
url = url.removeprefix('discord.gg/')
response = requests.post(url, headers=headers)
if response.status_code == 200:
print(f"令牌:{token} 成功加入了服务器!")
else:
print(f"使用令牌加入服务器失败:{token}。状态码:{response.status_code}")
追踪:
> 使用令牌加入服务器失败:<token>。状态码:400
(请注意,追踪不是100%真实的,我做了一个修改,用<token>
替换了真实的令牌)
英文:
What I'm experiencing:
> While developing a function to join discord servers using Python and Requests module, I keep getting the error code : 400.
My code:
def joiner():
invite_code = input("Enter the invite code: ")
with open('tokens.json', 'r') as file:
tokens = json.load(file)
tokens = tokens.get('token',[])
for token in tokens:
headers = {
"Authorization": f"{token}"
}
url = f"https://discord.com/api/v9/invites/{invite_code}"
if "discord.gg/" in url:
url = url.removeprefix('discord.gg/')
response = requests.post(url, headers=headers)
if response.status_code == 200:
print(f"Token: {token} has successfully joined the server!")
else:
print(f"Failed to join the server with token: {token}. Status code: {response.status_code}")
Traceback:
> Failed to join the server with token: <token>. Status code: 400
(Please note that the traceback isn't 100% the real one, there is one modification that I made and it's replacing the real token by <token>
)
答案1
得分: 1
在这种情况下,您应该检查Discord在response.text
中返回的错误消息,例如,在底部添加:
if 400 <= resp.status_code < 500:
print(response.text, file=sys.stderr)
英文:
In this situation, you should examine the error message that Discord returns in response.text
, e.g. at the bottom, add:
if 400 <= resp.status_code < 500:
print(response.text, file=sys.stderr)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论