gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:37:23 27 4
gpt4 key购买 nike

我想知道是否可以使用@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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com