使用请求与 Discord 按钮交互 (Python)

Interacting with Discord buttons using requests (Python)

我正在尝试使用 requests (python) 自动点击 discord 按钮,但每次测试时都会收到错误“400”。 我找到代码 here 并添加了一些修改,但它不起作用。任何想法如何解决它?我的代码:

import requests
import time
import json
header = {'authorization': "authtoken"}

def buttonclicker():
    ButtonCLicker = {'content': "test"}
    ReqClicker = requests.post('https://discordapp.com/api/v9/channels/968490796116492310/messages',data=ButtonClicker,headers=header)
    time.sleep(2)
    r = requests.get("https://discord.com/api/v9/channels/968490796116492310/messages", headers = header)
    JData = json.loads(r.text)[0]
    print(JData,"\n-------------")
    data = {
        "type": 3,
        "guild_id": 'XXX',
        "channel_id": 'XXX',
        "message_id": JData['id'], 
        "application_id": 'XXX', 
        "data": {
                "component_type": 2,
                "custom_id": JData['components'][0]['components'][1]['custom_id'] 
                }
            }
    print(data, "\n--------")
    r = requests.post('https://discord.com/api/v9/interactions', json = data, headers = header)
    print(r.json)
buttonclicker()

来自 print(r.json) 的错误:

<bound method Response.json of <Response [400]>>

您需要 session_id 数据

试试这个

import random, string
import requests
import time
import json
header = {'authorization': "authtoken"}

def buttonclicker():
    ButtonCLicker = {'content': "test"}
    ReqClicker = requests.post('https://discordapp.com/api/v9/channels/968490796116492310/messages',data=ButtonClicker,headers=header)
    time.sleep(2)
    r = requests.get("https://discord.com/api/v9/channels/968490796116492310/messages", headers = header)
    JData = json.loads(r.text)[0]
    sessionID = "".join(random.choice(string.ascii_letters + string.digits) for _ in range(32))
    print(JData,"\n-------------")
    data = {
        "type": 3,
        "guild_id": 'XXX',
        "channel_id": 'XXX',
        "message_id": JData['id'], 
        "application_id": 'XXX',
        "session_id": sessionID, 
        "data": {
                "component_type": 2,
                "custom_id": JData['components'][0]['components'][1]['custom_id'] 
                }
            }
    print(data, "\n--------")
    r = requests.post('https://discord.com/api/v9/interactions', json = data, headers = header)
    print(r.json)
buttonclicker()