Adding Discord Rich Presence to Renpy Games

A place to discuss things that aren't specific to any one creator or game.
Forum rules
Ren'Py specific questions should be posted in the Ren'Py Questions and Annoucements forum, not here.
Post Reply
Message
Author
ArianeB
Regular
Posts: 25
Joined: Sun Jul 25, 2010 6:41 pm
Completed: Date Ariane, Something's In The Air
Location: USA
Contact:

Adding Discord Rich Presence to Renpy Games

#1 Post by ArianeB »

I know this topic is in the cookbook, but it is poorly documented, and a very messy process. I found a process and some code that should be way easier to use, and thought I'd share it here.
Image
I'm going to add Discord Rich Presence to the demo game "The Question" in the Renpy build as an example. You should be able to do this for any other game.

Step 1: Sign up your game to Discord.

This is easy as going to https://discordapp.com/developers/applications/, and adding the name of your game to obtain a client ID. I added "The Question" and got a client ID of 601663968288833536

Image

While there I uploaded a 512x512 image from the game and called it "thequestion" as a lone Rich Presence asset.

Step 2: Load the proper python code into your Renpy Game.

For Windows:
  • 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.)
  • 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.
  • 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
  • On the command prompt window type “cd” without quotes, then a space, then ctrl-v your directory name, then press enter.
For Mac:
  • Open your Terminal (/Applications/Utilities/Terminal.app)
  • 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.

Code: Select all

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

Code: Select all

python -m pip install --target game/python-packages requests
Note: You used to be able to do this with only the first line, but now you get "requests not found" errors, so best bet is to load both. Luckily they are small codes.

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.

Code: Select all

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) ## Substitute with your own client ID from step 1
        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' ## Substitute with your own image name from step 1
            }
        )
        discord_rpc.update_connection()
        discord_rpc.run_callbacks()

    return
You will need to change the commented 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 last "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

Code: Select all

# The game starts here.
label start:
    python:
        callbacks = {
            'ready': readyCallback,
            'disconnected': disconnectedCallback,
            'error': errorCallback,
        }
        discord_rpc.initialize('601663968288833536', callbacks=callbacks, log=False) ## Substitute with your own client ID from step 1
        start = time.time()
        discord_rpc.update_connection()
        discord_rpc.run_callbacks()
        discord_rpc.update_presence(
            **{
                'details': 'At College', ## Substitute with the starting location of your game
                'state': 'Lecture Hall', ## Substitute with the starting state of your game
                'large_image_key': 'thequestion', ## Substitute with your own image name from step 1
                'start_timestamp': start
            }
        )

        discord_rpc.update_connection()
        discord_rpc.run_callbacks()
    #the real start of the game
Next we want to add state changes as new paths branch off. This code is a subset of the above code.

Code: Select all

label rightaway:  ## This is from "the Question" example 
    python:
        start = time.time()
        discord_rpc.update_connection()
        discord_rpc.run_callbacks()
        discord_rpc.update_presence(
            **{
                'details': 'You Chose', ## Substitute with the next location of your game
                'state': 'Right Away', ## Substitute with the next state of your game
                'large_image_key': 'thequestion', ## Substitute with your own image name uploaded to discord servers
                '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 as you play. Everyone on every server you are logged into should see the same.

More details and screenshots at https://arianeb.com/2019/07/19/adding-d ... npy-games/

User avatar
Andredron
Miko-Class Veteran
Posts: 719
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Adding Discord Rich Presence to Renpy Games

#2 Post by Andredron »

ArianeB wrote: Fri Jul 19, 2019 11:13 pm I know this topic is in the cookbook, but it is poorly documented, and a very messy process. I found a process and some code that should be way easier to use, and thought I'd share it here.
Image
I'm going to add Discord Rich Presence to the demo game "The Question" in the Renpy build as an example. You should be able to do this for any other game.

Step 1: Sign up your game to Discord.

This is easy as going to https://discordapp.com/developers/applications/, and adding the name of your game to obtain a client ID. I added "The Question" and got a client ID of 601663968288833536

Image

While there I uploaded a 512x512 image from the game and called it "thequestion" as a lone Rich Presence asset.

Step 2: Load the proper python code into your Renpy Game.

For Windows:
  • 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.)
  • 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.
  • 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
  • On the command prompt window type “cd” without quotes, then a space, then ctrl-v your directory name, then press enter.
For Mac:
  • Open your Terminal (/Applications/Utilities/Terminal.app)
  • 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.

Code: Select all

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

Code: Select all

python -m pip install --target game/python-packages requests
Note: You used to be able to do this with only the first line, but now you get "requests not found" errors, so best bet is to load both. Luckily they are small codes.

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.

Code: Select all

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) ## Substitute with your own client ID from step 1
        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' ## Substitute with your own image name from step 1
            }
        )
        discord_rpc.update_connection()
        discord_rpc.run_callbacks()

    return
