|
| 1 | +# Top.gg Python SDK |
| 2 | + |
| 3 | +> For more information, see the documentation here: <https://topggpy.rtfd.io>. |
| 4 | +
|
| 5 | +The community-maintained Python SDK for Top.gg. |
| 6 | + |
| 7 | +## Chapters |
| 8 | + |
| 9 | +- [Installation](#installation) |
| 10 | +- [Setting up](#setting-up) |
| 11 | +- [Usage](#usage) |
| 12 | + - [Getting your project's information](#getting-your-projects-information) |
| 13 | + - [Getting your project's vote information of a user](#getting-your-projects-vote-information-of-a-user) |
| 14 | + - [Getting a cursor-based paginated list of votes for your project](#getting-a-cursor-based-paginated-list-of-votes-for-your-project) |
| 15 | + - [Posting your bot's application commands list](#posting-your-bots-application-commands-list) |
| 16 | + - [Generating widget URLs](#generating-widget-urls) |
| 17 | + - [Webhooks](#webhooks) |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```sh |
| 22 | +$ pip install topggpy |
| 23 | +``` |
| 24 | + |
| 25 | +## Setting up |
| 26 | + |
| 27 | +```py |
| 28 | +import topgg |
| 29 | + |
| 30 | +import os |
| 31 | + |
| 32 | + |
| 33 | +token = os.getenv('TOPGG_TOKEN') |
| 34 | +assert token is not None, 'Missing TOPGG_TOKEN environment variable.' |
| 35 | + |
| 36 | +client = topgg.Client(token) |
| 37 | +``` |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +### Getting your project's information |
| 42 | + |
| 43 | +```py |
| 44 | +project = await client.get_self() |
| 45 | +``` |
| 46 | + |
| 47 | +### Getting your project's vote information of a user |
| 48 | + |
| 49 | +#### Discord ID |
| 50 | + |
| 51 | +```py |
| 52 | +vote = await client.get_vote(topgg.UserSource.DISCORD, 661200758510977084) |
| 53 | +``` |
| 54 | + |
| 55 | +#### Top.gg ID |
| 56 | + |
| 57 | +```py |
| 58 | +vote = await client.get_vote(topgg.UserSource.TOPGG, 8226924471638491136) |
| 59 | +``` |
| 60 | + |
| 61 | +### Getting a cursor-based paginated list of votes for your project |
| 62 | + |
| 63 | +```py |
| 64 | +from datetime import datetime |
| 65 | + |
| 66 | + |
| 67 | +first_page = await client.get_votes(datetime(2026, 1, 1)) |
| 68 | + |
| 69 | +for vote in first_page: |
| 70 | + print(vote) |
| 71 | + |
| 72 | +second_page = await first_page.next() |
| 73 | + |
| 74 | +for vote in second_page: |
| 75 | + print(vote) |
| 76 | + |
| 77 | +third_page = await second_page.next() |
| 78 | +``` |
| 79 | + |
| 80 | +### Posting your bot's application commands list |
| 81 | + |
| 82 | +#### Discord.py |
| 83 | + |
| 84 | +```py |
| 85 | +commands = [command.to_dict() for command in await bot.tree.fetch_commands()] |
| 86 | + |
| 87 | +await client.post_commands(commands) |
| 88 | +``` |
| 89 | + |
| 90 | +#### Raw |
| 91 | + |
| 92 | +```py |
| 93 | +# Array of application commands that |
| 94 | +# can be serialized to Discord API's raw JSON format. |
| 95 | +await client.post_commands( |
| 96 | + [ |
| 97 | + { |
| 98 | + 'id': '1', |
| 99 | + 'type': 1, |
| 100 | + 'application_id': '1', |
| 101 | + 'name': 'test', |
| 102 | + 'description': 'command description', |
| 103 | + 'default_member_permissions': '', |
| 104 | + 'version': '1', |
| 105 | + } |
| 106 | + ] |
| 107 | +) |
| 108 | +``` |
| 109 | + |
| 110 | +### Generating widget URLs |
| 111 | + |
| 112 | +#### Large |
| 113 | + |
| 114 | +```py |
| 115 | +widget_url = topgg.Widget.large(topgg.Platform.DISCORD, topgg.ProjectType.BOT, 1026525568344264724) |
| 116 | +``` |
| 117 | + |
| 118 | +#### Votes |
| 119 | + |
| 120 | +```py |
| 121 | +widget_url = topgg.Widget.votes(topgg.Platform.DISCORD, topgg.ProjectType.BOT, 1026525568344264724) |
| 122 | +``` |
| 123 | + |
| 124 | +#### Owner |
| 125 | + |
| 126 | +```py |
| 127 | +widget_url = topgg.Widget.owner(topgg.Platform.DISCORD, topgg.ProjectType.BOT, 1026525568344264724) |
| 128 | +``` |
| 129 | + |
| 130 | +#### Social |
| 131 | + |
| 132 | +```py |
| 133 | +widget_url = topgg.Widget.social(topgg.Platform.DISCORD, topgg.ProjectType.BOT, 1026525568344264724) |
| 134 | +``` |
| 135 | + |
| 136 | +### Webhooks |
| 137 | + |
| 138 | +With express: |
| 139 | + |
| 140 | +```py |
| 141 | +import topgg |
| 142 | + |
| 143 | +from aiohttp import web |
| 144 | +import os |
| 145 | + |
| 146 | + |
| 147 | +secret = os.getenv('TOPGG_WEBHOOK_SECRET') |
| 148 | +assert secret is not None, 'Missing TOPGG_WEBHOOK_SECRET environment variable.' |
| 149 | + |
| 150 | +# POST /webhook |
| 151 | +webhooks = topgg.Webhooks('/webhook', secret) |
| 152 | + |
| 153 | +@webhooks.on(topgg.PayloadType.TEST) |
| 154 | +async def test_listener(payload: topgg.TestPayload, trace: str) -> web.Response: |
| 155 | + print(payload) |
| 156 | + |
| 157 | + return web.Response(status=204) |
| 158 | + |
| 159 | +await webhooks.start() |
| 160 | +``` |
0 commit comments