'Discord bot not going online when code is ran
my discord bot isn't going online when I run my code.
I am making a discord bot that logs messages, and when I run the code the discord bot does not go online. I have already tried changing/resetting the token but that didn't seem to work, and theres no error messages when I run it. I was also following a youtube tutorial: https://www.youtube.com/watch?v=dR9n1zmw-Go
Here is my code:
import websocket
import json
import threading
import time
import discord
token = "MYTOKEN"
def send_json_request(ws, send_json_request):
ws.send(json.dumps(request))
def recieve_json_response(ws):
response = ws.recv()
return json.loads(response)
def heartbeat(interval, ws):
print("Heartbeat begin")
while True:
time.sleep(interval)
heartbeatJSON = {
"op": 1,
"d": "null"
}
send_json_request(ws, heartbeatJSON)
print("Heartbeat sent")
ws = websocket.WebSocket()
ws.connect("wss://gateway.discord.gg/?v=9&encoding=json")
event = recieve_json_response(ws)
heartbeat_interval = event["d"]["heartbeat_interval"]
threading._start_new_thread(heartbeat(heartbeat_interval, ws))
payload = {
"op": 2,
"d": {
"token": token,
"properties": {
"$os": "windows",
"$browser": "chrome",
"$device": "pc"
}
}
}
send_json_request(ws, payload)
while True:
event = recieve_json_response(ws)
try:
print(f"{event['d']['author']['username']}: {event['d']['content']}")
op_code = event('op')
if op_code == 11:
print("Recieved heartbeat")
except:
pass
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
