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

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

问题描述

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

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

向关系(又名好友)列表发出另一个请求,以获取所有待处理的好友"列表包括 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
        }
    }
]

缺点:

您必须使用用户令牌来发送好友请求.

You have to use an user token for sending the friend request.

10/4/2020:添加了无效令牌和无效用户名的错误检测.

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

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

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