@bot.event 嵌齿轮 discord.py
@bot.event in a cog 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")
在我的 cog class 中,但出现错误
NameError: name 'self' is not defined
尽管我在 __init 中定义了 self.bot __.
在 cogs 中是否有不同的 bot.event 方法,或者根本不可能?
所以,我想出了一个方法来让它发挥作用。我所做的是创建了一个新函数,并将设置函数中的 bot 变量传递给它。然后我创建了一个新函数的后台任务,并在其中添加了@bot.event 运行。密码是
def xyz(bot):
@bot.event
async def on_member_join(member):
print("ABC")
def setup(bot):
bot.loop.create_task(xyz(bot))
bot.add_cog(cogClass(bot))
以防有人不明白我的解释
编辑:
这是一种糟糕的做事方式。改用心理方式
我不推荐 qspitzers 的回答,因为这不是将事件移动到 cog 的明智方法,而且答案可能会引发一些 unknown/unexpected 异常。
而是做这样的事情。
from discord.ext import commands
class Events:
def __init__(self, bot):
self.bot = bot
async def on_ready(self):
print('Ready!')
print('Logged in as ---->', self.bot.user)
print('ID:', self.bot.user.id)
async def on_message(self, message):
print(message)
def setup(bot):
bot.add_cog(Events(bot))
请记住,要将事件放置在齿轮内,您不需要装饰器。此外,cog 内的事件不会覆盖默认事件,这些事件将存储在 bot.extra_events
.
中
从 new-style cog, you must use the 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))
我想知道是否可以使用@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")
在我的 cog class 中,但出现错误
NameError: name 'self' is not defined
尽管我在 __init 中定义了 self.bot __.
在 cogs 中是否有不同的 bot.event 方法,或者根本不可能?
所以,我想出了一个方法来让它发挥作用。我所做的是创建了一个新函数,并将设置函数中的 bot 变量传递给它。然后我创建了一个新函数的后台任务,并在其中添加了@bot.event 运行。密码是
def xyz(bot):
@bot.event
async def on_member_join(member):
print("ABC")
def setup(bot):
bot.loop.create_task(xyz(bot))
bot.add_cog(cogClass(bot))
以防有人不明白我的解释
编辑: 这是一种糟糕的做事方式。改用心理方式
我不推荐 qspitzers 的回答,因为这不是将事件移动到 cog 的明智方法,而且答案可能会引发一些 unknown/unexpected 异常。
而是做这样的事情。
from discord.ext import commands
class Events:
def __init__(self, bot):
self.bot = bot
async def on_ready(self):
print('Ready!')
print('Logged in as ---->', self.bot.user)
print('ID:', self.bot.user.id)
async def on_message(self, message):
print(message)
def setup(bot):
bot.add_cog(Events(bot))
请记住,要将事件放置在齿轮内,您不需要装饰器。此外,cog 内的事件不会覆盖默认事件,这些事件将存储在 bot.extra_events
.
从 new-style cog, you must use the 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))