如何每天在 Discord Hikari Bot 中执行 async def?
How do I execute an async def in a Discord Hikari Bot every day?
我需要有关每天写消息的 Discord Hikari 机器人的帮助。我无法让循环工作。我试着用 apscheduler (AsyncIOScheduler) 来做,但是当我 运行 它什么都不做,当我 运行 异步循环时,它会执行,但它之后的一切都不会 运行,所以机器人没有启动。
import hikari
import lightbulb
import asyncio
import bot
from datetime import datetime
from lightbulb import commands, context
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
daily_plugin = lightbulb.Plugin("Daily")
sched = AsyncIOScheduler()
sched.start()
@sched.scheduled_job(CronTrigger(hour=2, minute=0))
async def msg1():
await bot.bot.rest.create_message(783996883080708096, "Morning Message")
print("Message")
#asyncio.get_event_loop().run_forever()
def load(bot: lightbulb.BotApp) -> None:
bot.add_plugin(daily_plugin)
(这是一个灯泡插件)
这里的问题完全是 asyncio.get_event_loop().run_forever()
调用。这将完全阻止并阻止灯泡 BotApp 启动。
你剩下的代码可以工作,我以 1 分钟的间隔对其进行了测试,如下所示:
# main.py
import os
import hikari
import lightbulb
from dotenv import load_dotenv
load_dotenv()
bot = lightbulb.BotApp(
token=os.environ["TOKEN"],
prefix="$",
intents=hikari.Intents.ALL,
ignore_bots=True,
)
bot.load_extensions("daily")
if __name__ == "__main__":
bot.run()
# daily.py
import lightbulb
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
daily_plugin = lightbulb.Plugin("Daily")
sched = AsyncIOScheduler()
sched.start()
@sched.scheduled_job(CronTrigger(minute="*/1"))
async def msg1() -> None:
await daily_plugin.app.rest.create_message(783996883080708096, "Morning Message")
def load(bot: lightbulb.BotApp) -> None:
bot.add_plugin(daily_plugin)
备选
您也可以考虑使用 hikari lifetime events and a lightbulb Plugin listener decorator to start the scheduler to prevent cluttering your namespace. If you have other places you may need to use the scheduler outside this extension, you could even store it inside the BotApp
's datastore 以便在任何有 BotApp
.
的地方轻松访问
# main.py
import os
import hikari
import lightbulb
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from dotenv import load_dotenv
load_dotenv()
bot = lightbulb.BotApp(
token=os.environ["TOKEN"],
prefix="$",
intents=hikari.Intents.ALL,
ignore_bots=True,
)
@bot.listen(hikari.StartingEvent)
async def on_starting(_: hikari.StartingEvent) -> None:
# This event fires once, while the BotApp is starting.
bot.d.sched = AsyncIOScheduler()
bot.d.sched.start()
bot.load_extensions("daily")
if __name__ == "__main__":
bot.run()
# daily.py
import hikari
import lightbulb
from apscheduler.triggers.cron import CronTrigger
daily_plugin = lightbulb.Plugin("Daily")
async def msg1() -> None:
await daily_plugin.app.rest.create_message(783996883080708096, "Morning Message")
@daily_plugin.listener(hikari.StartedEvent)
async def on_started(_: hikari.StartedEvent) -> None:
# This event fires once, when the BotApp is fully started.
daily_plugin.app.d.sched.add_job(msg1, CronTrigger(minute="*/1"))
def load(bot: lightbulb.BotApp) -> None:
bot.add_plugin(daily_plugin)
我需要有关每天写消息的 Discord Hikari 机器人的帮助。我无法让循环工作。我试着用 apscheduler (AsyncIOScheduler) 来做,但是当我 运行 它什么都不做,当我 运行 异步循环时,它会执行,但它之后的一切都不会 运行,所以机器人没有启动。
import hikari
import lightbulb
import asyncio
import bot
from datetime import datetime
from lightbulb import commands, context
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
daily_plugin = lightbulb.Plugin("Daily")
sched = AsyncIOScheduler()
sched.start()
@sched.scheduled_job(CronTrigger(hour=2, minute=0))
async def msg1():
await bot.bot.rest.create_message(783996883080708096, "Morning Message")
print("Message")
#asyncio.get_event_loop().run_forever()
def load(bot: lightbulb.BotApp) -> None:
bot.add_plugin(daily_plugin)
(这是一个灯泡插件)
这里的问题完全是 asyncio.get_event_loop().run_forever()
调用。这将完全阻止并阻止灯泡 BotApp 启动。
你剩下的代码可以工作,我以 1 分钟的间隔对其进行了测试,如下所示:
# main.py
import os
import hikari
import lightbulb
from dotenv import load_dotenv
load_dotenv()
bot = lightbulb.BotApp(
token=os.environ["TOKEN"],
prefix="$",
intents=hikari.Intents.ALL,
ignore_bots=True,
)
bot.load_extensions("daily")
if __name__ == "__main__":
bot.run()
# daily.py
import lightbulb
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
daily_plugin = lightbulb.Plugin("Daily")
sched = AsyncIOScheduler()
sched.start()
@sched.scheduled_job(CronTrigger(minute="*/1"))
async def msg1() -> None:
await daily_plugin.app.rest.create_message(783996883080708096, "Morning Message")
def load(bot: lightbulb.BotApp) -> None:
bot.add_plugin(daily_plugin)
备选
您也可以考虑使用 hikari lifetime events and a lightbulb Plugin listener decorator to start the scheduler to prevent cluttering your namespace. If you have other places you may need to use the scheduler outside this extension, you could even store it inside the BotApp
's datastore 以便在任何有 BotApp
.
# main.py
import os
import hikari
import lightbulb
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from dotenv import load_dotenv
load_dotenv()
bot = lightbulb.BotApp(
token=os.environ["TOKEN"],
prefix="$",
intents=hikari.Intents.ALL,
ignore_bots=True,
)
@bot.listen(hikari.StartingEvent)
async def on_starting(_: hikari.StartingEvent) -> None:
# This event fires once, while the BotApp is starting.
bot.d.sched = AsyncIOScheduler()
bot.d.sched.start()
bot.load_extensions("daily")
if __name__ == "__main__":
bot.run()
# daily.py
import hikari
import lightbulb
from apscheduler.triggers.cron import CronTrigger
daily_plugin = lightbulb.Plugin("Daily")
async def msg1() -> None:
await daily_plugin.app.rest.create_message(783996883080708096, "Morning Message")
@daily_plugin.listener(hikari.StartedEvent)
async def on_started(_: hikari.StartedEvent) -> None:
# This event fires once, when the BotApp is fully started.
daily_plugin.app.d.sched.add_job(msg1, CronTrigger(minute="*/1"))
def load(bot: lightbulb.BotApp) -> None:
bot.add_plugin(daily_plugin)