如何每隔x分钟发送一条消息?


fotddict = {}

@client.event
async def on_ready():
global fotddict
with open("factoftheday.json", "r") as f:
fotddict = json.load(f)

@client.command()
@commands.has_permissions(administrator=True)
async def fotd(ctx, channel : discord.TextChannel=None):

if channel is None:
embe=discord.Embed(title="<:redcross:781952086454960138>Error", description="**Please pass in all required arguments!**nNeed help?** https://dsc.gg/otaysupport**", color=0x7289da)
await ctx.send(embed=embe)

else:
#are you sure embed
msg = await ctx.send(embed=embed)

def checkifnotbotfact(reaction, user):
return user != client.user

await msg.add_reaction('💡')

reaction, user = await client.wait_for("reaction_add", timeout=60.0, check=checkifnotbotfact)

if str(reaction.emoji) == "💡": 

#confirm embed

global fotddict
fotddict[str(ctx.guild.id)] = channel.id

with open("factoftheday.json", "w") as f:
json.dump(fotddict, f)

@tasks.loop(seconds=10)
async def factsend(member):
x = randfacts.getFact()
channel_id = fotddict[str(member.guild.id)]
embed = discord.Embed(title="💡Fact of the day!", description=x, color=0x7289da)
await client.get_channel(channel_id).send(embed=embed)

@factsend.before_loop
async def before():
factsend.start()
await client.wait_until_ready()

问题:这是我的每日事实命令,它在json文件中添加了频道id +公会id(所以这不是问题)。我认为问题出在循环上,因为我不确定那部分是否正确。

目标:Bot每24小时发送一条带有事实的消息(为了测试目的,Task设置为10秒)

首先,你的@factsend.before_loop函数在循环执行之前被调用,所以你必须在其他地方开始循环,而不是在函数中。所以你必须把factsend.start()移到这个函数之外。

更正后的代码将是:

@client.event
async def on_ready():
global fotddict
with open("factoftheday.json", "r") as f:
fotddict = json.load(f)

@client.command()
@commands.has_permissions(administrator=True)
async def fotd(ctx, channel : discord.TextChannel=None):

if channel is None:
embe=discord.Embed(title="<:redcross:781952086454960138>Error", description="**Please pass in all required arguments!**nNeed help?** https://dsc.gg/otaysupport**", color=0x7289da)
await ctx.send(embed=embe)

else:
#are you sure embed
msg = await ctx.send(embed=embed)

def checkifnotbotfact(reaction, user):
return user != client.user

await msg.add_reaction('💡')

reaction, user = await client.wait_for("reaction_add", timeout=60.0, check=checkifnotbotfact)

if str(reaction.emoji) == "💡": 

#confirm embed

global fotddict
fotddict[str(ctx.guild.id)] = channel.id

with open("factoftheday.json", "w") as f:
json.dump(fotddict, f)

@tasks.loop(seconds=10)
async def factsend(member):
x = randfacts.getFact()
channel_id = fotddict[str(member.guild.id)]
embed = discord.Embed(title="💡Fact of the day!", description=x, color=0x7289da)
await client.get_channel(channel_id).send(embed=embed)

@factsend.before_loop
async def before():
await client.wait_until_ready()
factsend.start() #deplaced outside of the function

祝你今天愉快!

如果您打算让bot在本地运行(或将其托管在某个地方),那么您应该使用Advance Python Scheduler

from apscheduler.schedulers.blocking import BlockingScheduler
#Code goes Here
scheduler = BlockingScheduler()
scheduler.add_job(function_name_you_want_to_run(), 'interval', hours=24)
scheduler.start()

相关内容

最新更新