Skip to content

Commit 874ea12

Browse files
committed
Update social.py to use new DekuDeals games cog/data
1 parent 0d2c2b1 commit 874ea12

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

modules/games.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ def search(self, query: str) -> Optional[dict]:
156156

157157
return match
158158

159-
async def get_image(self, deku_id: str, as_url: bool = False) -> Union[str, None]:
159+
async def get_image(self, deku_id: str, as_url: bool = False):
160160
game = self.db.find_one({'deku_id': deku_id}, projection={'image': 1})
161161

162-
if not game or 'image' not in game or type not in game['image']:
162+
if not game or 'image' not in game:
163163
return None
164164

165165
url = game['image']
@@ -172,6 +172,10 @@ async def get_image(self, deku_id: str, as_url: bool = False) -> Union[str, None
172172
resp.raise_for_status()
173173
data = await resp.read()
174174
return io.BytesIO(data)
175+
176+
def get_name(self, deku_id: str):
177+
document = self.db.find_one({'deku_id': deku_id}, projection={'name': 1})
178+
return document['name'] if document else None
175179

176180
@app_commands.guilds(discord.Object(id=config.nintendoswitch))
177181
class GamesCommand(app_commands.Group):

modules/social.py

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
import config # type: ignore
1616
import discord
1717
import emoji_data
18-
import gridfs
1918
import numpy as np
2019
import pymongo
2120
import pytz
22-
import requests
23-
import token_bucket
2421
import yaml
2522
from discord import app_commands
2623
from discord.ext import commands
@@ -40,10 +37,6 @@ def __init__(self, bot):
4037

4138
self.Games = self.bot.get_cog('Games')
4239

43-
# !profile ratelimits
44-
self.bucket_storage = token_bucket.MemoryStorage()
45-
self.profile_bucket = token_bucket.Limiter(1 / 30, 2, self.bucket_storage) # burst limit 2, renews at 1 / 30 s
46-
4740
# Add context menus to command tree
4841
self.profileContextMenu = app_commands.ContextMenu(
4942
name='View Profile Card', callback=self._profile_view, type=discord.AppCommandType.user
@@ -125,8 +118,11 @@ def __init__(self, bot):
125118
}
126119

127120
self.easter_egg_games = {
128-
self.bot.user.id: ["3030-80463"], # Super Mario 3D All-Stars
129-
config.parakarry: ["3030-89844"], # Paper Mario: The Thousand-Year Door
121+
self.bot.user.id: ["602caef0-b928-4dcb-8785-3da2a7fb747d"], # Super Mario 3D All-Stars
122+
config.parakarry: [
123+
"c47d6eda-ae22-4036-afca-bb7df4e81738", # Paper Mario: The Thousand-Year Door
124+
"dd2dbc1d-938c-495a-a630-75ac96024695" # Nintendo 64 – Nintendo Classics
125+
],
130126
}
131127

132128
self.easter_egg_text = [ # Message text for bot easter egg, keep under 12 chars
@@ -349,12 +345,12 @@ def _cache_flag_image(self, name) -> Image:
349345

350346
return self.flagImgCache[name]
351347

352-
async def _cache_game_img(self, guid: str, theme) -> Image:
348+
async def _cache_game_img(self, deku_id: str, theme) -> Image:
353349
EXPIRY, IMAGE = 0, 1
354350
do_recache = False
355351

356-
if guid in self.gameImgCache:
357-
if time.time() > self.gameImgCache[guid][EXPIRY]: # Expired in cache
352+
if deku_id in self.gameImgCache:
353+
if time.time() > self.gameImgCache[deku_id][EXPIRY]: # Expired in cache
358354
do_recache = True
359355
else: # Not in cache
360356
do_recache = True
@@ -364,7 +360,7 @@ async def _cache_game_img(self, guid: str, theme) -> Image:
364360
return theme['missingImage']
365361

366362
try:
367-
gameImg = await self.Games.get_image(guid, 'icon_url')
363+
gameImg = await self.Games.get_image(deku_id)
368364

369365
if gameImg:
370366
gameIcon = Image.open(gameImg).convert('RGBA').resize((120, 120))
@@ -375,12 +371,12 @@ async def _cache_game_img(self, guid: str, theme) -> Image:
375371
logging.error('Error caching game icon', exc_info=e)
376372
gameIcon = None
377373

378-
self.gameImgCache[guid] = (time.time() + 60 * 60 * 48, gameIcon) # Expire in 48 hours
374+
self.gameImgCache[deku_id] = (time.time() + 60 * 60 * 48, gameIcon) # Expire in 48 hours
379375

380-
if self.gameImgCache[guid][IMAGE] is None:
376+
if self.gameImgCache[deku_id][IMAGE] is None:
381377
return theme['missingImage']
382378

383-
return self.gameImgCache[guid][IMAGE]
379+
return self.gameImgCache[deku_id][IMAGE]
384380

385381
def _generate_background_preview(self, backgrounds) -> discord.File:
386382
# square_length: Gets smallest square dimensions that will fit length of backgrounds, ie len 17 -> 25
@@ -605,16 +601,16 @@ async def _generate_profile_card(self, profile: dict, background: dict) -> disco
605601
setGames = list(dict.fromkeys(setGames)) # Remove duplicates from list, just in case
606602
setGames = setGames[:5] # Limit to 5 results, just in case
607603

608-
for game_guid in setGames:
604+
for game_deku_id in setGames:
609605
if not self.Games:
610606
continue
611607

612-
gameName = self.Games.get_preferred_name(game_guid)
608+
gameName = self.Games.get_name(game_deku_id)
613609

614610
if not gameName:
615611
continue
616612

617-
gameIcon = await self._cache_game_img(game_guid, theme)
613+
gameIcon = await self._cache_game_img(game_deku_id, theme)
618614
card.paste(gameIcon, gameIconLocations[gameCount], gameIcon)
619615

620616
nameW = 1285
@@ -786,12 +782,13 @@ async def generate_user_flow_embed(self, member: discord.Member, new_user: bool
786782
main_img = await self._generate_profile_card_from_member(member)
787783
embed.set_image(url='attachment://profile.png')
788784

785+
commandID = 0
789786
for command in self.bot.tree.get_commands(guild=discord.Object(id=config.nintendoswitch)):
790787
# Iterate over commands in the tree so we can get the profile command ID
791-
if command.name == 'profile':
788+
if command.name == 'profile' and command.extras:
789+
commandID = command.extras['id']
792790
break
793791

794-
commandID = command.extras['id']
795792
if new_user:
796793
# We need to minorly modify description for info for first time user flow
797794
embed_description = (
@@ -986,39 +983,39 @@ async def _profile_games(
986983

987984
db = mclient.bowser.games
988985

989-
# If user selected an auto-complete result, we will be provided the guid automatically which saves effort
986+
# If user selected an auto-complete result, we will be provided the deku_id automatically which saves effort
990987
flagConfirmation = False
991988
gameList = []
992-
guid1 = db.find_one({'guid': game1})
993-
guid2 = None if not game2 else db.find_one({'guid': game2})
994-
guid3 = None if not game3 else db.find_one({'guid': game3})
995-
guid4 = None if not game4 else db.find_one({'guid': game4})
996-
guid5 = None if not game5 else db.find_one({'guid': game5})
989+
deku_id1 = db.find_one({'deku_id': game1})
990+
deku_id2 = None if not game2 else db.find_one({'deku_id': game2})
991+
deku_id3 = None if not game3 else db.find_one({'deku_id': game3})
992+
deku_id4 = None if not game4 else db.find_one({'deku_id': game4})
993+
deku_id5 = None if not game5 else db.find_one({'deku_id': game5})
997994

998995
games = [game1, game2, game3, game4, game5]
999-
guids = [guid1, guid2, guid3, guid4, guid5]
996+
deku_ids = [deku_id1, deku_id2, deku_id3, deku_id4, deku_id5]
1000997

1001-
def resolve_guid(game_name: str):
998+
def resolve_deku_id(game_name: str):
1002999
return self.Games.search(game_name)
10031000

10041001
async def return_failure(interaction: discord.Interaction, game_name: str):
10051002
return await interaction.followup.send(
1006-
f'{config.redTick} I was unable to match the game named "{game_name}" with any game released on the Nintendo Switch. Please try again, or contact a moderator if you believe this is in error'
1003+
f'{config.redTick} I was unable to match the game named "{game_name}". Please try again, or contact DM Parakarry if you believe this is in error'
10071004
)
10081005

10091006
flagConfirmation = False
1010-
for idx, guid in enumerate(guids):
1007+
for idx, deku_id in enumerate(deku_ids):
10111008
if not games[idx]:
10121009
continue
10131010

1014-
if not guid:
1011+
if not deku_id:
10151012
flagConfirmation = True
1016-
guid = resolve_guid(games[idx])
1013+
deku_id = resolve_deku_id(games[idx])
10171014

1018-
if not guid:
1015+
if not deku_id:
10191016
return await return_failure(interaction, games[idx])
10201017

1021-
gameList.append(guid['guid'])
1018+
gameList.append(deku_id['deku_id'])
10221019

10231020
msg = None
10241021
if flagConfirmation:
@@ -1027,7 +1024,7 @@ async def return_failure(interaction: discord.Interaction, game_name: str):
10271024
title='Are these games correct?', description='*Use the buttons below to confirm*', color=0xF5FF00
10281025
)
10291026
for idx, game in enumerate(gameList):
1030-
embed.add_field(name=f'Game {idx + 1}', value=db.find_one({'guid': game})['name'])
1027+
embed.add_field(name=f'Game {idx + 1}', value=db.find_one({'deku_id': game})['name'])
10311028

10321029
view = tools.NormalConfirmation(timeout=90.0)
10331030
view.message = await interaction.followup.send(
@@ -1048,7 +1045,7 @@ async def return_failure(interaction: discord.Interaction, game_name: str):
10481045
elif not view.value:
10491046
# User selected No
10501047
return await view.message.edit(
1051-
content=f'{config.redTick} It looks like the games I matched for you were incorrect, sorry about that. Please rerun the command to try again. A tip to a great match is to click on an autocomplete option for each game and to type the title as completely as possible -- this will ensure that the correct game is selected. If you continue to experience difficulty in adding a game, please contact a moderator',
1048+
content=f'{config.redTick} It looks like the games I matched for you were incorrect, sorry about that. Please rerun the command to try again. A tip to a great match is to click on an autocomplete option for each game and to type the title as completely as possible -- this will ensure that the correct game is selected. If you continue to experience difficulty in adding a game, please DM Parakarry',
10521049
embed=None,
10531050
)
10541051

@@ -1292,11 +1289,11 @@ async def _profile_validate(
12921289
'usertime': "Not specified",
12931290
'trophies': [None] * 18,
12941291
'games': [
1295-
'3030-88442',
1296-
'3030-87348',
1297-
'3030-89546',
1298-
'3030-84825',
1299-
'3030-89623',
1292+
'01b3e429-9a02-4c45-a221-1b5cf101f433', # MEGA SIMULATOR BUNDLE - Forest Ranger Life Simulator...
1293+
'7dcab3d8-c1c1-4462-8807-3427c0a61a37', # G-Mode Archives+ Todo Ryunosuke Tantei Nikki Vol.6 ...
1294+
'344ca3de-1209-46c7-91da-96966951033b', # Puzzles & Colors Games for Kids Toddler Coloring Book Draw ...
1295+
'26eefce2-d2ea-4dee-a85b-c3680da69a1a', # THE Table Game Deluxe Pack -Mahjong, Go, Shogi, ...
1296+
'5b8bb732-f2fa-4964-a8f4-d102772506c5', # Prinny Presents NIS Classics Volume 2: Makai Kingdom: ...
13001297
], # Games with really long titles
13011298
}
13021299

0 commit comments

Comments
 (0)