Skip to content

Commit

Permalink
Update PyPresence to 4.2.1.
Browse files Browse the repository at this point in the history
Fixes error on Krita's boot.
  • Loading branch information
Firstbober committed Jan 16, 2022
1 parent 0885486 commit d287878
Show file tree
Hide file tree
Showing 6 changed files with 858 additions and 802 deletions.
63 changes: 26 additions & 37 deletions discord_rpc/baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,35 @@
import struct
import sys
import tempfile
from typing import Union
from typing import Union, Optional

# TODO: Get rid of this import * lol
from .exceptions import *
from .payloads import Payload
from .utils import get_ipc_path, get_event_loop


class BaseClient:

def __init__(self, client_id: str, **kwargs):
pipe = kwargs.get('pipe', 0)
pipe = kwargs.get('pipe', None)
loop = kwargs.get('loop', None)
handler = kwargs.get('handler', None)
self.isasync = kwargs.get('isasync', False)

client_id = str(client_id)
if sys.platform == 'linux' or sys.platform == 'darwin':
tempdir = (os.environ.get('XDG_RUNTIME_DIR') or tempfile.gettempdir())
snap_path = '{0}/snap.discord'.format(tempdir)
pipe_file = 'discord-ipc-{0}'.format(pipe)
if os.path.isdir(snap_path):
self.ipc_path = '{0}/{1}'.format(snap_path, pipe_file)
else:
self.ipc_path = '{0}/{1}'.format(tempdir, pipe_file)
elif sys.platform == 'win32':
self.ipc_path = r'\\?\pipe\discord-ipc-' + str(pipe)
self.ipc_path = get_ipc_path(pipe)

if not self.ipc_path:
raise DiscordNotFound

if loop is not None:
self.update_event_loop(loop)
else:
self.update_event_loop(self.get_event_loop())
self.update_event_loop(get_event_loop())

self.sock_reader = None # type: asyncio.StreamReader
self.sock_writer = None # type: asyncio.StreamWriter
self.sock_reader: Optional[asyncio.StreamReader] = None
self.sock_writer: Optional[asyncio.StreamWriter] = None

self.client_id = client_id

Expand Down Expand Up @@ -66,23 +62,8 @@ def __init__(self, client_id: str, **kwargs):
else:
self._events_on = False

def get_event_loop(self, force_fresh=False):
if sys.platform == 'linux' or sys.platform == 'darwin':
if force_fresh:
return asyncio.new_event_loop()
loop = asyncio.get_event_loop()
if loop.is_closed():
return asyncio.new_event_loop()
return loop
elif sys.platform == 'win32':
if force_fresh:
return asyncio.ProactorEventLoop()
loop = asyncio.get_event_loop()
if isinstance(loop, asyncio.ProactorEventLoop) and not loop.is_closed():
return loop
return asyncio.ProactorEventLoop()

def update_event_loop(self, loop):
# noinspection PyAttributeOutsideInit
self.loop = loop
asyncio.set_event_loop(self.loop)

Expand All @@ -91,16 +72,18 @@ def _err_handle(self, loop, context: dict):
if inspect.iscoroutinefunction(self.handler):
loop.run_until_complete(result)

# noinspection PyUnusedLocal
async def _async_err_handle(self, loop, context: dict):
await self.handler(context['exception'], context['future'])

async def read_output(self):
try:
data = await self.sock_reader.read(1024)
preamble = await self.sock_reader.read(8)
status_code, length = struct.unpack('<II', preamble[:8])
data = await self.sock_reader.read(length)
except BrokenPipeError:
raise InvalidID
status_code, length = struct.unpack('<II', data[:8])
payload = json.loads(data[8:].decode('utf-8'))
payload = json.loads(data.decode('utf-8'))
if payload["evt"] == "ERROR":
raise ServerError(payload["data"]["message"])
return payload
Expand All @@ -109,6 +92,9 @@ def send_data(self, op: int, payload: Union[dict, Payload]):
if isinstance(payload, Payload):
payload = payload.data
payload = json.dumps(payload)

assert self.sock_writer is not None, "You must connect your client before sending events!"

self.sock_writer.write(
struct.pack(
'<II',
Expand All @@ -118,7 +104,7 @@ def send_data(self, op: int, payload: Union[dict, Payload]):

async def handshake(self):
if sys.platform == 'linux' or sys.platform == 'darwin':
self.sock_reader, self.sock_writer = await asyncio.open_unix_connection(self.ipc_path, loop=self.loop)
self.sock_reader, self.sock_writer = await asyncio.open_unix_connection(self.ipc_path)
elif sys.platform == 'win32' or sys.platform == 'win64':
self.sock_reader = asyncio.StreamReader(loop=self.loop)
reader_protocol = asyncio.StreamReaderProtocol(
Expand All @@ -128,7 +114,10 @@ async def handshake(self):
except FileNotFoundError:
raise InvalidPipe
self.send_data(0, {'v': 1, 'client_id': self.client_id})
data = await self.sock_reader.read(1024)
code, length = struct.unpack('<ii', data[:8])
preamble = await self.sock_reader.read(8)
code, length = struct.unpack('<ii', preamble)
data = json.loads(await self.sock_reader.read(length))
if 'code' in data:
raise DiscordError(data['code'], data['message'])
if self._events_on:
self.sock_reader.feed_data = self.on_event
Loading

2 comments on commit d287878

@naturevel
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any prerequisites for the installation?

I enabled the plugin and restarted krita but discord isn't detecting it

@Olkris2666
Copy link
Contributor

@Olkris2666 Olkris2666 commented on d287878 Jan 19, 2022 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.