예제 #1
0
 def _reconnect(self):
     if not self._enabled:
         try:
             self._rpc = Client(self._client_id)
             self._rpc.start()
             self._enabled = True
         except self._errors:
             self._enabled = False
예제 #2
0
class DiscordRPC(Thread):
    """
    Discord RPC Thread

    Updates discord rich presence status periodically.
    """

    _client_id = ""  # set discord application id here
    _enabled = False
    _update = False

    _rpc = None
    _pid = None
    _errors = (ConnectionRefusedError, InvalidID, InvalidPipe,
               FileNotFoundError, ConnectionResetError)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._pid = os.getpid()

        self._details = {
            'details': "Regretting...",
            'state': None,
            'start': None,
            'img': None,
            'txt': None
        }

    def present(self, engine, start=None, details="Regretting...", state=None):
        """
        Set status for DiscordRPC.
        """
        self._details = {
            'details':
            details,
            'state':
            state,
            'start':
            time.time() * 1000 - start if start else None,
            'img':
            engine.account["api"],
            'txt':
            "{} at {}".format(engine.account["username"],
                              engine.account["api"])
        }
        self._update = True

    def run(self):
        set_loop(new_loop())
        while True:
            try:
                self._reconnect()
                if self._enabled and self._update:
                    self._rpc.set_activity(pid=self._pid,
                                           large_image="icon",
                                           large_text=self._details['details'],
                                           small_image=self._details['img'],
                                           small_text=self._details['txt'],
                                           details=self._details['details'],
                                           state=self._details['state'],
                                           start=self._details['start'])
                    self._update = False
                time.sleep(1)
            except self._errors:
                self._enabled = False
                try:
                    self._rpc.close()
                except AttributeError:
                    pass

    def _reconnect(self):
        if not self._enabled:
            try:
                self._rpc = Client(self._client_id)
                self._rpc.start()
                self._enabled = True

            except self._errors:
                self._enabled = False
예제 #3
0
class DiscordRPC(Thread):
    """
    Discord RPC Thread

    Updates discord rich presence status periodically.
    """
    regret: bool = True

    _rpc = None
    _client_id = "777075127266705408"  # set discord application id here
    _enabled = False
    _pid = None
    _last_run = 0
    _errors = (
        ConnectionRefusedError,
        InvalidID,
        InvalidPipe,
        FileNotFoundError,
        ConnectionResetError,
    )

    def __init__(self, *args, **kwargs):
        for attr in ['regret']:
            if attr in kwargs:
                setattr(self, attr, kwargs[attr])
                del kwargs[attr]

        super().__init__(*args, **kwargs)
        self._pid = os.getpid()

        self._details = {
            'details': "Regretting...",
            'state': None,
            'pos': None,
            'img': None,
            'txt': None
        }

    def present(self,
                engine: Engine,
                pos: int = None,
                details: str = "Regretting...",
                state: str = None):
        """
        Set status for DiscordRPC.
        """
        self._details = {
            'details':
            details,
            'state':
            state,
            'pos':
            pos,
            'img':
            engine.account["api"],
            'txt':
            "{} at {}".format(engine.account["username"],
                              engine.account["api"])
        }

    def run(self):
        set_loop(new_loop())
        while True:
            try:
                self._reconnect()
                if self._enabled:
                    if time() - self._last_run <= 0.25:
                        continue
                    self._last_run = time()

                    if self._details['details'] == "Regretting..." \
                            and not self.regret:
                        self._rpc.clear_activity(pid=self._pid)
                    else:
                        self._rpc.set_activity(
                            pid=self._pid,
                            large_image="icon",
                            large_text=self._details['details'],
                            small_image=self._details['img'],
                            small_text=self._details['txt'],
                            details=self._details['details'],
                            state=self._details['state'],
                            start=time() * 1000 - self._details['pos']
                            if self._details['pos'] else None)
            except self._errors:
                self._enabled = False
                try:
                    self._rpc.close()
                except AttributeError:
                    pass

    def _reconnect(self):
        if not self._enabled:
            try:
                self._rpc = Client(self._client_id)
                self._rpc.start()
                self._enabled = True
            except self._errors:
                self._enabled = False
예제 #4
0
class DiscordRPC(Thread):
    """
    Discord RPC Thread

    Updates discord rich presence status periodically.
    """
    _client_id = "740171019003756604"  # set discord application id here
    _enabled = False
    _update = False
    tab = None
    langs = []
    _start = -1

    _rpc = None
    _pid = None
    _errors = (ConnectionRefusedError, InvalidID, InvalidPipe,
               FileNotFoundError, ConnectionResetError)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._pid = os.getpid()
        self.__stop = TEvent()

    def absent(self, tab):
        """
            clear status from DiscordRPC
        """
        if tab is self.tab:
            self.tab = None
            self._update = True

    def present(self, tab=None, start=-1):
        """
        Set status for DiscordRPC.
        """
        if tab:
            self.tab = tab
            self._start = start
        else:
            self.tab = None
        self._update = True

    @property
    def doc(self):
        if self.tab:
            return self.tab.get_document()
        return None

    @property
    def lang(self):
        """Language name"""
        if self.doc and self.doc.props.language:
            return self.doc.props.language.get_name()
        return 'Unknown'

    @property
    def name(self):
        if self.doc:
            return self.doc.props.tepl_short_title
        return ''

    def stop(self):
        """Stop this thread"""
        self.__stop.set()

    def run(self):
        set_loop(new_loop())
        while True:
            if self.__stop.is_set() and not self._enabled:
                break
            try:
                self._reconnect()
                if self.__stop.is_set():
                    if self._enabled:
                        self._rpc.clear_activity(pid=self._pid)
                        self._rpc.close()
                        self._enabled = False
                        continue
                if self._enabled and self._update:
                    if self.doc:
                        data = {
                            'pid':
                            self._pid,
                            'large_image':
                            self.lang.lower()
                            if self.lang.lower() in self.langs else 'default',
                            'large_text':
                            self.name,
                            'small_image':
                            'default'
                            if self.lang.lower() in self.langs else None,
                            'details':
                            "Writing " + self.lang + " code",
                            'state':
                            "Editing " + self.name,
                            'start':
                            self._start
                        }
                        self._rpc.set_activity(**data)
                        if self.lang:
                            self._update = False
                    else:
                        self._rpc.clear_activity(pid=self._pid)
                        self._update = False
            except self._errors:
                self._enabled = False
                try:
                    self._rpc.close()
                except AttributeError:
                    pass

    def _reconnect(self):
        if not self._enabled:
            try:
                self._rpc = Client(self._client_id)
                self._rpc.start()
                self._enabled = True

            except self._errors:
                self._enabled = False