解析Discord-Tag以编程方式获取Discord-ID [英] Resolve Discord-Tag get Discord-ID programmatically

查看:522
本文介绍了解析Discord-Tag以编程方式获取Discord-ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的用户配置文件中添加一个功能,用户可以输入他们的discord标签,如果这样做,我想将其解析为指向他们的discordprofile的链接,这样用户只需单击discordtag即可打开配置文件。

I want to add a function to my userprofiles where users can enter their discord tag and if they do so, I want to resolve this to a link to their discordprofile so that users only have to click on the discordtag to open the profile in discord.

为此,我需要ID。我需要发送什么请求到discord api才能获得输入标签的用户ID?

For that I need the ID. What Request do I need to send to the discord api in order to get the user ID for the entered tag?

推荐答案

与不和谐的朋友进行实验。我发现您实际上可以通过发送朋友请求来获取用户ID。

After some experiments with discord friend sending. I found out that you can actually obtain user the user ID by sending friend request.

方法如下:


  1. 向example#1234

  1. Make a add request friend request to example#1234

做出添加请求好友请求对关系(AKA好友)列表进行另一个请求以获取所有待处理的 ; friends以及ID,用户名,头像...该列表实际上包含发送朋友请求的人的所有实际朋友。

Make another request to the relationship(AKA friend) list to get all pending "friends" with ID, username, avatar... This list actually contains all of the actual friends from the person that sent a friend request.

收件人在好友列表中找到所需的用户名,您需要做的就是循环搜索相应的用户名和标识符。

To find the requested username in the friend list, all you need is a loop searching for the corresponding username + discriminator.

输出ID(如果这就是您想要的),在用户名和区分符匹配之后。

Output the ID(if that's what you wanted), after the username and discriminator match.

删除待处理的请求。

这是我编写的python脚本,它将通过输入用户名,鉴别符和用户令牌(用于发送授权的好友请求)来输出用户的ID ):

Here's a python script I wrote that will output the ID of the user with inputs of username, discriminator, and an user token(used for sending an authorized friend request):

import requests
import json

# inputs
username = 'asdf'
discriminator = '1234'
TOKEN = 'ONLY USER TOKEN'

url = 'https://discord.com/api/v8/users/@me/relationships'

headers = {
    "authorization": TOKEN
}

# setting up a payload for sending friend request.
payload = {
    'username': username,
    'discriminator': discriminator
}

requests.post(url, json=payload, headers=headers) # step 1

result = requests.get(url, headers=headers).json() # step 2
if hasattr(result, 'message'):
    print('Invalid user token')
else:
    user_id = None

    for client in result: # step 3: a loop for finding the the username in the friend list
        if f'{client["user"]["username"]}#{client["user"]["discriminator"]}' == f'{username}#{discriminator}':
            user_id = client['id'] # step 4: save the user ID after finding it in the friend list
            break

    if user_id is None: # if no match is found then the user with that username and discriminator does not exist.
        print('user not found')
    else:
        url = f'https://discord.com/api/v8/users/@me/relationships/{user_id}'

        requests.delete(url, headers=headers) # step 5: delete the pending request

        print(user_id) # print out the user ID

这是步骤2中请求的json的数据结构:

And here's the data structure of the requested json from step 2:

[
    {
        "id": "12345678901",
        "type": 1,
        "nickname": null,
        "user": {
            "id": "12345678901",
            "username": "example1",
            "avatar": "1234567890abcdef",
            "discriminator": "1234",
            "public_flags": 123
        }
    },
    {
        "id": "12345678902",
        "type": 1,
        "nickname": null,
        "user": {
            "id": "12345678902",
            "username": "example2",
            "avatar": "1234567890abcdef",
            "discriminator": "1234",
            "public_flags": 123
        }
    },
    {
        "id": "12345678903",
        "type": 1,
        "nickname": null,
        "user": {
            "id": "12345678903",
            "username": "example3",
            "avatar": "1234567890abcdef",
            "discriminator": "1234",
            "public_flags": 123
        }
    }
]

缺点:

您必须使用用于发送好友请求的用户令牌

2020年10月4日:在错误检测中添加了无效的令牌和无效的用户名。

10/4/2020: Added in error detection for invalid token and invalid username.

这篇关于解析Discord-Tag以编程方式获取Discord-ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