def get_steam_level(self, steamid): with urllib.request.urlopen( "https://api.steampowered.com/IPlayerService/GetSteamLevel/v1/?key=" + self.api_key + "&steamid=" + str(steamid)) as url: return safe_get_list( safe_get_list(json.loads(url.read().decode()), "response"), "player_level", "Unknown")
def get_player_summary(self, steamid): with urllib.request.urlopen( "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=" + self.api_key + "&steamids=" + str(steamid)) as url: data = json.loads(url.read().decode()) return safe_get_list( safe_get_list(safe_get_list(data, "response"), "players"), 0, False)
def get_most_played_games(self, steamid, amount=5): games = [[0, 0] for i in range(amount)] for game in self.get_owned_games(steamid): if safe_get_list(game, "playtime_forever", 0) > min( [game[1] for game in games]): games[amount - 1][0] = safe_get_list(game, "appid") games[amount - 1][1] = safe_get_list(game, "playtime_forever") games = sorted(games, key=itemgetter(1), reverse=True) return games
def get_owned_games(self, steamid): with urllib.request.urlopen( "https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/?key=" + self.api_key + "&steamid=" + str(steamid)) as url: data = safe_get_list(json.loads(url.read().decode()), "response") if safe_get_list(data, "game_count", 0) == 0: return [] return safe_get_list(data, "games", [])
def get_game_titles(self): try: with urllib.request.urlopen( "https://api.steampowered.com/ISteamApps/GetAppList/v2/" ) as url: self.steam_titles = safe_get_list( safe_get_list(json.loads(url.read().decode()), "applist"), "apps") except: self.steam_titles = None return self.steam_titles
async def addiction(self, ctx, user=None, amount: int = 3): # Remove the message send by the author await asyncio.sleep(0.05) await ctx.message.delete() if user is None: embed = discord.Embed( title="Addiction Checker", description="Please provide a valid Steam ID or Profile URL", color=COLOR) embed.set_footer(text="Requested by {}".format( discord.utils.get(self.bot.get_all_members(), id=ctx.message.author.id)), icon_url=ctx.message.author.avatar_url) await ctx.send(embed=embed) return steamid = SteamAPI.get_steam_id(user) mostplayed = SteamAPI.get_most_played_games(steamid, amount=amount) steamsummary = SteamAPI.get_player_summary(steamid) embed = discord.Embed(title="Addiction Checker", description="Here's what I could find about " + str(safe_get_list(steamsummary, "personaname")) + "." + "\nThey seem to be addicted to:", color=COLOR) embed.add_field(name="Game", value=str('\n '.join([ SteamAPI.get_game_title(mostplayed[i][0]) for i in range(amount) ]))) embed.add_field(name="Playtime", value=str('\n '.join([ (str(int(mostplayed[i][1] / 60)) + "h") for i in range(amount) ]))) embed.add_field(name="Profile", value=str(safe_get_list(steamsummary, "profileurl")), inline=False) embed.set_thumbnail( url=str("https://steamcdn-a.akamaihd.net/steam/apps/{0}/header.jpg" .format(mostplayed[0][0]))) embed.set_footer( text= f"Requested by {ctx.message.author.nick if ctx.message.author.nick is not None else ctx.message.author.name}", icon_url=ctx.message.author.avatar_url) await ctx.send(embed=embed)
def get_steam_id(self, user): match = re.match( r'((http|https):\/\/steamcommunity\.com\/(profiles|id)\/(?P<steamid>\w+))', user) if match: user = match.group("steamid") with urllib.request.urlopen( "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=" + self.api_key + "&steamids=" + str(user)) as url: data = json.loads(url.read().decode()) if safe_get_list(safe_get_list(data, "response"), "players", []) == []: with urllib.request.urlopen( "https://api.steampowered.com/ISteamUser/ResolveVanityURL/v1/?key=" + self.api_key + "&vanityurl=" + str(user)) as url: data = json.loads(url.read().decode()) if safe_get_list(safe_get_list(data, "response"), "success") == 1: user = safe_get_list(safe_get_list(data, "response"), "steamid") else: user = None return user
async def steam(self, ctx, user=None): # Remove the message send by the author await asyncio.sleep(0.05) await ctx.message.delete() if user is None: embed = discord.Embed( title="Steam Accounts", description="Please provide a valid Steam ID or Profile URL", color=COLOR) embed.set_footer(text="Requested by {}".format( discord.utils.get(self.bot.get_all_members(), id=ctx.message.author.id)), icon_url=ctx.message.author.avatar_url) await ctx.send(embed=embed) return steamid = SteamAPI.get_steam_id(user) steamsummary = SteamAPI.get_player_summary(steamid) if not steamsummary: embed = discord.Embed( title="Steam Accounts", description="Please provide a valid steam id or url", color=COLOR) embed.set_footer(text="Requested by {}".format( discord.utils.get(self.bot.get_all_members(), id=ctx.message.author.id)), icon_url=ctx.message.author.avatar_url) await ctx.send(embed=embed) return embed = discord.Embed( title="Steam Account", description="Here's what I could find about " + str(safe_get_list(steamsummary, "personaname", steamid)) + ".", color=COLOR) embed.add_field( name="Name (Real Name)", value=str( safe_get_list(steamsummary, "personaname", "Unknown") + " (" + safe_get_list(steamsummary, "realname", "Private") + ")")) embed.add_field(name="ID", value=str( safe_get_list(steamsummary, "steamid", "Unknown"))) try: embed.add_field(name="Country", value=str( pycountry.countries.get(alpha_2=safe_get_list( steamsummary, "loccountrycode")).name)) except AttributeError: embed.add_field(name="Country", value=str("Private")) embed.add_field(name="Status", value=str( safe_get_list([ "Offline", "Online", "Busy", "Away", "Snooze", "Looking to Trade" ], safe_get_list(steamsummary, "personastate"), "Unknown"))) try: embed.add_field( name="Joined", value=str( datetime.datetime.fromtimestamp( int(safe_get_list(steamsummary, "timecreated", None))).strftime('%Y-%m-%d'))) except TypeError: embed.add_field(name="Joined", value=str("Unknown")) embed.add_field(name="Level", value=str(SteamAPI.get_steam_level(steamid))) embed.add_field(name="Profile", value=str( safe_get_list(steamsummary, "profileurl", "Unknown"))) embed.set_thumbnail(url=safe_get_list( steamsummary, "avatarfull", "https://steamuserimages-a.akamaihd.net/ugc/868480752636433334/1D2881C5C9B3AD28A1D8852903A8F9E1FF45C2C8/" )) embed.set_footer( text= f"Requested by {ctx.message.author.nick if ctx.message.author.nick is not None else ctx.message.author.name}", icon_url=ctx.message.author.avatar_url) embed.set_footer( text= f"Requested by {ctx.message.author.nick if ctx.message.author.nick is not None else ctx.message.author.name}", icon_url=ctx.message.author.avatar_url) await ctx.send(embed=embed)
async def dictionary(self, ctx, word): await asyncio.sleep(0.05) await ctx.message.delete() embed = discord.Embed( title="Dictionary", description="Here is what I found for `{}`: \n\n___".format(word), color=COLOR) meaning = self.pydictionary.meaning(word) if safe_get_list(meaning, "Noun", False): embed.add_field( name="Noun", value=str('`1.` ' + safe_get_list( safe_get_list(meaning, "Noun"), 0, "").capitalize() + '\n`2.` ' + safe_get_list(safe_get_list(meaning, "Noun"), 1, "").capitalize()), inline=False) if safe_get_list(meaning, "Verb", False): embed.add_field( name="Verb", value=str('`1.` ' + safe_get_list( safe_get_list(meaning, "Verb"), 0, "").capitalize() + '\n`2.` ' + safe_get_list(safe_get_list(meaning, "Verb"), 1, "").capitalize()), inline=False) if safe_get_list(meaning, "Adjective", False): embed.add_field( name="Adjective", value=str('`1.` ' + safe_get_list( safe_get_list(meaning, "Adjective"), 0, "").capitalize() + '\n`2.` ' + safe_get_list(safe_get_list(meaning, "Adjective"), 1, "").capitalize()), inline=False) if safe_get_list(meaning, "Adverb", False): embed.add_field( name="Adverb", value=str('`1.` ' + safe_get_list( safe_get_list(meaning, "Adverb"), 0, "").capitalize() + '\n`2.` ' + safe_get_list(safe_get_list(meaning, "Adverb"), 1, "").capitalize()), inline=False) await ctx.send(embed=embed)
async def dictionary(self, ctx, word): await asyncio.sleep(0.05) await ctx.message.delete() embed = discord.Embed( title="Dictionary", description="Here is what I found for `{}`: \n\n___".format(word), color=COLOR) meaning = self.pydictionary.meaning(word) if safe_get_list(meaning, "Noun", False): embed.add_field( name="Noun", value=str('`1.` ' + safe_get_list( safe_get_list(meaning, "Noun"), 0, "").capitalize() + '\n`2.` ' + safe_get_list(safe_get_list(meaning, "Noun"), 1, "").capitalize()), inline=False) if safe_get_list(meaning, "Verb", False): embed.add_field( name="Verb", value=str('`1.` ' + safe_get_list( safe_get_list(meaning, "Verb"), 0, "").capitalize() + '\n`2.` ' + safe_get_list(safe_get_list(meaning, "Verb"), 1, "").capitalize()), inline=False) if safe_get_list(meaning, "Adjective", False): embed.add_field( name="Adjective", value=str('`1.` ' + safe_get_list( safe_get_list(meaning, "Adjective"), 0, "").capitalize() + '\n`2.` ' + safe_get_list(safe_get_list(meaning, "Adjective"), 1, "").capitalize()), inline=False) if safe_get_list(meaning, "Adverb", False): embed.add_field( name="Adverb", value=str('`1.` ' + safe_get_list( safe_get_list(meaning, "Adverb"), 0, "").capitalize() + '\n`2.` ' + safe_get_list(safe_get_list(meaning, "Adverb"), 1, "").capitalize()), inline=False) embed.set_footer( text= f"Requested by {ctx.message.author.nick if ctx.message.author.nick is not None else ctx.message.author.name}", icon_url=ctx.message.author.avatar_url) await ctx.send(embed=embed)
def get_game_title(self, appid): for title in self.steam_titles: if str(safe_get_list(title, "appid")) == str(appid): return safe_get_list(title, "name") return "Unknown"