3

I auth my bot via OAuth2 URL (https://discordapp.com/api/oauth2/authorize?client_id=398437519408103444&permissions=59392&scope=bot)

I want send message to channel at my discord server

Code:

$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));

But response is:

array(2) {
  ["code"]=>
  int(40001)
  ["message"]=>
  string(12) "Unauthorized"
}

API link: https://discordapp.com/developers/docs/resources/channel#create-message

Dmitriy
  • 159
  • 2
  • 12
  • 1
    Read [this](https://discordapp.com/developers/docs/reference) or better, [use a library](https://github.com/discordapp/discord-api-docs/blob/master/docs/topics/Community_Resources.md). – Charlotte Dunois Jan 08 '18 at 00:09
  • Especially [the docs related to opcodes](https://discord.com/developers/docs/topics/opcodes-and-status-codes): `4001 Unknown opcode You sent an invalid Gateway opcode or an invalid payload for an opcode. Don't do that!` – dan1st Nov 09 '20 at 12:20

3 Answers3

1

Turns out you just need to connect to the gateway with the auth token once and it should allow sending messages through the api from that point forward. I used the html file in the link to do this once and all good from then on.

https://github.com/restcord/restcord/blob/master/extra/gateway.html

Original github issue that lead me to this is here https://github.com/restcord/restcord/issues/68

Ryan Worth
  • 151
  • 2
  • 9
0

From what I could make out from the documentation; Discord wants the bot to get logged into the Discord Server and show its presence there first. Only then will Discord allow the Bot to send messages using its API, into the same server.

The documentation is not very clear -- It looks to me that a Bot cannot conventionally login into a Server. This is because a Bot does not have an email address. It is only given a special token.

I guess one of the built in libraries that Discord provides should be able to do this. Whatever I could make out, your bot needs to find out the Websocket gateway it has to connect to first.

This API call works and it does provide you the gateway end point https://discord.com/api/v6/gateway

But I am not good at websocket connection (wss:// protocol) so I skipped that step and moreover, I was impatient.

Luckily I got one route to solve the problem. That was to download the Electron based Windows executable called "LiveBot" from here: https://github.com/SebOuellette/LiveBot

Warning: Thats a very large ZIP file there as Electron based executables usually are. Just start LiveBot on a Windows machine; and provide your Bot token there.

That will make the Bot behave like a regular user on your server. Once Discord comes to know that your Bot was able to connect to the server it is attached to; then after that point it will allow messages to be sent.

My experience states that if your Bot has NOT been able to connect to your server at least once, you will always get a 40001 error code. Just the way you described.

This is not a programmatic approach but it worked for me. Since only one connection was needed, that is all I did. After that I was able to use the REST API using plain old Curl to send out direct messages; instead of one of the Discord libraries.

Hope this works for you too.

S. Francis
  • 119
  • 5
-1

POST/channels/{channel.id}/messages

Before using this endpoint, you must connect to and identify with a gateway at least once.

Post a message to a guild text or DM channel. If operating on a guild channel, this endpoint requires the 'SEND_MESSAGES' permission to be present on the current user.

Does your bot have the 'SEND_MESSAGES' permission in that channel?

no? well goto the channel settings and give it permission.

Has your bot connected and identified with the gateway at least one?

no? well you should probably go do that, best to use a library already made for it.

If this issue still persists despite attempting the solutions above perhaps you are malforming your POST request somehow.

mental
  • 836
  • 6
  • 18