前提
PythonでDiscord Api Wrapperを作っています。
heartbeat_intervalのミリ秒ごとに{ "op": 1, "d": int(time.time()) }
を送信したいのですが、
エラーが発生して送信できません。
実現したいこと
- jsonを送信できるようにする
発生している問題・エラーメッセージ
py
1Exception in thread Thread-1: 2Traceback (most recent call last): 3 File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/threading.py", line 932, in _bootstrap_inner 4 self.run() 5 File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/threading.py", line 1254, in run 6 self.function(*self.args, **self.kwargs) 7TypeError: 'NoneType' object is not callable
該当のソースコード
py
1#!/usr/bin/env python 2import json 3import requests 4import websocket 5from threading import Timer 6import time 7import rel 8intents = "" 9def keep_alive(ws): 10 11 payload = { 12 'op': 1, 13 'd': int(time.time()) 14 } 15 16 return ws.send(json.dumps(payload)) 17 18def on_message(ws, message): 19 msg = json.loads(message) 20 print(msg) 21 if msg["op"] == 10: 22 a = Timer(msg["d"]["heartbeat_interval"] / 1000, keep_alive(ws)) 23 a.start() 24 25def on_error(ws, error): 26 print(error) 27 28 29def on_close(ws, close_status_code, close_msg): 30 print("### closed ###") 31 32 33def on_open(ws): 34 print("Opened connection") 35 36 37class Intents: 38 global intents 39 40 def all(): 41 return 3276799 42 43class Rate_Limits(Exception): 44 """Rate_Limits_Error""" 45 pass 46class Client(): 47 def __init__(self, intents: int): 48 self.intents = intents 49 50 def login(self): 51 req = requests.get('https://discord.com/api/v10/gateway') 52 websocket_curl = req.text 53 ws_url = json.loads(websocket_curl) 54 try: 55 if ws_url["code"] == 0: 56 raise Rate_Limits(ws_url["message"]) 57 except Rate_Limits: 58 raise Rate_Limits(ws_url["message"]) 59 except KeyError: 60 pass 61 else: 62 pass 63 64 65 print(ws_url) 66 ws = websocket.WebSocketApp(ws_url["url"] + "?v=10&encoding=json", 67 on_open=on_open, 68 on_message=on_message, 69 on_error=on_error, 70 on_close=on_close) 71 ws.run_forever(dispatcher=rel) 72 rel.signal(2, rel.abort) 73 rel.dispatch() 74 75a = Client(intents=Intents.all()) 76a.login()
補足情報(FW/ツールのバージョンなど)
Python 3.8.12
回答1件
あなたの回答
tips
プレビュー