You will need to change the commented 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 last "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

Code: Select all

# The game starts here.
label start:
    python:
        callbacks = {
            'ready': readyCallback,
            'disconnected': disconnectedCallback,
            'error': errorCallback,
        }
        discord_rpc.initialize('601663968288833536', callbacks=callbacks, log=False) ## Substitute with your own client ID from step 1
        start = time.time()
        discord_rpc.update_connection()
        discord_rpc.run_callbacks()
        discord_rpc.update_presence(
            **{
                'details': 'At College', ## Substitute with the starting location of your game
                'state': 'Lecture Hall', ## Substitute with the starting state of your game
                'large_image_key': 'thequestion', ## Substitute with your own image name from step 1
                'start_timestamp': start
            }
        )

        discord_rpc.update_connection()
        discord_rpc.run_callbacks()
    #the real start of the game
Next we want to add state changes as new paths branch off. This code is a subset of the above code.

Code: Select all

label rightaway:  ## This is from "the Question" example 
    python:
        start = time.time()
        discord_rpc.update_connection()
        discord_rpc.run_callbacks()
        discord_rpc.update_presence(
            **{
                'details': 'You Chose', ## Substitute with the next location of your game
                'state': 'Right Away', ## Substitute with the next state of your game
                'large_image_key': 'thequestion', ## Substitute with your own image name uploaded to discord servers
                '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 as you play. Everyone on every server you are logged into should see the same.

More details and screenshots at https://arianeb.com/2019/07/19/adding-d ... npy-games/
Your code really works and more precisely explains everything. Thank!
The question is, and perhaps something similar can be done with such an example -

https://github.com/NoMore201/googleplay-api

abysswatcher
Regular
Posts: 42
Joined: Sun Apr 12, 2020 11:50 pm
Projects: To-Live:The Struggle
Organization: Youyu de Shijie(憂鬱的世界)
Github: LuYifeng112
itch: https://luyifeng.itc
Location: New Zealand
Contact:

Re: Adding Discord Rich Presence to Renpy Games

#3 Post by abysswatcher »

Hey, I tried what you said and it totally worked! Thanks
Just curious would it be possible to implement a "spectator" option? and how?
The goal of the revolution is to achieve the people's rights, but during the course of the revolution, we must stress military power - and the two are mutually contradictory.
-Sun Yat-sen
"Become a Visual Novel writer they said, it will be fun" (little did they know they were right)

User avatar
Gabmag
Regular
Posts: 76
Joined: Wed Sep 19, 2018 10:49 pm
Completed: A Date with Death, The Divine Speaker, start;again, Amelie, The Sun and the Moon
Projects: Dreambound
Organization: Two and a Half Studios
IRC Nick: Gabmag
Location: Australia
Contact:

Re: Adding Discord Rich Presence to Renpy Games

#4 Post by Gabmag »

Hey!! With recent changes making this not work, are you planning to update this at all? Thanks!
i make games sometimes, other times i cry.

Post Reply

Who is online

Users browsing this forum: No registered users