#๐ How to send "create message" events to discord websocket as JSON?
43 messages ยท Page 1 of 1 (latest)
@slim pine
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
send to discord websocket? are you trying to create your own discord bot framework?
want framework? No, I experiment with the discord api thats it and understand it:
while True:
events = receive_data(ws)
print(events)
if events["t"] == "READY":
print("Bot has started.....")
Hey @slim pine!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
when event["t"] == "MESSAGE_CREATE" i want to send a message
like a reply
I tried with message = {"content" : "Hello world"} but when I send it , the websocket closes
send_data(ws, message)
that is fairly advanced - I would strongly recommend using a framework like discord.py or hikari instead of implementing it yourself
alternatively, you can try looking at their source code to see how they implement it and use that as a base for your own project
I receive events in the while loop
when I update channel or send messages on my test discord
but I dont want to receive always I also want to send
It's closing because the websocket does not expect that input. For reference, the websocket is practically only used for identifying yourself, resuming connections and acknowledging heartbeats.
For sending messages, you use the REST API: POST /channels/{channel.id}/messages
ah ok, yeah that I have done yesterday with the requets library
as a test
so you mean the websocket doesnt want any inputs?
it gives me inputs to work with?
I receive json data that I then convert into a python obj
You can send data to the gateway. However, they are in a specific format and are again only for a few events (docs).
MESSAGE_CREATE is a Receive-only event. You can't send those.
ok ok yeah your right. I see it. Than I must work with POST DELETE GET PATCH PUT
Yeah. The docs are pretty clear on what you can do with the gateway vs the REST API.
I think simply said when getaway event is qual to an event like MESSAGE_CREATE I can extract some datas store it in variables and fire it with the right link with requests.get post delete ....
elif events["t"] == "MESSAGE_CREATE":
requests.post("https://discord.com/api/channels/mychannelid/messages", json={"content":"hello world"},headers=header)
would you approve this?
That seems fine, although you'd ofc replace the channel id with the one you get from the API.
It will send it each time your bot sees a new message. Given that sending a message produces the event, it sees your bot's messages and makes it enter into a sort of loop.
That's why people check if their messages weren't sent by their own bot.
Yeah, that's what I mentioned earlier. You'd handle it like you typically would when doing a Discord bot (here's the message object for reference).
I will acknowledge etrotta's previous mention that the Discord API can get fairly complex and you should look into the documentation which tends to be pretty clear on most topics. You can also refer to other existing frameworks and there's also the Discord Developers server if you have any doubts (we have a thread too: https://discord.com/channels/267624335836053506/965291516031549500)
ah thanks I dont want to build a framework or something else I want to just experiment with it
and btw it is good to know what discord py makes under the hood @open wadi
thanks a lot guys
my todays accomplishements are connect to the getaway , sending heartbeats in an other thread , send the identifier and receiving events
Sure, knowing how your favorite libraries work is definitely great! 
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.