Reputation: 1056
I created code to send messages to channel I want but there is a problem that it send message only one time and for next message we have to wait a little bit although all requests are returning correct response each time. I can consectively send message to two channels but one message in like 2-3 minutes to single channel. Am I doing something wrong or it is there some new way discord blocks the message. Please help.
import requests
token = ""
channel_id = ""
cookies = {
...
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
# 'Accept-Encoding': 'gzip, deflate, br',
# Already added when you pass json=
# 'Content-Type': 'application/json',
'Authorization': token,
'X-Super-Properties': 'eyJvcyI6IldpbmRvd3MiLCJicm93c2VyIjoiRmlyZWZveCIsImRldmljZSI6IiIsInN5c3RlbV9sb2NhbGUiOiJlbi1VUyIsImJyb3dzZXJfdXNlcl9hZ2VudCI6Ik1vemlsbGEvNS4wIChXaW5kb3dzIE5UIDEwLjA7IFdpbjY0OyB4NjQ7IHJ2OjEwMi4wKSBHZWNrby8yMDEwMDEwMSBGaXJlZm94LzEwMi4wIiwiYnJvd3Nlcl92ZXJzaW9uIjoiMTAyLjAiLCJvc192ZXJzaW9uIjoiMTAiLCJyZWZlcnJlciI6IiIsInJlZmVycmluZ19kb21haW4iOiIiLCJyZWZlcnJlcl9jdXJyZW50IjoiIiwicmVmZXJyaW5nX2RvbWFpbl9jdXJyZW50IjoiIiwicmVsZWFzZV9jaGFubmVsIjoic3RhYmxlIiwiY2xpZW50X2J1aWxkX251bWJlciI6MTM4MjU0LCJjbGllbnRfZXZlbnRfc291cmNlIjpudWxsfQ==',
'X-Discord-Locale': 'en-US',
'X-Debug-Options': 'bugReporterEnabled',
'Origin': 'https://discord.com',
'Alt-Used': 'discord.com',
'Connection': 'keep-alive',
'Referer': 'https://discord.com/channels/@me/' + channel_id,
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
}
json_data = {
'content': 'message',
'nonce': '1000213022410539008',
'tts': False,
}
response = requests.post(f'https://discord.com/api/v9/channels/{channal_id}/messages', cookies=cookies, headers=headers, json=json_data)
print(response.json())
I would like to explain it more. I can message many persons but can not message same person twice for a period of 4-5 minutes. Thanks in advance
Upvotes: 3
Views: 3280
Reputation: 1
I’ve faced the same problem. In my case, the solution was to change the value nonce to a random value before sending every message.
json_data = {
'content': 'message',
'nonce': str(random.randint(10**18, 10**18 + 2*(10**17))),
'tts': False,
}
Upvotes: 0
Reputation: 762
Best method I found is to use the discord package.
pip install discord.py
Then in your script/program:
from discord import Webhook, RequestsWebhookAdapter
webhook = Webhook.from_url("https://discord.com/api/webhooks/ **Application ID Goes Here**/**Webhook ID Goes Here**", adapter=RequestsWebhookAdapter())
webhook.send(**Your variables / data goes here**)
https://pypi.org/project/discord.py/
Just these two lines of code does it for me. You can manage your Discord application here to find your Application ID: https://discord.com/developers/applications
Your webhook ID you can set up in Discord in your Channel > Server Settings > Integrations
Upvotes: 1
Reputation: 11
I think the variable name in the requests.post()
was misspelled, but putting that aside, there's a lot of unnecessary headers and cookies.
Here's my code:
import requests
token = "<YOUR_TOKEN>"
message = "Hello World!"
channel_id = "896155634372861982"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0',
'Authorization': token
}
json_data = {
'content': message
}
response = requests.post(f"https://discord.com/api/v9/channels/{channel_id}/messages", headers=headers, json=json_data)
print(response)
The only two headers I kept are the User-Agent
and Authorization
although I don't think the User-Agent
is needed, but it's probably a good idea so your requests aren't too suspicious.
Also, make sure to change your channel_id.
I also completely removed the cookies, and unnecessary json data.
Upvotes: 1
Reputation: 191
Sending messages on Discord is different from reading messages or really most other requests because it requires you to be connected to the Gateway, a websocket which sends updates depending on what you request. After you connect the request you are making will finally go through.
Also make sure your Authorization
header is in the form of Bot <token>
instead of just <token>
Upvotes: 2