我通过 OAuth2 URL (https://discordapp.com/api/oauth2/authorize?client_id=398437519408103444&permissions=59392&scope=bot
) 验证我的机器人
我想在我的 discord 服务器上发送消息到 channel
代码:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://discordapp.com/api/channels/'.$channel_id.'/messages');
curl_setopt($curl, CURLOPT_HTTPHEADER, [
// 'Content-Type: application/json',
'Authorization: Bot '.$this->token
]);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
'content' => 'test'
]));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($curl);
curl_close ($curl);
var_dump(json_decode($server_output, true));
但是响应是:
array(2) {
["code"]=>
int(40001)
["message"]=>
string(12) "Unauthorized"
}
API 链接:https://discordapp.com/developers/docs/resources/channel#create-message
最佳答案
事实证明,您只需要使用身份验证 token 连接到网关一次,从那时起它应该允许通过 api 发送消息。我使用链接中的 html 文件来完成此操作,从此一切顺利。
https://github.com/restcord/restcord/blob/master/extra/gateway.html
导致我到这里的原始 github 问题在这里 https://github.com/restcord/restcord/issues/68
https://stackoverflow.com/questions/48096094/
相关文章:
macos - 如何在 Mac 终端中使用 SoX 从文本文件中修剪特定时间的音频文件?
javascript - Angular CLI 如何全局定义变量 [window.myVar]
docker - 如何解决 "docker: Error response from daemon:
javascript - 未知提供者 : $cookies angularjs
android - 有时使用 AlarmManager 不会触发警报
react-native - 在 React Native WebView 中启用弹出窗口
c# - Dynamodb : No defined key schema. 必须为所有表定义至少包