Issue
I have simple function that replies with a certain message when a person uses @here
or @everyone
in the server
#Ping related stuff
if message.mention_everyone:
if checker_ping(message.author.id):
await message.channel.send("U used a ping")
and the function checker_ping
async def checker_ping(id):
for i in data:
if id == i:
return True
where data
is nothing but a list of userids
file = open('pings.txt', 'r+')
data = list(re.sub(":.", '', ''.join(file.readlines())).splitlines())
what it does is if a person with the userid specified in the ping.txt
uses the @here
or @everyone
it sends a message as specified in the code
The problem arises when someone uses it
RuntimeWarning: coroutine 'checker_ping' was never awaited
if checker_ping(message.author.id):
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
i tried searching it up and doing various things such as storing the return value in a variable and then running it, awaiting the if statement and much more and all of them resulted in the same error
Solution
The root of your problem is that you tried to defined a synchronous function as an asynchronous one.
Code:
with open("stacktest/pings.txt","r+") as f:
data = list(re.sub(":.", '', ''.join(f.readlines())).splitlines())
print(data)
def checker_ping(userid):
return userid in data
@bot.event
async def on_message(message:discord.Message):
if message.mention_everyone:
if checker_ping(str(message.author.id)):
await message.channel.send("U used a ping")
Smaller mistakes:
- You tried comparing a number to a string. In your function
message.author.id
returns an integer and you compared that to a list element which is a string. - This is not really a mistake but an optimization. I made the function return if userid is in the data with
return userid in data
. This creates a boolean which will either return True or False. - You opened a file with simply
file = open()
but never closed it. Usewith open() as something
instead as i provided. (This is mostly for optimization too)
Answered By - Rexy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.