【问题标题】:task.loop in function is not showing any errors but also not working函数中的 task.loop 没有显示任何错误,但也无法正常工作
【发布时间】:2021-05-23 18:04:27
【问题描述】:

我正在尝试创建一个函数,该函数需要时区的时间并使用条件语句,如果时间达到正确的指定条件,那么它会发送一条消息 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') 

【问题讨论】:

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


    【解决方案1】:

    我对 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') 
    

    【讨论】:

    • 刚试过这个方法,同样的问题。它什么也没做
    猜你喜欢
    • 2013-05-21
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多