python-3.x - 齿轮 discord.py 中的 @bot.event

标签 python-3.x discord discord.py

我想知道是否可以使用@bot.event
在 discord.py 的齿轮中。我试过做

@self.bot.event
async def on_member_join(self, ctx, member):
    channel = discord.utils.get(member.guild.channels, name='general')
    await channel.send("hello")

在我的齿轮课上,但我得到了错误
NameError: name 'self' is not defined

即使我在 __init __ 中定义了 self.bot。

在 cogs 中执行 bot.event 是否有不同的方式,或者只是不可能?

最佳答案

要从 new-style cog 注册事件,您必须使用 commands.Cog.listener 装饰器。下面是转换为新样式的mental 示例:

from discord.ext import commands

class Events(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self):
        print('Ready!')
        print('Logged in as ---->', self.bot.user)
        print('ID:', self.bot.user.id)

    @commands.Cog.listener()
    async def on_message(self, message):
        print(message)

def setup(bot):
    bot.add_cog(Events(bot))

关于python-3.x - 齿轮 discord.py 中的 @bot.event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48038953/

相关文章:

python-3.x - 使用 pandas apply 不满足条件时跳过一行

python - Discord.py:获取带有 id 的用户对象

python - 模块未找到错误 : No module named 'cogs'

python - Django预保存信号

python - pandas 数据框到字典列表

python - 修改html文件中所有本地链接

python - 斜杠命令不适用于带有 discord bot 的 openai

javascript - Node.js 异步/Promise 与 JIMP 的解释?

Discord.py新超时命令错误: "AttributeError: ' User' object has no attribute 'timeout_for' "

python - 如果 channel 是私有(private)的,我怎么能得到 "check"呢?