Replaces asyncio coroutines with native async

Replaces @asyncio.coroutine decorator with async
Replaces yield from with await
This commit is contained in:
2022-12-18 02:03:46 +01:00
parent ad2324d53c
commit f8db215b31
4 changed files with 124 additions and 163 deletions
+22 -27
View File
@@ -1,4 +1,3 @@
import asyncio
import discord
from discord.ext import commands
from pprint import pprint
@@ -25,8 +24,7 @@ class Points(commands.Cog):
return True
@commands.command(pass_context=True, hidden=False)
@asyncio.coroutine
def geefpunten(self, ctx, user, points: int):
async def geefpunten(self, ctx, user, points: int):
"""Wees eens lief. Geef iemand wat punten.
Bijvoorbeeld:
!geefpunten @ikbenjp 42
@@ -37,29 +35,29 @@ class Points(commands.Cog):
self.reloadPointsCount()
if points == 0:
yield from ctx.channel.send("0 punten? Gul hoor.")
await ctx.channel.send("0 punten? Gul hoor.")
return
if points < 0:
yield from self.sendErrorDm(ctx, "Daar hebben we de !neempunten functie voor. Voor meer info typ: ```!help neempunten```")
await self.sendErrorDm(ctx, "Daar hebben we de !neempunten functie voor. Voor meer info typ: ```!help neempunten```")
return
if user[0:2] != "<@":
yield from self.sendErrorDm(ctx, "Je moet iemand taggen om punten te geven. :)")
await self.sendErrorDm(ctx, "Je moet iemand taggen om punten te geven. :)")
#TODO ook userId oid toestaan?
return
userObj = ctx.message.mentions[0]
if userObj.id == ctx.author.id:
yield from ctx.channel.send("Nee. Je mag niet jezelf punten geven.")
await ctx.channel.send("Nee. Je mag niet jezelf punten geven.")
time.sleep(0.6)
yield from ctx.channel.send("Gekkie.")
await ctx.channel.send("Gekkie.")
return
if not self.isUserOnList(userObj):
yield from ctx.channel.send("Nee. Die krijgt geen punten.")
await ctx.channel.send("Nee. Die krijgt geen punten.")
return
if self.getUserBalance(ctx.author) < points:
yield from ctx.channel.send("Daar heb jij niet genoeg puntjes voor. :bell:")
await ctx.channel.send("Daar heb jij niet genoeg puntjes voor. :bell:")
return
# Execute the transaction:
@@ -68,7 +66,7 @@ class Points(commands.Cog):
self.savePointsCount()
message = self.getMessageForAwardingPoints(ctx.author.name, userObj.name, points)
yield from ctx.channel.send(message)
await ctx.channel.send(message)
def execute_geefpunten(self, giverId, recieverId, points):
# First, try to give away (at most 10) bankpoints
@@ -86,8 +84,7 @@ class Points(commands.Cog):
@commands.command(pass_context=True, hidden=False)
@asyncio.coroutine
def neempunten(self, ctx, user, points: int):
async def neempunten(self, ctx, user, points: int):
"""Tijd om iemand the shamen. Haal wat punten weg.
Bijvoorbeeld:
!neempunten @mark 5
@@ -98,27 +95,27 @@ class Points(commands.Cog):
self.reloadPointsCount()
if points < 1:
yield from ctx.channel.send("Volgens mij snap je het niet.")
await ctx.channel.send("Volgens mij snap je het niet.")
return
if points > 10:
yield from self.sendErrorDm(ctx, "Je mag niet meer dan 10 punten afnemen.")
await self.sendErrorDm(ctx, "Je mag niet meer dan 10 punten afnemen.")
return
remainingToTakeAway = self.getUsersRemainingPointsToTakeToday(ctx.author.id)
if points > remainingToTakeAway: # The limit per day is 10
yield from self.sendErrorDm(ctx, "Je mag vandaag nog maar " + str(remainingToTakeAway) + " punten wegnemen.")
await self.sendErrorDm(ctx, "Je mag vandaag nog maar " + str(remainingToTakeAway) + " punten wegnemen.")
return
if user[0:2] != "<@":
yield from self.sendErrorDm(ctx, "Je moet iemand taggen. :)")
await self.sendErrorDm(ctx, "Je moet iemand taggen. :)")
return
userObj = ctx.message.mentions[0]
if not self.isUserOnList(userObj):
yield from ctx.channel.send("Nee.")
await ctx.channel.send("Nee.")
return
if self.getUserBalance(userObj) < points:
yield from ctx.channel.send(userObj.name + " mag niet rood staan.")
await ctx.channel.send(userObj.name + " mag niet rood staan.")
return
# Execute the transaction:
@@ -127,7 +124,7 @@ class Points(commands.Cog):
self.savePointsCount()
message = self.getMessageForTakingPoints(userObj.name, ctx.author.name, points)
yield from ctx.channel.send(message)
await ctx.channel.send(message)
def execute_neempunten(self, takerId, victimId, points):
self.increaseUsersPointTakenOnDate(takerId, points)
@@ -135,8 +132,7 @@ class Points(commands.Cog):
@commands.command(pass_context=True, hidden=False)
@asyncio.coroutine
def puntentelling(self, ctx):
async def puntentelling(self, ctx):
"""Hoeveel punten heeft iedereen?
"""
@@ -148,18 +144,17 @@ class Points(commands.Cog):
if user[0] != self.BANK_ID:
message += str(user[1]["points"]).rjust(4) + " " + user[1]["name"] + "\n"
message += str(self.pointsCount[self.BANK_ID]["points"]).rjust(4) + " " + self.pointsCount[self.BANK_ID]["name"] + "```"
yield from ctx.channel.send(message)
await ctx.channel.send(message)
# No public shaming. Send somebody a direct correction message.
@asyncio.coroutine
def sendErrorDm(self, ctx, message):
async def sendErrorDm(self, ctx, message):
try:
yield from ctx.message.delete()
await ctx.message.delete()
except:
print("No permission to delete message")
try:
yield from ctx.message.author.send(message)
await ctx.message.author.send(message)
except:
print("No permission to send DM")