Zzz*_*h11 2 python bots discord discord.py
我已经正确设置了齿轮(我知道,因为我有一个单独的齿轮来处理所有命令,因此 on_message 不会弄乱它们),但 on_message 只是不执行任何操作。
我已经尝试将其包含到另一个齿轮中,但我仍然没有收到任何错误,它只是不起作用。我也尝试过使用不同形式的 @bot.event 但这些都会导致错误。最后,我知道 cog 正在工作,因为主 .py 中的 on_ready 提醒我它已成功加载。
这是齿轮中的代码,应该读取所有消息(减去所有导入内容):
class autoresponse(commands.Cog):
def __init__(self, bot):
self.bot = bot
async def on_message(self, message):
print(message.content)
def setup(bot):
bot.add_cog(autoresponse(bot))
Run Code Online (Sandbox Code Playgroud)
这是加载它的代码
@bot.event
async def on_ready():
print('bot is up')
await bot.change_presence(status=discord.Status.online, activity=discord.Game("bl help"))
for cog in [f.replace('.py', "") for f in listdir("cogs") if isfile(join("cogs", f))]:
try:
if not "__init__" in cog:
bot.load_extension("cogs." + cog)
print("Loaded cog")
except Exception as e:
print("Cog {} not loaded!".format(cog))
traceback.print_exc()
Run Code Online (Sandbox Code Playgroud)
希望机器人应该将所有消息打印到控制台,因为这样我就会知道它的工作原理,并可以继续执行我希望它执行的其他操作。
cog 中的事件侦听器需要用以下内容装饰commands.Cog.listener
@commands.Cog.listener()
async def on_message(self, message):
print(message.content)
Run Code Online (Sandbox Code Playgroud)
新型齿轮的文档可以在这里找到
| 归档时间: |
|
| 查看次数: |
8461 次 |
| 最近记录: |