1
0
Fork 0
mirror of synced 2024-09-28 23:41:41 +12:00

Corrected issue when no option is provided

This commit is contained in:
phxntxm 2016-08-20 00:13:13 -05:00
parent 92ca0831e0
commit 880fa0e17b

View file

@ -54,13 +54,16 @@ app_id_map = {"portal 2": 620,
"fallout 4": 377160, "fallout 4": 377160,
"fallout new vegas": 22490 "fallout new vegas": 22490
} }
def get_app_id(game: str): def get_app_id(game: str):
best_match = process.extractOne(game, app_id_map.keys()) best_match = process.extractOne(game, app_id_map.keys())
if best_match[1] > 80: if best_match[1] > 80:
return app_id_map.get(best_match[0]) return app_id_map.get(best_match[0])
else: else:
return None return None
class Steam: class Steam:
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
@ -84,8 +87,7 @@ class Steam:
return int(tree[0].text) return int(tree[0].text)
except ValueError: except ValueError:
return None return None
@commands.group(pass_context=True, invoke_without_command=True) @commands.group(pass_context=True, invoke_without_command=True)
@checks.custom_perms(send_messages=True) @checks.custom_perms(send_messages=True)
async def steam(self, ctx, member: discord.Member, *option: str): async def steam(self, ctx, member: discord.Member, *option: str):
@ -96,7 +98,8 @@ class Steam:
# First make sure the person requested isn't the bot # First make sure the person requested isn't the bot
# Need to keep up the bot's sassy attitude, right? # Need to keep up the bot's sassy attitude, right?
if member == ctx.message.server.me: if member == ctx.message.server.me:
await self.bot.say("I don't play video games anymore, I crushed everyone so badly I made everyone cry last time.") await self.bot.say(
"I don't play video games anymore, I crushed everyone so badly I made everyone cry last time.")
return return
# Get the user's steam id if it exists # Get the user's steam id if it exists
try: try:
@ -124,14 +127,18 @@ class Steam:
if app_id is None: if app_id is None:
await self.bot.say("Sorry, I couldn't find a close match for the game {}".format(game)) await self.bot.say("Sorry, I couldn't find a close match for the game {}".format(game))
return return
url = "{}/ISteamUserStats/GetPlayerAchievements/v0001/?key={}&steamid={}&appid={}".format(base_url, self.key, steam_id, app_id) url = "{}/ISteamUserStats/GetPlayerAchievements/v0001/?key={}&steamid={}&appid={}".format(base_url,
self.key,
steam_id,
app_id)
elif option[0] == "games": elif option[0] == "games":
await self.bot.say("Currently disabled, only achievements and info are available as options") await self.bot.say("Currently disabled, only achievements and info are available as options")
return return
except IndexError: except IndexError:
option = ['info']
url = "{}/ISteamUser/GetPlayerSummaries/v0002/?key={}&steamids={}".format(base_url, self.key, steam_id) url = "{}/ISteamUser/GetPlayerSummaries/v0002/?key={}&steamids={}".format(base_url, self.key, steam_id)
# Make the request and get the data in json response, all url's we're using will give a json response # Make the request and get the data in json response, all url's we're using will give a json response
try: try:
async with self.session.get(url, headers=self.headers) as response: async with self.session.get(url, headers=self.headers) as response:
@ -139,7 +146,7 @@ class Steam:
except: except:
await self.bot.say("Sorry, I failed looking up that user's information. I'll try better next time ;-;") await self.bot.say("Sorry, I failed looking up that user's information. I'll try better next time ;-;")
return return
if option[0] == "info": if option[0] == "info":
data = data['response']['players'][0] data = data['response']['players'][0]
# We need to take into account private profiles, so first add public stuff # We need to take into account private profiles, so first add public stuff
@ -175,7 +182,9 @@ class Steam:
if data['playerstats']['error'] == "Requested app has no stats": if data['playerstats']['error'] == "Requested app has no stats":
await self.bot.say("{} has no achievements on the game {}".format(member.display_name, game)) await self.bot.say("{} has no achievements on the game {}".format(member.display_name, game))
elif data['playerstats']['error'] == "Profile is not public": elif data['playerstats']['error'] == "Profile is not public":
await self.bot.say("Sorry, {} has a private steam account! I cannot lookup their achievements!".format(member.display_name)) await self.bot.say(
"Sorry, {} has a private steam account! I cannot lookup their achievements!".format(
member.display_name))
return return
# Get all achievements for this game, making sure they actually exist # Get all achievements for this game, making sure they actually exist
try: try:
@ -185,9 +194,10 @@ class Steam:
return return
# Now get all achievements that the user has achieved # Now get all achievements that the user has achieved
successful_achievements = [data for data in all_achievements if data['achieved'] == 1] successful_achievements = [data for data in all_achievements if data['achieved'] == 1]
await self.bot.say("{} has achieved {}/{} achievements on the game {}".format(member.display_name, len(successful_achievements), len(all_achievements), game)) await self.bot.say("{} has achieved {}/{} achievements on the game {}".format(member.display_name,
len(successful_achievements),
len(all_achievements), game))
@steam.command(name='add', aliases=['link', 'create'], pass_context=True) @steam.command(name='add', aliases=['link', 'create'], pass_context=True)
@checks.custom_perms(send_messages=True) @checks.custom_perms(send_messages=True)
async def add_steam(self, ctx, profile: str): async def add_steam(self, ctx, profile: str):
@ -198,7 +208,7 @@ class Steam:
user = re.search("((?<=://)?steamcommunity.com/(id|profile)/)+(.*)", profile).group(3) user = re.search("((?<=://)?steamcommunity.com/(id|profile)/)+(.*)", profile).group(3)
except AttributeError: except AttributeError:
user = profile user = profile
# To look up userdata, we need the steam ID. Try to convert to an int, if we can, it's the steam ID # To look up userdata, we need the steam ID. Try to convert to an int, if we can, it's the steam ID
# If we can't convert to an int, use our method to find the steam ID for a certain user # If we can't convert to an int, use our method to find the steam ID for a certain user
try: try:
@ -209,7 +219,7 @@ class Steam:
if steam_id is None: if steam_id is None:
await self.bot.say("Sorry, couldn't find that Steam user!") await self.bot.say("Sorry, couldn't find that Steam user!")
return return
# Save the author's steam ID, ensuring to only overwrite the steam id if they already exist # Save the author's steam ID, ensuring to only overwrite the steam id if they already exist
author = ctx.message.author author = ctx.message.author
steam_users = config.get_content('steam_users') or {} steam_users = config.get_content('steam_users') or {}
@ -217,10 +227,11 @@ class Steam:
steam_users[author.id]['steam_id'] = steam_id steam_users[author.id]['steam_id'] = steam_id
else: else:
steam_users[author.id] = {'steam_id': steam_id} steam_users[author.id] = {'steam_id': steam_id}
config.save_content('steam_users', steam_users) config.save_content('steam_users', steam_users)
await self.bot.say("I have just saved your steam account, you should now be able to view stats for your account!") await self.bot.say(
"I have just saved your steam account, you should now be able to view stats for your account!")
@commands.command(pass_context=True) @commands.command(pass_context=True)
@checks.custom_perms(send_messages=True) @checks.custom_perms(send_messages=True)
async def csgo(self, ctx, member: discord.Member): async def csgo(self, ctx, member: discord.Member):
@ -231,14 +242,16 @@ class Steam:
await self.bot.say("Sorry, but I don't have that user's steam account saved!") await self.bot.say("Sorry, but I don't have that user's steam account saved!")
return return
url = "{}/ISteamUserStats/GetUserStatsForGame/v0002/?key={}&appid=730&steamid={}".format(base_url, self.key, steam_id) url = "{}/ISteamUserStats/GetUserStatsForGame/v0002/?key={}&appid=730&steamid={}".format(base_url, self.key,
steam_id)
async with self.session.get(url, headers=self.headers) as response: async with self.session.get(url, headers=self.headers) as response:
data = await response.json() data = await response.json()
stuff_to_print = ['total_kills', 'total_deaths', 'total_wins', 'total_mvps'] stuff_to_print = ['total_kills', 'total_deaths', 'total_wins', 'total_mvps']
stats = "\n".join( stats = "\n".join(
"{}: {}".format(d['name'], d['value']) for d in data['playerstats']['stats'] if d['name'] in stuff_to_print) "{}: {}".format(d['name'], d['value']) for d in data['playerstats']['stats'] if d['name'] in stuff_to_print)
await self.bot.say("CS:GO Stats for user {}: \n```\n{}```".format(member.display_name, stats.title().replace("_", " "))) await self.bot.say(
"CS:GO Stats for user {}: \n```\n{}```".format(member.display_name, stats.title().replace("_", " ")))
def setup(bot): def setup(bot):