【问题标题】:how to test event in discord.py如何在 discord.py 中测试事件
【发布时间】:2020-12-11 15:11:57
【问题描述】:

让我们说 on_member_join 事件

@commands.Cog.listener()
async def on_member_join(self, member):
    # On member joins we find a channel called general and if it exists,
    # send an embed welcoming them to our guild
    channel = discord.utils.get(member.guild.text_channels, name="welcome")
    if channel:
        embed = discord.Embed(
            description="Welcome to our guild!",
            color=random.choice(self.bot.color_list),
        )
        embed.set_thumbnail(url=member.avatar_url)
        embed.set_author(name=member.name, icon_url=member.avatar_url)
        embed.set_footer(text=member.guild, icon_url=member.guild.icon_url)
        embed.timestamp = datetime.datetime.utcnow()

        await channel.send(embed=embed)

这是我的事件(我知道它有效)我如何以任何方式测试它以执行命令以发出事件(而不是通过添加和删除某人手动)?

[类似于 .emit on_member_join @user 其中 arg1 是事件,如果需要 arg2 是提及或频道 ID]

有什么命令吗?

类似于 Discord.js: Manually triggering events 但在 discord.py 中而不是在 JavaScript 中

【问题讨论】:

  • 你能进一步解释你想要达到的目标吗?这没有多大意义。
  • 好吧(Discord.js:手动触发事件)[stackoverflow.com/questions/57478980/… discord.py 中是否有选项可以做同样的事情?

标签: events event-handling discord discord.py


【解决方案1】:

目前 discord.py api 中没有官方支持执行类似操作,但您始终可以在 cog 中执行如下命令:

@commands.command()
async def test_join(self, ctx, member: discord.Member):
   await self.on_member_join(member)

从技术上讲,有一种方法可能没有记录在案......

here 中的第 131 行。我不会为此提供额外支持,因为它没有记录在案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 2014-09-30
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多