Python 如何每x分钟发送一条消息? fotddict={} @客户端事件 _ready()上的异步定义: 全球fotddict 以open(“factoftheday.json”,“r”)作为f: fotddict=json.load(f) @client.command() @commands.has_权限(administrator=True) 异步def fotd(ctx,通道:discord.TextChannel=None): 如果通道为无: embe=discord.Embed(title=“Error”,description=“**请输入所有必需的参数!***\n需要的帮助?**https://dsc.gg/otaysupport**,颜色=0x7289da) 等待ctx.send(嵌入=embe) 其他: #你确定吗 msg=wait ctx.send(嵌入=embed) def checkifnotbotfact(反应,用户): 返回用户!=客户端用户 wait msg.add_reaction('

Python 如何每x分钟发送一条消息? fotddict={} @客户端事件 _ready()上的异步定义: 全球fotddict 以open(“factoftheday.json”,“r”)作为f: fotddict=json.load(f) @client.command() @commands.has_权限(administrator=True) 异步def fotd(ctx,通道:discord.TextChannel=None): 如果通道为无: embe=discord.Embed(title=“Error”,description=“**请输入所有必需的参数!***\n需要的帮助?**https://dsc.gg/otaysupport**,颜色=0x7289da) 等待ctx.send(嵌入=embe) 其他: #你确定吗 msg=wait ctx.send(嵌入=embed) def checkifnotbotfact(反应,用户): 返回用户!=客户端用户 wait msg.add_reaction(',python,discord.py,Python,Discord.py,如果要让bot在本地运行(或在某个地方托管),那么应该使用 首先,在循环执行之前调用@factsend.before\u loop函数,因此必须在其他位置启动循环,而不是在函数中。因此必须将factsend.start()放在该函数之外 更正代码为: @client.event _ready()上的异步定义: 全球fotddict 以open(“factoftheday.json”,“r”)作为f: fotddict=json.load(f) @client.command() @command

如果要让bot在本地运行(或在某个地方托管),那么应该使用


首先,在循环执行之前调用
@factsend.before\u loop
函数,因此必须在其他位置启动循环,而不是在函数中。因此必须将
factsend.start()
放在该函数之外

更正代码为:

@client.event
_ready()上的异步定义:
全球fotddict
以open(“factoftheday.json”,“r”)作为f:
fotddict=json.load(f)
@client.command()
@commands.has_权限(administrator=True)
异步def fotd(ctx,通道:discord.TextChannel=None):
如果通道为无:
embe=discord.Embed(title=“Error”,description=“**请输入所有必需的参数!***\n需要的帮助?**https://dsc.gg/otaysupport**,颜色=0x7289da)
等待ctx.send(嵌入=embe)
其他:
#你确定吗
msg=wait ctx.send(嵌入=embed)
def checkifnotbotfact(反应,用户):
返回用户!=client.user

wait msg.add_reaction('抱歉,但是discord.py有一个集成的系统,可以很好地工作。这很糟糕,真的很糟糕。它阻止了整个代码(甚至名称本身也说明了这一点),您不应该在异步代码中使用阻塞函数,这会破坏它的全部用途。非常感谢,我有一个问题。如果我重新启动bot,计时器将“重置”对吗?我认为是的
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('If you are going to leave the bot running locally (or host it somewhere), then you should use 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()