现在我只是使用 datetime 并且每分钟循环检查一次是否在分钟位置有 0、15、30 或 45。一段时间后,机器人会失去时间并在指定时间之后发送消息。有没有办法让我使用unix/修复不同步?或者 unix 会修复不同步吗?
import os
import discord
import random
import datetime
import asyncio
from discord.ext import commands, tasks
ecchi = []
time = datetime.datetime.now
class Hentai(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
if time().second != 0:
waittime = 62-time().second
await asyncio.sleep(waittime)
if self.time_check.current_loop == 0:
self.time_check.start()
else:
if self.time_check.current_loop == 0:
self.time_check.start()
# Fetches random image from "hentai" folder:
for filename in os.listdir('./assets/hentai'):
path = os.path.join('./assets/hentai', filename)
if os.path.isfile(path):
if os.path.getsize(path) > 8000000:
continue
if filename.endswith('.jpg'):
ecchi.append(filename)
elif filename.endswith('.png'):
ecchi.append(filename)
@commands.command(brief = "Automatically sends a random ecchi/hentai image every 15 minutes, or manually send one")
async def hentai(self, ctx):
random_image = random.choice(ecchi)
file = discord.File(f"./assets/hentai/{random_image}")
await ctx.send(file = file)
# Automatic 15 minute loop
@tasks.loop(minutes=15)
async def send_hentai(self):
channels = (12345678901234567890, 12345678901234567890)
for channel_id in channels:
channel = self.bot.get_channel(channel_id)
random_image = random.choice(ecchi)
file = discord.File(f"./assets/hentai/{random_image}")
await channel.send(file = file)
@tasks.loop(minutes=1)
async def time_check(self):
if time().minute == 0:
if self.send_hentai.current_loop == 0:
self.send_hentai.start()
self.time_check.stop()
if time().minute == 15:
if self.send_hentai.current_loop == 0:
self.send_hentai.start()
self.time_check.stop()
if time().minute == 30:
if self.send_hentai.current_loop == 0:
self.send_hentai.start()
self.time_check.stop()
if time().minute == 45:
if self.send_hentai.current_loop == 0:
self.send_hentai.start()
self.time_check.stop()
def setup(bot):
bot.add_cog(Hentai(bot))
答案 0 :(得分:0)
import os
import discord
import random
import datetime
import asyncio
from discord.ext import commands, tasks
ecchi = []
def UtcNow():
now = datetime.datetime.utcnow()
return (now - datetime.datetime(1970, 1, 1)).total_seconds()
class Hentai(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
time = UtcNow()
if time != 0:
waittime = 60-time
await asyncio.sleep(waittime)
if self.time_check.current_loop == 0:
self.time_check.start()
else:
if self.time_check.current_loop == 0:
self.time_check.start()
# Fetches random image from "hentai" folder:
for filename in os.listdir('./assets/hentai'):
path = os.path.join('./assets/hentai', filename)
if os.path.isfile(path):
if os.path.getsize(path) > 8000000:
continue
if filename.endswith('.jpg'):
ecchi.append(filename)
elif filename.endswith('.png'):
ecchi.append(filename)
@commands.command(brief = "Automatically sends a random ecchi/hentai image every 15 minutes, or manually send one (Possibly against TOS)")
async def hentai(self, ctx):
random_image = random.choice(ecchi)
file = discord.File(f"./assets/hentai/{random_image}")
await ctx.send(file = file)
# Automatic 15 minute loop
currentloop = 0
@tasks.loop(seconds=5)
async def time_check(self):
if self.currentloop > 0:
self.currentloop -= 1
time = UtcNow()
if ((int(time/60)%15) == 0 and self.currentloop == 0):
self.currentloop = 12
channels = (751890144977748068, 854619663475277824)
for channel_id in channels:
channel = self.bot.get_channel(channel_id)
random_image = random.choice(ecchi)
file = discord.File(f"./assets/hentai/{random_image}")
await channel.send(file = file)
def setup(bot):
bot.add_cog(Hentai(bot))