add asyncio lock to webhooks to remove possibility duplicate votes
@@ -23,6 +23,8 @@ class Webhooks(commands.Cog):
self.webhooks_task = bot.loop.create_task(self.webhooks_setup())
self.stats_task = bot.loop.create_task(self.update_stats())
+ self.lock = asyncio.Lock()
+
def cog_unload(self):
self.bot.loop.create_task(self.server_runner.cleanup())
self.bot.loop.create_task(self.webhook_server.close())
@@ -101,6 +103,15 @@ class Webhooks(commands.Cog):
uid = int(data.user)
+ async with self.lock:
+ db_user = await self.db.fetch_user(uid)
+
+ streak_time = db_user["streak_time"]
+ vote_streak = db_user["vote_streak"]
+
+ if arrow.get(streak_time) > arrow.utcnow().shift(hours=-12):
+ return
+
self.bot.logger.info(f"\u001b[32;1m{uid} voted on top.gg\u001b[0m")
self.d.votes_topgg += 1
@@ -111,11 +122,6 @@ class Webhooks(commands.Cog):
amount *= len(self.d.mining.pickaxes) - self.d.mining.pickaxes.index(await self.db.fetch_pickaxe(uid))
- db_user = await self.db.fetch_user(uid)
-
- streak_time = db_user["streak_time"]
- vote_streak = db_user["vote_streak"]
-
if vote_streak is None or vote_streak == 0:
vote_streak = 0
@@ -124,9 +130,6 @@ class Webhooks(commands.Cog):
if streak_time is None: # time
streak_time = 0
- if arrow.get(streak_time) > arrow.utcnow().shift(hours=-12):
- return
-
if arrow.utcnow().shift(days=-1, hours=-12) > arrow.get(streak_time): # vote expired
vote_streak = 1
