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
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:
- Install the latest version of Python from https://www.python.org/
- 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.
- 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
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
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
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
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()
More details and screenshots at https://arianeb.com/2019/07/19/adding-d ... npy-games/