Adding Discord Rich Presence to Renpy Games

It has been reported that due to changes in Discord methods, this no longer works.  I’ll leave it here if someone wants to figure out how to get it to work, but I have decided that it is too much bother to try and fix it myself.  I’m no longer adding discord presence to my games anyways.

discordsample

I searched the web looking for a tutorial like this, and with a little help from Animadoria, the moderator of Date Ariane Reddit, I finally figured it out.

First thing you need is a Renpy game you are working on that you want to add Discord Rich Presence to.

Second you need to sign up your game on Discord.  First go here: https://discordapp.com/developers/applications/ and click on “New Application” and fill the info about your game.  You might want to add a 1024×1024 image to represent your game.  What you should get in return is an 18 digit “Client ID” which you will use a lot here.

(Note: You do not need to have your game reviewed by Discord, nor do you need to sign up for the developers license to do this)

discordsample3

For this tutorial, I’m going to register “The Question” which is a free tutorial game that comes with Renpy. The client ID is 601663968288833536

While you are here, click on “Rich Presence” then “Art Assets” and upload some 512×512 images of the game. These should be safe for work and not contain sex or nudity as these will display publicly on Discord servers and you don’t want to get banned from Discord.

discordsample4

Loading the Code

Step 3, if you have Windows.

  1. Install the latest version of Python from https://www.python.org/
  2. While installing, make sure you check the box “Add to PATH”, it may prompt you to use administration mode to make path longer if it is already too long. (this is left over from DOS which is next.)
  3. Click on Cortana, or your start menu search bar if you are not on Windows 10 yet, and type “cmd” without quotes.  Welcome to DOS.
  4. Open up a folder file and go to the directory where your game is stored, click on the white space after your name and copy the text (ctrl-c) of your directory name, like mine is C:\Users\(my name)\Documents\renpy\the_question
  5. On the command prompt window type “cd” without quotes, then a space, then ctrl-v your directory name, then press enter.

discordsample2

If you have MacOS:

  1. Open your Terminal (/Applications/Utilities/Terminal.app)
  2. Go to the directory using cd [path to base folder of the game]. In my case, it’s /Users/[My account]/Documents/Renpy/the_question

Now to load the discord code we need to type two lines at the prompt and press enter after each. I made it so you can copy and paste if you want.

python -m pip install --target game/python-packages discord-rpc.py
python -m pip install --target game/python-packages requests

Your game directory should now have a new folder in it called python-packages and it should contain 13 sub folders including two starting with discord_rpc and two starting with requests.

Now to get your code to work in your game.  You will need to edit your script.rpy file.

We are going to start with the initialization code which should be put at the very top of the script.rpy file.

init -20 python:
    import discord_rpc
    import time

    def readyCallback(current_user):
        print('Our user: {}'.format(current_user))

    def disconnectedCallback(codeno, codemsg):
        print('Disconnected from Discord rich presence RPC. Code {}: {}'.format(
            codeno, codemsg
        ))

    def errorCallback(errno, errmsg):
        print('An error occurred! Error {}: {}'.format(
            errno, errmsg
        ))

label before_main_menu:
    python:
        # Note: 'event_name': callback
        callbacks = {
            'ready': readyCallback,
            'disconnected': disconnectedCallback,
            'error': errorCallback,
        }
        discord_rpc.initialize('601663968288833536', callbacks=callbacks, log=False)
        start = time.time()
        print(start)
        discord_rpc.update_connection()
        discord_rpc.run_callbacks()
        discord_rpc.update_presence(
            **{
                'details': 'Main Menu',
                'start_timestamp': start,
                'large_image_key': 'thequestion'
            }
        )
        discord_rpc.update_connection()
        discord_rpc.run_callbacks()

    return

You will need to change the red text to fit your own game.

If you already have a “label before_main_menu:” section of your game, put the python block at the beginning, and whatever else in the section below it before the return command.

Now we need to initialize the RPC when you start the game. This is done at the “label start:” part of script.rpy

# The game starts here.
label start:
    python:
        callbacks = {
            'ready': readyCallback,
            'disconnected': disconnectedCallback,
            'error': errorCallback,
        }
        discord_rpc.initialize('601663968288833536', callbacks=callbacks, log=False)
        start = time.time()
        discord_rpc.update_connection()
        discord_rpc.run_callbacks()
        discord_rpc.update_presence(
            **{
                'details': 'At College',
                'state': 'Lecture Hall',
                'large_image_key': 'thequestion',
                'start_timestamp': start
            }
        )

        discord_rpc.update_connection()
        discord_rpc.run_callbacks()
    #the real start of the game

Again, change the red code to fit your game.

Next we want to add state changes as new paths branch off.  This code is a subset of the above code.

label rightaway:
    python:
        start = time.time()
        discord_rpc.update_connection()
        discord_rpc.run_callbacks()
        discord_rpc.update_presence(
            **{
                'details': 'You Chose',
                'state': 'Right Away',
                'large_image_key': 'thequestion',
                'start_timestamp': start
            }
        )

        discord_rpc.update_connection()
        discord_rpc.run_callbacks()

Now go ahead and launch your game with discord running and watch your status.

discordsample5

8 comments

  • Very easy to implement — takes almost no time.

    Also, if you want to join our Discord server/guild, it’s on the sidebar!

  • Hey. This works! The only problem I got is with the time.
    Everytime you call that python block, time restarts in discord.

    What I did to fix it is adding a persistent variable that sets time.time() whenever you open the game in a “label splashscreen:” and then in every other python block instead of using “‘start_timestamp’: start” I used “‘start_timestamp’: store.persistent.initTime”, for example.

    This lacks of some stuff like the small icon, or the text when you hover both large and small images.
    I had to do some investigation to find that. But this works! Thank you.

    • Do you have any links where the code for stuff like the small icon, or text when you hover both large and small images can be found? I’m trying to find the documentation of the discord-rpc.py but I’m hitting a dead end on search engines.

  • Pingback: Free Patch to upgrade Date Ariane HD to 2021 version – Ariane's Life in the Metaverse

  • im getting an error saying indentation mismatch

  • Hello, Ariane
    Google linked your solutions, both here and on the lemmasoft forums, when someone on the official Ren’Py Discord server asked for a way to integrate rich presence into their game. On the forum, the fourth post indicates that changes have made the guide inoperable (https://lemmasoft.renai.us/forums/viewtopic.php?p=548203#p548203)
    As I appreciate your efforts to make this information accessible, I am wondering if you might be interested in updating your guides? This is merely a heads-up; no obligation is intended or expected!
    Yours, James

  • This still works fine for me in 2023; downside is that it doesn’t actually get detected as a game by discord, it only appears under the profile of whoever’s playing the game. still a good guide though

  • Henrique Machado

    It still works, but do you know if it works on another machines, or is it just a local thing ?

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.