我正在尝试设置运行子例程的时间表。我正在尝试使用子例程示例在计划被触发时将消息发送到不和谐频道。起初,我尝试尝试仅发送消息但出现错误。然后,我尝试研究如何解决此问题,并尝试了使用asyncio的不同方法,但所有方法均无效。
如果任何人都可以向我提供有关如何执行此操作的任何指示,那么将不胜感激。
import discord
import asyncio
import time
import schedule # pip install schedule
client = discord.Client()
@client.event
async def on_ready():
print("Connected!")
async def example(message):
await client.get_channel(CHANNEL ID).send(message)
client.run(SECRET KEY)
def scheduledEvent():
loop = asyncio.get_event_loop()
loop.run_until_complete(example("Test Message"))
loop.close()
schedule.every().minute.do(scheduledEvent)
while True:
schedule.run_pending()
time.sleep(1)
答案 0 :(得分:0)
您不能在与异步事件循环相同的线程中运行阻塞的schedule
代码(在僵尸程序断开连接之前,您当前的代码甚至不会尝试安排任务)。相反,您应该使用内置的tasks
扩展名,该扩展名允许您安排任务。
import discord
from discord.ext import tasks, commands
CHANNEL_ID = 1234
TOKEN = 'abc'
client = discord.Client()
@client.event
async def on_ready():
print("Connected!")
@tasks.loop(minutes=1)
async def example():
await client.get_channel(CHANNEL_ID).send("Test Message")
@example.before_loop
async def before_example():
await client.wait_until_ready()
example.start()
clinet.run(TOKEN)