【问题标题】:Emit custom events discord.py发出自定义事件 discord.py
【发布时间】:2021-02-24 20:53:43
【问题描述】:

在 discord.py 中,我什至可以像这样使用监听器:

@bot.event
async def on_event_name(*args, **kwargs):
    # do stuff

我见过一些似乎发出自定义事件的库,默认情况下不包含在 discord.py 中。我想知道这是怎么做到的?我正在寻找这样的东西:

# bot.py:
@bot.event
async def my_event_name(data):
    #do stuff

# somewhere else
await bot.emmit('my_event_name', data)

【问题讨论】:

  • @kr8gz 不,那是为了手动调用命令。我想调用自定义事件
  • 你可以只使用函数。
  • @Cloud 我可以,但我宁愿使用事件

标签: python discord.py


【解决方案1】:

您可以使用Bot.dispatch 制作自己的活动

这是一个例子:

bot.dispatch("my_event", a, b, c)

被听到

a, b, c = await bot.wait_for("my_event")

@bot.event
async def on_my_event(a, b, c):
  # ...

(别忘了在函数的开头写上“on_”)

【讨论】:

  • 非常感谢,这正是我所需要的。
  • 这很好,但我很好奇 - 如果您想动态或按某种计划调用自定义事件怎么办?例如,每天下午 6 点触发自定义事件。或者定期检查数据库中的新信息(可能是“bot_message_queue”表中的条目),然后触发事件?
  • 我想我想问的是有没有某种事件调度器,你可以告诉你自己的事件每隔一段时间触发一次?
  • 看这个帖子,回答一个调度器:stackoverflow.com/questions/64491012/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
  • 2020-09-16
  • 2015-02-09
  • 1970-01-01
相关资源
最近更新 更多