Top-gg-Community / python-sdk

A simple API wrapper for top.gg written in Python
https://docs.top.gg/docs/Libraries/python
MIT License
90 stars 54 forks source link

topgg.endpoint doesn't work #70

Closed Hype3808 closed 2 years ago

Hype3808 commented 2 years ago

topgg.endpoint doesn't work.

Expected Behavior

printing topgg.types.BotVoteData

Current Behavior

No error raised. Nothing happened and printed

Development Environment

Python Version: 3.8.12 topggpy version: Master

Steps to Reproduce

# callback.py
@topgg.endpoint("/dbl", topgg.WebhookType.BOT, 'bizon_webhook1531')
def dbl_vote(data: topgg.types.BotVoteData, client: discord.Client=topgg.data(discord.Client)):
    print(data)
# main.py
# This is a subclass of commands.AutoShardedBot

class Bot(commands.AutoShardedBot):
    def __init__(self):
        super().__init__(...)
        self.webhook_menager = topgg.WebhookManager()
        self.webhook_menager.set_data(self)
        self.webhook_menager.endpoint(dbl_vote)
        self.loop.run_until_complete(self.webhook_menager.start(10000))

    async def on_ready(self):
        if self.webhook_menager.is_running:
            print("TopGG webhook is running!")
        else:
            await self.webhook_menager.start(10000)

image

Context

Possible Solution

Detailed Description

Possible Implementation

Notes

Could be my problem, could be not.

norinorin commented 2 years ago

Hi, as of now we only support invariant types for data injection—covariant types are supported by #68 though, so your code should work as that gets merged. Anyway, you got 2 options here, first one is to move the callback after the class definition and pass Bot as opposed to Client to data(), e.g.,

class Bot(commands.AutoShardedBot):
    ...

@topgg.endpoint(...)
def dbl_vote(data: topgg.types.BotVoteData, bot: Bot = topgg.data(Bot)):
    ...

The second option would be to have it a method instead of a function, e.g.,

class Bot(commands.AutoShardedBot):
    def __init__(self, ...):
        ...
        (
            webhook_manager.endpoint()
            .type(...)  # topgg.WebhookType
            .route(...)  # "/dbl"
            .auth(...)
            .callback(self.dbl_vote)
            .add_to_manager()
        )

    def dbl_vote(self, data: topgg.BotVoteData):
        ...

I'm not sure why you're not getting any errors though, you should be getting a KeyError because there's no datum with the key discord.Client.

Hype3808 commented 2 years ago

The first solution fixed my problem! Tysm!