discord.py:在启动时运行命令

时间:2020-09-13 11:47:50

标签: python discord discord.py discord.py-rewrite

我有一个命令,它检查一些预定的事件,然后更新一些频道。 我希望此命令在启动时运行,但是如果不访问上下文对象,就无法调用该命令。

ctx.invoke(命令)和bot.get_cog(“ Command”)。command(ctx)都需要一些上下文对象。 创建我自己的上下文似乎是不可能的,因为我无权访问Message对象。

这似乎应该是非常基本的行为,但我无法弄清楚。

编辑: 我需要能够在启动时调用此“某物”命令,它使用上下文对象来获取行会的所有通道,因此我将需要创建/获取上下文对象或以某种方式找到当前行会的所有通道我以其他方式。

@commands.command(name=something)
async def something(ctx):
  channel = discord.utils.get(ctx.guild.channels, name="some_name")
  await channel.send("something")

1 个答案:

答案 0 :(得分:1)

创建一个可从命令和代码其他部分调用的单独协程。所以代替

@bot.command()
async def my_command(ctx):
    ...

@bot.event
async def on_ready():
    await ctx.invoke(my_command) # doesn't work

你可以做

async def do_stuff():
    ...

@bot.command()
async def my_command(ctx):
    await do_stuff()

@bot.event
async def on_ready():
    await do_stuff()