Article
08 July 2024
- 7 min read
- 1.5K view
Creating custom Discord selfbots when Discord’s webhooks aren’t enough.
I wanted to send a friend a message on Discord or Slack using a simple device like a Raspberry Pi. My friends often have tech troubles, so having a script to send results back to me through DMs would be helpful.
Webhooks are easy to set up but limit the control over your bot. Using methods that abide by the API and TOS is safer.
Support Docs on Creating Webhooks
import requests
import json
if __name__ == '__main__':
webhook_url = 'https://discordapp.com/api/webhooks/.../'
data = {
'text': 'A message sent with the power of Python in Discord.'
}
response = requests.post(webhook_url, data=json.dumps(data), headers={'Content-Type': 'application/json'})
print('Response: ' + str(response.text))
print('Response code: ' + str(response.status_code))
import requests
import json
if __name__ == '__main__':
webhook_url = 'https://hooks.slack.com/services/{API_GUID}'
data = {
'icon_emoji': ':ghost:',
'text': 'A message sent with the power of Python in Slack.',
'username': 'Python Bot',
'icon_emoji': ':robot_face:'
}
response = requests.post(webhook_url, data=json.dumps(data), headers={'Content-Type': 'application/json'})
print('Response: ' + str(response.text))
print('Response code: ' + str(response.status_code))
Userbots provide more control but using them can lead to a ban as it’s against the TOS for Discord.
pip3 install requests unidecode
import requests
import random
import time
from unidecode import unidecode
token = "your-token-here"
discord = "https://discordapp.com/api/v6/"
chatid = 795743570995839018
def send_message(chatid, content, proxy=None):
return requests.post(discord + "channels/" + str(chatid) + "/messages#", proxies=proxy, data={"content": str(content), "nonce": str(random.randint(10000000, 99999999))}, headers={"Authorization": token}).text
def get_message(chatid, proxy=None):
res = requests.get(discord + "channels/" + str(chatid) + "/messages?limit=1", proxies=proxy, headers={"Authorization": token}).text
try:
content = res.split('"content": "')[1].split('"')[0]
except IndexError as e:
content = res.split('"message": "')[1].split('"')[0], res.split('"retry_after": ')[-1].split("\n")[0]
return content
def get_author_id(chatid, proxy=None):
res = requests.get(discord + "channels/" + str(chatid) + "/messages?limit=1", proxies=proxy, headers={"Authorization": token}).text
return res.split('"author":')[1].split('"id": "')[1].split('"')[0]
if __name__ == "__main__":
while True:
time.sleep(0.05)
message = get_message(chatid)
if message == "!yobot":
send_message(chatid, f"How may I help you <@{get_author_id(chatid)}>?")
elif message[0] == "You are being rate limited.":
print(f"Waiting off the rate limit for {int(message[1]) / 1000}s")
time.sleep(int(message[1]) / 1000)
For more details and the continuation, check the original article.