运行时警告:从未等待过协程 |穿线

时间:2021-06-13 09:35:56

标签: python async-await python-multithreading

我是初学者并尝试编写 Discordbot。 Bot 应该在线程中设置一个计时器(以便您可以启动多个计时器),如果计时器结束,则他会发送消息表示计时器结束。 我收到此错误:

Warning (from warnings module):
  File "C:\Program Files\Python37\lib\threading.py", line 870
    self._target(*self._args, **self._kwargs)
RuntimeWarning: coroutine 'MyClient.on_message.<locals>.timer' was never awaited

这是我的代码错误的一部分:

import discord
import time
from discord.utils import get
import threading

class MyClient(discord.Client):
    async def on_ready(self):
        print("Login Successful")#Login

    async def on_message(self, message):
        async def timer(timervalue, attr3):
            print("Timer started")
            time.sleep(timervalue*60**2)
            if attr3[:1] == "!":
                for i in range(25):
                    await message.channel.send("@everyone " + attr3[1:], delete_after=7.5) #send the message/third attribute more times
            else:
                await message.channel.send(attr3)#send the message/third attribute
                
        if message.author == client.user:
            return

        if message.content.startswith("$c"):
            userid = str(message.author) #username with Hashtag
            username = userid[:len(userid)-5] #username without Hashtag
            mention = message.author.mention
            
            attribute = message.content.split(' ')[1]
            try:
                attr2 = message.content.split(' ')[2]
            except IndexError:
                pass
            
            try:
                attr3 = message.content.split(' ')[3]
            except IndexError:
                pass

            elif attribute.lower() == "timer": #set a timer
                if attr2.lower()[:1] == "h": #input in hours
                    try:
                        timervalue = float(attr2[1:])
                        await message.channel.send(mention + " " + "der Timer klingelt in " + str(timervalue) + " Stunden.")
                        try:
                            threading.Thread(target=timer, args=(timervalue, attr3)).start()
                        except UnboundLocalError:
                            threading.Thread(target=timer, args=(timervalue, "@everyone timer is over")).start()
                        #go into thread
                            
                    except ValueError:
                        await message.channel.send(mention + " -" + attr2[1:] + "- isnt a number.", delete_after=7.5)

                else: #if input is incorrect
                     await message.channel.send(mention + " -" + attr2[:1] + "- isnt -h- or -m-.", delete_after=7.5)

            else: #if input is incorrect
                await message.channel.send(mention + " -" + attribute + "- isnt an attribute.", delete_after=7.5)
                await message.delete()

client = MyClient()

感谢您的帮助!

0 个答案:

没有答案