Created
May 19, 2020 21:28
-
-
Save asarra/8a5da6b039801be293af50baeeaf1d2f to your computer and use it in GitHub Desktop.
This is an ancient Discord library I made from pure scratch in Python 2.7 with the help of the official Discord documentation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2.7 -S | |
from multiprocessing import Process | |
from threading import Thread | |
import websocket #pip install websocket_client | |
import time | |
import json | |
import base64 | |
import requests | |
link="https://discordapp.com/api/" | |
API_ENDPOINT = 'https://discordapp.com/api/v6' | |
CLIENT_ID = None | |
CLIENT_SECRET = None | |
urls={ | |
"base":"https://discordapp.com/api/oauth2/authorize", | |
"token":"https://discordapp.com/api/oauth2/token", | |
"revoke":"https://discordapp.com/api/oauth2/token/revoke", | |
"api":"https://discordapp.com/api/v6", | |
"api_new":"https://discordapp.com/api/v7" | |
} | |
def get_token(): | |
data = {'grant_type': 'client_credentials','scope': 'identify connections'} | |
r = requests.post('%s/oauth2/token' % API_ENDPOINT, data, headers={'Content-Type': 'application/x-www-form-urlencoded'}, auth=(CLIENT_ID, CLIENT_SECRET)) | |
r.raise_for_status() | |
return r.json() | |
def me(): | |
bot_token=None #hashed string! | |
r = requests.get('%s/oauth2/applications/@me'%API_ENDPOINT, headers={"Authorization":"Bot %s"%bot_token}) | |
r.raise_for_status() | |
return r.json() | |
def get_guilds(): | |
global link | |
guildsofbot="users/@me/guilds" | |
bot_token=None #hashed string! | |
r = requests.get('%s%s'%(link,guildsofbot), headers={"Authorization":"Bot %s"%bot_token}) | |
r.raise_for_status() | |
return r.json() | |
def get_guild_info(): | |
global link | |
guild_id=None #int! | |
bot_token=None #hashed string! | |
r = requests.get('%s/guilds/%s'%(link,guild_id), headers={"Authorization":"Bot %s"%bot_token}) | |
r.raise_for_status() | |
return r.json() | |
def getChannels(): | |
global link | |
guild_id=None #int! | |
bot_token=None #hashed string! | |
r = requests.get('%s/guilds/%s/channels'%(link,guild_id), headers={"Authorization":"Bot %s"%bot_token}) | |
r.raise_for_status() | |
return r.json() | |
def identify(): | |
global ws | |
heartbeat = '{"op": 1,"d": 251}' | |
#adding op and d fixed that problem | |
p = '{"op": 2,"d":{"token": "","properties": {"$os": "linux","$browser": "chrome","$device": "malaka"},"compress": false,"large_threshold": 250,"presence": {"game": {"name": "PUBG MOBILE!","type": 0},"status": "online","afk": false}}}' #token needs to be inserted! | |
h_json = json.dumps(json.loads(heartbeat)) | |
p_json = json.dumps(json.loads(p)) | |
ws.send(h_json) | |
ws.send(p_json) | |
def sendMessage(): | |
global link | |
guild_id=None #Data type is int! | |
botfuqery_tc=None #cringy variable name for a channel in my discord guild lol. This needs to be of type int! | |
bot_token=None #this needs to be of type string | |
data='{"content": "Test"}'#,"tts": false,"embed": {"title": "Hello, Embed!","description": "This is an embedded message."}}' | |
data=json.loads(data) | |
r = requests.post('%schannels/%s/messages'%(link,botfuqery_tc), data=data, headers={"Authorization":"Bot %s"%bot_token,"Content-Type":"application/json","Content-Type":"application/x-www-form-urlencoded"}) | |
r.raise_for_status() | |
return r.json() | |
def voice(): | |
#import socket;sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM);host = ('localhost',5566);sock.sendto("Hello\n",host) | |
global ws,session_id | |
p='{"op": 4,"d": {"guild_id": "","channel_id": "","self_mute": false,"self_deaf": false}}' | |
p_json = json.dumps(json.loads(p)) #took out guild_id and channel_id values! | |
#vws = websocket.WebSocket();vws.connect("wss://gateway.discord.gg/?v=6");print "Received: ",ws.recv() | |
ws.send(p_json) | |
def heartbeatLoop(ws,heartbeat_interval): | |
heartbeat = '{"op": 1,"d": 251}' | |
h_json = json.dumps(json.loads(heartbeat)) | |
interval=heartbeat_interval/1000 | |
while True: | |
print "STOP-3" | |
try: | |
ws.send(h_json) | |
time.sleep(interval) | |
except Exception as e: | |
print e | |
if "socket is already closed" in e: | |
print "True" | |
break | |
else: | |
pass | |
def presenceUpdate(ws): | |
presence='{"op":3,"d":{"game": {"name": "GAMEBOY!","type": 1},"status": "online","afk": false,"since":0}}' | |
presence_json=json.dumps(json.loads(presence)) | |
while True: | |
try: | |
time.sleep(12) | |
ws.send(presence_json) | |
print "OMG%sOMG"%ws.recv() | |
except Exception as e: | |
print e | |
return False | |
def handler(ws): | |
while True: | |
try: | |
message=ws.recv() | |
#print "HANDLEREVENT" | |
try: | |
json_message=json.loads(message) | |
if json_message["t"]=="READY": | |
session_id=json_message["d"]["session_id"] | |
global session_id | |
elif json_message["t"]=="MESSAGE_CREATE": | |
pass | |
elif json_message["t"]=="GUILD_CREATE": | |
pass | |
elif json_message["t"]=="VOICE_STATE_UPDATE": | |
pass | |
elif json_message["t"]=="PRESENCE_UPDATE": | |
pass | |
elif json_message["t"]=="VOICE_SERVER_UPDATE": | |
json_message["d"]["endpoint"] | |
token=json_message["d"]["token"] | |
global endpoint,token | |
elif json_message["op"]=="10":#HEARTBEAT RE/START | |
heartbeat_interval=json_message["d"]["heartbeat_interval"] | |
heartbeatLoop2=Thread(target=heartbeatLoop,args=(ws,heartbeat_interval,)) | |
heartbeatLoop2.start() | |
elif json_message["op"]=="11":#HEARTBEAT ACKNOWLEDGEMENT | |
pass | |
else: | |
print "Message:",message | |
pass | |
except Exception as e: | |
print "Exception",e | |
time.sleep(5) | |
pass | |
except Exception as e: | |
print e | |
print "-%s-"%message | |
return False | |
if __name__=='__main__': | |
ws=websocket.WebSocket() | |
ws.connect("wss://gateway.discord.gg/?v=6&encoding=json") | |
thread_handler=Thread(target=handler, args=(ws,)) | |
thread_presenceUpdate=Thread(target=presenceUpdate, args=(ws,)) | |
identify() | |
sendMessage() | |
voice() | |
thread_handler.start() | |
thread_presenceUpdate.start() | |
thread_presenceUpdate.join() | |
thread_handler.join() | |
heartbeatLoop2.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment