-1

I don't really know what these are called, but I'm going to use Discord.py as an example. I want to create a class for handling and recieving actions on a website using a style similar to Discord.py.

client = discord.Client()

@client.event
async def on_messages(message):

I want to know how to create the @xxx.event as well as the async functions. What is this called and how do I implement it?

An example of this would be a bot for a social media platform like this:

@client.event
async def on_post_comment(comment):
   ...

@client.event
async def on_comment_reply(comment, reply):
   ...

@clinet.event
async def on_post_liked(user):
   ...
Walker
  • 121
  • 1
  • 9
  • Can you explain a bit more what problem you're trying to solve exactly? In what type of scenario (on what sort of website) would you want to handle what type of events? What should happen? What platform are you developing this website on? Do you have existing code this needs to fit into to solve some problem? (please respond by updating the question, to improve the question) – Grismar Jul 21 '22 at 03:20
  • Edited to give an example. I don't really know how to phrase it since i'm trying to figure out what this type of thing actually is and how to implement it. I don't know any terms since I'm not sure exactly what this is. – Walker Jul 21 '22 at 04:27
  • About that in general: the best way to learn programming is often to try and solve a problem and learn how to code it best (or in different ways) using the language you're learning - not to pick a part of the language and figuring out what it's good for. You may not need it (every language will have a ton of features you don't really need at all) and if you do, you should be able to find what you need when you do. – Grismar Jul 21 '22 at 05:09

1 Answers1

0

I don't know how it works for other modules. But you can use commands.Cog.listener for events in cogs.

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)

I don't know what module you are trying to use this for. But next time look it up for your specific module.

  • It's not necessarily for discord.py i've also seen this on Hanger https://github.com/SoulSen/Hanger and several others. I just want to make a class that can listen to actions and then respond to them. – Walker Jul 21 '22 at 04:24