Skip to content

Commit

Permalink
added ""support"" for custom games
Browse files Browse the repository at this point in the history
  • Loading branch information
JustTemmie committed Aug 7, 2022
1 parent 9d12fa5 commit 4b86b2f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 28 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ create a file named `config.json` in the same directory as this file and fill it
"COVER_ART": {
"ENABLED": false,
"STEAM_GRID_API_KEY": "STEAM_GRID_API_KEY"
},

"CUSTOM_GAME_OVERWRITE": {
"ENABLED": false,
"NAME": "NAME"
}
}
```
Expand Down Expand Up @@ -61,7 +66,7 @@ the only thing you need to fill out on their site is the application name itself
for example i named mine "a game on steam" as shown in the screenshot above.

# Cover Art
and then lastly we have the `COVER_ART` section.
and then we have the `COVER_ART` section.

this will download an icon from steamGridDB and use it as the cover art for the discord presence.

Expand All @@ -73,6 +78,12 @@ you can get your API key here https://www.steamgriddb.com/profile/preferences/ap

additionally, this caches the url to a file named icons.txt, so if you don't like an icon it found you can replace the url in that file for whatever game.

# Custom Game Overwrite
if you want to display a game that isn't on steam, you can use the `CUSTOM_GAME_OVERWRITE` section.

set enabled to true and fill in the name of the game you want to display.

this will still try to grab an icon from steamGridDB, but if it can't find one you can try giving it one yourself.
# Python
python3.8 or higher is required.

Expand Down
5 changes: 5 additions & 0 deletions exampleconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@
"COVER_ART": {
"ENABLED": false,
"STEAM_GRID_API_KEY": "STEAM_GRID_API_KEY"
},

"CUSTOM_GAME_OVERWRITE": {
"ENABLED": false,
"NAME": "NAME"
}
}
3 changes: 2 additions & 1 deletion icons.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Deep Rock Galactic=https://cdn2.steamgriddb.com/file/sgdb-cdn/icon/fb508ef074ee78a0e58c68be06d8a2eb/32/256x256.png
Bloons TD 6=https://cdn2.steamgriddb.com/file/sgdb-cdn/icon/19e321d9f307ccfc1c37106191cbbc74/32/256x256.png
Terraria=https://cdn2.steamgriddb.com/file/sgdb-cdn/icon/83a368f54768f506b833130584455df4/32/512x512.png
Terraria=https://cdn2.steamgriddb.com/file/sgdb-cdn/icon_thumb/5830bc2bbfda076fdc00e649f2c82e1f.png
The Legend of Zelda: Ocarina of Time=https://cdn2.steamgriddb.com/file/sgdb-cdn/icon/a896144046a1b5bd6e3e034d00b4f73a.png
79 changes: 53 additions & 26 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def get_config():
GRID_ENABLED = config["COVER_ART"]["ENABLED"]
GRID_KEY = config["COVER_ART"]["STEAM_GRID_API_KEY"]

do_custom_game = config["CUSTOM_GAME_OVERWRITE"]["ENABLED"]
custom_game_name = config["CUSTOM_GAME_OVERWRITE"]["NAME"]



def get_steam_presence():
r = requests.get(f"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key={KEY}&format=json&steamids={str(USER)}").json()
Expand All @@ -66,20 +70,8 @@ def get_steam_presence():
except:
pass

def get_steam_grid_icon(gameName):
try:
with open(f'icons.txt', 'r') as icons:
for i in icons:
if gameName in i:
return i.split("=")[1]

results = sgdb.search_game(gameName)

# yes this is terrible code but i really couldn't figure out a better way to do this, sorry - pull request anyone?
result = str(results).split(',')[0][1:]
steamGridAppID = result[9:].split(' ')[0]

resolutions = [
def try_fetching_icon(gameName, steamGridAppID):
resolutions = [
512,
256,
128,
Expand All @@ -88,28 +80,53 @@ def get_steam_grid_icon(gameName):
16
]

grids = sgdb.get_icons_by_gameid(game_ids=[steamGridAppID])
icon = grids[0]
# basically some of the icons are .ico files, discord cannot display these
# what this does is basically brute force test a bunch of resolutions and pick the first one that works
# as steamgriddb actually hosts png versions of all the .ico files, they're just not returned by the API
grids = sgdb.get_icons_by_gameid(game_ids=[steamGridAppID])

# basically some of the icons are .ico files, discord cannot display these
# what this does is basically brute force test a bunch of resolutions and pick the first one that works
# as steamgriddb actually hosts png versions of all the .ico files, they're just not returned by the API
for icon in grids:
for res in resolutions:
icon = str(icon)
newURL = icon[:-4] + f"/32/{res}x{res}.png"

r = requests.get(newURL)
if r.status_code == 200:
break
return newURL

if res == 16:
return None

if res == 16:
with open(f'icons.txt', 'a') as icons:
icons.write(f"\n{gameName}=None")
icons.close()
print(f"could not find icon for {gameName} either ignore this or manually add one to icons.txt")



def get_steam_grid_icon(gameName):
try:
with open(f'icons.txt', 'r') as icons:
for i in icons:
if gameName in i:
return i.split("=")[1]

results = sgdb.search_game(gameName)

# yes this is terrible code but i really couldn't figure out a better way to do this, sorry - pull request anyone?
result = str(results).split(',')[0][1:]
steamGridAppID = result[9:].split(' ')[0]

newURL = try_fetching_icon(gameName, steamGridAppID)

if newURL == "":
return None

with open(f'icons.txt', 'a') as icons:
icons.write(f"{gameName}={newURL}\n")
icons.write(f"\n{gameName}={newURL}")
icons.close()

return newURL

except Exception as e:
print(f"ERROR: [{datetime.now().strftime('%d-%b-%Y %H:%M:%S')}] problem while fetching icon, this is likely because no icons exist as it's a niece game or something - error: {e}\n(can probably just be ignored lmao)\n")
return None
Expand All @@ -118,7 +135,7 @@ def set_game(game_title, game_icon, start_time):
if coverImage is None:
RPC.update(state=game_title, start=start_time)
else:
RPC.update(state=game_title, large_image=f"{game_icon[:-1]}", large_text=f"{game_title}", start=start_time)
RPC.update(state=game_title, large_image=f"{game_icon}", large_text=f"{game_title}", start=start_time)

if __name__ == "__main__":
if GRID_ENABLED:
Expand All @@ -130,7 +147,16 @@ def set_game(game_title, game_icon, start_time):
coverImage = None

while True:
game_title = get_steam_presence()
config = get_config()
do_custom_game = config["CUSTOM_GAME_OVERWRITE"]["ENABLED"]
custom_game_name = config["CUSTOM_GAME_OVERWRITE"]["NAME"]

if not do_custom_game:
game_title = get_steam_presence()

else:
game_title = custom_game_name


if game_title is None:
# note, this completely hides your current rich presence
Expand All @@ -146,5 +172,6 @@ def set_game(game_title, game_icon, start_time):

set_game(game_title, coverImage, startTime)


sleep(15)

13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "steam-presence-on-discord",
"version": "1.1",
"description": "A script which takes the game you're playing on steam and displays it on discord",
"author": "JustTemmie",
"repository": "JustTemmie/steam-presence-on-discord",
"license": "MIT",
"main": "main.py",
"dependencies": {
"python-steamgriddb": ">=1.0.5",
"pypresence": ">=4.2.1"
}
}

0 comments on commit 4b86b2f

Please sign in to comment.