为什么我的机器人不在不和谐 py 中发送消息

时间:2021-02-21 01:40:53

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

我的代码基本上做了当前时区的条件语句。如果该时区条件中的时间为真,则它会发送一条消息锁定角色。但是,当我将其放入 discord 的 py 函数中时,我的代码没有发送消息

import os
import discord
from discord.ext import commands 
import datetime
import pytz


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


f = 19
m = 0 
@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')

@client.event
async def time_check():
    await client.wait_until_ready()
    while not client.is_closed:
        cst = datetime.datetime.now(tz=pytz.timezone('US/Central')).time()
        if cst.hour == 19 and cst.minute == 35:
            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

client.loop.create_task(time_check())

          
client.run('TOKEN') 

1 个答案:

答案 0 :(得分:1)

因为“time_check”不是有效事件。

您可能希望将其作为循环运行。

就这样做。

from discord.ext import tasks
 # then on your time_check function, change @client.event for:

@tasks.loop(minutes=1)
async def time_check():
    # the rest of your function

#before client.run()
time_check.start()

希望有效