Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Move additional event handler functionality to Client #1907

Merged

Conversation

davidhozic
Copy link
Contributor

@davidhozic davidhozic commented Feb 5, 2023

Summary

The pull request introduces changes proposed in #1906 .

It allows the user to register multiple on_ event handlers inside the Client objects instead of just inBot.

import discord
import secret


class MyClient(discord.Client):
    async def on_ready(self):
        print("Default handler")


intents = discord.Intents.default()
intents.message_content = True
client = MyClient(intents=intents)


@client.event # Override the default handler
async def on_ready():
    print(f"Handler 1: {client.user.name} has logged in!")

@client.listen # Additional handler
async def on_ready():
    print(f"Handler 2: {client.user.name} has logged in!")

@client.listen("on_ready") # Additional handler
async def ready_handler():
    print(f"Handler 3: {client.user.name} has logged in!")

@client.listen("on_ready") # Additional handler
async def ready_handler():
    print(f"Handler 4: {client.user.name} has logged in!")

@client.listen()
async def on_message(message: discord.Message):
    print(f"Received message {message.content}")


@client.listen("on_guild_channel_update")
async def channel_updated(prev: discord.ChannelType, curr:discord.ChannelType):
    print(prev.name, curr.name)

client.run(secret.TOKEN)

Also add support to allow @listen without () at the end.

import discord
import secret

bot = discord.Bot()

@bot.listen()
async def on_ready():
    print(f"Handler 1: {bot.user.name} has logged in!")

@bot.listen # <-------- No ()
async def on_ready():
    print(f"Handler 2: {bot.user.name} has logged in!")

bot.run(secret.TOKEN)

Information

  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed).
  • This PR is not a code change (e.g. documentation, README, typehinting,
    examples, ...).

Checklist

  • I have searched the open pull requests for duplicates.
  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • If type: ignore comments were used, a comment is also left explaining why.

@davidhozic davidhozic requested a review from a team as a code owner February 5, 2023 14:14
discord/client.py Outdated Show resolved Hide resolved
discord/client.py Outdated Show resolved Hide resolved
discord/client.py Outdated Show resolved Hide resolved
discord/client.py Outdated Show resolved Hide resolved
discord/client.py Outdated Show resolved Hide resolved
@davidhozic davidhozic marked this pull request as draft February 5, 2023 18:28
@davidhozic davidhozic marked this pull request as ready for review February 5, 2023 19:39
@davidhozic davidhozic changed the title feat: Add ability to register multiple event handlers with support for custom handler names feat: Move additional event handler functionality to Client Feb 5, 2023
@codecov
Copy link

codecov bot commented Feb 7, 2023

Codecov Report

Merging #1907 (e1e8dc6) into master (8c95c94) will decrease coverage by 0.02%.
The diff coverage is 9.67%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1907      +/-   ##
==========================================
- Coverage   33.37%   33.36%   -0.02%     
==========================================
  Files          97       97              
  Lines       18847    18851       +4     
==========================================
- Hits         6290     6289       -1     
- Misses      12557    12562       +5     
Flag Coverage Δ
macos-latest-3.10 33.34% <9.67%> (-0.02%) ⬇️
macos-latest-3.11 33.34% <9.67%> (-0.02%) ⬇️
macos-latest-3.8 33.35% <9.67%> (-0.02%) ⬇️
macos-latest-3.9 33.35% <9.67%> (-0.02%) ⬇️
ubuntu-latest-3.10 33.34% <9.67%> (-0.02%) ⬇️
ubuntu-latest-3.11 33.34% <9.67%> (-0.02%) ⬇️
ubuntu-latest-3.8 33.35% <9.67%> (-0.02%) ⬇️
ubuntu-latest-3.9 33.35% <9.67%> (-0.02%) ⬇️
windows-latest-3.10 33.34% <9.67%> (-0.02%) ⬇️
windows-latest-3.11 33.34% <9.67%> (-0.02%) ⬇️
windows-latest-3.8 33.35% <9.67%> (-0.02%) ⬇️
windows-latest-3.9 33.35% <9.67%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
discord/bot.py 18.53% <ø> (+0.21%) ⬆️
discord/client.py 27.73% <9.67%> (-1.21%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8c95c94...e1e8dc6. Read the comment docs.

@Lulalaby Lulalaby changed the title feat: Move additional event handler functionality to Client feat: Move additional event handler functionality to Client Feb 23, 2023
@Lulalaby Lulalaby added this to the v2.5 milestone Feb 23, 2023
@Lulalaby Lulalaby requested review from Dorukyum, BobDotCom, VincentRPS and plun1331 and removed request for VincentRPS February 23, 2023 10:04
@Lulalaby Lulalaby enabled auto-merge (squash) February 23, 2023 10:05
@Lulalaby Lulalaby merged commit 40a26ca into Pycord-Development:master Feb 23, 2023
@davidhozic davidhozic deleted the feat/improve_event_handlers branch February 25, 2023 12:00
NeloBlivion added a commit to NeloBlivion/pycord that referenced this pull request Feb 26, 2023
Lulalaby pushed a commit that referenced this pull request Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Implements a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move @Bot.listen, Bot.add_listener and Bot.remove_listener into Client (multiple event handlers)
5 participants