diff --git a/cogs/playerlink.py b/cogs/playerlink.py index f69983b..9d29839 100755 --- a/cogs/playerlink.py +++ b/cogs/playerlink.py @@ -63,111 +63,113 @@ class PlayerLink(commands.Cog): channelid = ctx.channel.id authorid = ctx.author.id - dbLinker = PlayerDBLinker() - if dbLinker.discordidused(authorid): - await ctx.send(f"{ctx.author.mention}, your account is already linked!") - dbLinker.close() - return + if channelid == constants.DiscordLinkerID: + dbLinker = PlayerDBLinker() + if dbLinker.discordidused(authorid): + await ctx.send(f"{ctx.author.mention}, your account is already linked!") + dbLinker.close() + return + + code = get_random_string(8) - code = get_random_string(8) + def check(message: discord.Message): + return message.channel.id == channelid and message.author.id == authorid + + # creates the embed for the link message + def create_embed(discord_author, minecraftname, timer, error=None, success=False): + embed = discord.Embed(title="WorldCraft Linker") + embed.add_field(name="How to:", value= "A code has been sent to your minecraft chat in the WorldCraft server.\nSend the code in this channel.") + embed.add_field(name="Minecraft Name:", value=f"{minecraftname}", inline=False) + embed.add_field(name="Discord Name:", value=f"{discord_author.mention}", inline=False) - def check(message: discord.Message): - return message.channel.id == channelid and message.author.id == authorid + # use dictionary and set color string before the message: example => r(ed):message - # creates the embed for the link message - def create_embed(discord_author, minecraftname, timer, error=None, success=False): - embed = discord.Embed(title="WorldCraft Linker") - embed.add_field(name="How to:", value= "A code has been sent to your minecraft chat in the WorldCraft server.\nSend the code in this channel.") - embed.add_field(name="Minecraft Name:", value=f"{minecraftname}", inline=False) - embed.add_field(name="Discord Name:", value=f"{discord_author.mention}", inline=False) + if isinstance(error, WrongCodeError): + embed.add_field(name="Error", value=f"The code is wrong!", inline=False) + embed.colour = discord.Colour.red() + elif (success == True): + embed.add_field(name="Success", value=f"The link was successfull!", inline=False) + embed.colour = discord.Colour.green() + elif (timer.ended): + embed.add_field(name="Timer", value=f"The code is expired!", inline=False) + embed.colour = discord.Colour.red() + elif (timer.minutes == 2): + embed.add_field(name="Timer", value=f"The code will expire in { str(timer.minutes)} minutes", inline=False) + embed.colour = discord.Colour.orange() + elif (timer.minutes < 2): + embed.add_field(name="Timer", value=f"The code will expire in less than { str(timer.minutes + 1)} {'minutes' if timer.minutes + 1 > 1 else 'minute'}", inline=False) + embed.colour = discord.Colour.orange() + else: + embed.add_field(name="Timer", value=f"The code will expire in {str(timer.minutes).zfill(2)}:{str(timer.seconds).zfill(2)}", inline=False) + embed.colour = discord.Colour.orange() + return embed - # use dictionary and set color string before the message: example => r(ed):message - - if isinstance(error, WrongCodeError): - embed.add_field(name="Error", value=f"The code is wrong!", inline=False) - embed.colour = discord.Colour.red() - elif (success == True): - embed.add_field(name="Success", value=f"The link was successfull!", inline=False) - embed.colour = discord.Colour.green() - elif (timer.ended): - embed.add_field(name="Timer", value=f"The code is expired!", inline=False) - embed.colour = discord.Colour.red() - elif (timer.minutes == 2): - embed.add_field(name="Timer", value=f"The code will expire in { str(timer.minutes)} minutes", inline=False) - embed.colour = discord.Colour.orange() - elif (timer.minutes < 2): - embed.add_field(name="Timer", value=f"The code will expire in less than { str(timer.minutes + 1)} {'minutes' if timer.minutes + 1 > 1 else 'minute'}", inline=False) - embed.colour = discord.Colour.orange() - else: - embed.add_field(name="Timer", value=f"The code will expire in {str(timer.minutes).zfill(2)}:{str(timer.seconds).zfill(2)}", inline=False) - embed.colour = discord.Colour.orange() - return embed + async def send_timer_messages(timer, message): + while (not timer.ended): + # maybe create a task from this because this takes some time -> timer not accurate + if ((timer.minutes < 2 and timer.seconds == 59) or (timer.minutes == 0 and timer.seconds == 0)): + asyncio.create_task(message.edit(embed=create_embed(ctx.author, arg, timer))) + await asyncio.sleep(1) - async def send_timer_messages(timer, message): - while (not timer.ended): - # maybe create a task from this because this takes some time -> timer not accurate - if ((timer.minutes < 2 and timer.seconds == 59) or (timer.minutes == 0 and timer.seconds == 0)): - asyncio.create_task(message.edit(embed=create_embed(ctx.author, arg, timer))) - await asyncio.sleep(1) + try: + uuid = get_player_uuid(arg) + send_chat(ctx.author.name, arg, code) + #message_send_time = + timer = Timer(2, 0) + timer.start() - try: + message = await ctx.send(embed=create_embed(ctx.author, arg, timer)) - uuid = get_player_uuid(arg) - send_chat(ctx.author.name, arg, code) - #message_send_time = - timer = Timer(2, 0) - timer.start() + task = asyncio.create_task(send_timer_messages(timer, message)) - message = await ctx.send(embed=create_embed(ctx.author, arg, timer)) + # Wait for the code response + msg = await self.client.wait_for('message', check=check, timeout=120) - task = asyncio.create_task(send_timer_messages(timer, message)) + if msg.content == code: - # Wait for the code response - msg = await self.client.wait_for('message', check=check, timeout=120) - - if msg.content == code: - - dbLinker = PlayerDBLinker() - try: - dbLinker.linkPlayer(uuid, authorid) - await ctx.author.add_roles(self.get_linked_role()) + dbLinker = PlayerDBLinker() + try: + dbLinker.linkPlayer(uuid, authorid) + await ctx.author.add_roles(self.get_linked_role()) + timer.stop() + await message.edit(embed=create_embed(ctx.author, arg, timer, success=True)) + except SQLInsertError: + await message.edit(embed=create_embed(ctx.author, arg, timer)) + finally: + dbLinker.close() + else: + # this stops the timer task timer.stop() - await message.edit(embed=create_embed(ctx.author, arg, timer, success=True)) - except SQLInsertError: - await message.edit(embed=create_embed(ctx.author, arg, timer)) - finally: - dbLinker.close() - else: - # this stops the timer task - timer.stop() - await message.edit(embed=create_embed(ctx.author, arg, timer, WrongCodeError())) + await message.edit(embed=create_embed(ctx.author, arg, timer, WrongCodeError())) - except PlayerError: - await ctx.send("Player '" + arg + "' not found") - except: - await ctx.send("Something went wrong") + except PlayerError: + await ctx.send("Player '" + arg + "' not found") + except: + await ctx.send("Something went wrong") @commands.command(name="Unlink") - async def unlink(self, ctx, arg): + async def unlink(self, ctx): authorid = ctx.author.id - dbLinker = PlayerDBLinker() - try: - dbLinker.unlinkPlayer(arg, authorid) - await ctx.author.remove_roles(self.get_linked_role()) - await ctx.send("Unlinked your account") - except UnLinkError: - await ctx.send("The unlink was unsuccessfull") - except WrongMinecraftName: - await ctx.send("Wrong minecraft name!") - except PlayerNotLinked: - await ctx.send("Player not linked!") - finally: - dbLinker.close() + channelid = ctx.channel.id + if channelid == constants.DiscordLinkerID: + dbLinker = PlayerDBLinker() + try: + dbLinker.unlinkPlayer(authorid) + await ctx.author.remove_roles(self.get_linked_role()) + await ctx.send(f"{ctx.author.mention}, unlinked your account") + except UnLinkError: + await ctx.send("The unlink was unsuccessfull") + except WrongMinecraftName: + await ctx.send("Wrong minecraft name!") + except PlayerNotLinked: + await ctx.send("Player not linked!") + finally: + dbLinker.close() def setup(client): diff --git a/cogs/quiz.py b/cogs/quiz.py index 6e7e829..3a11e53 100644 --- a/cogs/quiz.py +++ b/cogs/quiz.py @@ -78,9 +78,10 @@ class QuizQuestions(commands.Cog): class Quiz(commands.Cog): def __init__(self, client): self.client = client - #self.interval = (5, 10) + self.interval = (5, 10) self.interval = (8*60, 30*60) self.auto = False + self.timeout = 60*10 @commands.command(name="quiz") @commands.check(checks.isModPlus) @@ -115,12 +116,18 @@ class Quiz(commands.Cog): embed.add_field(name="Question:", value=f"{question[1]}", inline=False) embed.add_field(name="Reward:", value=f"${question[3]} in game", inline=False) - + embed.add_field(name="End:", value=f"The quiz will end in 10 minutes!", inline=False) await channel.send(embed=embed) + + embed = discord.Embed() + embed.colour = discord.Colour.orange() + + embed.add_field(name="Question:", value=f"{question[1]}", inline=False) + embed.add_field(name="Reward:", value=f"${question[3]} in game", inline=False) embed.add_field(name="Answer:", value=f"{question[2]}", inline=False) try: - message = await self.client.wait_for("message", check=lambda message: message.content.lower() == question[2].lower(), timeout=60*10) + message = await self.client.wait_for("message", check=lambda message: message.content.lower() == question[2].lower(), timeout=self.timeout) self.give_reward(message.author.id, question[3]) playerdblinker = PlayerDBLinker() diff --git a/data/DatabaseConnection.py b/data/DatabaseConnection.py index 8d1c688..5ee8efd 100755 --- a/data/DatabaseConnection.py +++ b/data/DatabaseConnection.py @@ -185,18 +185,11 @@ class PlayerDBLinker: except: raise SQLInsertError() - def unlinkPlayer(self, minecraftname, discordid): + def unlinkPlayer(self, discordid): # get uuid from server database -> check if in linkerdatabase - serverdb_cursor = self.serverdbconn.get_cursor() discorddb_cursor = self.discorddbconn.get_cursor() - sql = "SELECT playerUUID FROM playerprofiles WHERE playerName = '{}' LIMIT 1".format(minecraftname) - serverdb_cursor.execute(sql) - res = serverdb_cursor.fetchone() - if res == None: - raise WrongMinecraftName() - playeruuid = res[0] - if self.checkPlayerLink(playeruuid, discordid): - sql2 = f"DELETE FROM {self.tablename} WHERE minecraftUUID = '{playeruuid}' AND discordid = '{discordid}'" + if self.discordidused(discordid): + sql2 = f"DELETE FROM {self.tablename} WHERE discordid = '{discordid}'" discorddb_cursor.execute(sql2) res2 = self.discorddbconn.get_db().commit() else: diff --git a/data/constants.py b/data/constants.py index ae092ff..8ba0de0 100755 --- a/data/constants.py +++ b/data/constants.py @@ -21,4 +21,5 @@ modPlusRoles = [roleAdmin, roleMod, roleOwner] # Channels ModLogs = 760807882899193867 #QuizChannelID_dev = 774418250665951232 -QuizChannelID = 774418250665951232 +QuizChannelID = 775776550871498752 +DiscordLinkerID = 776944220119760927