├── requirements.txt ├── media └── preview.png ├── DOCS.md ├── setup.py ├── discordrpc ├── button.py ├── __init__.py ├── types.py ├── utils.py ├── exceptions.py └── presence.py ├── examples ├── get-user.py ├── basic.py ├── rpc-local-time.py ├── rpc-with-progressbar.py ├── rpc-debug.py ├── rpc-with-button.py ├── rpc-with-timestamp.py ├── rpc-with-activitytype.py ├── print-rpc-output.py ├── rpc-with-image.py └── rpc-party.py ├── .github └── workflows │ ├── publish-pypi.yml │ └── publish-testpypi.yml ├── LICENSE ├── pyproject.toml ├── .gitignore └── README.md /requirements.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /media/preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Senophyx/Discord-RPC/HEAD/media/preview.png -------------------------------------------------------------------------------- /DOCS.md: -------------------------------------------------------------------------------- 1 | # Discord‑RPC — Documentation 2 | 3 | ## **Documentation has been moved to [https://senophyx.id/docs/discord-rpc/](https://senophyx.id/docs/discord-rpc/).** -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | """ 2 | setup.py configuration has been migrated to pyproject.toml 3 | latest legacy setup.py config : commit #829e81c 4 | """ 5 | 6 | from setuptools import setup 7 | setup() -------------------------------------------------------------------------------- /discordrpc/button.py: -------------------------------------------------------------------------------- 1 | from .exceptions import * 2 | 3 | 4 | def Button(text:str, url:str): 5 | if not url.startswith(("http://", "https://")): 6 | raise InvalidURL() 7 | return {"label": text, "url": url} 8 | -------------------------------------------------------------------------------- /examples/get-user.py: -------------------------------------------------------------------------------- 1 | import discordrpc 2 | 3 | rpc = discordrpc.RPC(app_id=123456789) 4 | 5 | print(rpc.User.id) 6 | print(rpc.User.name) 7 | print(f"@{rpc.User.username}") 8 | print(rpc.User.avatar) 9 | 10 | rpc.run() 11 | -------------------------------------------------------------------------------- /examples/basic.py: -------------------------------------------------------------------------------- 1 | import discordrpc 2 | 3 | rpc = discordrpc.RPC(app_id=12345678910) 4 | 5 | rpc.set_activity( 6 | state="A super simple rpc", 7 | details="simple RPC" 8 | ) 9 | 10 | # Required if you only run Discord RPC on this file or current instance. 11 | rpc.run() 12 | -------------------------------------------------------------------------------- /examples/rpc-local-time.py: -------------------------------------------------------------------------------- 1 | import discordrpc 2 | from discordrpc import use_local_time 3 | 4 | 5 | rpc = discordrpc.RPC(app_id=123456789) 6 | 7 | rpc.set_activity( 8 | state="Wow! It's shows my clock", 9 | details="Local time example", 10 | **use_local_time() 11 | ) 12 | 13 | rpc.run() 14 | -------------------------------------------------------------------------------- /examples/rpc-with-progressbar.py: -------------------------------------------------------------------------------- 1 | import discordrpc 2 | from discordrpc import Activity 3 | from discordrpc.utils import ProgressBar 4 | 5 | 6 | rpc = discordrpc.RPC(app_id=1234567891011) 7 | 8 | rpc.set_activity( 9 | state="With Progressbar", 10 | details="Music", 11 | act_type=Activity.Listening, 12 | **ProgressBar(50, 200) 13 | ) 14 | 15 | rpc.run() 16 | -------------------------------------------------------------------------------- /examples/rpc-debug.py: -------------------------------------------------------------------------------- 1 | ### Basically it's "print-rpc-output.py" but with more informative output :) 2 | 3 | import discordrpc 4 | 5 | # Add debug=True to get more outputs 6 | # Default = False 7 | rpc = discordrpc.RPC(app_id=123456789, debug=True) 8 | 9 | rpc.set_activity( 10 | state="A super simple rpc", 11 | details="simple RPC" 12 | ) 13 | 14 | 15 | rpc.run() -------------------------------------------------------------------------------- /examples/rpc-with-button.py: -------------------------------------------------------------------------------- 1 | import discordrpc 2 | from discordrpc import Button 3 | 4 | rpc = discordrpc.RPC(app_id=1234567891011) 5 | 6 | 7 | rpc.set_activity( 8 | state="Made by Senophyx", 9 | details="Discord-RPC", 10 | buttons=[ 11 | Button("Repository", "https://github.com/Senophyx/discord-rpc"), 12 | Button("Discord", "https://discord.gg/qpT2AeYZRN"), 13 | ] 14 | ) 15 | 16 | 17 | rpc.run() -------------------------------------------------------------------------------- /examples/rpc-with-timestamp.py: -------------------------------------------------------------------------------- 1 | import discordrpc 2 | 3 | # Import timestamp variable from discordrpc.utils to get current timestamp 4 | from discordrpc.utils import timestamp 5 | 6 | 7 | rpc = discordrpc.RPC(app_id=123456789) 8 | 9 | rpc.set_activity( 10 | state="With timestamp!", 11 | details="Timestamp", 12 | ts_start=timestamp, # Timestamp start 13 | ts_end=1752426021 # Timestamp end 14 | ) 15 | 16 | 17 | 18 | rpc.run() -------------------------------------------------------------------------------- /examples/rpc-with-activitytype.py: -------------------------------------------------------------------------------- 1 | import discordrpc 2 | from discordrpc import Activity 3 | import time 4 | 5 | 6 | rpc = discordrpc.RPC(app_id=123456789) 7 | 8 | 9 | current_time = int(time.time()) 10 | finish_time = current_time + 200 11 | 12 | rpc.set_activity( 13 | state="With activity type", 14 | details="Music", 15 | act_type=Activity.Listening, 16 | ts_start=current_time, 17 | ts_end=finish_time 18 | ) 19 | 20 | 21 | rpc.run() 22 | -------------------------------------------------------------------------------- /discordrpc/__init__.py: -------------------------------------------------------------------------------- 1 | from importlib.metadata import version as _pkg_ver, PackageNotFoundError 2 | from .presence import RPC 3 | from .button import Button 4 | from .exceptions import * 5 | from .types import * 6 | from .utils import * 7 | 8 | __title__ = "Discord RPC" 9 | try: 10 | __version__ = _pkg_ver('discord-rpc') 11 | except PackageNotFoundError: 12 | __version__ = "unknown" 13 | __authors__ = "Senophyx" 14 | __license__ = "MIT License" 15 | __copyright__ = "Copyright 2021-2025 Senophyx" 16 | -------------------------------------------------------------------------------- /examples/print-rpc-output.py: -------------------------------------------------------------------------------- 1 | import discordrpc 2 | 3 | 4 | # Set show_output to True to get RPC output (Like whether RPC works well or not) 5 | # Default = True 6 | rpc = discordrpc.RPC(app_id=123456789101112, output=True) 7 | 8 | 9 | button = discordrpc.button( 10 | button_one_label="Repository", 11 | button_one_url="https://github.com/Senophyx/discord-rpc", 12 | button_two_label="Discord Server", 13 | button_two_url="https://discord.gg/qpT2AeYZRN" 14 | ) 15 | 16 | rpc.set_activity( 17 | state="Made by Senophyx", 18 | details="Discord-RPC", 19 | buttons=button 20 | ) 21 | 22 | 23 | rpc.run() -------------------------------------------------------------------------------- /examples/rpc-with-image.py: -------------------------------------------------------------------------------- 1 | import discordrpc 2 | 3 | rpc = discordrpc.RPC(app_id=123456789) 4 | 5 | # Upload your image(s) here: 6 | # https://discord.com/developers/applications//rich-presence/assets 7 | 8 | rpc.set_activity( 9 | state="pip install discord-rpc", 10 | details="Discord-RPC by Senophyx", 11 | large_image="eternomm_logo", # Make sure you are using the same name that you used when uploading the image 12 | large_text="EterNomm", 13 | small_image="github", # Make sure you are using the same name that you used when uploading the image 14 | small_text="Github" 15 | ) 16 | 17 | 18 | 19 | rpc.run() 20 | -------------------------------------------------------------------------------- /examples/rpc-party.py: -------------------------------------------------------------------------------- 1 | import discordrpc 2 | 3 | rpc = discordrpc.RPC(app_id=123456789) 4 | 5 | rpc.set_activity( 6 | details='VALORANT', 7 | state='Join if you want!', 8 | 9 | party_id=12345, # Your party ID (Must be strings! even if it is an int it will still be changed to a string). 10 | #Not really useful, I guess. But it's required if you want to create party! 11 | party_size=[1, 10], # Party size (must in list), [current_size, max_size] 12 | 13 | join_secret='playvalowithme', # Not really useful, I guess. But it's required if you want to make 'Ask to Join' button! 14 | spectate_secret='spectateme', # Not really useful, I guess. But it's required! 15 | match_secret='idkbrofr' # Not really useful, I guess. But it's required! 16 | ) 17 | 18 | rpc.run() -------------------------------------------------------------------------------- /.github/workflows/publish-pypi.yml: -------------------------------------------------------------------------------- 1 | name: Publish to PyPI 2 | 3 | on: workflow_dispatch 4 | 5 | jobs: 6 | pypi-publish: 7 | name: Publish release to PyPI 8 | runs-on: ubuntu-latest 9 | environment: 10 | name: pypi 11 | url: https://pypi.org/p/discord-rpc 12 | permissions: 13 | id-token: write 14 | steps: 15 | - uses: actions/checkout@v4 16 | - name: Set up Python 17 | uses: actions/setup-python@v4 18 | with: 19 | python-version: "3.x" 20 | - name: Install dependencies 21 | run: | 22 | python -m pip install --upgrade pip 23 | pip install setuptools wheel 24 | - name: Build package 25 | run: | 26 | python setup.py sdist bdist_wheel # Could also be python -m build 27 | - name: Publish package distributions to PyPI 28 | uses: pypa/gh-action-pypi-publish@release/v1 -------------------------------------------------------------------------------- /.github/workflows/publish-testpypi.yml: -------------------------------------------------------------------------------- 1 | name: Publish to TestPyPI 2 | 3 | on: workflow_dispatch 4 | 5 | jobs: 6 | pypi-publish: 7 | name: Publish release to TestPyPI 8 | runs-on: ubuntu-latest 9 | environment: 10 | name: pypi 11 | url: https://pypi.org/p/discord-rpc 12 | permissions: 13 | id-token: write 14 | steps: 15 | - uses: actions/checkout@v4 16 | - name: Set up Python 17 | uses: actions/setup-python@v4 18 | with: 19 | python-version: "3.x" 20 | - name: Install dependencies 21 | run: | 22 | python -m pip install --upgrade pip 23 | pip install setuptools wheel 24 | - name: Build package 25 | run: | 26 | python setup.py sdist bdist_wheel # Could also be python -m build 27 | - name: Publish package distributions to PyPI 28 | uses: pypa/gh-action-pypi-publish@release/v1 29 | with: 30 | repository-url: https://test.pypi.org/legacy/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021-2025 Senophyx 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /discordrpc/types.py: -------------------------------------------------------------------------------- 1 | from enum import Enum 2 | 3 | 4 | # https://discord.com/developers/docs/events/gateway-events#activity-object-activity-types 5 | class Activity(Enum): 6 | Playing = 0 7 | Streaming = 1 8 | Listening = 2 9 | Watching = 3 10 | Custom = 4 11 | Competing = 5 12 | 13 | class StatusDisplay(Enum): 14 | Name = 0 15 | State = 1 16 | Details = 2 17 | 18 | class User(): 19 | def __init__(self, data:dict=None): 20 | data = data or {} 21 | self.id: int = int(data.get('id', 0)) 22 | self.username: str = data.get('username') 23 | self.name: str = data.get('global_name') 24 | self.avatar: str = self._parse_avatar(data) 25 | self.bot: bool = data.get('bot', False) 26 | self.premium_type: int = int(data.get('premium_type', 0)) # https://discord.com/developers/docs/resources/user#user-object-premium-types 27 | 28 | def _parse_avatar(self, data:dict, size:int=1024) -> str: 29 | if data.get('avatar'): 30 | ext = "gif" if data.get('avatar').startswith("a_") else "png" 31 | return f"https://cdn.discordapp.com/avatars/{self.id}/{data.get('avatar')}.{ext}?size={size}" 32 | else: 33 | return f"https://cdn.discordapp.com/embed/avatars/0.png" 34 | 35 | def __str__(self): 36 | return f"User({self.name})" 37 | -------------------------------------------------------------------------------- /discordrpc/utils.py: -------------------------------------------------------------------------------- 1 | import time 2 | from datetime import datetime 3 | from .exceptions import * 4 | 5 | # Credits to qwertyquerty 6 | # https://github.com/qwertyquerty/pypresence/blob/master/pypresence/utils.py#L12C1-L21C13 7 | 8 | def remove_none(d: dict): 9 | for item in d.copy(): 10 | if isinstance(d[item], dict): 11 | if len(d[item]): 12 | d[item] = remove_none(d[item]) 13 | if not len(d[item]): 14 | del d[item] 15 | elif d[item] is None: 16 | del d[item] 17 | return d 18 | 19 | 20 | timestamp = int(time.mktime(time.localtime())) 21 | 22 | 23 | def date_to_timestamp(date:str): 24 | return int(time.mktime( 25 | datetime.strptime(date, "%d/%m/%Y-%H:%M:%S").timetuple() 26 | )) 27 | 28 | def use_local_time(): 29 | now = datetime.now() 30 | seconds_since_midnight = now.hour * 3600 + now.minute * 60 + now.second 31 | return { 32 | "ts_start": int(time.time()) - seconds_since_midnight 33 | } 34 | 35 | def ProgressBar(current:int, duration:int) -> dict: 36 | if int(current) > int(duration): 37 | raise ProgressbarError("Current cannot exceed Duration") 38 | 39 | current_time = int(time.time()) - int(current) 40 | finish_time = current_time + int(duration) 41 | 42 | return { 43 | "ts_start": current_time, "ts_end": finish_time 44 | } -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools>=69", "wheel"] 3 | build-backend = "setuptools.build_meta" 4 | 5 | [project] 6 | name = "discord-rpc" 7 | ##### VERSION ##### 8 | version = "5.6b1" 9 | ################### 10 | description = "A Python wrapper for the Discord RPC API" 11 | readme = "README.md" 12 | requires-python = ">=3.7" 13 | license = "MIT" 14 | authors = [{ name = "Senophyx", email = "contact@senophyx.id" }] 15 | keywords = ["Discord", "rpc", "discord rpc"] 16 | classifiers = [ 17 | "Development Status :: 5 - Production/Stable", 18 | "Intended Audience :: Developers", 19 | "Intended Audience :: End Users/Desktop", 20 | "Intended Audience :: Other Audience", 21 | "Programming Language :: Python :: 3.7", 22 | "Programming Language :: Python :: 3.8", 23 | "Programming Language :: Python :: 3.9", 24 | "Programming Language :: Python :: 3.10", 25 | "Operating System :: Microsoft :: Windows", 26 | "Operating System :: MacOS", 27 | "Operating System :: POSIX :: Linux", 28 | "Programming Language :: Python" 29 | ] 30 | 31 | # There are currently no required packages in requirements.txt 32 | dependencies = [] 33 | 34 | [project.urls] 35 | Homepage = "https://github.com/Senophyx/discord-rpc" 36 | Documentation = "https://senophyx.id/docs/discord-rpc/" 37 | "Issue tracker" = "https://github.com/Senophyx/discord-rpc/issues" 38 | Discord = "https://discord.gg/qpT2AeYZRN" 39 | 40 | [tool.setuptools] 41 | include-package-data = true 42 | 43 | [tool.setuptools.packages.find] 44 | where = ["."] 45 | include = ["discordrpc*"] 46 | exclude = ["tests*", "examples*", "media*", "venv*", "build*", "dist*", "*egg-info*"] -------------------------------------------------------------------------------- /discordrpc/exceptions.py: -------------------------------------------------------------------------------- 1 | class RPCException(Exception): 2 | def __init__(self, message: str= None): 3 | if message is None: 4 | message = 'An error has occurred within DiscordRPC' 5 | super().__init__(message) 6 | 7 | class Error(RPCException): 8 | def __init__(self, message:str): 9 | super().__init__(message) 10 | 11 | class DiscordNotOpened(RPCException): 12 | def __init__(self): 13 | super().__init__("Error, could not find Discord. is Discord running?") 14 | 15 | class ActivityError(RPCException): 16 | def __init__(self): 17 | super().__init__("An error has occurred in activity payload, do you have set your activity correctly?") 18 | 19 | class InvalidURL(RPCException): 20 | def __init__(self): 21 | super().__init__("Invalid URL. Must include: http:// or https://") 22 | 23 | class InvalidID(RPCException): 24 | def __init__(self): 25 | super().__init__("Invalid ID, is the ID correct? Get Application ID on https://discord.com/developers/applications") 26 | 27 | class ButtonError(RPCException): 28 | def __init__(self, message: str = None): 29 | super().__init__(message=message) 30 | 31 | class ProgressbarError(RPCException): 32 | def __init__(self, message): 33 | super().__init__(message=message) 34 | 35 | class InvalidActivityType(RPCException): 36 | def __init__(self, message): 37 | super().__init__(f"Activity type must be , not {message}") 38 | 39 | # https://github.com/Senophyx/Discord-RPC/issues/28#issuecomment-2301287350 40 | class ActivityTypeDisabled(RPCException): 41 | def __init__(self): 42 | super().__init__(f"Activity type `Streaming` and `Custom` currently disabled. See https://github.com/Senophyx/Discord-RPC/issues/28#issuecomment-2301287350") 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | # macOS stuff 132 | .DS_Store 133 | 134 | # For my testing file 135 | *__test* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Visitors](https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fgithub.com%2FSenophyx%2FDiscord-RPC&label=Visitors&countColor=%2337d67a&style=flat&labelStyle=none)](https://github.com/Senophyx/Discord-RPC) 2 | [![Discord](https://img.shields.io/discord/887650006977347594?label=EterNomm&logo=discord)](https://discord.gg/qpT2AeYZRN) 3 | [![Total Downloads](https://static.pepy.tech/badge/discord-rpc)](https://pepy.tech/project/discord-rpc) 4 | [![PyPI](https://img.shields.io/pypi/v/discord-rpc?label=PyPI%20Version&logo=pypi)](https://pypi.org/project/discord-rpc) 5 | [![GitHub commit activity](https://img.shields.io/github/commit-activity/y/Senophyx/discord-rpc?label=Commit%20Activity&logo=github)](https://github.com/Senophyx/discord-rpc) 6 | 7 | # Discord RPC 8 | 9 | Dicord-RPC preview 10 | 11 | A Python wrapper for the Discord RPC API that allows you to create your own custom Rich Presence. 12 | 13 | [![Changelog](https://img.shields.io/badge/Changelog-blue?style=for-the-badge&logo=github)](https://senophyx.id/projects/discord-rpc/#change-logs) 14 | [![Documentation](https://img.shields.io/badge/Documentation-gray?style=for-the-badge&logo=googledocs&logoColor=white)](https://senophyx.id/docs/discord-rpc/) 15 | 16 | ## Install 17 | - PyPI 18 | ``` 19 | pip install discord-rpc 20 | ``` 21 | 22 | ## Quick example 23 | ```py 24 | import discordrpc 25 | 26 | rpc = discordrpc.RPC(app_id=12345678910) 27 | 28 | rpc.set_activity( 29 | state="A super simple rpc", 30 | details="simple RPC" 31 | ) 32 | 33 | # Required if you only run Discord RPC on this file or current instance. 34 | rpc.run() 35 | ``` 36 | `rpc.run()` is only used if you are only running Discord RPC on the current file/instance. If there are other programs/tasks on the current instance, `rpc.run()` does not need to be used. 37 | 38 | More examples [here](https://github.com/Senophyx/discord-rpc/tree/main/examples). 39 | 40 | 41 | ## Contributors 42 | Big thanks for contributors who help this project keep updated, and maintained. 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | ## Links 51 | - [Github Repository](https://github.com/Senophyx/Discord-RPC) 52 | - [PyPI Project page](https://pypi.org/project/discord-rpc/) 53 | - [TestPyPI Project page](https://test.pypi.org/project/discord-rpc/) 54 | - [Discord Server](https://discord.gg/qpT2AeYZRN) 55 | 56 | ## Licence & Copyright 57 | 58 | ``` 59 | This Project under MIT License 60 | Copyright (c) 2021-2025 Senophyx 61 | ``` -------------------------------------------------------------------------------- /discordrpc/presence.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import socket 4 | import json 5 | import struct 6 | import uuid 7 | import re 8 | from .exceptions import * 9 | from .types import * 10 | from .utils import * 11 | import logging 12 | import time 13 | 14 | OP_HANDSHAKE = 0 15 | OP_FRAME = 1 16 | OP_CLOSE = 2 17 | 18 | TRY_RECONNECTING = True 19 | 20 | ### Logger ### 21 | log = logging.getLogger("Discord RPC") 22 | log.setLevel(logging.INFO) 23 | logging.basicConfig(format="%(asctime)s :: [%(levelname)s @ %(filename)s.%(funcName)s:%(lineno)d] :: %(message)s", datefmt="%Y-%m-%d %H:%M:%S") 24 | 25 | 26 | class RPC: 27 | def __init__(self, app_id:int, debug:bool=False, output:bool=True, exit_if_discord_close:bool=True, exit_on_disconnect:bool=True): 28 | self.app_id = str(app_id) 29 | self.exit_if_discord_close = exit_if_discord_close 30 | self.exit_on_disconnect = exit_on_disconnect 31 | 32 | self.user_data = {} 33 | self.User = User() 34 | 35 | if debug == True: 36 | log.setLevel(logging.DEBUG) 37 | 38 | if output == False: 39 | log.disabled = True 40 | 41 | self.is_running = False 42 | self._setup() 43 | 44 | def _setup(self): 45 | if sys.platform == "win32": 46 | self.ipc = WindowsPipe(self.app_id, self.exit_if_discord_close, self.exit_on_disconnect) 47 | else: 48 | self.ipc = UnixPipe(self.app_id, self.exit_if_discord_close, self.exit_on_disconnect) 49 | 50 | if not self.ipc.connected: return 51 | self.user_data = self.ipc.handshake() 52 | self.User = User(self.user_data) 53 | 54 | def set_activity( 55 | self, 56 | state: str=None, details:str=None, act_type:Activity=Activity.Playing, status_type:StatusDisplay=StatusDisplay.Name, 57 | large_image:str=None, large_text:str=None, large_url:str=None, 58 | small_image:str=None, small_text:str=None, small_url:str=None, 59 | state_url:str=None, details_url:str=None, 60 | ts_start:int=None, ts_end:int=None, 61 | # progressbar:dict=None, 62 | # use_local_time:bool=False, 63 | party_id:str=None, party_size:list=None, 64 | join_secret:str=None, spectate_secret:str=None, 65 | match_secret:str=None, buttons:list=None, 66 | clear=False 67 | ) -> bool: 68 | 69 | if type(party_id) == int: 70 | party_id = str(party_id) 71 | 72 | if type(act_type) != Activity: 73 | raise InvalidActivityType(type(act_type)) 74 | 75 | # https://github.com/Senophyx/Discord-RPC/issues/28#issuecomment-2301287350 76 | if act_type in [Activity.Streaming, Activity.Custom]: 77 | raise ActivityTypeDisabled() 78 | 79 | if buttons and len(buttons) > 2: 80 | raise ButtonError("Max 2 buttons allowed") 81 | 82 | act = { 83 | "state": state, 84 | "details": details, 85 | "type": act_type.value, 86 | "status_display_type": status_type.value, 87 | "state_url": state_url, 88 | "details_url": details_url, 89 | "timestamps": { 90 | "start": ts_start, 91 | "end": ts_end 92 | }, 93 | "assets": { 94 | "large_image": large_image, 95 | "large_text": large_text, 96 | "large_url": large_url, 97 | "small_image": small_image, 98 | "small_text": small_text, 99 | "small_url": small_url 100 | }, 101 | "party": { 102 | "id": party_id, 103 | "size": party_size 104 | }, 105 | "secrets": { 106 | "join": join_secret, 107 | "spectate": spectate_secret, 108 | "match": match_secret 109 | }, 110 | "buttons": buttons 111 | } 112 | 113 | payload = { 114 | 'cmd': 'SET_ACTIVITY', 115 | 'args': { 116 | 'pid': os.getpid(), 117 | 'activity': None if clear else remove_none(act) 118 | }, 119 | 'nonce': str(uuid.uuid4()) 120 | } 121 | 122 | if not self.ipc.connected and TRY_RECONNECTING: 123 | self._setup() 124 | 125 | if not self.ipc.connected: 126 | return 127 | 128 | try: 129 | self.ipc._send(payload, OP_FRAME) 130 | self.is_running = True 131 | log.info('RPC set') 132 | return True 133 | except Exception as e: 134 | log.error('Failed to set RPC') 135 | self.disconnect() 136 | 137 | def clear(self): 138 | self.set_activity(clear=True) 139 | 140 | def disconnect(self): 141 | if not self.ipc.connected: 142 | return 143 | 144 | self.ipc.disconnect() 145 | self.is_running = False 146 | 147 | def run(self, update_every:int=1): 148 | try: 149 | while True: 150 | time.sleep(update_every) 151 | except KeyboardInterrupt: 152 | self.disconnect() 153 | 154 | class WindowsPipe: 155 | def __init__(self, app_id, exit_if_discord_close, exit_on_disconnect): 156 | self.app_id = app_id 157 | self.exit_if_discord_close = exit_if_discord_close 158 | self.exit_on_disconnect = exit_on_disconnect 159 | self.connected = True 160 | 161 | base_path = R'\\?\pipe\discord-ipc-{}' 162 | 163 | for i in range(10): 164 | path = base_path.format(i) 165 | 166 | try: 167 | self.socket = open(path, "w+b") 168 | except OSError as e: 169 | if self.exit_if_discord_close: 170 | log.debug("Failed to open {!r}: {}".format(path, e)) 171 | raise DiscordNotOpened() 172 | else: 173 | log.debug("Discord seems to be close.") 174 | else: 175 | break 176 | 177 | else: 178 | if self.exit_if_discord_close: 179 | raise DiscordNotOpened() 180 | else: 181 | log.warning("Discord is closed") 182 | self.connected = False 183 | 184 | if self.connected: 185 | log.debug(f"Connected to {path}") 186 | 187 | def _recv(self): 188 | enc_header = b'' 189 | header_size = 8 190 | 191 | while header_size: 192 | enc_header += self.socket.read(header_size) 193 | header_size -= len(enc_header) 194 | 195 | dec_header = struct.unpack("