【发布时间】:2021-05-23 16:05:16
【问题描述】:
我的代码基本上是对当前时区进行条件语句。如果该时区条件中的时间为真,则它会发送一条消息来锁定角色。但是,当我将代码放入 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')
【问题讨论】:
-
a) 如果没有随机硬编码的字符串和数字,这将更容易理解,而且每个 if 语句块都是相同的,所以也许它们应该是函数? b)您是否尝试过添加一些打印语句以查看执行的程度?你知道你正在进入while循环吗?你知道你输入了正确的 if 语句吗?
-
我的意思是,它是 WET 和硬编码的,但这不是问题。问题是他们将函数装饰为一个事件,但他没有调度它。一种解决方案是将事件分派到某处,或者将其作为循环任务运行。
标签: python python-3.x discord discord.py