【问题标题】:How i can add attachment to discord requests?我如何向不和谐请求添加附件?
【发布时间】:2021-10-17 18:29:52
【问题描述】:
我正在尝试制作发送不和谐消息 + 附件的 python 脚本,但我不知道该怎么做。我还需要从我的不和谐帐户发送消息,而不是从机器人/网络钩子发送消息。到目前为止,这是我的代码:
payload = {
'content': "Hello"
}
header = {
'authorization': "token"
}
r = requests.post("https://discord.com/api/v9/channels/874777490781003787/messages?limit=50", data=payload, headers=header)
【问题讨论】:
标签:
python
api
python-requests
discord
【解决方案1】:
用discord.py代替请求库的单线解决方案:
with open('my_image.png', 'rb') as f: channel.send(file=discord.File(f))
如果您真的必须使用 requests 库,则必须使用 Content-Type: multipart/form-data 上传文件,文件属性为 Content-Disposition: form-data; name="file"; filename="your_attachment.png" Content-Type: image/png,消息属性为 Content-Disposition: form-data; name="payload_json" {"content":"your_content","tts":false}。
所以把它们放在一个像这样嵌套在字典中的元组中:
files = {
'file': ('./picture.jpg', open('./picture.jpg', 'rb')),
}
r = requests.post("https://discord.com/api/v9/channels/874777490781003787/messages?limit=50", data=payload, headers=header, files=files)
在此处阅读docs。