我希望不和谐的python机器人每天在频道中发送特定消息两次。首先是欧洲/柏林时间的12点,然后是欧洲时间的18点(或仅从服务器时间开始)。
我该怎么做?我尝试了很多事情,但找不到解决方法。
答案 0 :(得分:0)
答案 1 :(得分:0)
@Just for fun
答案的略微修改版本。我在从 scheduler
和(我调整了 cron 条目)调用的 task()
函数下携带了 discord.loop.create_task()
。
#!/usr/bin/env python3
import logging
import os
from pathlib import Path
import discord
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from broker._utils.tools import get_dt_time
from dotenv import load_dotenv
# In case to disable logging
logging.getLogger("apscheduler.executors.default").propagate = False
class Discord_Bot:
def __init__(self):
dotenv_path = Path(".env_secret")
load_dotenv(dotenv_path=dotenv_path)
self.client = discord.Client()
self.TOKEN = os.getenv("TOKEN")
self.channel_id = int(os.getenv("CHANNEL_ALPY"))
try:
self.client.loop.create_task(self.task())
self.client.loop.run_until_complete(self.client.start(self.TOKEN))
except SystemExit:
pass
# handle_exit()
except KeyboardInterrupt:
# handle_exit()
self.client.loop.close()
print("Program ended.")
async def task(self):
scheduler = AsyncIOScheduler()
scheduler.add_job(self.send_message, "cron", hour="12")
scheduler.add_job(self.send_message, "cron", hour="18")
# For test purposes
scheduler.add_job(self.send_message, "cron", minute="*")
scheduler.start()
async def send_message(self, msg=""):
await self.client.wait_until_ready()
channel = self.client.get_channel(self.channel_id)
if not msg:
msg = f"Tick! The time is: {get_dt_time().strftime('%Y-%m-%d %H:%M:%S')}"
await channel.send(msg)
_discord = Discord_Bot()
示例输出:
[2021-08-01 14:52:40 base.py:445 - add_job()] Adding job tentatively -- it will be properly scheduled when the scheduler starts
[2021-08-01 14:52:40 base.py:445 - add_job()] Adding job tentatively -- it will be properly scheduled when the scheduler starts
[2021-08-01 14:52:40 base.py:445 - add_job()] Adding job tentatively -- it will be properly scheduled when the scheduler starts
[2021-08-01 14:52:40 base.py:886 - _real_add_job()] Added job "Discord_Bot.send_message" to job store "default"
[2021-08-01 14:52:40 base.py:886 - _real_add_job()] Added job "Discord_Bot.send_message" to job store "default"
[2021-08-01 14:52:40 base.py:886 - _real_add_job()] Added job "Discord_Bot.send_message" to job store "default"
[2021-08-01 14:52:40 base.py:171 - start()] Scheduler started
[2021-08-01 14:52:40 client.py:510 - login()] logging in using static token
[2021-08-01 14:52:42 gateway.py:403 - identify()] Shard ID None has sent the IDENTIFY payload.
[2021-08-01 14:52:42 gateway.py:494 - received_message()] Shard ID None has connected to Gateway: ["gateway-prd-main-jqb3",{"micros":37874,"calls":["discord-sessions-green-prd-2-83",{"micros":36893,"calls":["start_session",{"micros":33555,"calls":["api-prd-main-lx4l",{"micros":30479,"calls":["get_user",{"micros":2553},"add_authorized_ip",{"micros":2438},"get_guilds",{"micros":5864},"coros_wait",{"micros":1}]}]},"guilds_connect",{"micros":1,"calls":[]},"presence_connect",{"micros":2743,"calls":[]}]}]}] (Session ID: ff6cc313c16950abadccfab95a02d3a5).
[2021-08-01 14:53:00 base_py3.py:28 - run_coroutine_job()] Running job "Discord_Bot.send_message (trigger: cron[minute='*'], next run at: 2021-08-01 14:54:00 UTC)" (scheduled at 2021-08-01 14:53:00+00:00)
[2021-08-01 14:53:00 base_py3.py:41 - run_coroutine_job()] Job "Discord_Bot.send_message (trigger: cron[minute='*'], next run at: 2021-08-01 14:54:00 UTC)" executed successfully