问题
I have the cog set up properly (which I know, because I have a separate cog which handles all of the commands, so that on_message does not mess them up), but on_message just doesn't do anything.
I've tried including it into the other cog, but I still don't get an error with anything, it just doesn't work. I've also tried using different forms of @bot.event but these all just cause errors. Lastly, I know that the cog is working because the on_ready in the main .py alerts me that it had loaded successfully.
This is the code in the cog that should be reading all of the messages (minus all of the import stuff):
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))
and this is the code that loads it
@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()
Hopefully, the bot should just print all of the messages to the console, because then I'll know its working and can move forward with the other things I want it to do.
回答1:
Event listeners in a cog need to be decorated with commands.Cog.listener
@commands.Cog.listener()
async def on_message(self, message):
print(message.content)
The documentation for the new-style cogs can be found here
来源:https://stackoverflow.com/questions/55840817/how-can-i-use-a-python-discord-bot-cog-to-read-all-messages-that-are-sent-on-me