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

时间:2021-03-03 21:57:02

标签: python discord.py

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(所以这不是问题)。我认为问题在于循环,因为那是我不确定是否正确的部分。

目标:机器人每 24 小时发送一条包含事实的消息(出于测试目的,任务设置为 10 秒)

2 个答案:

答案 0 :(得分:2)

首先,您的 @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

祝您有美好的一天!

答案 1 :(得分:-1)

如果您打算让机器人在本地运行(或将其托管在某个地方),那么您应该使用 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()