Replies: 3 comments 13 replies
-
If there is neither, you must (currently v1.7.3) manually determine the URL via the application_id (an attribute of the activity). # activity is a `discord.Activity`-object
# status_embed is an `discord.Embed`-object
if hasattr(activity, 'large_image_url'):
status_embed.set_image(url=activity.large_image_url)
elif hasattr(activity, 'application_id'):
app_id = activity.application_id
async with aiohttp.ClientSession() as session:
async with session.get("https://discord.com/api/v9/applications/detectable") as response:
app_liste = await response.json()
for app in app_liste:
if str(app_id) == app["id"]:
if app['icon']:
status_embed.set_thumbnail(url=f"https://cdn.discordapp.com/app-icons/{app_id}/{app['icon']}.png")
break
# Here's something that doesn't matter now, where I look to see if the app has a description and/or summary and add that to the embed |
Beta Was this translation helpful? Give feedback.
-
I don't think there is actually a image for non-rich presence games. If there is |
Beta Was this translation helpful? Give feedback.
-
So I went nuclear and combined and improved (performance wise) suggestion from @mccoderpy with my own implementation of getting images from Steam. So this attempts to load rich presence images from Discord, icon from Discord by id or name and then web assets from Steam. There could be some issues when more than 1 game has the same name, but generally that should not happen frequently. Also Discord and Steam application lists are cached and refresh once per hour, because getting them is really slow (Steam one takes about 5-10 seconds to get). async def load_game_image(activity: Union[Activity, Game]):
# try:
if hasattr(activity, 'large_image_url'):
watcher._game_image_small = activity.small_image_url
watcher._game_image_large = activity.large_image_url
watcher._game_image_small_text = activity.small_image_text
watcher._game_image_large_text = activity.large_image_text
if hasattr(activity, 'application_id'):
app_id = activity.application_id
discord_app = list(filter(lambda app: app['id'] == str(app_id), app_list))
if discord_app:
_LOGGER.debug("FOUND discord app by application_id = %s", discord_app)
watcher._game_image_small = f"https://cdn.discordapp.com/app-icons/{discord_app[0]['id']}/{discord_app[0]['icon']}.png"
discord_app_by_name = list(filter(lambda app: app['name'] == str(activity.name), app_list))
if discord_app_by_name:
_LOGGER.debug("FOUND discord app by name = %s", discord_app_by_name)
watcher._game_image_small = \
f"https://cdn.discordapp.com/app-icons/{discord_app_by_name[0]['id']}/{discord_app_by_name[0]['icon']}.png"
steam_app_by_name = list(filter(lambda steam_app: steam_app['name'] == str(activity.name), steam_app_list))
if steam_app_by_name:
steam_app_id = steam_app_by_name[0]["appid"]
_LOGGER.debug("FOUND Steam app by name = %s", steam_app_by_name[0])
timestamp = calendar.timegm(time.gmtime())
game_image_capsule_231x87 = \
f"https://cdn.cloudflare.steamstatic.com/steam/apps/{steam_app_id}/capsule_231x87.jpg?t={timestamp}"
game_image_capsule_467x181 = \
f"https://cdn.cloudflare.steamstatic.com/steam/apps/{steam_app_id}/capsule_467x181.jpg?t={timestamp}"
game_image_capsule_616x353 = \
f"https://cdn.cloudflare.steamstatic.com/steam/apps/{steam_app_id}/capsule_616x353.jpg?t={timestamp}"
game_image_header = \
f"https://cdn.cloudflare.steamstatic.com/steam/apps/{steam_app_id}/header.jpg?t={timestamp}"
game_image_hero_capsule = \
f"https://cdn.cloudflare.steamstatic.com/steam/apps/{steam_app_id}/hero_capsule.jpg?t={timestamp}"
game_image_library_600x900 = \
f"https://cdn.cloudflare.steamstatic.com/steam/apps/{steam_app_id}/library_600x900.jpg?t={timestamp}"
game_image_library_hero = \
f"https://cdn.cloudflare.steamstatic.com/steam/apps/{steam_app_id}/library_hero.jpg?t={timestamp}"
game_image_logo = \
f"https://cdn.cloudflare.steamstatic.com/steam/apps/{steam_app_id}/logo.jpg?t={timestamp}"
game_image_page_bg_raw = \
f"https://cdn.cloudflare.steamstatic.com/steam/apps/{steam_app_id}/page_bg_raw.jpg?t={timestamp}"
if await check_resource_exists(game_image_capsule_231x87):
watcher._game_image_capsule_231x87 = game_image_capsule_231x87
if await check_resource_exists(game_image_capsule_467x181):
watcher._game_image_capsule_467x181 = game_image_capsule_467x181
if await check_resource_exists(game_image_capsule_616x353):
watcher._game_image_capsule_616x353 = game_image_capsule_616x353
if await check_resource_exists(game_image_header):
watcher._game_image_header = game_image_header
if await check_resource_exists(game_image_hero_capsule):
watcher._game_image_hero_capsule = game_image_hero_capsule
if await check_resource_exists(game_image_library_600x900):
watcher._game_image_library_600x900 = game_image_library_600x900
if await check_resource_exists(game_image_library_hero):
watcher._game_image_library_hero = game_image_library_hero
if await check_resource_exists(game_image_logo):
watcher._game_image_logo = game_image_logo
if await check_resource_exists(game_image_page_bg_raw):
watcher._game_image_page_bg_raw = game_image_page_bg_raw
async def check_resource_exists(url):
async with aiohttp.ClientSession() as session:
_LOGGER.debug("Checking if web resource [%s] exists", url)
async with session.head(url) as response:
_LOGGER.debug("Resource [%s] response status = %s", url, response.status)
return response.status == 200
@tasks.loop(hours=1)
async def refresh_application_lists():
await load_application_list_cache()
refresh_application_lists.start()
async def load_application_list_cache():
await load_discord_application_list()
await load_steam_application_list()
async def load_discord_application_list():
async with aiohttp.ClientSession() as session:
_LOGGER.debug("Loading Discord detectable applications")
async with session.get("https://discord.com/api/v9/applications/detectable") as response:
global app_list
app_list = await response.json()
_LOGGER.debug("Loading Discord detectable applications finished")
async def load_steam_application_list():
async with aiohttp.ClientSession() as steam_session:
_LOGGER.debug("Loading Steam detectable applications")
async with steam_session.get("https://api.steampowered.com/ISteamApps/GetAppList/v2/") as steam_response:
steam_app_list_response = Dict(await steam_response.json())
global steam_app_list
steam_app_list = steam_app_list_response['applist']['apps']
_LOGGER.debug("Loading Steam detectable applications finished")
async def start_server(event):
await load_application_list_cache()
await bot.start(token) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
How do I get the icon of a game that the member is playing?
I can do it fine in rich presence game, because then I get full Activity object which has Activity.large_image_url and Activity.small_image_url.
But when the game is not rich presence I only get Game object that has no image attributes.
I need to have both rich presence and non rich presence games covered.
Beta Was this translation helpful? Give feedback.
All reactions