Adding Discord Rich Presence to Renpy Games

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

As a bonus, I uploaded a discord friendly version of Date Ariane HD for Windows with these Discord tags.  Test it out. If it works, I’ll add it to the main game.

One thought on “Adding Discord Rich Presence to Renpy Games

  1. Very easy to implement — takes almost no time.

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

Leave a Reply to Animadoria Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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