本文介绍了@ bot.event在嵌齿轮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')
等待channel.send( hello)
在我的齿轮课中,但出现错误
NameError :即使未在__init __中定义self.bot,也未定义名称'self'
/ p>
在嵌齿轮中有执行bot.event的其他方法吗?还是不可能?
解决方案
要从,则必须使用装饰器。下面是mental的示例转换为新样式后的示例:
from discord.ext导入命令
类事件(commands.Cog):
def __init __(self,bot):
self.bot = bot
@ commands.Cog.listener()
async def on_ready (self):
print('Ready!')
print('登录为---->',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))
I was wondering if it is possible to use @bot.eventin a cog for discord.py. I have tried doing
@self.bot.event
async def on_member_join(self, ctx, member):
channel = discord.utils.get(member.guild.channels, name='general')
await channel.send("hello")
in my cog class, but I get the error
NameError: name 'self' is not defined
even though I define self.bot in my __init __.
Is there a different way of doing bot.event in cogs, or is it just not possible?
解决方案
To register an event from a new-style cog, you must use the commands.Cog.listener
decorator. Below is mental's example converted to the new style:
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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!