函数中的 task.loop 没有显示任何错误,但也没有工作

时间:2021-02-21 06:25:59

标签: python python-3.x discord discord.py

我正在尝试创建一个函数,它占用时区的时间并使用条件语句,如果时间达到正确的指定条件,则它会发送一条消息 ping 3 个角色。但是函数任务循环不起作用。

它不会显示任何错误,但它也不会在后台执行任何操作。我已经更新了 discord.py 模块,所以这不是问题。

import os
import discord
from discord.ext import commands, tasks
import datetime
import pytz
import asyncio


intents = discord.Intents.default() 
intents.members = True
client = commands.Bot(command_prefix = '?',intents=intents) #sets prefix 
client.remove_command('help')

@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.do_not_disturb, activity= discord.Activity(name="Around ;)", type=discord.ActivityType.watching))
    print('ready')

@tasks.loop(minutes=1)
async def time_event():
    await client.wait_until_ready()
    while not client.is_closed:
        cst = datetime.datetime.now(tz=pytz.timezone('US/Central')).time()
        if cst.hour == 00 and cst.minute == 17:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 19 and cst.minute == 40:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 9 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 13 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
            break
        elif cst.hour == 18 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
            break


time_event.start()
client.run('Token') 

1 个答案:

答案 0 :(得分:0)

我对 asyncio 不太了解。
但我知道一些可能对你有用的东西

import os
import discord
from discord.ext import commands, tasks # you don't need to import tasks
import datetime
import pytz
import asyncio


intents = discord.Intents.default() 
intents.members = True
client = commands.Bot(command_prefix = '?',intents=intents) #sets prefix 
client.remove_command('help')

@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.do_not_disturb, activity= discord.Activity(name="Around ;)", type=discord.ActivityType.watching))
    print('ready')

async def time_event():
    await client.wait_until_ready()
    while not client.is_closed:
        await asyncio.sleep(5) # Pause the loop (not the whole program) for a certain amount of time (in seconds)
        # You must use await asyncio.sleep otherwise the program will not run well (at least one second)
        cst = datetime.datetime.now(tz=pytz.timezone('US/Central')).time()
        if cst.hour == 00 and cst.minute == 17:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 19 and cst.minute == 40:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 9 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
        elif cst.hour == 13 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
            break # This break stops the loop
        elif cst.hour == 18 and cst.minute == 00:
            channel = client.get_channel(760182149839716423)
            await channel.send("<@&811801850771800134>")
            await channel.send("<@&811808231218610266>")
            await channel.send("<@&811802000463101963>")
            break # This break stops the loop


client.loop.create_task(time_event())
client.run('Token')