我正在嘗試了解 Discord API 的作業原理,并且正在努力在頻道中發送訊息。代碼執行良好,但沒有發送訊息。我試過這段代碼:
await request(`https://discord.com/api/v9/channels/${channel_id}/messages`, {
method: "POST",
body: content,
headers: this.headers,
});
但它不起作用。我究竟做錯了什么?
content是我要發送的字串,它是一個函式引數,channel_id.
標題是:
{
authorization: `Bot ${token}`,
accept: "*/*",
};
該請求回傳一個狀態碼400(錯誤請求)。
uj5u.com熱心網友回復:
我解決了。問題是我沒有指定Content-Type請求的內容,而是將內容作為字串傳遞。我添加了headers: "content-type": "application/json",并在正文中傳遞了內容的 Json 物件:
await request(`https://discord.com/api/v9/channels/${channel_id}/messages`, {
method: "POST",
headers: this.headers,
body: json({ content }),
});
和標題:
this.headers = {
authorization: `Bot ${token}`,
"content-type": "application/json",
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/524252.html
