From 302e600b1132356c41b84aa61141cd3363c2a7ec Mon Sep 17 00:00:00 2001 From: ThatGuyDed <44003663+ThatGuyDed@users.noreply.github.com> Date: Thu, 9 May 2019 19:19:35 +0100 Subject: [PATCH 1/3] Linking Accounts Created a very crude account linking method that allows the user to link a steam account to 1 discord account (discord accounts can only have 1 linked account but steam accounts can have multiple connected discord accounts). Also created a text file to store linked accounts --- UserIDs.txt | 1 + calculated_bot.py | 132 ++++++++++++++++++++++++++++++++++++++++++++-- config_example.py | 2 +- 3 files changed, 129 insertions(+), 6 deletions(-) create mode 100644 UserIDs.txt diff --git a/UserIDs.txt b/UserIDs.txt new file mode 100644 index 0000000..af033fc --- /dev/null +++ b/UserIDs.txt @@ -0,0 +1 @@ +298910672731766784:76561198404047726 diff --git a/calculated_bot.py b/calculated_bot.py index 89b8d9d..e49823b 100644 --- a/calculated_bot.py +++ b/calculated_bot.py @@ -13,7 +13,7 @@ from discord.ext.commands import Bot try: - from config import TOKEN, BOT_PREFIX + from config_example import TOKEN, BOT_PREFIX except ImportError: print('Unable to run bot, as token does not exist!') sys.exit() @@ -21,6 +21,8 @@ bot = Bot(BOT_PREFIX) bot.remove_command("help") +NotLinked = True + def get_json(url): try: @@ -274,9 +276,49 @@ async def get_profile(ctx): args = ctx.message.content.split(" ") if len(args) < 2: - await bot.send_message(ctx.message.channel, + PastUsers = open("UserIDs.txt", "r+") + NotLinked = True + for line in PastUsers: + check = line + check = check.strip() + check = check.split(":") + ##print(check) + if check[0] == ctx.message.author.id: + NotLinked = False + break + else: + NotLinked = True + if NotLinked: + await bot.send_message(ctx.message.channel, f"Not enough arguments! The proper form of this command is: `{BOT_PREFIX}profile `") - return + return + else: + # fetches the profile for the ID. if user can not be found, tell the user so. + id = check[1] + response_stats = get_json("https://calculated.gg/api/player/{}/profile_stats".format(id)) + + car_name = response_stats["car"]["carName"] + car_percentage = str(round(response_stats["car"]["carPercentage"] * 100, 1)) + "%" + avatar_link, avatar_name, platform, past_names = get_player_profile(id) + + list_past_names = "" + for name in past_names: + list_past_names = list_past_names + name + "\n" + + # creates stats_embed + stats_embed = discord.Embed( + color=discord.Color.blue() + ) + + stats_embed.set_author(name=avatar_name, url="https://calculated.gg/players/{}/overview".format(id), + icon_url="https://media.discordapp.net/attachments/495315775423381518/499488781536067595/bar_graph-512.png") + stats_embed.set_thumbnail(url=avatar_link) + stats_embed.add_field(name="Favourite car", value=car_name + " (" + car_percentage + ")") + stats_embed.add_field(name="Past names", value=list_past_names) + + # send message + await bot.send_message(ctx.message.channel, embed=stats_embed) + return elif len(args) > 2: await bot.send_message(ctx.message.channel, f"Too many arguments! The proper form of this command is: `{BOT_PREFIX}profile `") @@ -320,8 +362,45 @@ async def get_rank(ctx): args = ctx.message.content.split(" ") if len(args) < 2: - await bot.send_message(ctx.message.channel, - f"Not enough arguments! The proper form of this command is: `{BOT_PREFIX}ranks `") + PastUsers = open("UserIDs.txt", "r+") + NotLinked = True + for line in PastUsers: + check = line + check = check.strip() + check = check.split(":") + ##print(check) + if check[0] == ctx.message.author.id: + NotLinked = False + break + else: + NotLinked = True + if NotLinked: + await bot.send_message(ctx.message.channel, + f"Not enough arguments! The proper form of this command is: `{BOT_PREFIX}profile `") + return + else: + # fetches the profile for the ID. if user can not be found, tell the user so. + id = check[1] + avatar_link, avatar_name, platform, past_names = get_player_profile(id) + + # get user's ranks + ranks = get_json("https://calculated.gg/api/player/{}/ranks".format(id)) + + # create embed + stats_embed = discord.Embed( + color=discord.Color.blue() + ) + + stats_embed.set_author(name=avatar_name, url="https://calculated.gg/players/{}/overview".format(id), + icon_url="https://media.discordapp.net/attachments/495315775423381518/499488781536067595/bar_graph-512.png") + stats_embed.set_thumbnail(url=avatar_link) + order = ['duel', 'doubles', 'solo', 'standard', 'hoops', 'rumble', 'dropshot', 'snowday'] + for playlist in order: + stats_embed.add_field(name=playlist.title(), value=ranks[playlist]['name'] + " - " + str(ranks[playlist]['rating'])) + + # send embed + await bot.send_message(ctx.message.channel, embed=stats_embed) + return elif len(args) > 2: await bot.send_message(ctx.message.channel, @@ -641,6 +720,49 @@ async def status_replay(ctx): await bot.send_message(ctx.message.channel, message) return +#Command for linking accounts so that commands are quicker to run (And to stop me from looking stupid when I forget to put my account on the end of commands) +@bot.command(name="link", aliases=["l"], pass_context=True) +async def test(ctx): + #Gets id that is being linked to + args = ctx.message.content.split(" ") + UserID = args[1] + UserID = resolve_custom_url(args[1]) + #Opens text document with linked names + PastUsers = open("UserIDs.txt", "r+") + NotLinked = True + #Gets names from text document + for line in PastUsers: + check = line + check = check.strip() + check = check.split(":") + ##print(check) + #Checks to see if an account is already linked to discord account + if check[0] == ctx.message.author.id: + await bot.send_message(ctx.message.channel, "Your account is already linked!") + NotLinked = False + break + else: + NotLinked = True + if NotLinked: + #If the user isn't linked, tries to link with the account name / id given + try: + sender = ctx.message.author.id + save_info = sender + ":" + UserID + "\n" + ##print(save_info) + #Saves linked account info to text document then closes it and tells them it is linked + if UserID == "User not found": + await bot.send_message(ctx.message.channel, "User could not be found, please try again.") + return + else: + PastUsers.write(save_info) + PastUsers.close() + await bot.send_message(ctx.message.channel, "Account Successfully linked!") + + except: + await bot.send_message(ctx.message.channel, "User could not be found, please try again.") + return + ##print("Working") + # when bot user is ready, prints "READY", and set presence @bot.event diff --git a/config_example.py b/config_example.py index 2a75881..e81d24d 100644 --- a/config_example.py +++ b/config_example.py @@ -1,2 +1,2 @@ -TOKEN = "" +TOKEN = "NDk4MTc3NDk3NTY1ODg4NTI0.XNRfQA.a9ZCQEbZNLGka1yGcW1675yffQc" BOT_PREFIX = "!" \ No newline at end of file From 12d426279cdb21e5630a7557d5b5b304929cd757 Mon Sep 17 00:00:00 2001 From: ThatGuyDed <44003663+ThatGuyDed@users.noreply.github.com> Date: Thu, 9 May 2019 19:20:42 +0100 Subject: [PATCH 2/3] Removed my bot token Didn't think leaving my bot token would be a good idea :P --- config_example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config_example.py b/config_example.py index e81d24d..d7e1f52 100644 --- a/config_example.py +++ b/config_example.py @@ -1,2 +1,2 @@ -TOKEN = "NDk4MTc3NDk3NTY1ODg4NTI0.XNRfQA.a9ZCQEbZNLGka1yGcW1675yffQc" -BOT_PREFIX = "!" \ No newline at end of file +TOKEN = "" +BOT_PREFIX = "!" From 5f603dcbb03c9ad072c0a73d98ea90fe8ab96689 Mon Sep 17 00:00:00 2001 From: ThatGuyDed <44003663+ThatGuyDed@users.noreply.github.com> Date: Thu, 9 May 2019 19:37:04 +0100 Subject: [PATCH 3/3] Update calculated_bot.py Co-Authored-By: Matthew Mage --- calculated_bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/calculated_bot.py b/calculated_bot.py index e49823b..d9b7736 100644 --- a/calculated_bot.py +++ b/calculated_bot.py @@ -367,7 +367,7 @@ async def get_rank(ctx): for line in PastUsers: check = line check = check.strip() - check = check.split(":") + check = line.strip().split(":") ##print(check) if check[0] == ctx.message.author.id: NotLinked = False