Created timer class

This commit is contained in:
2020-11-07 23:49:16 +01:00
parent 909f6a332b
commit 73dfe3abf3

View File

@@ -5,6 +5,7 @@ import string
import socket
import asyncio
import threading
from functions.timer import Timer
from data import constants
from data.DatabaseConnection import *
@@ -97,13 +98,13 @@ class PlayerLink(commands.Cog):
embed.colour = discord.Colour.orange()
return embed
async def update_timer(timer, message):
async def send_timer_messages(timer, message):
while (not timer.ended):
await asyncio.sleep(1)
timer.update()
# 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:
@@ -111,14 +112,15 @@ class PlayerLink(commands.Cog):
uuid = get_player_uuid(arg)
send_chat(ctx.author.name, arg, code)
#message_send_time =
timer = Timer()
timer = Timer(2, 0)
timer.start()
message = await ctx.send(embed=create_embed(ctx.author, arg, timer))
# Start timer in background
task = asyncio.create_task(update_timer(timer, message))
task = asyncio.create_task(send_timer_messages(timer, message))
# Wait for the code response
msg = await self.client.wait_for('message', check=check)
msg = await self.client.wait_for('message', check=check, timeout=120)
if msg.content == code:
@@ -126,7 +128,7 @@ class PlayerLink(commands.Cog):
try:
dbLinker.linkPlayer(uuid, authorid)
await ctx.author.add_roles(self.get_linked_role())
timer.ended = True
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))
@@ -134,14 +136,13 @@ class PlayerLink(commands.Cog):
dbLinker.closeconnections()
else:
# this stops the timer task
task.cancel()
timer.ended = True
timer.stop()
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:
await ctx.send("Something went wrong")
@commands.command(name="Unlink")
async def unlink(self, ctx, arg):
@@ -161,21 +162,5 @@ class PlayerLink(commands.Cog):
dbLinker.closeconnections()
class Timer():
def __init__(self):
self.minutes = 2
self.seconds = 0
self.ended = False
def update(self):
if (self.minutes > 0 and self.seconds == 0):
self.minutes -= 1
self.seconds = 59
elif (self.seconds > 0):
self.seconds -= 1
if (self.minutes == 0 and self.seconds == 0):
self.ended = True
def setup(client):
client.add_cog(PlayerLink(client))