214 Commits

Author SHA1 Message Date
Zomatree efce2aedac add archived notice 2025-12-11 01:22:41 +00:00
green. ca37846df7 fix: dont compare offset-naive and offset-aware datetimes (#89) 2024-11-29 21:54:44 +00:00
Zomatree 86c07a6d6b fix: cleanup docs to include missing things 2024-10-29 17:02:40 +00:00
Zomatree a8be358339 fix: various bugs
Closes #85
Closes #82
2024-10-29 15:57:26 +00:00
Zomatree e34ca0afda Greedy support, closes #77 2024-06-11 21:33:11 +01:00
IAmTomahawkx 060e6ea32a fix: prevent new space from appearing if buffer is depleted 2024-05-29 21:00:20 +01:00
Zomatree 2dabb0dbb8 fix patch for older python versions 2024-05-29 19:11:56 +01:00
Zomatree 48e0f0fb2e better handling of optional args 2024-05-29 00:18:00 +01:00
Zomatree bc8d650659 bump aiohttp version, fixes #73 2024-05-13 13:36:12 +01:00
Zomatree d8fde3d0a6 fix converters not catching errors 2024-05-13 13:32:21 +01:00
Zomatree d4568c02ea fix create_role 2024-05-13 13:27:11 +01:00
Zomatree ff49f8e3e5 fix pyright changing generics in metaclasses 2024-05-13 13:23:27 +01:00
Zomatree eed960b9de chore: bump version 2024-02-20 17:15:01 +00:00
Zomatree 185bd00eb9 fix: typing coverage 2024-02-15 20:30:34 +00:00
Zomatree 41be16477d feat: Command cooldowns 2024-02-15 20:25:37 +00:00
Zomatree 5a22d6063d Fix typing bugs 2023-12-29 20:21:48 +00:00
Zomatree 7b95ec1b6b Simple reconnection logic 2023-12-29 20:11:55 +00:00
Zomatree dffbc1f665 Add an Object class to make mocking data with ids easier 2023-12-29 20:11:27 +00:00
Zomatree 1cfc21d4ff Undo on failed args when the default exists 2023-12-29 20:10:57 +00:00
Zomatree 9bf443e36b Fix channel.history not fetching members to go along with the messages 2023-12-29 20:09:38 +00:00
Zomatree 5d3250bcce keep waiting when wait_for check fails 2023-09-13 21:30:49 +01:00
Zomatree 1a697457b7 Fix member.edit 2023-09-11 15:53:54 +01:00
Zomatree 51c7e45821 provide autocomplete for events 2023-09-11 15:53:36 +01:00
Zomatree 1c041d1e9f re-export types 2023-09-11 15:53:05 +01:00
Zomatree 04b2bfbc40 Merge branch 'master' of github.com:revoltchat/revolt.py 2023-08-28 18:57:10 +01:00
Zomatree 02cefeab46 Fix member.edit 2023-08-28 18:57:05 +01:00
William M 5aa3c2c00a Fix wrong query param types for remove_reaction (#65) 2023-07-27 15:51:27 +01:00
Zomatree 11663d493a Fix circular imports 2023-07-24 20:00:13 +01:00
Zomatree 411a0f8203 Merge branch 'master' of github.com:revoltchat/revolt.py 2023-07-20 00:30:30 +01:00
William M 7ae2a56610 Fix the 'user' and 'remove_all' parameters not working in Message#remove_reaction() (#63)
* Fix parameters not being passed in remove_reaction

* Update revolt/http.py

---------

Co-authored-by: Angelo Kontaxis <angelokontaxis@hotmail.com>
2023-07-20 00:30:07 +01:00
MysticMia 04e63e9468 Edit function and class docstrings to fix copy-paste error and improve english (#62)
* Update context.py

* Update asset.py

spelling / grammar errors

* Fix missing 'not' in docstring in Context.server
2023-07-20 00:19:10 +01:00
Zomatree c8c11b5394 Fix nameerror
fixes #61
2023-07-20 00:15:37 +01:00
Zomatree 2749822782 Add missing type-hint 2023-07-02 19:58:21 +01:00
Zomatree 71a33c16b4 Fix mentions for users who dont share a server
fixes #60
2023-07-02 19:56:59 +01:00
Zomatree 5ee3c60ba5 Add listeners 2023-07-01 01:41:17 +01:00
Zomatree 8524ff4036 rename typevars to be more informative 2023-06-30 16:20:46 +01:00
Zomatree c97085cd55 provide original message in message_update event 2023-06-30 16:17:28 +01:00
Zomatree 85910a703a fix subcommands in cogs 2023-06-26 17:19:37 +01:00
Zomatree 042a7da85e fix help command argument parsing 2023-06-24 01:19:34 +01:00
Zomatree 1841d21bee fix subcommands being shown as base commands 2023-06-24 01:18:55 +01:00
Zomatree 69602fc812 make created_at a property 2023-06-24 01:18:01 +01:00
Zomatree a52febcb4e Fix incorrect docs, closes #59 2023-06-22 02:54:03 +01:00
Zomatree da6fb64b89 Fix trying to send a DM to yourself 2023-06-22 02:53:21 +01:00
Zomatree 072a4b1f2d fix for older python versions 2023-06-21 00:45:12 +01:00
Zomatree 826e0f8c85 Fix varies bugs, fixes #55, fixes #56 2023-06-21 00:41:15 +01:00
Zomatree a23615a5ae Fix bug for older python versions 2023-06-15 01:58:46 +01:00
Zomatree 2c30502b50 Raise an error if the token is invalid 2023-06-13 04:17:14 +01:00
Zomatree e23effd605 add missing annotations 2023-06-13 04:14:49 +01:00
Zomatree 4e70b8076c bump version 2023-06-13 03:34:42 +01:00
Zomatree 828b428f48 Add display_name and discriminator 2023-06-13 03:10:45 +01:00
Zomatree 4efaac40f6 fix FLAG_NAMES not being set 2023-06-04 15:22:21 +01:00
Zomatree 78f14fa9a0 Revert: test CI 2023-05-20 13:09:59 +01:00
Zomatree 300169d71a Add back type checking to CI 2023-05-20 13:07:21 +01:00
Zomatree 983907e0ad test CI 2023-05-20 13:04:20 +01:00
Zomatree 4c8553d5ef ignore external types 2023-05-20 03:18:19 +01:00
Zomatree f717163e17 include py.typed 2023-05-20 03:14:10 +01:00
Zomatree 60474fdeef fix verify-types parameter 2023-05-20 03:08:24 +01:00
Zomatree ccfae66e16 dont cache deps 2023-05-20 03:06:04 +01:00
Zomatree 60ff8d81c9 bring type completeness to 100% 2023-05-20 03:04:52 +01:00
TheBobBobs dfb45494ba Fix Message._update (#50)
* fix Message._update

* handle edited being int in MessageUpdate event
2023-05-19 23:16:33 +01:00
Zomatree 52c2be4e91 Fix using .server instead of .server_id
Fixes #51
2023-05-19 23:13:55 +01:00
Zomatree d5a15c0f44 fix typing error related to bad aiohttp types 2023-05-19 22:23:47 +01:00
Zomatree 4bf0530f8e clean up code 2023-05-11 07:02:39 +01:00
Zomatree 77ff484ad8 small bug fixes 2023-04-12 20:20:18 +01:00
Zomatree d909b0eb3e Merge branch 'master' of github.com:revoltchat/revolt.py 2023-04-12 16:45:11 +01:00
Zomatree 32d6b1d6e8 Add missing checks to __all__ 2023-04-12 16:41:37 +01:00
Zomatree f9ba869e75 fix fetch_emoji not returning an Emoji instance 2023-04-12 16:37:05 +01:00
Angelo Kontaxis 0e694db586 Merge pull request #49 from revoltchat/permissions
Permissions
2023-04-11 22:34:30 +01:00
Zomatree ba3f74dcd0 Add docs 2023-04-11 22:23:12 +01:00
Zomatree 625dd4eac2 finish implementation of permissions 2023-04-11 22:07:56 +01:00
Zomatree 0a6db1dc9c inital permissions calculations 2023-04-11 19:05:56 +01:00
Zomatree a672f949a1 fix servermemberjoin, closes #48 2023-03-27 00:43:12 +01:00
Zomatree df9893aaff add bulk message delete 2023-03-26 21:44:04 +01:00
Zomatree 35a4614b61 fix incorrect routes 2023-03-26 18:42:16 +01:00
Zomatree 263f99f281 use is not None in _update 2023-03-26 18:09:35 +01:00
Zomatree e99b6edee3 correctly handle role creation 2023-03-26 18:08:30 +01:00
Zomatree 720272d3cb switch from deprecated config 2023-03-26 18:08:06 +01:00
Zomatree ab5a2751df fix docs 2023-03-15 19:41:31 +00:00
Zomatree 6059e4e4dc remove bugged dep 2023-03-15 19:30:41 +00:00
Zomatree 63491ec76e update docs 2023-03-15 19:00:49 +00:00
Zomatree 76364572f1 timeout and upload_file 2023-02-22 18:49:55 +00:00
Zomatree 75f980e0f9 use our own msgpack types 2023-02-22 18:40:06 +00:00
Zomatree 8db036e131 fix CI 2023-01-02 22:10:07 +00:00
Zomatree 4d35de3ac8 qol updates 2023-01-02 22:06:25 +00:00
Zomatree ce5bf29f59 rework flags 2023-01-02 22:05:51 +00:00
Zomatree 656b9e6483 convert to hatch from poetry 2023-01-02 22:04:37 +00:00
Zomatree a92324149e fix issues stemming from bugs in the revolt api 2022-11-16 23:06:31 +00:00
Zomatree 9504c05e7b bump aiohttp dep to fix python 3.11 2022-11-16 23:05:45 +00:00
Zomatree d3f7275d1b add created_at to all models with ids 2022-11-16 23:05:06 +00:00
Zomatree 588d62e093 Use revolt.utils.client_session in example (#46) 2022-10-10 09:38:06 +01:00
Zomatree 8534458e28 fix type narrowing 2022-10-10 00:29:29 +01:00
Zomatree 95100a5b77 fix typing issues 2022-10-10 00:20:35 +01:00
Zomatree 81de5a6539 allow passing None to help_command to disable it 2022-10-10 00:19:37 +01:00
Zomatree b3e1ffa3bb reword typeguard comment to be updated 2022-10-10 00:18:24 +01:00
Zomatree c531aa649e bump doc deps 2022-10-08 14:38:39 +01:00
Zomatree 23354d030b make metaclass not generic 2022-09-26 01:26:03 +01:00
Zomatree bf8ded7588 make client generic 2022-09-26 01:16:33 +01:00
Zomatree 7ddfa2635c Fix colour always being none 2022-09-11 19:17:41 +01:00
Zomatree 7c412a056a fix race condition when joining a server 2022-08-21 13:19:45 +01:00
Zomatree 19cfa0a96c implement interactions 2022-08-18 22:51:47 +01:00
Zomatree 88af87a4e1 rename server_create event to server_join 2022-08-18 21:58:20 +01:00
Zomatree 6436901e6f move _members to a instance attr due being read only 2022-08-18 21:50:12 +01:00
Zomatree bdafa57437 fix incorrect __all__ 2022-08-18 21:04:18 +01:00
Zomatree 46c3cf893e fix varies bugs 2022-08-18 20:16:29 +01:00
Zomatree 737515f9b5 remove unnessary channel parameter 2022-08-18 03:25:51 +01:00
Zomatree 285974feef add reactions and interactions 2022-08-18 02:19:54 +01:00
Zomatree a065cd40c7 bump version 2022-08-08 19:31:00 +01:00
Zomatree 1ab91658c2 handle server join event 2022-08-08 19:23:25 +01:00
Zomatree 64ccd58b8f cleanup channel.edit 2022-07-16 00:21:26 +01:00
Zomatree d187fff90b add masquerade colour 2022-07-15 23:37:26 +01:00
Zomatree e34af623b3 fix last_message_id, fixes #41 2022-07-15 22:40:01 +01:00
Zomatree 2de49e6a6c change all keyerrors to lookup errors 2022-07-05 17:54:56 +00:00
Zomatree 424fa10208 cleanup code 2022-07-05 16:37:45 +00:00
Zomatree 052130d721 fix text channel docs 2022-07-05 16:36:55 +00:00
Zomatree efbb667fef handle channels in serverupdate event, fixes #39 2022-06-30 20:24:49 +01:00
Zomatree 09c50a0e97 fix incorrect docs 2022-06-30 20:24:13 +01:00
Zomatree 99b974f96e bump version 2022-06-23 02:10:19 +01:00
Zomatree 357fd7b071 update docs 2022-05-31 01:57:22 +01:00
Zomatree 1f411c12bf fix CI issues 2022-05-31 01:47:07 +01:00
Zomatree 25f3b4e4fc inital updated permissions system 2022-05-31 01:44:41 +01:00
Zomatree 6871754a27 remove trailing whitespace 2022-04-25 13:42:37 +01:00
Zomatree 0fdca7eb7d isort 2022-04-25 13:40:39 +01:00
Zomatree 7aa72d6e58 bump version to 0.1.7 2022-04-25 13:33:19 +01:00
HigherOrderLogic 94ba404912 Add cog_load and cog_unload method (#34)
* Add `cog_load` and `cog_unload` method

* Move `cog_load` to end

* Resolve conflict
2022-04-25 13:31:08 +01:00
Lewis L. Foster 4002be8d39 Implemented a servers property into revolt.Client (#36)
* Implement mention for users, members and channels

* Add servers property to revolt.Client

* Forgot to add the docstring for revolt.servers

* fix typo in revolt.Client.servers docstring
2022-04-25 13:30:30 +01:00
Achxy 8d1ee4609b Fix typo in the docstring of FeatureDisabled (#37) 2022-04-25 13:30:00 +01:00
Lewis L. Foster 8b7d850741 Implement mention for users, members and channels (#35) 2022-04-06 15:58:42 +01:00
Zomatree 2c25f2f05b fix ci issue 2022-04-05 06:21:13 +01:00
Zomatree e491ba0f6e rework converter internals to handle edgecases 2022-04-05 06:15:51 +01:00
Zomatree 7229b75c17 fix cog binding 2022-04-03 16:11:49 +01:00
Zomatree aea5c98dbb update justfile 2022-04-02 21:02:49 +01:00
Zomatree c61e58964c add missing docs to functions 2022-04-02 21:02:25 +01:00
Zomatree d5fb40690d switch to using NotRequired 2022-04-02 21:02:12 +01:00
Zomatree a539d8d8c1 fix help command to allow changing attrs 2022-04-02 20:36:05 +01:00
Zomatree 41eddffc1b make context generic over client 2022-03-30 19:03:03 +01:00
Zomatree 7f3253a3b5 add case_insenitive 2022-03-30 19:02:41 +01:00
Zomatree bbb3f18f42 add edit self 2022-03-30 19:00:02 +01:00
Zomatree a0717b180d remove manifest 2022-03-28 04:19:43 +01:00
Zomatree 85b6309192 fix workflow 2022-03-28 01:54:39 +01:00
Zomatree 16aa5308a9 dont cache pip 2022-03-28 01:03:15 +01:00
Zomatree d0263b5f8f switch to pyproject.toml 2022-03-28 01:02:04 +01:00
Zomatree 40ea19951f add basic help command framework 2022-03-26 18:03:13 +00:00
Zomatree f539590005 add group.commands 2022-03-26 18:02:12 +00:00
Zomatree 7b05466090 make add_command take one param and add remove_command 2022-03-26 18:01:44 +00:00
Zomatree 8ba3e047b9 add cog.qualified_name and cog.commands 2022-03-26 18:01:01 +00:00
Zomatree 2279f86446 fix codeblock in documentation 2022-03-23 23:49:28 +00:00
Zomatree 5b39fb1b74 fix voice channel permissions error 2022-03-23 23:39:39 +00:00
Zomatree c19003f2be initial user support 2022-03-23 23:31:45 +00:00
Zomatree c360bbda84 fix small issues 2022-03-23 23:29:16 +00:00
Zomatree b03ac0c9d2 add ClientSession helper function 2022-03-23 22:44:19 +00:00
Zomatree 21c9c78b6d fix up docs 2022-03-23 22:43:57 +00:00
Zomatree 452f1f45bc satisfy the type checker 2022-03-23 22:00:03 +00:00
Omkaar 8641f45400 Fix spelling errors (#30)
* Fix spelling errors in `websocket.py`

* Fix some errors in `utils.py`

* Fix spelling error in `asset.py`

* Fix spelling error in `enums.py`

* Fix some more errors
2022-03-11 10:37:54 +00:00
Zomatree 19d3d5ea4b fix 3.9 compat 2022-03-07 03:00:07 +00:00
Zomatree d7eb5cfe46 display errors from event handlers 2022-03-07 02:56:22 +00:00
Zomatree 07d8c9dd1b handle invalid json responses 2022-03-07 02:55:56 +00:00
Zomatree 0dc5e5510a fix message editing 2022-03-07 02:55:05 +00:00
Zomatree c83897a1f7 make channel.server_id optional 2022-03-07 02:54:25 +00:00
Zomatree be2af590af add cogs and extensions 2022-02-14 05:56:18 +00:00
Zomatree 16177e6698 fix missing inconsistancies 2022-02-14 05:51:46 +00:00
Zomatree 40e9e3a26e import * 2022-02-14 05:50:50 +00:00
Zomatree c29a763576 add embed support 2022-02-14 05:50:35 +00:00
Zomatree 5e757d129f fix parameters with no annotation 2022-02-08 22:15:45 +00:00
Zomatree b17b7fc171 inital implementation of group commands 2022-01-28 19:32:00 +00:00
Zomatree 9dfcf69e52 bump version to v0.1.6 2022-01-28 19:15:11 +00:00
Zomatree b1a91cecb1 fix typing fix error 2022-01-28 19:14:15 +00:00
Zomatree 7cf3fa3e01 bump version to 0.1.5 2022-01-27 23:41:56 +00:00
Zomatree bff242c3f6 fix deps not being auto installed from pypi 2022-01-27 23:41:21 +00:00
Zomatree 0fbb90f65e fix typing issues 2022-01-22 00:30:09 +00:00
Zomatree c7963f02e9 cover most api routes 2022-01-21 22:04:30 +00:00
Bye 5641390d5b Add pip install instructions (#25)
Co-authored-by: Zomatree <39768508+Zomatree@users.noreply.github.com>
2022-01-21 20:24:29 +00:00
Zomatree 7a37da571f bump version to 0.1.4 2022-01-05 18:45:23 +00:00
Zomatree 8aafa8b7f2 fix for 3.9 2022-01-05 18:36:32 +00:00
Zomatree 3754357812 bump version to 0.1.3 2022-01-05 18:33:44 +00:00
Zomatree 5b757bd5df fix union converters 2022-01-05 18:31:58 +00:00
Zomatree e938136b2e fix bug with channels 2022-01-05 18:24:06 +00:00
Zomatree 18e8366651 bump sphinx 2022-01-03 21:07:55 +00:00
Zomatree 85fbe30c32 Merge branch 'master' of github.com:Zomatree/revolt.py 2022-01-03 20:55:50 +00:00
Zomatree 02a839a903 bump version 2022-01-03 20:55:39 +00:00
LightSage 9fc5ee1c84 Fix user update (#23) 2022-01-03 20:54:05 +00:00
Zomatree 3200b06019 Merge branch 'master' of github.com:Zomatree/revolt.py 2022-01-03 20:51:22 +00:00
Zomatree df8815e68a add more converters 2022-01-03 20:51:19 +00:00
Ed L ccd379b24b update repo links, grammar (#24)
* chore: update repo links, grammar

* Update revolt/http.py

Co-authored-by: Zomatree <39768508+Zomatree@users.noreply.github.com>
2022-01-03 20:50:16 +00:00
Zomatree 1c5ceffc58 fix categories docs 2022-01-03 20:39:17 +00:00
Zomatree fd6ef5fc30 add utils.get 2022-01-03 20:37:21 +00:00
Zomatree 7ccd651e4b add server.categories and get_category 2022-01-03 20:00:50 +00:00
Zomatree 35566fff42 add client.users 2022-01-03 19:58:34 +00:00
Zomatree abbbc8c1eb fix client.get_* typehints 2022-01-03 19:58:02 +00:00
Zomatree 67af8b5bf0 update justfile 2022-01-03 19:57:23 +00:00
Zomatree 39098692bc add flags 2021-11-20 22:33:10 +00:00
Zomatree a16bac6207 isort 2021-11-13 23:59:25 +00:00
Zomatree 23d8ced599 Merge branch 'master' of github.com:Zomatree/revolt.py 2021-11-13 23:53:24 +00:00
Zomatree e5d65b9400 add Literal converter and rejig command client 2021-11-13 23:53:21 +00:00
Zomatree c874017a6b add more checks 2021-11-13 23:52:23 +00:00
LightSage 21d27bd44a Fix command repr (#22) 2021-11-13 10:35:57 +00:00
Zomatree 91fde98739 add more advance converters 2021-11-10 16:55:24 +00:00
Zomatree 34a80ea5e2 fix linter 2021-11-10 16:55:14 +00:00
Zomatree 7d6f33f95c auto install deps 2021-11-10 16:37:08 +00:00
Zomatree 1310f3a31a add the rest of the events 2021-11-08 13:59:16 +00:00
Zomatree 611939ed6f change check typings 2021-11-07 11:08:24 +00:00
Zomatree 5514d03b63 change example orefix 2021-11-07 10:39:19 +00:00
Zomatree bd984090c7 add basic check system 2021-11-07 10:33:41 +00:00
Zomatree b2558c1d37 fix deleting message outside cache 2021-11-07 10:26:39 +00:00
Zomatree 0c7cc738fc add masquerade 2021-11-07 10:26:12 +00:00
Zomatree d7175d0079 make context subclass messageable 2021-11-06 05:07:47 +00:00
rockboy987 b5727c818e Add replies parameter to messageable.send (#19)
* Add replies parameter to messageable.send

* Apply requested changes
2021-11-04 22:18:55 +00:00
rockboy987 89986731c1 Fix server to be optional in Channel (#21) 2021-11-03 04:37:10 +00:00
Zomatree 2a3071712e add proper argument parsing - converters and keyword arguments 2021-10-30 15:57:03 +01:00
Zomatree 77fa908ad5 add justfile 2021-10-29 09:16:43 +01:00
Zomatree b94c8cd58e fix argument splitter 2021-10-29 03:48:54 +01:00
Zomatree c5a67335d2 add more attributes to context 2021-10-29 03:48:38 +01:00
Zomatree 287b2d42ea add repr to command 2021-10-29 03:45:39 +01:00
Zomatree a2168e35e9 add server_update event + missing server attrs 2021-10-28 03:43:20 +01:00
Zomatree 497d666311 Closes #18 2021-10-28 03:42:06 +01:00
71 changed files with 6521 additions and 1418 deletions
+27 -7
View File
@@ -1,18 +1,38 @@
on: [push, pull_request] on: [push, pull_request]
name: pyright name: pyright
jobs: jobs:
pyright: pyright-type-checking:
strategy:
matrix:
version: ["3.9", "3.10", "3.11"]
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions/setup-python@v2 - uses: actions/setup-python@v2
with: with:
python-version: '3.9' python-version: ${{ matrix.version }}
- uses: BSFishy/pip-action@v1 - run: pip install .[speedups,docs]
with:
requirements: pyright_requirements.txt
- uses: jakebailey/pyright-action@v1 - uses: jakebailey/pyright-action@v1
with: with:
lib: true python-version: ${{ matrix.version }}
python-version: 3.9.7
working-directory: revolt working-directory: revolt
pyright-type-completeness:
strategy:
matrix:
version: ["3.9", "3.10", "3.11"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.version }}
- run: pip install .[speedups,docs]
- uses: jakebailey/pyright-action@v1
with:
python-version: ${{ matrix.version }}
working-directory: revolt
verify-types: revolt
ignore-external: true
+3
View File
@@ -5,3 +5,6 @@ dist
*.egg-info *.egg-info
docs/_build docs/_build
.vscode .vscode
.env
.mypy_cache
build
+9 -5
View File
@@ -1,12 +1,16 @@
version: 2 version: 2
sphinx: sphinx:
configuration: docs/conf.py configuration: docs/conf.py
python: python:
version: "3.9" install:
install: - method: pip
- requirements: docs_requirements.txt path: .
extra_requirements:
- docs
build: build:
image: testing tools:
python: "3.9"
os: "ubuntu-22.04"
Executable
+20
View File
@@ -0,0 +1,20 @@
set dotenv-load := true
test:
python test.py
build:
rm -rf dist/*
python -m build
upload:
python -m twine upload dist/*
lint:
pyright .
coverage:
pyright --ignoreexternal --verifytypes revolt
docs:
cd docs && make html
+18 -7
View File
@@ -1,21 +1,32 @@
# Revolt.py # Revolt.py
An async library to interact with the https://revolt.chat api. > # This project is archived and is no longer receiving updates.
This library will be focused on making bots and i will not implement anything only for user accounts. An async library to interact with the https://revolt.chat API.
Support server: https://app.revolt.chat/invite/FDXER6hr You can join the support server [here](https://rvlt.gg/FDXER6hr) and find the library's documentation [here](https://revoltpy.readthedocs.io/en/latest/).
Documentation is [here](https://revoltpy.readthedocs.io/en/latest/) ## Installing
You can use `pip` to install revolt.py. It differs slightly depending on what OS/Distro you use.
On Windows
```
py -m pip install -U revolt.py # -U to update
```
On macOS and Linux
```
python3 -m pip install -U revolt.py
```
## Example ## Example
More examples in the [examples folder](https://github.com/zomatree/revolt.py/blob/master/examples) More examples can be found in the [examples folder](https://github.com/revoltchat/revolt.py/blob/master/examples).
```py ```py
import revolt import revolt
import asyncio import asyncio
import aiohttp
class Client(revolt.Client): class Client(revolt.Client):
async def on_message(self, message: revolt.Message): async def on_message(self, message: revolt.Message):
@@ -23,7 +34,7 @@ class Client(revolt.Client):
await message.channel.send("hi how are you") await message.channel.send("hi how are you")
async def main(): async def main():
async with aiohttp.ClientSession() as session: async with revolt.utils.client_session() as session:
client = Client(session, "BOT TOKEN HERE") client = Client(session, "BOT TOKEN HERE")
await client.start() await client.start()
View File
+158 -62
View File
@@ -4,129 +4,154 @@ API Reference
=============== ===============
Client
~~~~~~~
.. autoclass:: Client .. autoclass:: Client
:members: :members:
:inherited-members:
Asset
~~~~~~
.. autoclass:: Asset .. autoclass:: Asset
:members: :members:
:inherited-members:
Channel .. autoclass:: PartialAsset
~~~~~~~~ :members:
:inherited-members:
.. autoclass:: Channel .. autoclass:: Channel
:members: :members:
:inherited-members:
SavedMessageChannel .. autoclass:: ServerChannel
~~~~~~~~~~~~~~~~~~~~ :members:
:inherited-members:
.. autoclass:: SavedMessageChannel .. autoclass:: SavedMessageChannel
:members: :members:
:inherited-members:
DMChannel
~~~~~~~~~~
.. autoclass:: DMChannel .. autoclass:: DMChannel
:members: :members:
:inherited-members:
GroupDMChannel
~~~~~~~~~~~~~~~
.. autoclass:: GroupDMChannel .. autoclass:: GroupDMChannel
:members: :members:
:inherited-members:
TextChannel
~~~~~~~~~~~~
.. autoclass:: TextChannel .. autoclass:: TextChannel
:members: :members:
:inherited-members:
VoiceChannel
~~~~~~~~~~~~
.. autoclass:: VoiceChannel .. autoclass:: VoiceChannel
:members: :members:
:inherited-members:
Embed
~~~~~~
.. autoclass:: Embed .. autoclass:: Embed
:members: :members:
:inherited-members:
.. autoclass:: WebsiteEmbed
:members:
:inherited-members:
File .. autoclass:: ImageEmbed
~~~~~ :members:
:inherited-members:
.. autoclass:: TextEmbed
:members:
:inherited-members:
.. autoclass:: NoneEmbed
:members:
:inherited-members:
.. autoclass:: SendableEmbed
:members:
:inherited-members:
.. autoclass:: File .. autoclass:: File
:members: :members:
:inherited-members:
Member
~~~~~~~
.. autoclass:: Member .. autoclass:: Member
:members: :members:
:inherited-members:
Message
~~~~~~~
.. autoclass:: Message .. autoclass:: Message
:members: :members:
:inherited-members:
Messageable .. autoclass:: MessageReply
~~~~~~~~~~~~ :members:
:inherited-members:
.. autoclass:: Masquerade
:members:
:inherited-members:
.. autoclass:: Messageable .. autoclass:: Messageable
:members: :members:
:inherited-members:
Permissions .. autoclass:: Permissions
~~~~~~~~~~~~
.. autoclass:: ChannelPermissions
:members: :members:
:inherited-members:
.. autoclass:: ServerPermissions .. autoclass:: UserPermissions
:members: :members:
:inherited-members:
Role .. autoclass:: PermissionsOverwrite
~~~~~ :members:
:inherited-members:
.. autoclass:: Role .. autoclass:: Role
:members: :members:
:inherited-members:
Server
~~~~~~~
.. autoclass:: Server .. autoclass:: Server
:members: :members:
:inherited-members:
User .. autoclass:: ServerBan
~~~~~ :members:
:inherited-members:
.. autoclass:: Category
:members:
:inherited-members:
.. autoclass:: SystemMessages
:members:
:inherited-members:
.. autoclass:: User .. autoclass:: User
:members: :members:
:inherited-members:
Relation
~~~~~~~~~
.. autonamedtuple:: Relation .. autonamedtuple:: Relation
Status
~~~~~~~
.. autonamedtuple:: Status .. autonamedtuple:: Status
.. autoclass:: UserBadges
:members:
.. autoclass:: UserProfile
:members:
.. autoclass:: Invite
:members:
.. autoclass:: Emoji
:members:
.. autoclass:: MessageInteractions
:members:
Enums Enums
====== ------
The api uses enums to say what variant of something is, these represent those enums The api uses enums to say what variant of something is,
these represent those enums
All enums subclass `aenum.Enum`. All enums subclass `aenum.Enum`.
@@ -138,10 +163,10 @@ All enums subclass `aenum.Enum`.
A private channel only you can access. A private channel only you can access.
.. attribute:: direct_message .. attribute:: direct_message
A private direct message channel between you and another user A private direct message channel between you and another user
.. attribute:: group .. attribute:: group
A private group channel for messages between a group of users A private group channel for messages between a group of users
.. attribute:: text_channel .. attribute:: text_channel
@@ -171,7 +196,7 @@ All enums subclass `aenum.Enum`.
The user is offline or invisible The user is offline or invisible
.. attribute:: RelationshipType .. class:: RelationshipType
Specifies the relationship between two users Specifies the relationship between two users
@@ -188,12 +213,83 @@ All enums subclass `aenum.Enum`.
They are sending you a friend request They are sending you a friend request
.. attribute:: none .. attribute:: none
You have no relationship with them You have no relationship with them
.. attribute:: outgoing_friend_request .. attribute:: outgoing_friend_request
You are sending them a friend request You are sending them a friend request
.. attribute:: user .. attribute:: user
That user is yourself That user is yourself
.. class:: AssetType
Specifies the type of asset
.. attribute:: image
The asset is an image
.. attribute:: video
The asset is a video
.. attribute:: text
The asset is a text file
.. attribute:: audio
The asset is an audio file
.. attribute:: file
The asset is a generic file
.. class:: SortType
The sort type for a message search
.. attribute:: latest
Sort by the latest message
.. attribute:: oldest
Sort by the oldest message
.. attribute:: relevance
Sort by the relevance of the message
.. class:: EmbedType
The type of embed
.. attribute:: website
The embed is a website
.. attribute:: image
The embed is an image
.. attribute:: text
The embed is text
.. attribute:: video
The embed is a video
.. attribute:: unknown
The embed is unknown
Utils
------
.. currentmodule:: revolt.utils
A collection a utility functions and classes to aid in making your bot
.. autofunction:: get
.. autofunction:: client_session
.. autoclass:: Ulid
:members:
.. autoclass:: Object
:members:
+2 -1
View File
@@ -17,13 +17,14 @@ import sphinx_nameko_theme
sys.path.insert(0, os.path.abspath('..')) sys.path.insert(0, os.path.abspath('..'))
import revolt
# -- Project information ----------------------------------------------------- # -- Project information -----------------------------------------------------
project = 'Revolt.py' project = 'Revolt.py'
copyright = '2021-present, Zomatree' copyright = '2021-present, Zomatree'
author = 'Zomatree' author = 'Zomatree'
version = "0.0.1" version = ".".join(map(str, revolt.__version__))
# -- General configuration --------------------------------------------------- # -- General configuration ---------------------------------------------------
+176 -5
View File
@@ -4,9 +4,9 @@ API Reference
=============== ===============
CommandsMixin CommandsClient
~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
.. autoclass:: revolt.ext.commands.CommandsMixin .. autoclass:: revolt.ext.commands.CommandsClient
:members: :members:
Context Context
@@ -19,11 +19,182 @@ Command
.. autoclass:: revolt.ext.commands.Command .. autoclass:: revolt.ext.commands.Command
:members: :members:
Group
~~~~~~~~
.. autoclass:: revolt.ext.commands.Group
:members:
Cog
~~~~
.. autoclass:: revolt.ext.commands.Cog
:members:
command
~~~~~~~~
.. autodecorator:: revolt.ext.commands.command
group
~~~~~~~~
.. autodecorator:: revolt.ext.commands.group
Checks
-------
check
~~~~~~
.. autodecorator:: revolt.ext.commands.check
is_bot_owner
~~~~~~~~~~~~~
.. autodecorator:: revolt.ext.commands.is_bot_owner
is_server_owner
~~~~~~~~~~~~~~~~
.. autodecorator:: revolt.ext.commands.is_server_owner
has_permissions
~~~~~~~~~~~~~~~~
.. autodecorator:: revolt.ext.commands.has_permissions
has_channel_permissions
~~~~~~~~~~~~~~~~~~~~~~~~
.. autodecorator:: revolt.ext.commands.has_channel_permissions
Converters
-----------
IntConverter
~~~~~~~~~~~~~
Converts the parameter to an int
BoolConverter
~~~~~~~~~~~~~~
Converts the parameter to a bool
CategoryConverter
~~~~~~~~~~~~~~~~~~
Converts the parameter to a category
UserConverter
~~~~~~~~~~~~~~
Converts the parameter to a category
MemberConverter
~~~~~~~~~~~~~~~~
Converts the parameter to a category
ChannelConverter
~~~~~~~~~~~~~~~~~
Converts the parameter to a category
Greedy
~~~~~~
Converts the parameter to a greedy parameter which will take as many arguments which convert successfully.
Allows you to have var-args in the middle of a signature.
Help Commands
--------------
HelpCommand
~~~~~~~~~~~~
.. autoclass:: revolt.ext.commands.HelpCommand
:members:
DefaultHelpCommand
~~~~~~~~~~~~~~~~~~~
.. autoclass:: revolt.ext.commands.DefaultHelpCommand
:members:
Exceptions Exceptions
=========== -----------
CommandError
~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.CommandError
:members:
CommandNotFound CommandNotFound
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
.. autoclass:: revolt.ext.commands.CommandNotFound .. autoexception:: revolt.ext.commands.CommandNotFound
:members: :members:
NoClosingQuote
~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.NoClosingQuote
:members:
CheckError
~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.CheckError
:members:
NotBotOwner
~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.NotBotOwner
:members:
NotServerOwner
~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.NotServerOwner
:members:
ServerOnly
~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.ServerOnly
:members:
MissingPermissionsError
~~~~~~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.MissingPermissionsError
:members:
ConverterError
~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.ConverterError
:members:
InvalidLiteralArgument
~~~~~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.InvalidLiteralArgument
:members:
BadBoolArgument
~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.BadBoolArgument
:members:
CategoryConverterError
~~~~~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.CategoryConverterError
:members:
ChannelConverterError
~~~~~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.ChannelConverterError
:members:
UserConverterError
~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.UserConverterError
:members:
MemberConverterError
~~~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.MemberConverterError
:members:
UnionConverterError
~~~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.UnionConverterError
:members:
MissingSetup
~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.MissingSetup
:members:
CommandOnCooldown
~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.CommandOnCooldown
:members:
-6
View File
@@ -1,6 +0,0 @@
Sphinx==3.5.4
sphinx-nameko-theme==0.0.3
aiohttp==3.7.4.post0
ulid-py==1.1.0
sphinx-toolbox==2.13.0
aenum==3.1.0
+2 -2
View File
@@ -6,9 +6,9 @@ import revolt
from revolt.ext import commands from revolt.ext import commands
class Client(commands.CommandsMixin, revolt.Client): class Client(commands.CommandsClient):
async def get_prefix(self, message: revolt.Message): async def get_prefix(self, message: revolt.Message):
return ">>>" return "!"
@commands.command() @commands.command()
async def ping(self, ctx: commands.Context): async def ping(self, ctx: commands.Context):
Executable
+69
View File
@@ -0,0 +1,69 @@
[project]
name = "revolt.py"
dynamic = ["version"]
description = "Python wrapper for the revolt.chat API"
requires-python = ">=3.9"
license = "MIT"
readme = "README.md"
keywords = ["wrapper", "async", "api", "websockets", "http"]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3 :: Only",
]
dependencies = [
"aiohttp==3.10.*",
"ulid-py==1.1.*",
"aenum==3.1.*",
"typing_extensions>=4.4.0"
]
[project.optional-dependencies]
speedups = [
"ujson==5.1.*",
"msgpack==1.0.*"
]
docs = [
"Sphinx==5.2.*",
"sphinx-nameko-theme==0.0.*",
"sphinx-toolbox==3.2.*",
"setuptools==65.4.*"
]
[project.urls]
Homepage = "https://github.com/revoltchat/revolt.py"
Documentation = "https://revoltpy.readthedocs.io/en/latest/"
"Source Code" = "https://github.com/revoltchat/revolt.py"
"Bug Tracker" = "https://github.com/revoltchat/revolt.py/issues"
[[project.authors]]
name = "Zomatree"
email = "me@zomatree.live"
[tool.hatch.version]
path = "revolt/__init__.py"
[tool.hatch.build]
only-packages = true
include = ["revolt/**/*"]
[tool.pyright]
reportPrivateUsage = false
reportImportCycles = false
reportIncompatibleMethodOverride = false
typeCheckingMode = "strict"
[tool.hatch.build.targets.sdist]
strict-naming = false
[tool.hatch.build.targets.wheel]
strict-naming = false
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
-6
View File
@@ -1,6 +0,0 @@
aiohttp==3.7.4.post0
ulid-py==1.1.0
aenum==3.1.0
ujson
msgpack==1.0.2
.
-3
View File
@@ -1,3 +0,0 @@
aiohttp==3.7.4.post0
ulid-py==1.1.0
aenum==3.1.0
+21 -17
View File
@@ -1,18 +1,22 @@
from .asset import Asset from . import utils as utils
from .channel import (Channel, DMChannel, GroupDMChannel, SavedMessageChannel, from . import types as types
TextChannel, VoiceChannel) from .asset import *
from .client import Client from .category import *
from .embed import Embed from .channel import *
from .enums import (AssetType, ChannelType, PresenceType, RelationshipType, from .client import *
SortType) from .embed import *
from .errors import HTTPError, RevoltError, ServerError from .emoji import *
from .file import File from .enums import *
from .member import Member from .errors import *
from .message import Message from .file import *
from .messageable import Messageable from .flags import *
from .permissions import ChannelPermissions, ServerPermissions from .invite import *
from .role import Role from .member import *
from .server import Server from .message import *
from .user import Relation, Status, User from .messageable import *
from .permissions import *
from .role import *
from .server import *
from .user import *
__version__ = (0, 1, 1) __version__ = "0.2.0"
+56 -42
View File
@@ -1,9 +1,10 @@
from __future__ import annotations from __future__ import annotations
import mimetypes
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from .enums import AssetType from .enums import AssetType
from .errors import AutumnDisabled from .utils import Ulid
if TYPE_CHECKING: if TYPE_CHECKING:
from io import IOBase from io import IOBase
@@ -12,17 +13,17 @@ if TYPE_CHECKING:
from .types import File as FilePayload from .types import File as FilePayload
__all__ = ("Asset",) __all__ = ("Asset", "PartialAsset")
class Asset: class Asset(Ulid):
"""Represents a file on revolt """Represents a file on revolt
Attributes Attributes
----------- -----------
id: :class:`str` id: :class:`str`
The id of the asset The id of the asset
tag: :class:`str` tag: :class:`str`
The tag of the asset, this corrasponds to where the asset is used The tag of the asset, this corresponds to where the asset is used
size: :class:`int` size: :class:`int`
Amount of bytes in the file Amount of bytes in the file
filename: :class:`str` filename: :class:`str`
@@ -35,65 +36,78 @@ class Asset:
The content type of the file The content type of the file
type: :class:`AssetType` type: :class:`AssetType`
The type of asset it is The type of asset it is
url: :class:`str`
The asset's url
""" """
__slots__ = ("state", "id", "tag", "size", "filename", "content_type", "width", "height", "type") __slots__ = ("state", "id", "tag", "size", "filename", "content_type", "width", "height", "type", "url")
def __init__(self, data: FilePayload, state: State): def __init__(self, data: FilePayload, state: State):
self.state = state self.state: State = state
self.id: str = data['_id']
self.tag: str = data['tag']
self.size: int = data['size']
self.filename: str = data['filename']
self.id = data['_id']
self.tag = data['tag']
self.size = data['size']
self.filename = data['filename']
metadata = data['metadata'] metadata = data['metadata']
self.height: int | None
self.width: int | None
if metadata["type"] == "Image" or metadata["type"] == "Video": # cant use `in` because type narrowing wont happen if metadata["type"] == "Image" or metadata["type"] == "Video": # cannot use `in` because type narrowing will not happen
self.height = metadata["height"] self.height = metadata["height"]
self.width = metadata["width"] self.width = metadata["width"]
else: else:
self.height = None self.height = None
self.width = None self.width = None
self.content_type = data["content_type"] self.content_type: str | None = data["content_type"]
self.type = AssetType(metadata["type"]) self.type: AssetType = AssetType(metadata["type"])
base_url = self.state.api_info["features"]["autumn"]["url"]
self.url: str = f"{base_url}/{self.tag}/{self.id}"
async def read(self) -> bytes: async def read(self) -> bytes:
"""Reads the files content into bytes""" """Reads the files content into bytes"""
return await self.state.http.request_file(self.url) return await self.state.http.request_file(self.url)
async def save(self, fp: IOBase): async def save(self, fp: IOBase) -> None:
"""Reads the files content and saves it to a file """Reads the files content and saves it to a file
Parameters Parameters
----------- -----------
fp: IOBase fp: IOBase
The file to write too. The file to write to
""" """
fp.write(await self.read()) fp.write(await self.read())
@property class PartialAsset(Asset):
def url(self) -> str: """Partial asset for when we get limited data about the asset
"""Returns the url for the asset
.. note:: This can error if autumn is disabled on the instance of revolt
Returns
--------
:class:`str`
The url
Raises Attributes
------- -----------
:class:`AutumnDisabled` id: :class:`str`
Raises if autumn is disabled The id of the asset, this will always be ``"0"``
size: :class:`int`
""" Amount of bytes in the file, this will always be ``0``
enabled = self.state.api_info["features"]["autumn"]["enabled"] filename: :class:`str`
The name of the file, this be always be ``""``
if not enabled: height: Optional[:class:`int`]
raise AutumnDisabled The height of the file if it is an image or video, this will always be ``None``
width: Optional[:class:`int`]
The width of the file if it is an image or video, this will always be ``None``
content_type: Optional[:class:`str`]
The content type of the file, this is guessed from the url's file extension if it has one
type: :class:`AssetType`
The type of asset it is, this always be ``AssetType.file``
"""
base_url = self.state.api_info["features"]["autumn"]["url"] def __init__(self, url: str, state: State):
self.state: State = state
return f"{base_url}/{self.tag}/{self.id}" self.id: str = "0"
self.size: int = 0
self.filename: str = ""
self.height: int | None = None
self.width: int | None = None
self.content_type: str | None = mimetypes.guess_extension(url)
self.type: AssetType = AssetType.file
self.url: str = url
+36
View File
@@ -0,0 +1,36 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from .utils import Ulid
if TYPE_CHECKING:
from .channel import Channel
from .state import State
from .types import Category as CategoryPayload
__all__ = ("Category",)
class Category(Ulid):
"""Represents a category in a server that stores channels.
Attributes
-----------
name: :class:`str`
The name of the category
id: :class:`str`
The id of the category
channel_ids: list[:class:`str`]
The ids of channels that are inside the category
"""
def __init__(self, data: CategoryPayload, state: State):
self.state: State = state
self.name: str = data["title"]
self.id: str = data["id"]
self.channel_ids: list[str] = data["channels"]
@property
def channels(self) -> list[Channel]:
"""Returns a list of channels that the category contains"""
return [self.state.get_channel(channel_id) for channel_id in self.channel_ids]
+335 -99
View File
@@ -1,10 +1,12 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, Optional, cast from typing import TYPE_CHECKING, Any, Optional, Union
from .asset import Asset
from .enums import ChannelType from .enums import ChannelType
from .messageable import Messageable from .messageable import Messageable
from .permissions import ChannelPermissions from .permissions import Permissions, PermissionsOverwrite
from .utils import Missing, Ulid
if TYPE_CHECKING: if TYPE_CHECKING:
from .message import Message from .message import Message
@@ -13,40 +15,106 @@ if TYPE_CHECKING:
from .state import State from .state import State
from .types import Channel as ChannelPayload from .types import Channel as ChannelPayload
from .types import DMChannel as DMChannelPayload from .types import DMChannel as DMChannelPayload
from .types import Group as GroupDMChannelPayload from .types import File as FilePayload
from .types import GroupDMChannel as GroupDMChannelPayload
from .types import Overwrite as OverwritePayload
from .types import SavedMessages as SavedMessagesPayload from .types import SavedMessages as SavedMessagesPayload
from .types import ServerChannel as ServerChannelPayload
from .types import TextChannel as TextChannelPayload from .types import TextChannel as TextChannelPayload
from .types import VoiceChannel as VoiceChannelPayload
from .user import User from .user import User
__all__ = ("Channel",) __all__ = ("DMChannel", "GroupDMChannel", "SavedMessageChannel", "TextChannel", "VoiceChannel", "Channel", "ServerChannel")
class Channel: class EditableChannel:
__slots__ = ()
state: State
id: str
async def edit(self, **kwargs: Any) -> None:
"""Edits the channel
Passing ``None`` to the parameters that accept it will remove them.
Parameters
-----------
name: str
The new name for the channel
description: Optional[str]
The new description for the channel
owner: User
The new owner for the group dm channel
icon: Optional[File]
The new icon for the channel
nsfw: bool
Sets whether the channel is nsfw or not
"""
remove: list[str] = []
if kwargs.get("icon", Missing) == None:
remove.append("Icon")
if kwargs.get("description", Missing) == None:
remove.append("Description")
if icon := kwargs.get("icon"):
asset = await self.state.http.upload_file(icon, "icons")
kwargs["icon"] = asset["id"]
if owner := kwargs.get("owner"):
kwargs["owner"] = owner.id
await self.state.http.edit_channel(self.id, remove, kwargs)
class Channel(Ulid):
"""Base class for all channels """Base class for all channels
Attributes Attributes
----------- -----------
id: :class:`str` id: :class:`str`
The id of the channel The id of the channel
channel_type: ChannelType channel_type: ChannelType
The type of the channel The type of the channel
server: Optional[:class:`Server`] server_id: Optional[:class:`str`]
The server the channel is part of The server id of the chanel, if any
""" """
__slots__ = ("state", "id", "channel_type", "server_id") __slots__ = ("state", "id", "channel_type", "server_id")
def __init__(self, data: ChannelPayload, state: State): def __init__(self, data: ChannelPayload, state: State):
self.state = state self.state: State = state
self.id = data["_id"] self.id: str = data["_id"]
self.channel_type = ChannelType(data["channel_type"]) self.channel_type: ChannelType = ChannelType(data["channel_type"])
self.server_id = "" self.server_id: Optional[str] = None
async def _get_channel_id(self) -> str:
return self.id
def _update(self, **_: Any) -> None:
pass
async def delete(self) -> None:
"""Deletes or closes the channel"""
await self.state.http.close_channel(self.id)
@property @property
def server(self) -> Server: def server(self) -> Server:
""":class:`Server` The server this voice channel belongs too
Raises
-------
:class:`LookupError`
Raises if the channel is not part of a server
"""
if not self.server_id:
raise LookupError
return self.state.get_server(self.server_id) return self.state.get_server(self.server_id)
def _update(self): @property
pass def mention(self) -> str:
""":class:`str`: Returns a string that allows you to mention the given channel."""
return f"<#{self.id}>"
class SavedMessageChannel(Channel, Messageable): class SavedMessageChannel(Channel, Messageable):
"""The Saved Message Channel""" """The Saved Message Channel"""
@@ -54,130 +122,298 @@ class SavedMessageChannel(Channel, Messageable):
super().__init__(data, state) super().__init__(data, state)
class DMChannel(Channel, Messageable): class DMChannel(Channel, Messageable):
"""A DM channel""" """A DM channel
Attributes
-----------
last_message_id: Optional[:class:`str`]
The id of the last message in this channel, if any
"""
__slots__ = ("last_message_id", "recipient_ids")
def __init__(self, data: DMChannelPayload, state: State): def __init__(self, data: DMChannelPayload, state: State):
super().__init__(data, state) super().__init__(data, state)
class GroupDMChannel(Channel, Messageable): self.recipient_ids: list[str] = data["recipients"]
__slots__ = ("recipients", "name", "owner", "permissions") self.last_message_id: str | None = data.get("last_message_id")
@property
def recipients(self) -> tuple[User, User]:
a, b = self.recipient_ids
return (self.state.get_user(a), self.state.get_user(b))
@property
def recipient(self) -> User:
if self.recipient_ids[0] != self.state.user_id:
user_id = self.recipient_ids[0]
else:
user_id = self.recipient_ids[1]
return self.state.get_user(user_id)
@property
def last_message(self) -> Message:
"""Gets the last message from the channel, shorthand for `client.get_message(channel.last_message_id)`
Returns
--------
:class:`Message` the last message in the channel
"""
if not self.last_message_id:
raise LookupError
return self.state.get_message(self.last_message_id)
class GroupDMChannel(Channel, Messageable, EditableChannel):
"""A group DM channel
Attributes
-----------
recipients: list[:class:`User`]
The recipients of the group dm channel
name: :class:`str`
The name of the group dm channel
owner: :class:`User`
The user who created the group dm channel
icon: Optional[:class:`Asset`]
The icon of the group dm channel
permissions: :class:`ChannelPermissions`
The permissions of the users inside the group dm channel
description: Optional[:class:`str`]
The description of the channel, if any
last_message_id: Optional[:class:`str`]
The id of the last message in this channel, if any
"""
__slots__ = ("recipient_ids", "name", "owner_id", "permissions", "icon", "description", "last_message_id")
"""A group DM channel"""
def __init__(self, data: GroupDMChannelPayload, state: State): def __init__(self, data: GroupDMChannelPayload, state: State):
super().__init__(data, state) super().__init__(data, state)
self.recipients = [state.get_user(user_id) for user_id in data["recipients"]] self.recipient_ids: list[str] = data["recipients"]
self.name = data["name"] self.name: str = data["name"]
self.owner = state.get_user(data["owner"]) self.owner_id: str = data["owner"]
self.description: str | None = data.get("description")
self.last_message_id: str | None = data.get("last_message_id")
if perms := data.get("permissions"): self.icon: Asset | None
self.permissions = ChannelPermissions(perms)
def _update(self, *, name: Optional[str] = None, recipients: Optional[list[str]] = None): if icon := data.get("icon"):
if name: self.icon = Asset(icon, state)
else:
self.icon = None
self.permissions: Permissions = Permissions(data.get("permissions", 0))
def _update(self, *, name: Optional[str] = None, recipients: Optional[list[str]] = None, description: Optional[str] = None) -> None:
if name is not None:
self.name = name self.name = name
if recipients:
self.recipients = [self.state.get_user(user_id) for user_id in recipients]
async def set_default_permissions(self, permissions: ChannelPermissions) -> None: if recipients is not None:
self.recipient_ids = recipients
if description is not None:
self.description = description
@property
def recipients(self) -> list[User]:
return [self.state.get_user(user_id) for user_id in self.recipient_ids]
@property
def owner(self) -> User:
return self.state.get_user(self.owner_id)
async def set_default_permissions(self, permissions: Permissions) -> None:
"""Sets the default permissions for a group. """Sets the default permissions for a group.
Parameters Parameters
----------- -----------
permissions: :class:`ChannelPermissions` permissions: :class:`ChannelPermissions`
The new default group permissions The new default group permissions
""" """
await self.state.http.set_channel_default_permissions(self.id, permissions.value) await self.state.http.set_group_channel_default_permissions(self.id, permissions.value)
class TextChannel(Channel, Messageable):
__slots__ = ("name", "description", "last_message_id", "server_id", "default_permissions", "role_permissions")
"""A text channel"""
def __init__(self, data: TextChannelPayload, state: State):
super().__init__(data, state)
self.server_id = data["server"]
self.name = data["name"]
self.description = data.get("description")
last_message_id = data.get("last_message")
self.last_message_id = last_message_id
if perms := data.get("default_permissions"):
self.default_permissions = ChannelPermissions(perms)
if role_perms := data.get("role_permissions"):
self.role_permissions = {role_id: ChannelPermissions(perms) for role_id, perms in role_perms.items()}
@property @property
def last_message(self) -> Message: def last_message(self) -> Message:
"""Gets the last message from the channel, shorthand for `client.get_message(channel.last_message_id)`
Returns
--------
:class:`Message` the last message in the channel
"""
if not self.last_message_id:
raise LookupError
return self.state.get_message(self.last_message_id) return self.state.get_message(self.last_message_id)
def _update(self, *, name: Optional[str] = None, description: Optional[str] = None): class ServerChannel(Channel):
if name: """Base class for all guild channels
self.name = name
if description: Attributes
self.description = description -----------
server_id: :class:`str`
async def set_default_permissions(self, permissions: ChannelPermissions) -> None: The id of the server this text channel belongs to
"""Sets the default permissions for a channel. name: :class:`str`
Parameters The name of the text channel
----------- description: Optional[:class:`str`]
permissions: :class:`ChannelPermissions` The description of the channel, if any
The new default channel permissions nsfw: bool
""" Sets whether the channel is nsfw or not
await self.state.http.set_channel_default_permissions(self.id, permissions.value) default_permissions: :class:`ChannelPermissions`
The default permissions for all users in the text channel
async def set_role_permissions(self, role: Role, permissions: ChannelPermissions) -> None: """
"""Sets the permissions for a role in a channel. def __init__(self, data: ServerChannelPayload, state: State):
Parameters
-----------
permissions: :class:`ChannelPermissions`
The new channel permissions
"""
await self.state.http.set_channel_role_permissions(self.id, role.id, permissions.value)
class VoiceChannel(Channel):
"""A voice channel"""
def __init__(self, data: VoiceChannelPayload, state: State):
super().__init__(data, state) super().__init__(data, state)
self.server_id = data["server"] self.server_id: Optional[str] = data["server"]
self.name = data["name"] self.name: str = data["name"]
self.description = data.get("description") self.description: Optional[str] = data.get("description")
self.nsfw: bool = data.get("nsfw", False)
self.active: bool = False
self.default_permissions: PermissionsOverwrite = PermissionsOverwrite._from_overwrite(data.get("default_permissions", {"a": 0, "d": 0}))
if perms := data.get("default_permissions"): permissions: dict[str, PermissionsOverwrite] = {}
self.default_permissions = ChannelPermissions(perms)
if role_perms := data.get("role_permissions"): for role_name, overwrite_data in data.get("role_permissions", {}).items():
self.role_permissions = {role_id: ChannelPermissions(perms) for role_id, perms in role_perms.items()} overwrite = PermissionsOverwrite._from_overwrite(overwrite_data)
permissions[role_name] = overwrite
def _update(self, *, name: Optional[str] = None, description: Optional[str] = None): self.permissions: dict[str, PermissionsOverwrite] = permissions
if name:
self.name = name
if description: self.icon: Asset | None
self.description = description
if icon := data.get("icon"):
self.icon = Asset(icon, state)
else:
self.icon = None
async def set_default_permissions(self, permissions: PermissionsOverwrite) -> None:
"""Sets the default permissions for the channel.
async def set_default_permissions(self, permissions: ChannelPermissions) -> None:
"""Sets the default permissions for a voice channel.
Parameters Parameters
----------- -----------
permissions: :class:`ChannelPermissions` permissions: :class:`PermissionsOverwrite`
The new default channel permissions The new default channel permissions
""" """
await self.state.http.set_channel_default_permissions(self.id, permissions.value) allow, deny = permissions.to_pair()
await self.state.http.set_guild_channel_default_permissions(self.id, allow.value, deny.value)
async def set_role_permissions(self, role: Role, permissions: PermissionsOverwrite) -> None:
"""Sets the permissions for a role in the channel.
async def set_role_permissions(self, role: Role, permissions: ChannelPermissions) -> None:
"""Sets the permissions for a role in a voice channel
Parameters Parameters
----------- -----------
role: :class:`Role`
The role to set permissions for
permissions: :class:`ChannelPermissions` permissions: :class:`ChannelPermissions`
The new channel permissions The new channel permissions
""" """
await self.state.http.set_channel_role_permissions(self.id, role.id, permissions.value) allow, deny = permissions.to_pair()
def channel_factory(data: ChannelPayload, state: State) -> Channel: await self.state.http.set_guild_channel_role_permissions(self.id, role.id, allow.value, deny.value)
if data["channel_type"] == "SavedMessage":
def _update(self, *, name: Optional[str] = None, description: Optional[str] = None, icon: Optional[FilePayload] = None, nsfw: Optional[bool] = None, active: Optional[bool] = None, role_permissions: Optional[dict[str, OverwritePayload]] = None, default_permissions: Optional[OverwritePayload] = None):
if name is not None:
self.name = name
if description is not None:
self.description = description
if icon is not None:
self.icon = Asset(icon, self.state)
if nsfw is not None:
self.nsfw = nsfw
if active is not None:
self.active = active
if role_permissions is not None:
permissions = {}
for role_name, overwrite_data in role_permissions.items():
overwrite = PermissionsOverwrite._from_overwrite(overwrite_data)
permissions[role_name] = overwrite
self.permissions = permissions
if default_permissions is not None:
self.default_permissions = PermissionsOverwrite._from_overwrite(default_permissions)
class TextChannel(ServerChannel, Messageable, EditableChannel):
"""A text channel
Subclasses :class:`ServerChannel` and :class:`Messageable`
Attributes
-----------
name: :class:`str`
The name of the text channel
server_id: :class:`str`
The id of the server this text channel belongs to
last_message_id: Optional[:class:`str`]
The id of the last message in this channel, if any
default_permissions: :class:`ChannelPermissions`
The default permissions for all users in the text channel
role_permissions: dict[:class:`str`, :class:`ChannelPermissions`]
A dictionary of role id's to the permissions of that role in the text channel
icon: Optional[:class:`Asset`]
The icon of the text channel, if any
description: Optional[:class:`str`]
The description of the channel, if any
"""
__slots__ = ("name", "description", "last_message_id", "default_permissions", "icon", "overwrites")
def __init__(self, data: TextChannelPayload, state: State):
super().__init__(data, state)
self.last_message_id: str | None = data.get("last_message_id")
async def _get_channel_id(self) -> str:
return self.id
@property
def last_message(self) -> Message:
"""Gets the last message from the channel, shorthand for `client.get_message(channel.last_message_id)`
Returns
--------
:class:`Message` the last message in the channel
"""
if not self.last_message_id:
raise LookupError
return self.state.get_message(self.last_message_id)
class VoiceChannel(ServerChannel, EditableChannel):
"""A voice channel
Subclasses :class:`ServerChannel`
Attributes
-----------
name: :class:`str`
The name of the voice channel
server_id: :class:`str`
The id of the server this voice channel belongs to
last_message_id: Optional[:class:`str`]
The id of the last message in this channel, if any
default_permissions: :class:`ChannelPermissions`
The default permissions for all users in the voice channel
role_permissions: dict[:class:`str`, :class:`ChannelPermissions`]
A dictionary of role id's to the permissions of that role in the voice channel
icon: Optional[:class:`Asset`]
The icon of the voice channel, if any
description: Optional[:class:`str`]
The description of the channel, if any
"""
def channel_factory(data: ChannelPayload, state: State) -> Union[DMChannel, GroupDMChannel, SavedMessageChannel, TextChannel, VoiceChannel]:
if data["channel_type"] == "SavedMessages":
return SavedMessageChannel(data, state) return SavedMessageChannel(data, state)
elif data["channel_type"] == "DirectMessage": elif data["channel_type"] == "DirectMessage":
return DMChannel(data, state) return DMChannel(data, state)
+444 -44
View File
@@ -2,13 +2,23 @@ from __future__ import annotations
import asyncio import asyncio
import logging import logging
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Optional, TypeVar from typing import TYPE_CHECKING, Any, Callable, Coroutine, Literal, Optional, TypeVar, Union, cast, overload
from typing_extensions import ParamSpec
import aiohttp import aiohttp
from .errors import RevoltError
from .channel import (DMChannel, GroupDMChannel, SavedMessageChannel,
TextChannel, VoiceChannel, channel_factory)
from .http import HttpClient from .http import HttpClient
from .invite import Invite
from .message import Message
from .state import State from .state import State
from .utils import Missing, Ulid
from .websocket import WebsocketHandler from .websocket import WebsocketHandler
from .emoji import Emoji
from .server import Server
from .user import User
try: try:
import ujson as json import ujson as json
@@ -17,18 +27,21 @@ except ImportError:
if TYPE_CHECKING: if TYPE_CHECKING:
from .channel import Channel from .channel import Channel
from .server import Server from .file import File
from .types import ApiInfo from .types import ApiInfo
from .user import User
import revolt
__all__ = ("Client",) __all__ = ("Client",)
logger = logging.getLogger("revolt") logger: logging.Logger = logging.getLogger("revolt")
P = ParamSpec("P")
R = TypeVar("R")
class Client: class Client:
"""The client for interacting with revolt """The client for interacting with revolt
Parameters Parameters
----------- -----------
session: :class:`aiohttp.ClientSession` session: :class:`aiohttp.ClientSession`
@@ -39,26 +52,30 @@ class Client:
The api url for the revolt instance you are connecting to, by default it uses the offical instance hosted at revolt.chat The api url for the revolt instance you are connecting to, by default it uses the offical instance hosted at revolt.chat
max_messages: :class:`int` max_messages: :class:`int`
The max amount of messages stored in the cache, by default this is 5k The max amount of messages stored in the cache, by default this is 5k
bot: :class:`bool`
Denotes whether the account used is a bot account or user account, by default this it assumes a bot account
""" """
def __init__(self, session: aiohttp.ClientSession, token: str, api_url: str = "https://api.revolt.chat", max_messages: int = 5000): def __init__(self, session: aiohttp.ClientSession, token: str, *, api_url: str = "https://api.revolt.chat", max_messages: int = 5000, bot: bool = True):
self.session = session self.session: aiohttp.ClientSession = session
self.token = token self.token: str = token
self.api_url = api_url self.api_url: str = api_url
self.max_messages = max_messages self.max_messages: int = max_messages
self.bot: bool = bot
self.api_info: ApiInfo self.api_info: ApiInfo
self.http: HttpClient self.http: HttpClient
self.state: State self.state: State
self.websocket: WebsocketHandler self.websocket: WebsocketHandler
self.listeners: dict[str, list[tuple[Callable[..., bool], asyncio.Future[Any]]]] = {} self.temp_listeners: dict[str, list[tuple[Callable[..., bool], asyncio.Future[Any]]]] = {}
self.listeners: dict[str, list[Callable[..., Coroutine[Any, Any, Any]]]] = {}
super().__init__() super().__init__()
def dispatch(self, event: str, *args: Any): def dispatch(self, event: str, *args: Any) -> None:
"""Dispatch an event, this is typically used for testing and internals. """Dispatch an event, this is typically used for testing and internals.
Parameters Parameters
---------- ----------
event: class:`str` event: class:`str`
@@ -66,79 +83,94 @@ class Client:
args: :class:`Any` args: :class:`Any`
The arguments passed to the event The arguments passed to the event
""" """
for check, future in self.listeners.pop(event, []):
if check(*args):
if len(args) == 1:
future.set_result(args[0])
else:
future.set_result(args)
func = getattr(self, f"on_{event}", None) if temp_listeners := self.temp_listeners.get(event, None):
if func: for check, future in temp_listeners:
if check(*args):
if len(args) == 1:
future.set_result(args[0])
else:
future.set_result(args)
self.temp_listeners[event] = [(c, f) for c, f in temp_listeners if not f.done()]
for listener in self.listeners.get(event, []):
asyncio.create_task(listener(*args))
if func := getattr(self, f"on_{event}", None):
asyncio.create_task(func(*args)) asyncio.create_task(func(*args))
async def get_api_info(self) -> ApiInfo: async def get_api_info(self) -> ApiInfo:
async with self.session.get(self.api_url) as resp: async with self.session.get(self.api_url) as resp:
return json.loads(await resp.text()) text = await resp.text()
async def start(self): try:
return json.loads(text)
except:
raise RevoltError(f"Cant fetch api info:\n{text}")
async def start(self, *, reconnect: bool = True) -> None:
"""Starts the client""" """Starts the client"""
api_info = await self.get_api_info() api_info = await self.get_api_info()
self.api_info = api_info self.api_info = api_info
self.http = HttpClient(self.session, self.token, self.api_url, self.api_info) self.http = HttpClient(self.session, self.token, self.api_url, self.api_info, self.bot)
self.state = State(self.http, api_info, self.max_messages) self.state = State(self.http, api_info, self.max_messages)
self.websocket = WebsocketHandler(self.session, self.token, api_info["ws"], self.dispatch, self.state) self.websocket = WebsocketHandler(self.session, self.token, api_info["ws"], self.dispatch, self.state)
await self.websocket.start()
def get_user(self, id: str) -> Optional[User]: await self.websocket.start(reconnect)
async def stop(self) -> None:
await self.websocket.websocket.close()
def get_user(self, id: str) -> User:
"""Gets a user from the cache """Gets a user from the cache
Parameters Parameters
----------- -----------
id: :class:`str` id: :class:`str`
The id of the user The id of the user
Returns Returns
-------- --------
Optional[:class:`User`] :class:`User`
The user if found The user
""" """
return self.state.get_user(id) return self.state.get_user(id)
def get_channel(self, id: str) -> Optional[Channel]: def get_channel(self, id: str) -> Channel:
"""Gets a channel from the cache """Gets a channel from the cache
Parameters Parameters
----------- -----------
id: :class:`str` id: :class:`str`
The id of the channel The id of the channel
Returns Returns
-------- --------
Optional[:class:`Channel`] :class:`Channel`
The channel if found The channel
""" """
return self.state.get_channel(id) return self.state.get_channel(id)
def get_server(self, id: str) -> Optional[Server]: def get_server(self, id: str) -> Server:
"""Gets a server from the cache """Gets a server from the cache
Parameters Parameters
----------- -----------
id: :class:`str` id: :class:`str`
The id of the server The id of the server
Returns Returns
-------- --------
Optional[:class:`Server`] :class:`Server`
The server if found The server
""" """
return self.state.get_server(id) return self.state.get_server(id)
async def wait_for(self, event: str, *, check: Optional[Callable[..., bool]] = None, timeout: Optional[float] = None) -> Any: async def wait_for(self, event: str, *, check: Optional[Callable[..., bool]] = None, timeout: Optional[float] = None) -> Any:
"""Waits for an event """Waits for an event
Parameters Parameters
----------- -----------
event: :class:`str` event: :class:`str`
@@ -162,6 +194,374 @@ class Client:
check = lambda *_: True check = lambda *_: True
future = asyncio.get_running_loop().create_future() future = asyncio.get_running_loop().create_future()
self.listeners.setdefault(event, []).append((check, future)) self.temp_listeners.setdefault(event, []).append((check, future))
return await asyncio.wait_for(future, timeout) return await asyncio.wait_for(future, timeout)
def listen(self, name: str | None = None) -> Callable[[Callable[P, Coroutine[Any, Any, R]]], Callable[P, Coroutine[Any, Any, R]]]:
"""Registers a listener for an event, multiple listeners can be registered to the same event without conflict
Parameters
-----------
name: Optional[:class:`str`]
The name of the event to register this under, this defaults to the function's name
"""
def inner(func: Callable[P, Coroutine[Any, Any, R]]) -> Callable[P, Coroutine[Any, Any, R]]:
nonlocal name
if not name:
if not func.__name__.startswith("on_"):
raise RevoltError("listener name must begin with `on_`")
name = func.__name__[3:]
self.listeners.setdefault(name, []).append(func)
return func
return inner
@overload
def remove_listener(self, func: Callable[P, Coroutine[Any, Any, R]], *, event: str = ...) -> Callable[..., Coroutine[Any, Any, R]] | None:
...
@overload
def remove_listener(self, func: Callable[P, Coroutine[Any, Any, Any]], *, event: None = ...) -> None:
...
def remove_listener(self, func: Callable[P, Coroutine[Any, Any, R]], *, event: str | None = None) -> Callable[..., Coroutine[Any, Any, R]] | None:
"""Removes a listener registered, if the `event` parameter is passed, the listener will only be removed from that event, this can be used if the same listener is registed to multiple events at once.
Parameters
-----------
func: Callable
The function for the listener to be removed
event: Optional[:class:`str`]
The name of the event to remove this from, passing `None` will make this remove the listener from all events this is registered under
"""
if event is None:
for listeners in self.listeners.values():
try:
listeners.remove(func)
except ValueError:
pass
else:
try:
self.listeners[event].remove(func)
return func
except ValueError:
pass
@property
def user(self) -> User:
""":class:`User` the user corrasponding to the client"""
user = self.websocket.user
assert user
return user
@property
def users(self) -> list[User]:
"""list[:class:`User`] All users the client can see"""
return list(self.state.users.values())
@property
def servers(self) -> list[Server]:
"""list[:class:'Server'] All servers the client can see"""
return list(self.state.servers.values())
@property
def global_emojis(self) -> list[Emoji]:
return self.state.global_emojis
async def fetch_user(self, user_id: str) -> User:
"""Fetchs a user
Parameters
-----------
user_id: :class:`str`
The id of the user you are fetching
Returns
--------
:class:`User`
The user with the matching id
"""
payload = await self.http.fetch_user(user_id)
return User(payload, self.state)
async def fetch_dm_channels(self) -> list[Union[DMChannel, GroupDMChannel]]:
"""Fetchs all dm channels the client has made
Returns
--------
list[Union[:class:`DMChanel`, :class:`GroupDMChannel`]]
A list of :class:`DMChannel` or :class`GroupDMChannel`
"""
channel_payloads = await self.http.fetch_dm_channels()
return cast(list[Union[DMChannel, GroupDMChannel]], [channel_factory(payload, self.state) for payload in channel_payloads])
async def fetch_channel(self, channel_id: str) -> Union[DMChannel, GroupDMChannel, SavedMessageChannel, TextChannel, VoiceChannel]:
"""Fetches a channel
Parameters
-----------
channel_id: :class:`str`
The id of the channel
Returns
--------
Union[:class:`DMChannel`, :class:`GroupDMChannel`, :class:`SavedMessageChannel`, :class:`TextChannel`, :class:`VoiceChannel`]
The channel with the matching id
"""
payload = await self.http.fetch_channel(channel_id)
return channel_factory(payload, self.state)
async def fetch_server(self, server_id: str) -> Server:
"""Fetchs a server
Parameters
-----------
server_id: :class:`str`
The id of the server you are fetching
Returns
--------
:class:`Server`
The server with the matching id
"""
payload = await self.http.fetch_server(server_id)
return Server(payload, self.state)
async def fetch_invite(self, code: str) -> Invite:
"""Fetchs an invite
Parameters
-----------
code: :class:`str`
The code of the invite you are fetching
Returns
--------
:class:`Invite`
The invite with the matching code
"""
payload = await self.http.fetch_invite(code)
return Invite(payload, code, self.state)
def get_message(self, message_id: str) -> Message:
"""Gets a message from the cache
Parameters
-----------
message_id: :class:`str`
The id of the message you are getting
Returns
--------
:class:`Message`
The message with the matching id
Raises
-------
LookupError
This raises if the message is not found in the cache
"""
for message in self.state.messages:
if message.id == message_id:
return message
raise LookupError
async def edit_self(self, **kwargs: Any) -> None:
"""Edits the client's own user
Parameters
-----------
avatar: Optional[:class:`File`]
The avatar to change to, passing in ``None`` will remove the avatar
"""
if kwargs.get("avatar", Missing) is None:
del kwargs["avatar"]
remove = ["Avatar"]
else:
remove = None
await self.state.http.edit_self(remove, kwargs)
async def edit_status(self, **kwargs: Any) -> None:
"""Edits the client's own status
Parameters
-----------
presence: :class:`PresenceType`
The presence to change to
text: Optional[:class:`str`]
The text to change the status to, passing in ``None`` will remove the status
"""
if kwargs.get("text", Missing) is None:
del kwargs["text"]
remove = ["StatusText"]
else:
remove = None
if presence := kwargs.get("presence"):
kwargs["presence"] = presence.value
await self.state.http.edit_self(remove, {"status": kwargs})
async def edit_profile(self, **kwargs: Any) -> None:
"""Edits the client's own profile
Parameters
-----------
content: Optional[:class:`str`]
The new content for the profile, passing in ``None`` will remove the profile content
background: Optional[:class:`File`]
The new background for the profile, passing in ``None`` will remove the profile background
"""
remove: list[str] = []
if kwargs.get("content", Missing) is None:
del kwargs["content"]
remove.append("ProfileContent")
if kwargs.get("background", Missing) is None:
del kwargs["background"]
remove.append("ProfileBackground")
await self.state.http.edit_self(remove, {"profile": kwargs})
async def fetch_emoji(self, emoji_id: str) -> Emoji:
"""Fetches an emoji
Parameters
-----------
emoji_id: str
The id of the emoji
Returns
--------
:class:`Emoji`
The emoji with the corrasponding id
"""
emoji = await self.state.http.fetch_emoji(emoji_id)
return Emoji(emoji, self.state)
async def upload_file(self, file: File, tag: Literal['attachments', 'avatars', 'backgrounds', 'icons', 'banners', 'emojis']) -> Ulid:
"""Uploads a file to revolt
Parameters
-----------
file: :class:`File`
The file to upload
tag: :class:`str`
The type of file to upload, this should a string of either `'attachments'`, `'avatars'`, `'backgrounds'`, `'icons'`, `'banners'` or `'emojis'`
Returns
--------
:class:`Ulid`
The id of the file that was uploaded
"""
asset = await self.http.upload_file(file, tag)
ulid = Ulid()
ulid.id = asset["id"]
return ulid
# events
async def on_ready(self) -> None:
pass
async def on_message(self, message: revolt.Message) -> None:
pass
async def on_raw_message_update(self, payload: revolt.types.MessageUpdateEventPayload) -> None:
pass
async def on_message_update(self, before: revolt.Message, after: revolt.Message) -> None:
pass
async def on_raw_message_delete(self, payload: revolt.types.MessageDeleteEventPayload) -> None:
pass
async def on_message_delete(self, message: revolt.Message) -> None:
pass
async def on_channel_create(self, channel: revolt.Channel) -> None:
pass
async def on_channel_update(self, before: revolt.Channel, after: revolt.Channel) -> None:
pass
async def on_channel_delete(self, channel: revolt.Channel) -> None:
pass
async def on_typing_start(self, channel: revolt.Channel, user: revolt.User) -> None:
pass
async def on_typing_stop(self, channel: revolt.Channel, user: revolt.User) -> None:
pass
async def on_server_update(self, before: revolt.Server, after: revolt.Server) -> None:
pass
async def on_server_delete(self, server: revolt.Server) -> None:
pass
async def on_server_join(self, server: revolt.Server) -> None:
pass
async def on_member_update(self, before: revolt.Member, after: revolt.Member) -> None:
pass
async def on_member_join(self, member: revolt.Member) -> None:
pass
async def on_member_leave(self, member: revolt.Member) -> None:
pass
async def on_role_create(self, role: revolt.Role) -> None:
pass
async def on_role_update(self, before: revolt.Role, after: revolt.Role) -> None:
pass
async def on_role_delete(self, role: revolt.Role) -> None:
pass
async def on_user_update(self, before: revolt.User, after: revolt.User) -> None:
pass
async def on_user_relationship_update(self, user: revolt.User, before: revolt.RelationshipType, after: revolt.RelationshipType) -> None:
pass
async def on_raw_reaction_add(self, payload: revolt.types.MessageReactEventPayload) -> None:
pass
async def on_reaction_add(self, message: revolt.Message, user: revolt.User, emoji_id: str) -> None:
pass
async def on_raw_reaction_remove(self, payload: revolt.types.MessageUnreactEventPayload) -> None:
pass
async def on_reaction_remove(self, message: revolt.Message, user: revolt.User, emoji_id: str) -> None:
pass
async def on_raw_reaction_clear(self, payload: revolt.types.MessageRemoveReactionEventPayload) -> None:
pass
async def on_reaction_clear(self, message: revolt.Message, user: revolt.User, emoji_id: str) -> None:
pass
async def raw_bulk_message_delete(self, payload: revolt.types.BulkMessageDeleteEventPayload) -> None:
pass
async def bulk_message_delete(self, messages: list[revolt.Message]) -> None:
pass
+141 -8
View File
@@ -1,17 +1,150 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Optional, TypedDict, Union
from typing_extensions import NotRequired, Unpack
from revolt.types.embed import WebsiteSpecial
from .asset import Asset
from .enums import EmbedType
if TYPE_CHECKING: if TYPE_CHECKING:
from .state import State
from .types import Embed as EmbedPayload from .types import Embed as EmbedPayload
from .types import ImageEmbed as ImageEmbedPayload
from .types import SendableEmbed as SendableEmbedPayload
from .types import TextEmbed as TextEmbedPayload
from .types import WebsiteEmbed as WebsiteEmbedPayload
from .types import JanuaryImage, JanuaryVideo
__all__ = ("Embed", "WebsiteEmbed", "ImageEmbed", "TextEmbed", "NoneEmbed", "to_embed", "SendableEmbed")
__all__ = ("Embed",) class WebsiteEmbed:
type = EmbedType.website
class Embed: def __init__(self, embed: WebsiteEmbedPayload):
@classmethod self.url: str | None = embed.get("url")
def from_dict(cls, data: EmbedPayload): self.special: WebsiteSpecial | None = embed.get("special")
... self.title: str | None = embed.get("title")
self.description: str | None = embed.get("description")
self.image: JanuaryImage | None = embed.get("image")
self.video: JanuaryVideo | None = embed.get("video")
self.site_name: str | None = embed.get("site_name")
self.icon_url: str | None = embed.get("icon_url")
self.colour: str | None = embed.get("colour")
def to_dict(self) -> EmbedPayload: class ImageEmbed:
... type: EmbedType = EmbedType.image
def __init__(self, image: ImageEmbedPayload):
self.url: str = image.get("url")
self.width: int = image.get("width")
self.height: int = image.get("height")
self.size: str = image.get("size")
class TextEmbed:
type: EmbedType = EmbedType.text
def __init__(self, embed: TextEmbedPayload, state: State):
self.icon_url: str | None = embed.get("icon_url")
self.url: str | None = embed.get("url")
self.title: str | None = embed.get("title")
self.description: str | None = embed.get("description")
self.media: Asset | None
if media := embed.get("media"):
self.media = Asset(media, state)
else:
self.media = None
self.colour: str | None = embed.get("colour")
class NoneEmbed:
type: EmbedType = EmbedType.none
Embed = Union[WebsiteEmbed, ImageEmbed, TextEmbed, NoneEmbed]
def to_embed(payload: EmbedPayload, state: State) -> Embed:
if payload["type"] == "Website":
return WebsiteEmbed(payload)
elif payload["type"] == "Image":
return ImageEmbed(payload)
elif payload["type"] == "Text":
return TextEmbed(payload, state)
else:
return NoneEmbed()
class EmbedParameters(TypedDict):
title: NotRequired[str]
description: NotRequired[str]
media: NotRequired[str]
icon_url: NotRequired[str]
colour: NotRequired[str]
url: NotRequired[str]
class SendableEmbed:
"""
Represents an embed that can be sent in a message, you will never receive this, you will receive :class:`Embed`.
Attributes
-----------
title: Optional[:class:`str`]
The title of the embed
description: Optional[:class:`str`]
The description of the embed
media: Optional[:class:`str`]
The file inside the embed, this is the ID of the file, you can use :meth:`Client.upload_file` to get an ID.
icon_url: Optional[:class:`str`]
The url of the icon url
colour: Optional[:class:`str`]
The embed's accent colour, this is any valid `CSS color <https://developer.mozilla.org/en-US/docs/Web/CSS/color_value>`_
url: Optional[:class:`str`]
URL for hyperlinking the embed's title
"""
def __init__(self, **attrs: Unpack[EmbedParameters]):
self.title: Optional[str] = None
self.description: Optional[str] = None
self.media: Optional[str] = None
self.icon_url: Optional[str] = None
self.colour: Optional[str] = None
self.url: Optional[str] = None
for key, value in attrs.items():
setattr(self, key, value)
def to_dict(self) -> SendableEmbedPayload:
"""Converts the embed to a dictionary which Revolt accepts
Returns
--------
:class:`dict[str, Any]`
The embed
"""
output: SendableEmbedPayload = {"type": "Text"}
if title := self.title:
output["title"] = title
if description := self.description:
output["description"] = description
if media := self.media:
output["media"] = media
if icon_url := self.icon_url:
output["icon_url"] = icon_url
if colour := self.colour:
output["colour"] = colour
if url := self.url:
output["url"] = url
return output
+55
View File
@@ -0,0 +1,55 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from .utils import Ulid
if TYPE_CHECKING:
from .server import Server
from .state import State
from .types import Emoji as EmojiPayload
__all__ = ("Emoji",)
class Emoji(Ulid):
"""Represents a custom emoji.
Attributes
-----------
id: :class:`str`
The id of the emoji
author_id: :class:`str`
The id of the of user who created the emoji
name: :class:`str`
The name of the emoji
animated: :class:`bool`
Whether or not the emoji is animated
nsfw: :class:`bool`
Whether or not the emoji is nsfw
server_id: Optional[:class:`str`]
The server id this emoji belongs to, if any
"""
def __init__(self, payload: EmojiPayload, state: State):
self.state: State = state
self.id: str = payload["_id"]
self.author_id: str = payload["creator_id"]
self.name: str = payload["name"]
self.animated: bool = payload.get("animated", False)
self.nsfw: bool = payload.get("nsfw", False)
self.server_id: str | None = payload["parent"].get("id")
async def delete(self) -> None:
"""Deletes the emoji."""
await self.state.http.delete_emoji(self.id)
@property
def server(self) -> Server:
"""Returns the server this emoji is part of
Returns
--------
:class:`Server`
The Server this emoji is part of
"""
return self.state.get_server(self.server_id) # type: ignore
+10 -2
View File
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
# typing doesnt understand aenum so im pretending its stdlib enum while type checking # typing does not understand aenum so I am pretending its stdlib enum while type checking
if TYPE_CHECKING: if TYPE_CHECKING:
import enum import enum
@@ -14,10 +14,11 @@ __all__ = (
"RelationshipType", "RelationshipType",
"AssetType", "AssetType",
"SortType", "SortType",
"EmbedType"
) )
class ChannelType(enum.Enum): class ChannelType(enum.Enum):
saved_message = "SavedMessage" saved_messages = "SavedMessages"
direct_message = "DirectMessage" direct_message = "DirectMessage"
group = "Group" group = "Group"
text_channel = "TextChannel" text_channel = "TextChannel"
@@ -28,6 +29,7 @@ class PresenceType(enum.Enum):
idle = "Idle" idle = "Idle"
invisible = "Invisible" invisible = "Invisible"
online = "Online" online = "Online"
focus = "Focus"
class RelationshipType(enum.Enum): class RelationshipType(enum.Enum):
blocked = "Blocked" blocked = "Blocked"
@@ -49,3 +51,9 @@ class SortType(enum.Enum):
latest = "Latest" latest = "Latest"
oldest = "Oldest" oldest = "Oldest"
relevance = "Relevance" relevance = "Relevance"
class EmbedType(enum.Enum):
website = "Website"
image = "Image"
text = "Text"
none = "None"
+6 -2
View File
@@ -4,6 +4,7 @@ __all__ = (
"ServerError", "ServerError",
"FeatureDisabled", "FeatureDisabled",
"AutumnDisabled", "AutumnDisabled",
"Forbidden",
) )
class RevoltError(Exception): class RevoltError(Exception):
@@ -16,7 +17,10 @@ class ServerError(RevoltError):
"Internal server error" "Internal server error"
class FeatureDisabled(RevoltError): class FeatureDisabled(RevoltError):
"""Base class for any any feature disabled""" "Base class for any feature disabled errors"
class AutumnDisabled(FeatureDisabled): class AutumnDisabled(FeatureDisabled):
"""The autumn feature is disabled""" "The autumn feature is disabled"
class Forbidden(HTTPError):
"Missing permissions"
+7 -1
View File
@@ -1,4 +1,10 @@
from .checks import *
from .client import *
from .cog import *
from .command import * from .command import *
from .context import * from .context import *
from .converters import *
from .cooldown import *
from .errors import * from .errors import *
from .mixin import * from .group import *
from .help import *
+95
View File
@@ -0,0 +1,95 @@
from __future__ import annotations
from typing import Any, Callable, Coroutine, Union, cast
from typing_extensions import TypeVar
import revolt
from .command import Command
from .context import Context
from .errors import (MissingPermissionsError, NotBotOwner, NotServerOwner,
ServerOnly)
from .utils import ClientT_D
__all__ = ("check", "Check", "is_bot_owner", "is_server_owner", "has_permissions", "has_channel_permissions")
T = TypeVar("T", Callable[..., Any], Command, default=Command)
Check = Callable[[Context[ClientT_D]], Union[Any, Coroutine[Any, Any, Any]]]
def check(check: Check[ClientT_D]) -> Callable[[T], T]:
"""A decorator for adding command checks
Parameters
-----------
check: Callable[[Context], Union[Any, Coroutine[Any, Any, Any]]]
The function to be called, must take one parameter, context and optionally be a coroutine, the return value denoating whether the check should pass or fail
"""
def inner(func: T) -> T:
if isinstance(func, Command):
command = cast(Command[ClientT_D], func) # cant verify generic at runtime so must cast
command.checks.append(check)
else:
checks = getattr(func, "_checks", [])
checks.append(check)
func._checks = checks # type: ignore
return func # type: ignore
return inner
def is_bot_owner() -> Callable[[T], T]:
"""A command check for limiting the command to only the bot's owner"""
@check
def inner(context: Context[ClientT_D]):
if user_id := context.client.user.owner_id:
if context.author.id == user_id:
return True
else:
if context.author.id == context.client.user.id:
return True
raise NotBotOwner
return inner
def is_server_owner() -> Callable[[T], T]:
"""A command check for limiting the command to only a server's owner"""
@check
def inner(context: Context[ClientT_D]) -> bool:
if not context.server_id:
raise ServerOnly
if context.author.id == context.server.owner_id:
return True
raise NotServerOwner
return inner
def has_permissions(**permissions: bool) -> Callable[[T], T]:
@check
def inner(context: Context[ClientT_D]) -> bool:
author = context.author
if not author.has_permissions(**permissions):
raise MissingPermissionsError(permissions)
return True
return inner
def has_channel_permissions(**permissions: bool) -> Callable[[T], T]:
@check
def inner(context: Context[ClientT_D]) -> bool:
author = context.author
if not isinstance(author, revolt.Member):
raise ServerOnly
if not author.has_channel_permissions(context.channel, **permissions):
raise MissingPermissionsError(permissions)
return True
return inner
+419
View File
@@ -0,0 +1,419 @@
from __future__ import annotations
import sys
import traceback
from importlib import import_module
from typing import (TYPE_CHECKING, Any, Coroutine, Optional, Protocol, TypeVar, Union,
overload, runtime_checkable)
from typing_extensions import Self
import revolt
if TYPE_CHECKING:
from .help import HelpCommand
import aiohttp
from .cog import Cog
from .command import Command
from .context import Context
from .errors import CheckError, CommandNotFound, MissingSetup
from .view import StringView
__all__ = (
"CommandsMeta",
"CommandsClient"
)
V = TypeVar("V")
T = TypeVar("T")
@runtime_checkable
class ExtensionProtocol(Protocol):
@staticmethod
def setup(client: CommandsClient) -> None:
raise NotImplementedError
class CommandsMeta(type):
_commands: list[Command[Any]]
def __new__(cls, name: str, bases: tuple[type, ...], attrs: dict[str, Any]) -> Any:
commands: list[Command[Any]] = []
self = super().__new__(cls, name, bases, attrs)
for base in reversed(self.__mro__):
for value in base.__dict__.values():
if isinstance(value, Command) and value.parent is None: # type: ignore
commands.append(value) # type: ignore
self._commands = commands
return self
class CaseInsensitiveDict(dict[str, V]):
def __setitem__(self, key: str, value: V) -> None:
super().__setitem__(key.casefold(), value)
def __getitem__(self, key: str) -> V:
return super().__getitem__(key.casefold())
def __contains__(self, key: object) -> bool:
if isinstance(key, str):
return super().__contains__(key.casefold())
else:
return False
@overload
def get(self, key: str) -> V | None:
...
@overload
def get(self, key: str, default: V | T) -> V | T:
...
def get(self, key: str, default: Optional[T] = None) -> V | T | None:
return super().get(key.casefold(), default)
def __delitem__(self, key: str) -> None:
super().__delitem__(key.casefold())
class CommandsClient(revolt.Client, metaclass=CommandsMeta):
"""A subclass of :class:`~revolt.Client` which has support for commands.
Parameters
-----------
session: :class:`~aiohttp.ClientSession`
The aiohttp session to use for http request and the websocket
token: :class:`str`
The bots token
api_url: :class:`str`
The api url for the revolt instance you are connecting to, by default it uses the offical instance hosted at revolt.chat
max_messages: :class:`int`
The max amount of messages stored in the cache, by default this is 5k
bot: :class:`bool`
Denotes whether the account used is a bot account or user account, by default this it assumes a bot account
help_command: Optional[:class:`~revolt.ext.commands.HelpCommand`]
Sets the custom help command, or remove it if passed ``None``
case_insensitive: :class:`bool`
Whether or not commands should be case insensitive
"""
_commands: list[Command[Self]]
def __init__(
self,
session: aiohttp.ClientSession,
token: str,
*,
api_url: str = "https://api.revolt.chat",
max_messages: int = 5000,
bot: bool = True,
help_command: Union[HelpCommand[Self], None, revolt.utils._Missing] = revolt.utils.Missing,
case_insensitive: bool = False
):
from .help import DefaultHelpCommand, HelpCommandImpl
self.all_commands: dict[str, Command[Self]] | CaseInsensitiveDict[Command[Self]] = {} if not case_insensitive else CaseInsensitiveDict()
self.cogs: dict[str, Cog[Self]] = {}
self.extensions: dict[str, ExtensionProtocol] = {}
for command in self._commands:
self.all_commands[command.name] = command
for alias in command.aliases:
self.all_commands[alias] = command
self.help_command: HelpCommand[Self] | None
if help_command is not None:
self.help_command = help_command or DefaultHelpCommand[Self]()
self.add_command(HelpCommandImpl(self))
else:
self.help_command = None
super().__init__(session, token, api_url=api_url, max_messages=max_messages, bot=bot)
@property
def commands(self) -> list[Command[Self]]:
"""Gets all commands registered
Returns
--------
list[:class:`Command`]
The registered commands
"""
return list(set(self.all_commands.values()))
async def get_prefix(self, message: revolt.Message) -> Union[str, list[str]]:
"""Overwrite this function to set the prefix used for commands, this function is called for every message.
Parameters
-----------
message: :class:`Message`
The message that was sent
Returns
--------
Union[:class:`str`, list[:class:`str`]]
The prefix(s) for the commands
"""
raise NotImplementedError
def get_command(self, name: str) -> Command[Self]:
"""Gets a command.
Parameters
-----------
name: :class:`str`
The name or alias of the command
Returns
--------
:class:`Command`
The command with the name
"""
return self.all_commands[name]
def add_command(self, command: Command[Self]) -> None:
"""Adds a command, this is typically only used for dynamic commands, you should use the `commands.command` decorator for most usecases.
Parameters
-----------
name: :class:`str`
The name or alias of the command
command: :class:`Command`
The command to be added
"""
self.all_commands[command.name] = command
for alias in command.aliases:
self.all_commands[alias] = command
def remove_command(self, name: str) -> Optional[Command[Self]]:
"""Removes a command.
Parameters
-----------
name: :class:`str`
The name or alias of the command
Returns
--------
Optional[:class:`Command`]
The command that was removed
"""
command = self.all_commands.pop(name, None)
if command is not None:
for alias in command.aliases:
self.all_commands.pop(alias, None)
return command
def get_view(self, message: revolt.Message) -> type[StringView]:
"""Returns the StringView class to use, this can be overwritten to customize how arguments are parsed
Returns
--------
type[:class:`StringView`]
The string view class to use
"""
return StringView
def get_context(self, message: revolt.Message) -> type[Context[Self]]:
"""Returns the Context class to use, this can be overwritten to add extra features to context
Returns
--------
type[:class:`Context`]
The context class to use
"""
return Context[Self]
async def process_commands(self, message: revolt.Message) -> Any:
"""Processes commands, if you overwrite `Client.on_message` you should manually call this function inside the event.
Parameters
-----------
message: :class:`Message`
The message to process commands on
Returns
--------
Any
The return of the command, if any
"""
content = message.content
prefixes = await self.get_prefix(message)
if isinstance(prefixes, str):
prefixes = [prefixes]
for prefix in prefixes:
if content.startswith(prefix):
content = content[len(prefix):]
break
else:
return
if not content:
return
view = self.get_view(message)(content)
try:
command_name = view.get_next_word()
except StopIteration:
return
context_cls = self.get_context(message)
try:
command = self.get_command(command_name)
except KeyError:
context = context_cls(None, command_name, view, message, self)
return self.dispatch("command_error", context, CommandNotFound(command_name))
context = context_cls(command, command_name, view, message, self)
try:
self.dispatch("command", context)
if not await self.global_check(context):
raise CheckError(f"the global check for the command failed")
if not await context.can_run():
raise CheckError(f"the check(s) for the command failed")
output = await context.invoke()
self.dispatch("after_command_invoke", context, output)
return output
except Exception as e:
await command._error_handler(command.cog or self, context, e)
self.dispatch("command_error", context, e)
async def on_command_error(self, ctx: Context[Self], error: Exception, /) -> None:
traceback.print_exception(type(error), error, error.__traceback__)
def on_message(self, message: revolt.Message) -> Coroutine[Any, Any, Any]:
return self.process_commands(message)
async def global_check(self, context: Context[Self]) -> bool:
"""A global check that stops commands from running on certain criteria.
Parameters
-----------
context: :class:`Context`
The context for the invokation of the command
Returns
--------
:class:`bool` represents if the command should run or not
"""
return True
def add_cog(self, cog: Cog[Self]) -> None:
"""Adds a cog, this cog must subclass `Cog`.
Parameters
-----------
cog: :class:`Cog`
The cog to be added
"""
cog._inject(self)
def remove_cog(self, cog_name: str) -> Cog[Self]:
"""Removes a cog.
Parameters
-----------
cog_name: :class:`str`
The name of the cog to be removed
Returns
--------
:class:`Cog`
The cog that was removed
"""
cog = self.cogs.pop(cog_name)
cog._uninject(self)
return cog
def load_extension(self, name: str) -> None:
"""Loads an extension, this takes a module name and runs the setup function inside of it.
Parameters
-----------
name: :class:`str`
The name of the extension to be loaded
"""
extension = import_module(name)
if not isinstance(extension, ExtensionProtocol):
raise MissingSetup(f"'{extension}' is missing a setup function")
self.extensions[name] = extension
extension.setup(self)
def unload_extension(self, name: str) -> None:
"""Unloads an extension, this takes a module name and runs the teardown function inside of it.
Parameters
-----------
name: :class:`str`
The name of the extension to be unloaded
"""
extension = self.extensions.pop(name)
del sys.modules[name]
if teardown := getattr(extension, "teardown", None):
teardown(self)
def reload_extension(self, name: str) -> None:
"""Reloads an extension, this will unload and reload the extension.
Parameters
-----------
name: :class:`str`
The name of the extension to be reloaded
"""
self.unload_extension(name)
self.load_extension(name)
def get_cog(self, name: str) -> Cog[Self]:
"""Gets a cog.
Parameters
-----------
name: :class:`str`
The name of the cog to get
Returns
--------
:class:`Cog`
The cog that was requested
"""
return self.cogs[name]
def get_extension(self, name: str) -> ExtensionProtocol:
"""Gets an extension.
Parameters
-----------
name: :class:`str`
The name of the extension to get
Returns
--------
:class:`ExtensionProtocol`
The extension that was requested
"""
return self.extensions[name]
+108
View File
@@ -0,0 +1,108 @@
from __future__ import annotations
from typing import Any, Callable, Coroutine, Generic, Optional, TypeVar
from typing_extensions import ParamSpec
from revolt.errors import RevoltError
from .command import Command
from .utils import ClientT_D
P = ParamSpec("P")
R = TypeVar("R")
__all__ = ("Cog", "CogMeta")
class CogMeta(type):
_cog_commands: list[Command[Any]]
_cog_listeners: dict[str, list[str]]
qualified_name: str
def __new__(cls, name: str, bases: tuple[type, ...], attrs: dict[str, Any], *, qualified_name: Optional[str] = None, extras: dict[str, Any] | None = None) -> Any:
commands: list[Command[Any]] = []
listeners: dict[str, list[str]] = {}
self = super().__new__(cls, name, bases, attrs)
extras = extras or {}
for base in reversed(self.__mro__):
for key, value in base.__dict__.items():
if isinstance(value, Command):
for extra_key, extra_value in extras.items():
setattr(value, extra_key, extra_value) # type: ignore
commands.append(value) # type: ignore
elif event_name := getattr(value, "__listener_name", None):
listeners.setdefault(event_name, []).append(key)
self._cog_commands = commands
self._cog_listeners = listeners
self.qualified_name = qualified_name or name
return self
class Cog(Generic[ClientT_D], metaclass=CogMeta):
_cog_commands: list[Command[ClientT_D]]
_cog_listeners: dict[str, list[str]]
qualified_name: str
def cog_load(self) -> None:
"""A special method that is called when the cog gets loaded."""
pass
def cog_unload(self) -> None:
"""A special method that is called when the cog gets removed."""
pass
def _inject(self, client: ClientT_D) -> None:
client.cogs[self.qualified_name] = self
try:
for command in self._cog_commands:
command.cog = self
if command.parent is None:
client.add_command(command)
for key, listeners in self._cog_listeners.items():
for listener_name in listeners:
client.listeners.setdefault(key, []).append(getattr(self, listener_name))
except Exception as e:
self._uninject(client)
raise e
self.cog_load()
def _uninject(self, client: ClientT_D) -> None:
for name, command in client.all_commands.copy().items():
if command in self._cog_commands:
try:
del client.all_commands[name]
except KeyError:
pass
for key, listeners in self._cog_listeners.items():
for listener_name in listeners:
try:
client.listeners[key].remove(getattr(self, listener_name))
except ValueError:
pass
self.cog_unload()
@property
def commands(self) -> list[Command[ClientT_D]]:
return self._cog_commands
@staticmethod
def listen(name: str | None = None) -> Callable[[Callable[P, Coroutine[Any, Any, R]]], Callable[P, Coroutine[Any, Any, R]]]:
def inner(func: Callable[P, Coroutine[Any, Any, R]]) -> Callable[P, Coroutine[Any, Any, R]]:
if not func.__name__.startswith("on_"):
raise RevoltError("event name must start with `on_`")
setattr(func, "__listener_name", name or func.__name__[3:])
return func
return inner
+246 -34
View File
@@ -1,23 +1,43 @@
from __future__ import annotations from __future__ import annotations
import inspect
import traceback import traceback
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Optional from contextlib import suppress
from typing import (TYPE_CHECKING, Annotated, Any, Callable, Coroutine,
Generic, Literal, Optional, Union, get_args, get_origin)
from typing_extensions import ParamSpec
import sys
import revolt if sys.version_info >= (3, 10):
from revolt.utils import copy_doc from types import UnionType
UnionTypes: tuple[Any, ...] = (Union, UnionType)
else:
UnionTypes = (Union,)
from ...utils import maybe_coroutine
from .errors import CommandOnCooldown, InvalidLiteralArgument, UnionConverterError
from .utils import ClientT_Co_D, evaluate_parameters, ClientT_Co
from .cooldown import BucketType, CooldownMapping
if TYPE_CHECKING: if TYPE_CHECKING:
from .checks import Check
from .cog import Cog
from .context import Context from .context import Context
from .group import Group
__all__: tuple[str, ...] = (
__all__ = (
"Command", "Command",
"command" "command"
) )
class Command: NoneType: type[None] = type(None)
P = ParamSpec("P")
class Command(Generic[ClientT_Co_D]):
"""Class for holding info about a command. """Class for holding info about a command.
Parameters Parameters
----------- -----------
callback: Callable[..., Coroutine[Any, Any, Any]] callback: Callable[..., Coroutine[Any, Any, Any]]
@@ -26,17 +46,52 @@ class Command:
The name of the command The name of the command
aliases: list[:class:`str`] aliases: list[:class:`str`]
The aliases of the command The aliases of the command
parent: Optional[:class:`Group`]
The parent of the command if this command is a subcommand
cog: Optional[:class:`Cog`]
The cog the command is apart of.
usage: Optional[:class:`str`]
The usage string for the command
checks: Optional[list[Callable]]
The list of checks the command has
cooldown: Optional[:class:`Cooldown`]
The cooldown for the command to restrict how often the command can be used
description: Optional[:class:`str`]
The commands description if it has one
hidden: :class:`bool`
Whether or not the command should be hidden from the help command
""" """
__slots__ = ("callback", "name", "aliases", "_client") __slots__ = ("callback", "name", "aliases", "signature", "checks", "parent", "_error_handler", "cog", "description", "usage", "parameters", "hidden", "cooldown", "cooldown_bucket")
_client: revolt.Client def __init__(
self,
callback: Callable[..., Coroutine[Any, Any, Any]],
name: str,
*,
aliases: list[str] | None = None,
usage: Optional[str] = None,
checks: list[Check[ClientT_Co_D]] | None = None,
cooldown: Optional[CooldownMapping] | None = None,
bucket: Optional[BucketType | Callable[[Context[ClientT_Co_D]], Coroutine[Any, Any, str]]] = None,
description: str | None = None,
hidden: bool = False,
):
self.callback: Callable[..., Coroutine[Any, Any, Any]] = callback
self.name: str = name
self.aliases: list[str] = aliases or []
self.usage: str | None = usage
self.signature: inspect.Signature = inspect.signature(self.callback)
self.parameters: list[inspect.Parameter] = evaluate_parameters(self.signature.parameters.values(), getattr(callback, "__globals__", {}))
self.checks: list[Check[ClientT_Co_D]] = checks or getattr(callback, "_checks", [])
self.cooldown: CooldownMapping | None = cooldown or getattr(callback, "_cooldown", None)
self.cooldown_bucket: BucketType | Callable[[Context[ClientT_Co_D]], Coroutine[Any, Any, str]] = bucket or getattr(callback, "_bucket", BucketType.default)
self.parent: Optional[Group[ClientT_Co_D]] = None
self.cog: Optional[Cog[ClientT_Co_D]] = None
self._error_handler: Callable[[Any, Context[ClientT_Co_D], Exception], Coroutine[Any, Any, Any]] = type(self)._default_error_handler
self.description: str | None = description or callback.__doc__
self.hidden: bool = hidden
def __init__(self, callback: Callable[..., Coroutine[Any, Any, Any]], name: str, aliases: list[str]): async def invoke(self, context: Context[ClientT_Co_D], *args: Any, **kwargs: Any) -> Any:
self.callback = callback
self.name = name
self.aliases = aliases
async def invoke(self, context: Context, args: list[Any]) -> Any:
"""Runs the command and calls the error handler if the command errors. """Runs the command and calls the error handler if the command errors.
Parameters Parameters
@@ -47,17 +102,16 @@ class Command:
The arguments for the command The arguments for the command
""" """
try: try:
return await self.callback(self._client, context, *args) return await self.callback(self.cog or context.client, context, *args, **kwargs)
except Exception as err: except Exception as err:
return await self._error_handler(context, err) return await self._error_handler(self.cog or context.client, context, err)
@copy_doc(invoke) def __call__(self, context: Context[ClientT_Co_D], *args: Any, **kwargs: Any) -> Any:
def __call__(self, context: Context, args: list[Any]) -> Any: return self.invoke(context, *args, **kwargs)
return self.invoke(context, args)
def error(self, func: Callable[..., Coroutine[Any, Any, Any]]): def error(self, func: Callable[..., Coroutine[Any, Any, Any]]) -> Callable[..., Coroutine[Any, Any, Any]]:
"""Sets the error handler for the command. """Sets the error handler for the command.
Parameters Parameters
----------- -----------
func: Callable[..., Coroutine[Any, Any, Any]] func: Callable[..., Coroutine[Any, Any, Any]]
@@ -72,29 +126,187 @@ class Command:
await ctx.send(str(error)) await ctx.send(str(error))
""" """
self._error_handler = func # type: ignore self._error_handler = func
return func return func
@staticmethod async def _default_error_handler(self, ctx: Context[ClientT_Co_D], error: Exception):
async def _error_handler(ctx: Context, error: Exception):
traceback.print_exception(type(error), error, error.__traceback__) traceback.print_exception(type(error), error, error.__traceback__)
def command(*, name: Optional[str] = None, aliases: Optional[list[str]] = None, cls: type[Command] = Command): @classmethod
"""Turns a function into a :class:`Command`. async def handle_origin(cls, context: Context[ClientT_Co_D], origin: Any, annotation: Any, arg: str) -> Any:
if origin in UnionTypes:
for converter in get_args(annotation):
try:
return await cls.convert_argument(arg, converter, context)
except:
if converter is NoneType:
context.view.undo()
return None
raise UnionConverterError(arg)
elif origin is Annotated:
annotated_args = get_args(annotation)
if annotated_args[1] == "_revolt_greedy_marker":
real_annotation = get_args(annotated_args[0])[0]
converted_args: list[Any] = []
converted_args.append(await cls.convert_argument(arg, real_annotation, context))
for arg in context.view:
try:
converted_args.append(await cls.convert_argument(arg, real_annotation, context))
except:
context.view.undo()
break
return converted_args
else:
return await cls.convert_argument(arg, annotated_args[1], context)
elif origin is Literal:
if arg in get_args(annotation):
return arg
else:
raise InvalidLiteralArgument(arg)
@classmethod
async def convert_argument(cls, arg: str, annotation: Any, context: Context[ClientT_Co_D]) -> Any:
if annotation is not inspect.Signature.empty:
if annotation is str: # no converting is needed - its already a string
return arg
origin: Any
if origin := get_origin(annotation):
return await cls.handle_origin(context, origin, annotation, arg)
else:
return await maybe_coroutine(annotation, arg, context)
else:
return arg
async def parse_arguments(self, context: Context[ClientT_Co_D]) -> None:
# please pr if you can think of a better way to do this
for parameter in self.parameters[2:]:
if parameter.kind == parameter.KEYWORD_ONLY:
try:
arg = await self.convert_argument(context.view.get_rest(), parameter.annotation, context)
except StopIteration:
if parameter.default is not parameter.empty:
arg = parameter.default
elif is_optional(parameter.annotation):
arg = None
else:
raise
context.kwargs[parameter.name] = arg
elif parameter.kind == parameter.VAR_POSITIONAL:
with suppress(StopIteration):
while True:
context.args.append(await self.convert_argument(context.view.get_next_word(), parameter.annotation, context))
elif parameter.kind == parameter.POSITIONAL_OR_KEYWORD:
try:
rest = context.view.get_next_word()
arg = await self.convert_argument(rest, parameter.annotation, context)
except StopIteration:
if parameter.default is not parameter.empty:
arg = parameter.default
elif is_optional(parameter.annotation):
arg = None
else:
raise
context.args.append(arg)
async def run_cooldown(self, context: Context[ClientT_Co_D]) -> None:
if mapping := self.cooldown:
if isinstance(self.cooldown_bucket, BucketType):
key = self.cooldown_bucket.resolve(context)
else:
key = await self.cooldown_bucket(context)
cooldown = mapping.get_bucket(key)
if retry_after := cooldown.update_cooldown():
raise CommandOnCooldown(retry_after)
def __repr__(self) -> str:
return f"<{self.__class__.__name__} name=\"{self.name}\">"
@property
def short_description(self) -> Optional[str]:
"""Returns the first line of the description or None if there is no description."""
if self.description:
return self.description.split("\n")[0]
def get_usage(self) -> str:
"""Returns the usage string for the command."""
if self.usage:
return self.usage
parents: list[str] = []
if self.parent:
parent = self.parent
while parent:
parents.append(parent.name)
parent = parent.parent
parameters: list[str] = []
for parameter in self.parameters[2:]:
if parameter.kind == parameter.POSITIONAL_OR_KEYWORD:
if parameter.default is not parameter.empty:
parameters.append(f"[{parameter.name}]")
else:
parameters.append(f"<{parameter.name}>")
elif parameter.kind == parameter.KEYWORD_ONLY:
if parameter.default is not parameter.empty:
parameters.append(f"[{parameter.name}]")
else:
parameters.append(f"<{parameter.name}...>")
elif parameter.kind == parameter.VAR_POSITIONAL:
parameters.append(f"[{parameter.name}...]")
return f"{' '.join(parents[::-1])} {self.name} {' '.join(parameters)}"
def is_optional(arg: Any) -> bool:
return get_origin(arg) in UnionTypes and any(arg is NoneType for arg in get_args(arg))
def command(
*,
name: Optional[str] = None,
aliases: Optional[list[str]] = None,
cls: type[Command[ClientT_Co]] = Command,
usage: Optional[str] = None
) -> Callable[[Callable[..., Coroutine[Any, Any, Any]]], Command[ClientT_Co]]:
"""A decorator that turns a function into a :class:`Command`.n
Parameters Parameters
----------- -----------
name: Optional[:class:`str`] name: Optional[:class:`str`]
The name of the command, this defaults to the functions name The name of the command, this defaults to the functions name
aliases: Optional[list[:class:`str`]] aliases: Optional[list[:class:`str`]]
The aliases of the command, defaults to no aliases The aliases of the command, defaults to no aliases
cls: type[:class:`Command`]
The class used for creating the command, this defaults to :class:`Command` but can be used to use a custom command subclass
usage: Optional[:class:`str`]
The signature for how the command should be called
Returns Returns
-------- --------
:class:`Command` Callable[Callable[..., Coroutine], :class:`Command`]
The command A function that takes the command callback and returns a :class:`Command`
""" """
def inner(func: Callable[..., Coroutine[Any, Any, Any]]): def inner(func: Callable[..., Coroutine[Any, Any, Any]]) -> Command[ClientT_Co]:
return cls(func, name or func.__name__, aliases or []) return cls(func, name or func.__name__, aliases=aliases or [], usage=usage)
return inner return inner
+83 -17
View File
@@ -1,20 +1,26 @@
from __future__ import annotations from __future__ import annotations
from typing import Any, Optional from typing import TYPE_CHECKING, Any, Generic, Optional
import revolt import revolt
from revolt.utils import copy_doc from revolt.utils import maybe_coroutine
from .command import Command from .command import Command
from .group import Group
from .utils import ClientT_Co_D
if TYPE_CHECKING:
from .view import StringView
from revolt.state import State
__all__ = ( __all__ = (
"Context", "Context",
) )
class Context: class Context(revolt.Messageable, Generic[ClientT_Co_D]):
"""Stores metadata the commands execution. """Stores metadata the commands execution.
Parameters Attributes
----------- -----------
command: Optional[:class:`Command`] command: Optional[:class:`Command`]
The command, this can be `None` when no command was found and the error handler is being executed The command, this can be `None` when no command was found and the error handler is being executed
@@ -22,16 +28,53 @@ class Context:
The command name that was used, this can be an alias, the commands name or a command that doesnt exist The command name that was used, this can be an alias, the commands name or a command that doesnt exist
message: :class:`Message` message: :class:`Message`
The message that was sent to invoke the command The message that was sent to invoke the command
channel: :class:`Messageable`
The channel the command was invoked in
server_id: Optional[:class:`Server`]
The server the command was invoked in
author: Union[:class:`Member`, :class:`User`]
The user or member that invoked the commad, will be :class:`User` in DMs
args: list[:class:`str`]
The positional arguments being passed to the command
kwargs: dict[:class:`str`, Any]
The keyword arguments being passed to the command
client: :class:`CommandsClient`
The revolt client
""" """
__slots__ = ("command", "invoked_with", "message") __slots__ = ("command", "invoked_with", "args", "message", "channel", "author", "view", "kwargs", "state", "client", "server_id")
def __init__(self, command: Optional[Command], invoked_with: str, message: revolt.Message): async def _get_channel_id(self) -> str:
self.command = command return self.channel.id
self.invoked_with = invoked_with
self.message = message
async def invoke(self, args: list[str]) -> Any: def __init__(self, command: Optional[Command[ClientT_Co_D]], invoked_with: str, view: StringView, message: revolt.Message, client: ClientT_Co_D):
"""Invokes the command, this is equal to `await context.command.invoke(context, args)`. self.command: Command[ClientT_Co_D] | None = command
self.invoked_with: str = invoked_with
self.view: StringView = view
self.message: revolt.Message = message
self.client: ClientT_Co_D = client
self.args: list[Any] = []
self.kwargs: dict[str, Any] = {}
self.server_id: str | None = message.server_id
self.channel: revolt.TextChannel | revolt.GroupDMChannel | revolt.DMChannel | revolt.SavedMessageChannel = message.channel
self.author: revolt.Member | revolt.User = message.author
self.state: State = message.state
@property
def server(self) -> revolt.Server:
""":class:`Server` The server this context belongs too
Raises
-------
:class:`LookupError`
Raises if the context is not from a server
"""
if not self.server_id:
raise LookupError
return self.state.get_server(self.server_id)
async def invoke(self) -> Any:
"""Invokes the command.
.. note:: If the command is `None`, this function will do nothing. .. note:: If the command is `None`, this function will do nothing.
@@ -40,9 +83,32 @@ class Context:
args: list[:class:`str`] args: list[:class:`str`]
The args being passed to the command The args being passed to the command
""" """
if command := self.command:
return await command.invoke(self, args)
@copy_doc(revolt.Messageable.send) if command := self.command:
async def send(self, content: Optional[str] = None, *, embeds: Optional[list[revolt.Embed]] = None, embed: Optional[revolt.Embed] = None, attachments: Optional[list[revolt.File]] = None) -> revolt.Message: if isinstance(command, Group):
return await self.message.channel.send(content, embeds=embeds, embed=embed, attachments=attachments) try:
subcommand_name = self.view.get_next_word()
except StopIteration:
pass
else:
if subcommand := command.subcommands.get(subcommand_name):
self.command = command = subcommand
return await self.invoke()
self.view.undo()
await command.run_cooldown(self)
await command.parse_arguments(self)
return await command.invoke(self, *self.args, **self.kwargs)
async def can_run(self, command: Optional[Command[ClientT_Co_D]] = None) -> bool:
"""Runs all of the commands checks, and returns true if all of them pass"""
command = command or self.command
return all([await maybe_coroutine(check, self) for check in (command.checks if command else [])])
async def send_help(self, argument: Command[Any] | Group[Any] | ClientT_Co_D | None = None) -> None:
argument = argument or self.client
command = self.client.get_command("help")
await command.invoke(self, argument)
+126
View File
@@ -0,0 +1,126 @@
from __future__ import annotations
import re
from typing import TYPE_CHECKING, Annotated, TypeVar
from revolt import Category, Channel, Member, User, utils
from .context import Context
from .errors import (BadBoolArgument, CategoryConverterError,
ChannelConverterError, MemberConverterError, ServerOnly,
UserConverterError)
if TYPE_CHECKING:
from .client import CommandsClient
T = TypeVar("T")
__all__: tuple[str, ...] = ("bool_converter", "category_converter", "channel_converter", "user_converter", "member_converter", "IntConverter", "BoolConverter", "CategoryConverter", "UserConverter", "MemberConverter", "ChannelConverter", "Greedy")
channel_regex: re.Pattern[str] = re.compile("<#([A-z0-9]{26})>")
user_regex: re.Pattern[str] = re.compile("<@([A-z0-9]{26})>")
ClientT = TypeVar("ClientT", bound="CommandsClient")
def bool_converter(arg: str, _: Context[ClientT]) -> bool:
lowered = arg.lower()
if lowered in ("yes", "true", "ye", "y", "1", "on", "enable"):
return True
elif lowered in ("no", "false", "n", "f", "0", "off", "disabled"):
return False
else:
raise BadBoolArgument(lowered)
def category_converter(arg: str, context: Context[ClientT]) -> Category:
if not context.server_id:
raise ServerOnly
try:
return context.server.get_category(arg)
except LookupError:
try:
return utils.get(context.server.categories, name=arg)
except LookupError:
raise CategoryConverterError(arg)
def channel_converter(arg: str, context: Context[ClientT]) -> Channel:
if not context.server_id:
raise ServerOnly
if (match := channel_regex.match(arg)):
arg = match.group(1)
try:
return context.server.get_channel(arg)
except LookupError:
try:
return utils.get(context.server.channels, name=arg)
except LookupError:
raise ChannelConverterError(arg)
def user_converter(arg: str, context: Context[ClientT]) -> User:
if (match := user_regex.match(arg)):
arg = match.group(1)
try:
return context.client.get_user(arg)
except LookupError:
try:
parts = arg.split("#")
if len(parts) == 1:
return (
utils.get(context.client.users, original_name=arg)
or utils.get(context.client.users, display_name=arg)
)
elif len(parts) == 2:
return (
utils.get(context.client.users, original_name=parts[0], discriminator=parts[1])
or utils.get(context.client.users, display_name=parts[0], discriminator=parts[1])
)
else:
raise LookupError
except LookupError:
raise UserConverterError(arg)
def member_converter(arg: str, context: Context[ClientT]) -> Member:
if not context.server_id:
raise ServerOnly
if (match := user_regex.match(arg)):
arg = match.group(1)
try:
return context.server.get_member(arg)
except LookupError:
try:
parts = arg.split("#")
if len(parts) == 1:
return (
utils.get(context.server.members, original_name=arg)
or utils.get(context.server.members, display_name=arg)
)
elif len(parts) == 2:
return (
utils.get(context.server.members, original_name=parts[0], discriminator=parts[1])
or utils.get(context.server.members, display_name=parts[0], discriminator=parts[1])
)
else:
raise LookupError
except LookupError:
raise MemberConverterError(arg)
def int_converter(arg: str, context: Context[ClientT]) -> int:
return int(arg)
IntConverter = Annotated[int, int_converter]
BoolConverter = Annotated[bool, bool_converter]
CategoryConverter = Annotated[Category, category_converter]
UserConverter = Annotated[User, user_converter]
MemberConverter = Annotated[Member, member_converter]
ChannelConverter = Annotated[Channel, channel_converter]
Greedy = Annotated[list[T], "_revolt_greedy_marker"]
+144
View File
@@ -0,0 +1,144 @@
from __future__ import annotations
import time
from typing import TYPE_CHECKING, Any, Callable, Coroutine, TypeVar, cast
from .errors import ServerOnly
if TYPE_CHECKING:
from enum import Enum
from .context import Context
from .utils import ClientT_Co_D, ClientT_Co
else:
from aenum import Enum
__all__ = ("Cooldown", "CooldownMapping", "BucketType", "cooldown")
T = TypeVar("T")
class Cooldown:
"""Represent a single cooldown for a single key
Parameters
-----------
rate: :class:`int`
How many times it can be used
per: :class:`int`
How long the window is before the ratelimit resets
"""
def __init__(self, rate: int, per: int):
self.rate: int = rate
self.per: int = per
self.window: float = 0.0
self.tokens: int = rate
self.last: float = 0.0
def get_tokens(self, current: float | None) -> int:
current = current or time.time()
if current > (self.window + self.per):
return self.rate
else:
return self.tokens
def update_cooldown(self) -> float | None:
current = time.time()
self.last = current
self.tokens = self.get_tokens(current)
if self.tokens == 0:
return self.per - (current - self.window)
self.tokens -= 1
if self.tokens == 0:
self.window = current
return None
class CooldownMapping:
"""Holds all cooldowns for every key"""
def __init__(self, rate: int, per: int):
self.rate = rate
self.per = per
self.cache: dict[str, Cooldown] = {}
def verify_cache(self) -> None:
current = time.time()
self.cache = {k: v for k, v in self.cache.items() if current < (v.last + v.per)}
def get_bucket(self, key: str) -> Cooldown:
self.verify_cache()
if not (rl := self.cache.get(key)):
self.cache[key] = rl = Cooldown(self.rate, self.per)
return rl
class BucketType(Enum):
default = 0
user = 1
server = 2
channel = 3
member = 4
def resolve(self, context: Context[ClientT_Co_D]) -> str:
if self == BucketType.default:
return f"{context.author.id}{context.channel.id}"
elif self == BucketType.user:
return context.author.id
elif self == BucketType.server:
if id := context.server_id:
return id
raise ServerOnly
elif self == BucketType.channel:
return context.channel.id
else: # BucketType.member
if server_id := context.server_id:
return f"{context.author.id}{server_id}"
raise ServerOnly
def cooldown(rate: int, per: int, *, bucket: BucketType | Callable[[Context[ClientT_Co]], Coroutine[Any, Any, str]] = BucketType.default) -> Callable[[T], T]:
"""Adds a cooldown to a command
Parameters
-----------
rate: :class:`int`
How many times it can be used
per: :class:`int`
How long the window is before the ratelimit resets
bucket: Optional[Union[:class:`BucketType`, Callable[[Context], str]]]
Controls how the key is generated for the cooldowns
Examples
--------
.. code-block:: python
@commands.command()
@commands.cooldown(1, 5)
async def ping(self, ctx: Context):
await ctx.send("Pong")
"""
def inner(func: T) -> T:
from .command import Command
if isinstance(func, Command):
command = cast(Command[ClientT_Co], func) # cant verify generic at runtime so must cast
command.cooldown = CooldownMapping(rate, per)
command.cooldown_bucket = bucket
else:
func._cooldown = CooldownMapping(rate, per) # type: ignore
func._bucket = bucket # type: ignore
return func # type: ignore
return inner
+103 -3
View File
@@ -1,10 +1,32 @@
from revolt import RevoltError
__all__ = ( __all__ = (
"CommandError",
"CommandNotFound", "CommandNotFound",
"NoClosingQuote",
"CheckError",
"NotBotOwner",
"NotServerOwner",
"ServerOnly",
"MissingPermissionsError",
"ConverterError",
"InvalidLiteralArgument",
"BadBoolArgument",
"CategoryConverterError",
"ChannelConverterError",
"UserConverterError",
"MemberConverterError",
"UnionConverterError",
"MissingSetup",
"CommandOnCooldown"
) )
class CommandNotFound(Exception): class CommandError(RevoltError):
"""base error for all command's related errors"""
class CommandNotFound(CommandError):
"""Raised when a command isnt found. """Raised when a command isnt found.
Parameters Parameters
----------- -----------
command_name: :class:`str` command_name: :class:`str`
@@ -13,4 +35,82 @@ class CommandNotFound(Exception):
__slots__ = ("command_name",) __slots__ = ("command_name",)
def __init__(self, command_name: str): def __init__(self, command_name: str):
self.command_name = command_name self.command_name: str = command_name
class NoClosingQuote(CommandError):
"""Raised when there is no closing quote for a command argument"""
class CheckError(CommandError):
"""Raised when a check fails for a command"""
class NotBotOwner(CheckError):
"""Raised when the `is_bot_owner` check fails"""
class NotServerOwner(CheckError):
"""Raised when the `is_server_owner` check fails"""
class ServerOnly(CheckError):
"""Raised when a check requires the command to be ran in a server"""
class MissingPermissionsError(CheckError):
"""Raised when a check requires permissions the user does not have
Attributes
-----------
permissions: :class:`dict[str, bool]`
The permissions which the user did not have
"""
def __init__(self, permissions: dict[str, bool]):
self.permissions = permissions
class ConverterError(CommandError):
"""Base class for all converter errors"""
class InvalidLiteralArgument(ConverterError):
"""Raised when the argument is not a valid literal argument"""
class BadBoolArgument(ConverterError):
"""Raised when the bool converter fails"""
class CategoryConverterError(ConverterError):
"""Raised when the Category conveter fails"""
def __init__(self, argument: str):
self.argument = argument
class ChannelConverterError(ConverterError):
"""Raised when the Channel conveter fails"""
def __init__(self, argument: str):
self.argument = argument
class UserConverterError(ConverterError):
"""Raised when the Category conveter fails"""
def __init__(self, argument: str):
self.argument = argument
class MemberConverterError(ConverterError):
"""Raised when the Category conveter fails"""
def __init__(self, argument: str):
self.argument = argument
class UnionConverterError(ConverterError):
"""Raised when all converters in a union fails"""
def __init__(self, argument: str):
self.argument = argument
class MissingSetup(CommandError):
"""Raised when an extension is missing the `setup` function"""
class CommandOnCooldown(CommandError):
"""Raised when a command is on cooldown
Attributes
-----------
retry_after: :class:`float`
How long the user must wait until the cooldown resets
"""
__slots__ = ("retry_after",)
def __init__(self, retry_after: float):
self.retry_after: float = retry_after
+181
View File
@@ -0,0 +1,181 @@
from __future__ import annotations
from typing import Any, Callable, Coroutine, Optional
from .command import Command
from .utils import ClientT_Co_D, ClientT_D
__all__ = (
"Group",
"group"
)
class Group(Command[ClientT_Co_D]):
"""Class for holding info about a group command.
Parameters
-----------
callback: Callable[..., Coroutine[Any, Any, Any]]
The callback for the group command
name: :class:`str`
The name of the command
aliases: list[:class:`str`]
The aliases of the group command
subcommands: dict[:class:`str`, :class:`Command`]
The group's subcommands.
"""
__slots__: tuple[str, ...] = ("subcommands",)
def __init__(self, callback: Callable[..., Coroutine[Any, Any, Any]], name: str, aliases: list[str]):
self.subcommands: dict[str, Command[ClientT_Co_D]] = {}
super().__init__(callback, name, aliases=aliases)
def command(self, *, name: Optional[str] = None, aliases: Optional[list[str]] = None, cls: type[Command[ClientT_Co_D]] = Command[ClientT_Co_D]) -> Callable[[Callable[..., Coroutine[Any, Any, Any]]], Command[ClientT_Co_D]]:
"""A decorator that turns a function into a :class:`Command` and registers the command as a subcommand.
Parameters
-----------
name: Optional[:class:`str`]
The name of the command, this defaults to the functions name
aliases: Optional[list[:class:`str`]]
The aliases of the command, defaults to no aliases
cls: type[:class:`Command`]
The class used for creating the command, this defaults to :class:`Command` but can be used to use a custom command subclass
Returns
--------
Callable[Callable[..., Coroutine], :class:`Command`]
A function that takes the command callback and returns a :class:`Command`
"""
def inner(func: Callable[..., Coroutine[Any, Any, Any]]):
command = cls(func, name or func.__name__, aliases=aliases or [])
command.parent = self
self.subcommands[command.name] = command
for alias in command.aliases:
self.subcommands[alias] = command
return command
return inner
def group(self, *, name: Optional[str] = None, aliases: Optional[list[str]] = None, cls: Optional[type[Group[ClientT_Co_D]]] = None) -> Callable[[Callable[..., Coroutine[Any, Any, Any]]], Group[ClientT_Co_D]]:
"""A decorator that turns a function into a :class:`Group` and registers the command as a subcommand
Parameters
-----------
name: Optional[:class:`str`]
The name of the group command, this defaults to the functions name
aliases: Optional[list[:class:`str`]]
The aliases of the group command, defaults to no aliases
cls: type[:class:`Group`]
The class used for creating the command, this defaults to :class:`Group` but can be used to use a custom group subclass
Returns
--------
Callable[Callable[..., Coroutine], :class:`Group`]
A function that takes the command callback and returns a :class:`Group`
"""
cls = cls or type(self)
def inner(func: Callable[..., Coroutine[Any, Any, Any]]):
command = cls(func, name or func.__name__, aliases or [])
command.parent = self
self.subcommands[command.name] = command
for alias in command.aliases:
self.subcommands[alias] = command
return command
return inner
def __repr__(self) -> str:
return f"<Group name=\"{self.name}\">"
@property
def commands(self) -> list[Command[ClientT_Co_D]]:
"""Gets all commands registered
Returns
--------
list[:class:`Command`]
The registered commands
"""
return list(set(self.subcommands.values()))
def get_command(self, name: str) -> Command[ClientT_Co_D]:
"""Gets a command.
Parameters
-----------
name: :class:`str`
The name or alias of the command
Returns
--------
:class:`Command`
The command with the name
"""
return self.subcommands[name]
def add_command(self, command: Command[ClientT_Co_D]) -> None:
"""Adds a command, this is typically only used for dynamic commands, you should use the `commands.command` decorator for most usecases.
Parameters
-----------
name: :class:`str`
The name or alias of the command
command: :class:`Command`
The command to be added
"""
self.subcommands[command.name] = command
for alias in command.aliases:
self.subcommands[alias] = command
def remove_command(self, name: str) -> Optional[Command[ClientT_Co_D]]:
"""Removes a command.
Parameters
-----------
name: :class:`str`
The name or alias of the command
Returns
--------
Optional[:class:`Command`]
The command that was removed
"""
command = self.subcommands.pop(name, None)
if command is not None:
for alias in command.aliases:
self.subcommands.pop(alias, None)
return command
def group(*, name: Optional[str] = None, aliases: Optional[list[str]] = None, cls: type[Group[ClientT_D]] = Group) -> Callable[[Callable[..., Coroutine[Any, Any, Any]]], Group[ClientT_D]]:
"""A decorator that turns a function into a :class:`Group`
Parameters
-----------
name: Optional[:class:`str`]
The name of the group command, this defaults to the functions name
aliases: Optional[list[:class:`str`]]
The aliases of the group command, defaults to no aliases
cls: type[:class:`Group`]
The class used for creating the command, this defaults to :class:`Group` but can be used to use a custom group subclass
Returns
--------
Callable[Callable[..., Coroutine], :class:`Group`]
A function that takes the command callback and returns a :class:`Group`
"""
def inner(func: Callable[..., Coroutine[Any, Any, Any]]):
return cls(func, name or func.__name__, aliases or [])
return inner
+216
View File
@@ -0,0 +1,216 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Generic, Optional, TypedDict, Union, cast
from typing_extensions import NotRequired
from .cog import Cog
from .command import Command
from .context import Context
from .group import Group
from .utils import ClientT_Co_D, ClientT_D
from revolt import File, Message, Messageable, MessageReply, SendableEmbed
if TYPE_CHECKING:
from .cog import Cog
__all__ = ("MessagePayload", "HelpCommand", "DefaultHelpCommand", "help_command_impl")
class MessagePayload(TypedDict):
content: str
embed: NotRequired[SendableEmbed]
embeds: NotRequired[list[SendableEmbed]]
attachments: NotRequired[list[File]]
replies: NotRequired[list[MessageReply]]
class HelpCommand(ABC, Generic[ClientT_Co_D]):
@abstractmethod
async def create_global_help(self, context: Context[ClientT_Co_D], commands: dict[Optional[Cog[ClientT_Co_D]], list[Command[ClientT_Co_D]]]) -> Union[str, SendableEmbed, MessagePayload]:
raise NotImplementedError
@abstractmethod
async def create_command_help(self, context: Context[ClientT_Co_D], command: Command[ClientT_Co_D]) -> Union[str, SendableEmbed, MessagePayload]:
raise NotImplementedError
@abstractmethod
async def create_group_help(self, context: Context[ClientT_Co_D], group: Group[ClientT_Co_D]) -> Union[str, SendableEmbed, MessagePayload]:
raise NotImplementedError
@abstractmethod
async def create_cog_help(self, context: Context[ClientT_Co_D], cog: Cog[ClientT_Co_D]) -> Union[str, SendableEmbed, MessagePayload]:
raise NotImplementedError
async def send_help_command(self, context: Context[ClientT_Co_D], message_payload: MessagePayload) -> Message:
return await context.send(**message_payload)
async def filter_commands(self, context: Context[ClientT_Co_D], commands: list[Command[ClientT_Co_D]]) -> list[Command[ClientT_Co_D]]:
filtered: list[Command[ClientT_Co_D]] = []
for command in commands:
if command.hidden:
continue
try:
if await context.can_run(command):
filtered.append(command)
except Exception:
pass
return filtered
async def group_commands(self, context: Context[ClientT_Co_D], commands: list[Command[ClientT_Co_D]]) -> dict[Optional[Cog[ClientT_Co_D]], list[Command[ClientT_Co_D]]]:
cogs: dict[Optional[Cog[ClientT_Co_D]], list[Command[ClientT_Co_D]]] = {}
for command in commands:
cogs.setdefault(command.cog, []).append(command)
return cogs
async def handle_message(self, context: Context[ClientT_Co_D], message: Message) -> None:
pass
async def get_channel(self, context: Context) -> Messageable:
return context
@abstractmethod
async def handle_no_command_found(self, context: Context[ClientT_Co_D], name: str) -> Union[str, SendableEmbed, MessagePayload]:
raise NotImplementedError
class DefaultHelpCommand(HelpCommand[ClientT_Co_D]):
def __init__(self, default_cog_name: str = "No Cog"):
self.default_cog_name = default_cog_name
async def create_global_help(self, context: Context[ClientT_Co_D], commands: dict[Optional[Cog[ClientT_Co_D]], list[Command[ClientT_Co_D]]]) -> Union[str, SendableEmbed, MessagePayload]:
lines = ["```"]
for cog, cog_commands in commands.items():
cog_lines: list[str] = []
cog_lines.append(f"{cog.qualified_name if cog else self.default_cog_name}:")
for command in cog_commands:
cog_lines.append(f" {command.name} - {command.short_description or 'No description'}")
lines.append("\n".join(cog_lines))
lines.append("```")
return "\n".join(lines)
async def create_cog_help(self, context: Context[ClientT_Co_D], cog: Cog[ClientT_Co_D]) -> Union[str, SendableEmbed, MessagePayload]:
lines = ["```"]
lines.append(f"{cog.qualified_name}:")
for command in cog.commands:
lines.append(f" {command.name} - {command.short_description or 'No description'}")
lines.append("```")
return "\n".join(lines)
async def create_command_help(self, context: Context[ClientT_Co_D], command: Command[ClientT_Co_D]) -> Union[str, SendableEmbed, MessagePayload]:
lines = ["```"]
lines.append(f"{command.name}:")
lines.append(f" Usage: {command.get_usage()}")
if command.aliases:
lines.append(f" Aliases: {', '.join(command.aliases)}")
if command.description:
lines.append(command.description)
lines.append("```")
return "\n".join(lines)
async def create_group_help(self, context: Context[ClientT_Co_D], group: Group[ClientT_Co_D]) -> Union[str, SendableEmbed, MessagePayload]:
lines = ["```"]
lines.append(f"{group.name}:")
lines.append(f" Usage: {group.get_usage()}")
if group.aliases:
lines.append(f" Aliases: {', '.join(group.aliases)}")
if group.description:
lines.append(group.description)
for command in group.commands:
lines.append(f" {command.name} - {command.short_description or 'No description'}")
lines.append("```")
return "\n".join(lines)
async def handle_no_command_found(self, context: Context[ClientT_Co_D], name: str) -> str:
return f"Command `{name}` not found."
class HelpCommandImpl(Command[ClientT_Co_D]):
def __init__(self, client: ClientT_Co_D):
self.client = client
async def callback(_: Union[ClientT_Co_D, Cog[ClientT_Co_D]], context: Context[ClientT_Co_D], *args: str) -> None:
await help_command_impl(context.client, context, *args)
super().__init__(callback=callback, name="help", aliases=[])
self.description: str | None = "Shows help for a command, cog or the entire bot"
async def help_command_impl(client: ClientT_D, context: Context[ClientT_D], *arguments: str) -> None:
help_command = client.help_command
if not help_command:
return
filtered_commands = await help_command.filter_commands(context, client.commands)
commands = await help_command.group_commands(context, filtered_commands)
if not arguments:
payload = await help_command.create_global_help(context, commands)
else:
parent: ClientT_D | Group[ClientT_D] = client
for param in arguments:
try:
command = parent.get_command(param)
except LookupError:
try:
cog = client.get_cog(param)
except LookupError:
payload = await help_command.handle_no_command_found(context, param)
else:
payload = await help_command.create_cog_help(context, cog)
finally:
break
if isinstance(command, Group):
parent = command
else:
payload = await help_command.create_command_help(context, command)
break
else:
if TYPE_CHECKING:
command = cast(Command[ClientT_D], ...)
if isinstance(command, Group):
payload = await help_command.create_group_help(context, command)
else:
payload = await help_command.create_command_help(context, command)
if TYPE_CHECKING:
payload = cast(MessagePayload, ...)
msg_payload: MessagePayload
if isinstance(payload, str):
msg_payload = {"content": payload}
elif isinstance(payload, SendableEmbed):
msg_payload = {"embed": payload, "content": " "}
else:
msg_payload = payload
message = await help_command.send_help_command(context, msg_payload)
await help_command.handle_message(context, message)
-184
View File
@@ -1,184 +0,0 @@
from __future__ import annotations
from typing import Any, Callable, Union
import revolt
from .command import Command
from .context import Context
from .errors import CommandNotFound
__all__ = (
"CommandsMeta",
"CommandsMixin"
)
class CommandsMeta(type):
_commands: list[Command]
def __new__(cls, name: str, bases: tuple[type, ...], attrs: dict[str, Any]):
commands: list[Command] = []
for value in attrs.values():
if isinstance(value, Command):
commands.append(value)
self = super().__new__(cls, name, bases, attrs)
self._commands = commands
for command in commands:
command._client = self # type: ignore
return self
class CommandsMixin(metaclass=CommandsMeta):
"""Main class that adds commands, this class should be subclassed along with `revolt.Client`."""
_commands: list[Command]
dispatch: Callable[..., None]
def __init__(self, *args, **kwargs):
self.all_commands = {}
for command in self._commands:
self.all_commands[command.name] = command
for alias in command.aliases:
self.all_commands[alias] = command
super().__init__(*args, **kwargs)
@property
def commands(self) -> list[Command]:
return list(set(self.all_commands.values()))
async def get_prefix(self, message: revolt.Message) -> Union[str, list[str]]:
"""Overwrite this function to set the prefix used for commands, this function is called for every message.
Parameters
-----------
message: :class:`Message`
The message that was sent
Returns
--------
Union[:class:`str`, list[:class:`str`]]
The prefix(s) for the commands
"""
raise NotImplementedError
def get_command(self, name: str) -> Command:
"""Gets a command.
Parameters
-----------
name: :class:`str`
The name or alias of the command
Returns
--------
:class:`Command`
The command with the name
"""
return self.all_commands[name]
def add_command(self, name: str, command: Command):
"""Adds a command, this is typically only used for dynamic commands, you should use the `commands.command` decorator for most usecases.
Parameters
-----------
name: :class:`str`
The name or alias of the command
command: :class:`Command`
The command to be added
"""
self.all_commands[name] = command
def split_content(self, content: str) -> list[str]:
"""Splits a string into seperate parameters, overwrite this function to change how arguments are split.
Parameters
-----------
content: :class:`str`
The content of the message
Returns
--------
list[:class:`str`]
The arguments from the content
"""
args = []
temp: list[str] = []
quoted = False
quote_char = ""
for char in content:
if char == " " and not quoted:
args.append("".join(temp))
temp = []
elif char in ["\"", "'"] and not quoted:
quoted = True
quote_char = char
elif char == quote_char and quoted:
quoted = False
args.append("".join(temp))
else:
temp.append(char)
return args
def get_context(self, message: revolt.Message) -> type[Context]:
return Context
async def process_commands(self, message: revolt.Message) -> Any:
"""Processes commands, if you overwrite `Client.on_message` you should manually call this function inside the event.
Parameters
-----------
message: :class:`Message`
The message to process commands on
Returns
--------
Any
The return of the command, if any
"""
content = message.content
if not isinstance(content, str):
return
prefixes = await self.get_prefix(message)
if isinstance(prefixes, str):
prefixes = [prefixes]
for prefix in prefixes:
if content.startswith(prefix):
content = content[len(prefix):]
break
else:
return
if not content:
return
command_name, *args = self.split_content(content)
try:
command = self.get_command(command_name)
except KeyError:
return self.dispatch("command_error", Context(None, command_name, message), CommandNotFound(command_name))
context = self.get_context(message)(command, command_name, message)
try:
return await context.invoke(args)
except Exception as e:
self.dispatch("command_error", context, e)
on_message = process_commands
+30
View File
@@ -0,0 +1,30 @@
from __future__ import annotations
from inspect import Parameter
from typing import TYPE_CHECKING, Any, Iterable
from typing_extensions import TypeVar
if TYPE_CHECKING:
from .client import CommandsClient
from .context import Context
__all__ = ("evaluate_parameters",)
ClientT_Co = TypeVar("ClientT_Co", bound="CommandsClient", covariant=True)
ClientT_D = TypeVar("ClientT_D", bound="CommandsClient", default="CommandsClient")
ClientT_Co_D = TypeVar("ClientT_Co_D", bound="CommandsClient", default="CommandsClient", covariant=True)
ContextT = TypeVar("ContextT", bound="Context", default="Context")
def evaluate_parameters(parameters: Iterable[Parameter], globals: dict[str, Any]) -> list[Parameter]:
new_parameters: list[Parameter] = []
for parameter in parameters:
if parameter.annotation is not parameter.empty:
if isinstance(parameter.annotation, str):
parameter = parameter.replace(annotation=eval(parameter.annotation, globals))
new_parameters.append(parameter)
return new_parameters
+62
View File
@@ -0,0 +1,62 @@
from typing import Iterator
from typing_extensions import Self
from .errors import NoClosingQuote
class StringView:
def __init__(self, string: str):
self.value: Iterator[str] = iter(string)
self.temp: str = ""
self.should_undo: bool = False
def undo(self) -> None:
self.should_undo = True
def next_char(self) -> str:
return next(self.value)
def get_rest(self) -> str:
if self.should_undo:
return f"{self.temp} {''.join(self.value)}".rstrip()
# prevent a new space appearing at end if the buffer is depleted
return "".join(self.value)
def get_next_word(self) -> str:
if self.should_undo:
self.should_undo = False
return self.temp
char = self.next_char()
temp: list[str] = []
while char == " ":
char = self.next_char()
if char in ["\"", "'"]:
quote = char
try:
while (char := self.next_char()) != quote:
temp.append(char)
except StopIteration:
raise NoClosingQuote
else:
temp.append(char)
try:
while (char := self.next_char()) not in " \n":
temp.append(char)
except StopIteration:
pass
output = "".join(temp)
self.temp = output
return output
def __iter__(self) -> Self:
return self
def __next__(self) -> str:
return self.get_next_word()
+12 -9
View File
@@ -1,12 +1,13 @@
from __future__ import annotations
import io import io
import os from typing import Optional, Union, cast
from typing import Optional, Union
__all__ = ("File",) __all__ = ("File",)
class File: class File:
"""Respresents a file about to be uploaded to revolt """Respresents a file about to be uploaded to revolt
Parameters Parameters
----------- -----------
file: Union[str, bytes] file: Union[str, bytes]
@@ -17,19 +18,21 @@ class File:
Determines if the file will be a spoiler, this prefexes the filename with `SPOILER_` Determines if the file will be a spoiler, this prefexes the filename with `SPOILER_`
""" """
__slots__ = ("f", "spoiler", "filename") __slots__ = ("f", "spoiler", "filename")
def __init__(self, file: Union[str, bytes], *, filename: Optional[str] = None, spoiler: bool = False): def __init__(self, file: Union[str, bytes], *, filename: Optional[str] = None, spoiler: bool = False):
self.f: io.BufferedIOBase
if isinstance(file, str): if isinstance(file, str):
self.f = open(file, "rb") self.f = open(file, "rb")
elif isinstance(file, bytes): else:
self.f = io.BytesIO(file) self.f = io.BytesIO(file)
if filename is None and isinstance(file, str): if filename is None and isinstance(file, str):
filename = self.f.name filename = cast(Optional[str], self.f.name)
self.spoiler = spoiler or (filename and filename.startswith("SPOILER_")) self.spoiler: bool = spoiler or (bool(filename) and filename.startswith("SPOILER_"))
if self.spoiler and (filename and not filename.startswith("SPOILER_")): if self.spoiler and (filename and not filename.startswith("SPOILER_")):
filename = f"SPOILER_{filename}" filename = f"SPOILER_{filename}"
self.filename = filename self.filename: str | None = filename
+156
View File
@@ -0,0 +1,156 @@
from __future__ import annotations
from typing import Callable, Iterator, Optional, Union, overload
from typing_extensions import Self
__all__ = ("Flag", "Flags", "UserBadges")
class Flag:
__slots__ = ("flag", "__doc__")
def __init__(self, func: Callable[[], int]):
self.flag: int = func()
self.__doc__: str | None = func.__doc__
@overload
def __get__(self: Self, instance: None, owner: type[Flags]) -> Self:
...
@overload
def __get__(self, instance: Flags, owner: type[Flags]) -> bool:
...
def __get__(self: Self, instance: Optional[Flags], owner: type[Flags]) -> Union[Self, bool]:
if instance is None:
return self
return instance._check_flag(self.flag)
def __set__(self, instance: Flags, value: bool) -> None:
instance._set_flag(self.flag, value)
class Flags:
FLAG_NAMES: list[str]
def __init_subclass__(cls) -> None:
cls.FLAG_NAMES = []
for name in dir(cls):
value = getattr(cls, name)
if isinstance(value, Flag):
cls.FLAG_NAMES.append(name)
def __init__(self, value: int = 0, **flags: bool):
self.value = value
for k, v in flags.items():
setattr(self, k, v)
@classmethod
def _from_value(cls, value: int) -> Self:
self = cls.__new__(cls)
self.value = value
return self
def _check_flag(self, flag: int) -> bool:
return (self.value & flag) == flag
def _set_flag(self, flag: int, value: bool) -> None:
if value:
self.value |= flag
else:
self.value &= ~flag
def __eq__(self, other: Self) -> bool:
return self.value == other.value
def __ne__(self, other: Self) -> bool:
return not self.__eq__(other)
def __or__(self, other: Self) -> Self:
return self.__class__._from_value(self.value | other.value)
def __and__(self, other: Self) -> Self:
return self.__class__._from_value(self.value & other.value)
def __invert__(self) -> Self:
return self.__class__._from_value(~self.value)
def __add__(self, other: Self) -> Self:
return self | other
def __sub__(self, other: Self) -> Self:
return self & ~other
def __lt__(self, other: Self) -> bool:
return self.value < other.value
def __gt__(self, other: Self) -> bool:
return self.value > other.value
def __repr__(self) -> str:
return f"<{self.__class__.__name__} value={self.value}>"
def __iter__(self) -> Iterator[tuple[str, bool]]:
for name, value in self.__class__.__dict__.items():
if isinstance(value, Flag):
yield name, self._check_flag(value.flag)
def __hash__(self) -> int:
return hash(self.value)
class UserBadges(Flags):
"""Contains all user badges"""
@Flag
def developer():
""":class:`bool` The developer badge."""
return 1 << 0
@Flag
def translator():
""":class:`bool` The translator badge."""
return 1 << 1
@Flag
def supporter():
""":class:`bool` The supporter badge."""
return 1 << 2
@Flag
def responsible_disclosure():
""":class:`bool` The responsible disclosure badge."""
return 1 << 3
@Flag
def founder():
""":class:`bool` The founder badge."""
return 1 << 4
@Flag
def platform_moderation():
""":class:`bool` The platform moderation badge."""
return 1 << 5
@Flag
def active_supporter():
""":class:`bool` The active supporter badge."""
return 1 << 6
@Flag
def paw():
""":class:`bool` The paw badge."""
return 1 << 7
@Flag
def early_adopter():
""":class:`bool` The early adopter badge."""
return 1 << 8
@Flag
def reserved_relevant_joke_badge_1():
""":class:`bool` The reserved relevant joke badge 1 badge."""
return 1 << 9
+191 -103
View File
@@ -6,7 +6,8 @@ from typing import (TYPE_CHECKING, Any, Coroutine, Literal, Optional, TypeVar,
import aiohttp import aiohttp
import ulid import ulid
from .errors import HTTPError, ServerError
from .errors import Forbidden, HTTPError, ServerError
from .file import File from .file import File
try: try:
@@ -19,17 +20,18 @@ if TYPE_CHECKING:
from .enums import SortType from .enums import SortType
from .file import File from .file import File
from .types import ApiInfo
from .types import Autumn as AutumnPayload from .types import Autumn as AutumnPayload
from .types import Channel, DMChannel from .types import Emoji as EmojiPayload
from .types import Embed as EmbedPayload from .types import Interactions as InteractionsPayload
from .types import GetServerMembers, Member from .types import Masquerade as MasqueradePayload
from .types import Member as MemberPayload
from .types import Message as MessagePayload from .types import Message as MessagePayload
from .types import (MessageWithUserData, Role, Server, ServerBans, from .types import SendableEmbed as SendableEmbedPayload
ServerInvite, TextChannel)
from .types import User as UserPayload from .types import User as UserPayload
from .types import UserProfile, VoiceChannel from .types import (Server, ServerBans, TextChannel, UserProfile, VoiceChannel, Member, Invite, ApiInfo, Channel, SavedMessages,
DMChannel, EmojiParent, GetServerMembers, GroupDMChannel, MessageReplyPayload, MessageWithUserData, PartialInvite, CreateRole)
from aiohttp.client import _RequestOptions
__all__ = ("HttpClient",) __all__ = ("HttpClient",)
@@ -37,22 +39,23 @@ T = TypeVar("T")
Request = Coroutine[Any, Any, T] Request = Coroutine[Any, Any, T]
class HttpClient: class HttpClient:
__slots__ = ("session", "token", "api_url", "api_info") __slots__ = ("session", "token", "api_url", "api_info", "auth_header")
def __init__(self, session: aiohttp.ClientSession, token: str, api_url: str, api_info: ApiInfo): def __init__(self, session: aiohttp.ClientSession, token: str, api_url: str, api_info: ApiInfo, bot: bool = True):
self.session = session self.session: aiohttp.ClientSession = session
self.token = token self.token: str = token
self.api_url = api_url self.api_url: str = api_url
self.api_info = api_info self.api_info: ApiInfo = api_info
self.auth_header: str = "x-bot-token" if bot else "x-session-token"
async def request(self, method: Literal["GET", "POST", "PUT", "DELETE", "PATCH"], route: str, *, json: Optional[dict[str, Any]] = None, nonce: bool = True, params: Optional[dict[str, Any]] = None) -> Any: async def request(self, method: Literal["GET", "POST", "PUT", "DELETE", "PATCH"], route: str, *, json: Optional[dict[str, Any]] = None, nonce: bool = True, params: Optional[dict[str, Any]] = None) -> Any:
url = f"{self.api_url}{route}" url = f"{self.api_url}{route}"
kwargs = {} kwargs: _RequestOptions = {}
headers = { headers = {
"User-Agent": "Revolt.py https://github.com/Zomatree/revolt.py", "User-Agent": "Revolt.py (https://github.com/revoltchat/revolt.py)",
"x-bot-token": self.token self.auth_header: self.token
} }
if json: if json:
@@ -71,7 +74,10 @@ class HttpClient:
async with self.session.request(method, url, **kwargs) as resp: async with self.session.request(method, url, **kwargs) as resp:
text = await resp.text() text = await resp.text()
if text: if text:
response = _json.loads(await resp.text()) try:
response = _json.loads(await resp.text())
except ValueError:
raise HTTPError(f"Invalid json response:\n{text}") from None
else: else:
response = text response = text
@@ -79,14 +85,16 @@ class HttpClient:
if 200 <= resp_code <= 300: if 200 <= resp_code <= 300:
return response return response
elif resp_code == 400: elif resp_code == 401:
raise HTTPError raise Forbidden("401: Missing Permissions")
else:
raise HTTPError(resp_code)
async def upload_file(self, file: File, tag: str) -> AutumnPayload: async def upload_file(self, file: File, tag: Literal["attachments", "avatars", "backgrounds", "icons", "banners", "emojis"]) -> AutumnPayload:
url = f"{self.api_info['features']['autumn']['url']}/{tag}" url = f"{self.api_info['features']['autumn']['url']}/{tag}"
headers = { headers = {
"User-Agent": "Revolt.py https://github.com/Zomatree/revolt.py" "User-Agent": "Revolt.py (https://github.com/revoltchat/revolt.py)"
} }
form = aiohttp.FormData() form = aiohttp.FormData()
@@ -104,12 +112,12 @@ class HttpClient:
else: else:
return response return response
async def send_message(self, channel: str, content: Optional[str], embeds: Optional[list[EmbedPayload]], attachments: Optional[list[File]]) -> MessagePayload: async def send_message(self, channel: str, content: Optional[str], embeds: Optional[list[SendableEmbedPayload]], attachments: Optional[list[File]], replies: Optional[list[MessageReplyPayload]], masquerade: Optional[MasqueradePayload], interactions: Optional[InteractionsPayload]) -> MessagePayload:
json: dict[str, Any] = {} json: dict[str, Any] = {}
if content: if content:
json["content"] = content json["content"] = content
if embeds: if embeds:
json["embeds"] = embeds json["embeds"] = embeds
@@ -119,13 +127,29 @@ class HttpClient:
for attachment in attachments: for attachment in attachments:
data = await self.upload_file(attachment, "attachments") data = await self.upload_file(attachment, "attachments")
attachment_ids.append(data["id"]) attachment_ids.append(data["id"])
json["attachments"] = attachment_ids json["attachments"] = attachment_ids
if replies:
json["replies"] = replies
if masquerade:
json["masquerade"] = masquerade
if interactions:
json["interactions"] = interactions
return await self.request("POST", f"/channels/{channel}/messages", json=json) return await self.request("POST", f"/channels/{channel}/messages", json=json)
def edit_message(self, channel: str, message: str, content: str) -> Request[None]: def edit_message(self, channel: str, message: str, content: Optional[str], embeds: Optional[list[SendableEmbedPayload]] = None) -> Request[None]:
json = {"content": content} json: dict[str, Any] = {}
if content is not None:
json["content"] = content
if embeds is not None:
json["embeds"] = embeds
return self.request("PATCH", f"/channels/{channel}/messages/{message}", json=json) return self.request("PATCH", f"/channels/{channel}/messages/{message}", json=json)
def delete_message(self, channel: str, message: str) -> Request[None]: def delete_message(self, channel: str, message: str) -> Request[None]:
@@ -133,48 +157,48 @@ class HttpClient:
def fetch_message(self, channel: str, message: str) -> Request[MessagePayload]: def fetch_message(self, channel: str, message: str) -> Request[MessagePayload]:
return self.request("GET", f"/channels/{channel}/messages/{message}") return self.request("GET", f"/channels/{channel}/messages/{message}")
@overload @overload
def fetch_messages( def fetch_messages(
self, self,
channel: str, channel: str,
sort: SortType, sort: SortType,
*, *,
limit: Optional[int] = ..., limit: Optional[int] = ...,
before: Optional[str] = ..., before: Optional[str] = ...,
after: Optional[str] = ..., after: Optional[str] = ...,
nearby: Optional[str] = ..., nearby: Optional[str] = ...,
include_users: Literal[False] = ... include_users: Literal[False] = ...
) -> Request[list[MessagePayload]]: ) -> Request[list[MessagePayload]]:
... ...
@overload @overload
def fetch_messages( def fetch_messages(
self, self,
channel: str, channel: str,
sort: SortType, sort: SortType,
*, *,
limit: Optional[int] = ..., limit: Optional[int] = ...,
before: Optional[str] = ..., before: Optional[str] = ...,
after: Optional[str] = ..., after: Optional[str] = ...,
nearby: Optional[str] = ..., nearby: Optional[str] = ...,
include_users: Literal[True] = ... include_users: Literal[True] = ...
) -> Request[MessageWithUserData]: ) -> Request[MessageWithUserData]:
... ...
def fetch_messages( def fetch_messages(
self, self,
channel: str, channel: str,
sort: SortType, sort: SortType,
*, *,
limit: Optional[int] = None, limit: Optional[int] = None,
before: Optional[str] = None, before: Optional[str] = None,
after: Optional[str] = None, after: Optional[str] = None,
nearby: Optional[str] = None, nearby: Optional[str] = None,
include_users: bool = False include_users: bool = False
) -> Request[Union[list[MessagePayload], MessageWithUserData]]: ) -> Request[Union[list[MessagePayload], MessageWithUserData]]:
json = {"sort": sort.value, "include_users": str(include_users)} json: dict[str, Any] = {"sort": sort.value, "include_users": str(include_users)}
if limit: if limit:
json["limit"] = limit json["limit"] = limit
@@ -192,12 +216,12 @@ class HttpClient:
@overload @overload
def search_messages( def search_messages(
self, self,
channel: str, channel: str,
query: str, query: str,
*, *,
limit: Optional[int] = ..., limit: Optional[int] = ...,
before: Optional[str] = ..., before: Optional[str] = ...,
after: Optional[str] = ..., after: Optional[str] = ...,
sort: Optional[SortType] = ..., sort: Optional[SortType] = ...,
include_users: Literal[False] = ... include_users: Literal[False] = ...
@@ -206,12 +230,12 @@ class HttpClient:
@overload @overload
def search_messages( def search_messages(
self, self,
channel: str, channel: str,
query: str, query: str,
*, *,
limit: Optional[int] = ..., limit: Optional[int] = ...,
before: Optional[str] = ..., before: Optional[str] = ...,
after: Optional[str] = ..., after: Optional[str] = ...,
sort: Optional[SortType] = ..., sort: Optional[SortType] = ...,
include_users: Literal[True] = ... include_users: Literal[True] = ...
@@ -219,18 +243,18 @@ class HttpClient:
... ...
def search_messages( def search_messages(
self, self,
channel: str, channel: str,
query: str, query: str,
*, *,
limit: Optional[int] = None, limit: Optional[int] = None,
before: Optional[str] = None, before: Optional[str] = None,
after: Optional[str] = None, after: Optional[str] = None,
sort: Optional[SortType] = None, sort: Optional[SortType] = None,
include_users: bool = False include_users: bool = False
) -> Request[Union[list[MessagePayload], MessageWithUserData]]: ) -> Request[Union[list[MessagePayload], MessageWithUserData]]:
json = {"query": query, "include_users": include_users} json: dict[str, Any] = {"query": query, "include_users": include_users}
if limit: if limit:
json["limit"] = limit json["limit"] = limit
@@ -258,11 +282,11 @@ class HttpClient:
def fetch_default_avatar(self, user_id: str) -> Request[bytes]: def fetch_default_avatar(self, user_id: str) -> Request[bytes]:
return self.request_file(f"{self.api_url}/users/{user_id}/default_avatar") return self.request_file(f"{self.api_url}/users/{user_id}/default_avatar")
def fetch_dm_channels(self) -> Request[list[Channel]]: def fetch_dm_channels(self) -> Request[list[Union[DMChannel, GroupDMChannel]]]:
return self.request("GET", "/users/dms") return self.request("GET", "/users/dms")
def open_dm(self, user_id: str) -> Request[DMChannel]: def open_dm(self, user_id: str) -> Request[DMChannel | SavedMessages]:
return self.request("GET", f"/users/{user_id}/dm") return self.request("GET", f"/users/{user_id}/dm")
def fetch_channel(self, channel_id: str) -> Request[Channel]: def fetch_channel(self, channel_id: str) -> Request[Channel]:
@@ -271,20 +295,20 @@ class HttpClient:
def close_channel(self, channel_id: str) -> Request[None]: def close_channel(self, channel_id: str) -> Request[None]:
return self.request("DELETE", f"/channels/{channel_id}") return self.request("DELETE", f"/channels/{channel_id}")
def set_channel_role_permissions(self, channel_id: str, role_id: str, channel_permissions: int) -> Request[None]:
payload = {"permissions": channel_permissions}
return self.request("PUT", f"/channels/{channel_id}/permissions/{role_id}", json=payload)
def set_channel_default_permissions(self, channel_id: str, channel_permissions: int) -> Request[None]:
payload = {"permissions": channel_permissions}
return self.request("PUT", f"/channels/{channel_id}/permissions/default", json=payload)
def fetch_server(self, server_id: str) -> Request[Server]: def fetch_server(self, server_id: str) -> Request[Server]:
return self.request("GET", f"/servers/{server_id}") return self.request("GET", f"/servers/{server_id}")
def delete_leave_server(self, server_id: str) -> Request[None]: def delete_leave_server(self, server_id: str) -> Request[None]:
return self.request("DELETE", f"/servers/{server_id}") return self.request("DELETE", f"/servers/{server_id}")
@overload
def create_channel(self, server_id: str, channel_type: Literal["Text"], name: str, description: Optional[str]) -> Request[TextChannel]:
...
@overload
def create_channel(self, server_id: str, channel_type: Literal["Voice"], name: str, description: Optional[str]) -> Request[VoiceChannel]:
...
def create_channel(self, server_id: str, channel_type: Literal["Text", "Voice"], name: str, description: Optional[str]) -> Request[Union[TextChannel, VoiceChannel]]: def create_channel(self, server_id: str, channel_type: Literal["Text", "Voice"], name: str, description: Optional[str]) -> Request[Union[TextChannel, VoiceChannel]]:
payload = { payload = {
"type": channel_type, "type": channel_type,
@@ -296,7 +320,7 @@ class HttpClient:
return self.request("POST", f"/servers/{server_id}/channels", json=payload) return self.request("POST", f"/servers/{server_id}/channels", json=payload)
def fetch_server_invites(self, server_id: str) -> Request[list[ServerInvite]]: def fetch_server_invites(self, server_id: str) -> Request[list[PartialInvite]]:
return self.request("GET", f"/servers/{server_id}/invites") return self.request("GET", f"/servers/{server_id}/invites")
def fetch_member(self, server_id: str, member_id: str) -> Request[Member]: def fetch_member(self, server_id: str, member_id: str) -> Request[Member]:
@@ -315,32 +339,96 @@ class HttpClient:
def unban_member(self, server_id: str, member_id: str) -> Request[None]: def unban_member(self, server_id: str, member_id: str) -> Request[None]:
return self.request("DELETE", f"/servers/{server_id}/bans/{member_id}") return self.request("DELETE", f"/servers/{server_id}/bans/{member_id}")
def fetch_bans(self, server_id: str) -> Request[ServerBans]: def fetch_bans(self, server_id: str) -> Request[ServerBans]:
return self.request("GET", f"/servers/{server_id}/bans") return self.request("GET", f"/servers/{server_id}/bans")
def set_role_permissions(self, server_id: str, role_id: str, server_permissions: int, channel_permissions: int) -> Request[None]: def create_role(self, server_id: str, name: str) -> Request[CreateRole]:
payload = {
"permissions": {
"server": server_permissions,
"channel": channel_permissions
}
}
return self.request("PUT", f"/servers/{server_id}/permissions/{role_id}", json=payload, nonce=False)
def set_default_permissions(self, server_id: str, server_permissions: int, channel_permissions: int) -> Request[None]:
payload = {
"permissions": {
"server": server_permissions,
"channel": channel_permissions
}
}
return self.request("PUT", f"/servers/{server_id}/permissions/default", json=payload, nonce=False)
def create_role(self, server_id: str, name: str) -> Request[Role]:
return self.request("POST", f"/servers/{server_id}/roles", json={"name": name}, nonce=False) return self.request("POST", f"/servers/{server_id}/roles", json={"name": name}, nonce=False)
def delete_role(self, server_id: str, role_id: str) -> Request[None]: def delete_role(self, server_id: str, role_id: str) -> Request[None]:
return self.request("DELETE", f"/servers/{server_id}/roles/{role_id}") return self.request("DELETE", f"/servers/{server_id}/roles/{role_id}")
def fetch_invite(self, code: str) -> Request[Invite]:
return self.request("GET", f"/invites/{code}")
def delete_invite(self, code: str) -> Request[None]:
return self.request("DELETE", f"/invites/{code}")
def edit_channel(self, channel_id: str, remove: list[str] | None, values: dict[str, Any]) -> Request[None]:
if remove:
values["remove"] = remove
return self.request("PATCH", f"/channels/{channel_id}", json=values)
def edit_role(self, server_id: str, role_id: str, remove: list[str] | None, values: dict[str, Any]) -> Request[None]:
if remove:
values["remove"] = remove
return self.request("PATCH", f"/servers/{server_id}/roles/{role_id}", json=values)
async def edit_self(self, remove: list[str] | None, values: dict[str, Any]) -> Request[None]:
if remove:
values["remove"] = remove
if avatar := values.get("avatar"):
asset = await self.upload_file(avatar, "avatars")
values["avatar"] = asset["id"]
if profile := values.get("profile"):
if background := profile.background():
asset = await self.upload_file(background, "backgrounds")
profile["background"] = asset["id"]
return await self.request("PATCH", "/users/@me", json=values)
def set_guild_channel_default_permissions(self, channel_id: str, allow: int, deny: int) -> Request[None]:
return self.request("PUT", f"/channels/{channel_id}/permissions/default", json={"permissions": {"allow": allow, "deny": deny}})
def set_guild_channel_role_permissions(self, channel_id: str, role_id: str, allow: int, deny: int) -> Request[None]:
return self.request("PUT", f"/channels/{channel_id}/permissions/{role_id}", json={"permissions": {"allow": allow, "deny": deny}})
def set_group_channel_default_permissions(self, channel_id: str, value: int) -> Request[None]:
return self.request("PUT", f"/channels/{channel_id}/permissions/default", json={"permissions": value})
def set_server_role_permissions(self, server_id: str, role_id: str, allow: int, deny: int) -> Request[None]:
return self.request("PUT", f"/servers/{server_id}/permissions/{role_id}", json={"permissions": {"allow": allow, "deny": deny}})
def set_server_default_permissions(self, server_id: str, value: int) -> Request[None]:
return self.request("PUT", f"/servers/{server_id}/permissions/default", json={"permissions": value})
def add_reaction(self, channel_id: str, message_id: str, emoji: str) -> Request[None]:
return self.request("PUT", f"/channels/{channel_id}/messages/{message_id}/reactions/{emoji}")
def remove_reaction(self, channel_id: str, message_id: str, emoji: str, user_id: Optional[str], remove_all: bool) -> Request[None]:
parameters: dict[str, str] = {}
if user_id:
parameters["user_id"] = user_id
parameters["remove_all"] = "true" if remove_all else "false"
return self.request("DELETE", f"/channels/{channel_id}/messages/{message_id}/reactions/{emoji}", params=parameters)
def remove_all_reactions(self, channel_id: str, message_id: str) -> Request[None]:
return self.request("DELETE", f"/channels/{channel_id}/messages/{message_id}/reactions")
def delete_emoji(self, emoji_id: str) -> Request[None]:
return self.request("DELETE", f"/custom/emoji/{emoji_id}")
def fetch_emoji(self, emoji_id: str) -> Request[EmojiPayload]:
return self.request("GET", f"/custom/emoji/{emoji_id}")
async def create_emoji(self, name: str, file: File, nsfw: bool, parent: EmojiParent) -> EmojiPayload:
asset = await self.upload_file(file, "emojis")
return await self.request("PUT", f"/custom/emoji/{asset['id']}", json={"name": name, "parent": parent, "nsfw": nsfw})
def edit_member(self, server_id: str, member_id: str, remove: list[str] | None, values: dict[str, Any]) -> Request[MemberPayload]:
if remove:
values["remove"] = remove
return self.request("PATCH", f"/servers/{server_id}/members/{member_id}", json=values)
def delete_messages(self, channel_id: str, messages: list[str]) -> Request[None]:
return self.request("DELETE", f"/channels/{channel_id}/messages/bulk", json={"ids": messages})
+81
View File
@@ -0,0 +1,81 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from .asset import Asset
from .utils import Ulid
if TYPE_CHECKING:
from .state import State
from .channel import Channel
from .server import Server
from .types import Invite as InvitePayload
from .user import User
__all__ = ("Invite",)
class Invite(Ulid):
"""Represents a server invite.
Attributes
-----------
code: :class:`str`
The code for the invite
id: :class:`str`
Alias for :attr:`code`
server: :class:`Server`
The server this invite is for
channel: :class:`Channel`
The channel this invite is for
user_name: :class:`str`
The name of the user who made the invite
user: Optional[:class:`User`]
The user who made the invite, this is only set if this was fetched via :meth:`Server.fetch_invites`
user_avatar: Optional[:class:`Asset`]
The invite creator's avatar, if any
member_count: :class:`int`
The member count of the server this invite is for
"""
__slots__ = ("state", "code", "id", "server", "channel", "user_name", "user_avatar", "user", "member_count")
def __init__(self, data: InvitePayload, code: str, state: State):
self.state: State = state
self.code: str = code
self.id: str = code
self.server: Server = state.get_server(data["server_id"])
self.channel: Channel = self.server.get_channel(data["channel_id"])
self.user_name: str = data["user_name"]
self.user: User | None = None
self.user_avatar: Asset | None
if avatar := data.get("user_avatar"):
self.user_avatar = Asset(avatar, state)
else:
self.user_avatar = None
self.member_count: int = data["member_count"]
@staticmethod
def _from_partial(code: str, server: str, creator: str, channel: str, state: State) -> Invite:
invite = Invite.__new__(Invite)
invite.state = state
invite.code = code
invite.server = state.get_server(server)
invite.channel = state.get_channel(channel)
invite.user = state.get_user(creator)
invite.user_name = invite.user.name
invite.user_avatar = invite.user.avatar
invite.member_count = len(invite.server.members)
return invite
async def delete(self) -> None:
"""Deletes the invite"""
await self.state.http.delete_invite(self.code)
+210 -21
View File
@@ -1,24 +1,34 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, Optional import datetime
from typing import TYPE_CHECKING, Any, Optional
from .utils import _Missing, Missing, parse_timestamp
from .asset import Asset
from .permissions import Permissions
from .permissions_calculator import calculate_permissions
from .user import User from .user import User
from .file import File
if TYPE_CHECKING: if TYPE_CHECKING:
from .channel import Channel
from .server import Server from .server import Server
from .state import State from .state import State
from .types import File as FilePayload
from .types import Member as MemberPayload from .types import Member as MemberPayload
from .role import Role
__all__ = ("Member",) __all__ = ("Member",)
def flattern_user(member: Member, user: User): def flattern_user(member: Member, user: User) -> None:
for attr in user.__flattern_attributes__: for attr in user.__flattern_attributes__:
setattr(member, attr, getattr(user, attr)) setattr(member, attr, getattr(user, attr))
class Member(User): class Member(User):
"""Represents a member of a server, subclasses :class:`User` """Represents a member of a server, subclasses :class:`User`
Attributes Attributes
----------- -----------
nickname: Optional[:class:`str`] nickname: Optional[:class:`str`]
@@ -27,31 +37,210 @@ class Member(User):
The roles of the member, ordered by the role's rank in decending order The roles of the member, ordered by the role's rank in decending order
server: :class:`Server` server: :class:`Server`
The server the member belongs to The server the member belongs to
guild_avatar: Optional[:class:`Asset`]
The member's guild avatar if any
""" """
__slots__ = ("_state", "nickname", "roles", "server") __slots__ = ("state", "nickname", "roles", "server", "guild_avatar", "joined_at", "current_timeout")
def __init__(self, data: MemberPayload, server: Server, state: State): def __init__(self, data: MemberPayload, server: Server, state: State):
user = state.get_user(data["_id"]["user"]) user = state.get_user(data["_id"]["user"])
assert user
# due to not having a user payload and only a user object we have to manually add all the attributes instead of calling User.__init__
flattern_user(self, user) flattern_user(self, user)
user._members[server.id] = self
self._state = state self.state: State = state
self.nickname = data.get("nickname")
roles = []
for role in (server.get_role(role_id) for role_id in data.get("roles", [])): self.guild_avatar: Asset | None
if role:
roles.append(role)
self.roles = sorted(roles, key=lambda role: role.rank, reverse=True) if avatar := data.get("avatar"):
self.guild_avatar = Asset(avatar, state)
self.server = server else:
self.guild_avatar = None
roles = [server.get_role(role_id) for role_id in data.get("roles", [])]
self.roles: list[Role] = sorted(roles, key=lambda role: role.rank, reverse=True)
self.server: Server = server
self.nickname: str | None = data.get("nickname")
self.joined_at: datetime.datetime = parse_timestamp(data["joined_at"])
self.current_timeout: datetime.datetime | None
if current_timeout := data.get("timeout"):
self.current_timeout = parse_timestamp(current_timeout)
else:
self.current_timeout = None
@property @property
def owner(self) -> Optional[User]: def avatar(self) -> Optional[Asset]:
owner_id = self.owner_id """Optional[:class:`Asset`] The avatar the member is displaying, this includes guild avatars and masqueraded avatar"""
return self.masquerade_avatar or self.guild_avatar or self.original_avatar
if not owner_id: @property
return def name(self) -> str:
""":class:`str` The name the user is displaying, this includes (in order) their masqueraded name, display name and orginal name"""
return self.nickname or self.display_name or self.masquerade_name or self.original_name
return self.state.get_user(owner_id) @property
def mention(self) -> str:
""":class:`str`: Returns a string that allows you to mention the given member."""
return f"<@{self.id}>"
def _update(
self,
*,
nickname: Optional[str] = None,
avatar: Optional[FilePayload] = None,
roles: Optional[list[str]] = None,
timeout: Optional[str | int] = None
) -> None:
if nickname is not None:
self.nickname = nickname
if avatar is not None:
self.guild_avatar = Asset(avatar, self.state)
if roles is not None:
member_roles = [self.server.get_role(role_id) for role_id in roles]
self.roles = sorted(member_roles, key=lambda role: role.rank, reverse=True)
if timeout is not None:
self.current_timeout = parse_timestamp(timeout)
async def kick(self) -> None:
"""Kicks the member from the server"""
await self.state.http.kick_member(self.server.id, self.id)
async def ban(self, *, reason: Optional[str] = None) -> None:
"""Bans the member from the server
Parameters
-----------
reason: Optional[:class:`str`]
The reason for the ban
"""
await self.state.http.ban_member(self.server.id, self.id, reason)
async def unban(self) -> None:
"""Unbans the member from the server"""
await self.state.http.unban_member(self.server.id, self.id)
async def edit(
self,
*,
nickname: str | None | _Missing = Missing,
roles: list[Role] | None | _Missing = Missing,
avatar: File | None | _Missing = Missing,
timeout: datetime.timedelta | None | _Missing = Missing
) -> None:
"""Edits the member
Parameters
-----------
nickname: Union[:class:`str`, :class:`None`]
The new nickname, or :class:`None` to reset it
roles: Union[list[:class:`Role`], :class:`None`]
The new roles for the member, or :class:`None` to clear it
avatar: Union[:class:`File`, :class:`None`]
The new server avatar, or :class:`None` to reset it
timeout: Union[:class:`datetime.timedelta`, :class:`None`]
The new timeout length for the member, or :class:`None` to reset it
"""
remove: list[str] = []
data: dict[str, Any] = {}
if nickname is None:
remove.append("Nickname")
elif nickname is not Missing:
data["nickname"] = nickname
if roles is None:
remove.append("Roles")
elif not isinstance(roles, _Missing):
data["roles"] = [role.id for role in roles]
if avatar is None:
remove.append("Avatar")
elif not isinstance(avatar, _Missing):
data["avatar"] = (await self.state.http.upload_file(avatar, "avatars"))["id"]
if timeout is None:
remove.append("Timeout")
elif not isinstance(timeout, _Missing):
data["timeout"] = (datetime.datetime.now(datetime.timezone.utc) + timeout).isoformat()
await self.state.http.edit_member(self.server.id, self.id, remove, data)
async def timeout(self, length: datetime.timedelta) -> None:
"""Timeouts the member
Parameters
-----------
length: :class:`datetime.timedelta`
The length of the timeout
"""
ends_at = datetime.datetime.now(tz=datetime.timezone.utc) + length
await self.state.http.edit_member(self.server.id, self.id, None, {"timeout": ends_at.isoformat()})
def get_permissions(self) -> Permissions:
"""Gets the permissions for the member in the server
Returns
--------
:class:`Permissions`
The members permissions
"""
return calculate_permissions(self, self.server)
def get_channel_permissions(self, channel: Channel) -> Permissions:
"""Gets the permissions for the member in the server taking into account the channel as well
Parameters
-----------
channel: :class:`Channel`
The channel to calculate permissions with
Returns
--------
:class:`Permissions`
The members permissions
"""
return calculate_permissions(self, channel)
def has_permissions(self, **permissions: bool) -> bool:
"""Computes if the member has the specified permissions
Parameters
-----------
permissions: :class:`bool`
The permissions to check, this also accepted `False` if you need to check if the member does not have the permission
Returns
--------
:class:`bool`
Whether or not they have the permissions
"""
calculated_perms = self.get_permissions()
return all([getattr(calculated_perms, key, False) == value for key, value in permissions.items()])
def has_channel_permissions(self, channel: Channel, **permissions: bool) -> bool:
"""Computes if the member has the specified permissions, taking into account the channel as well
Parameters
-----------
channel: :class:`Channel`
The channel to calculate permissions with
permissions: :class:`bool`
The permissions to check, this also accepted `False` if you need to check if the member does not have the permission
Returns
--------
:class:`bool`
Whether or not they have the permissions
"""
calculated_perms = self.get_channel_permissions(channel)
return all([getattr(calculated_perms, key, False) == value for key, value in permissions.items()])
+261 -37
View File
@@ -1,22 +1,35 @@
from __future__ import annotations from __future__ import annotations
import datetime import datetime
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Any, Coroutine, Optional, Union
from .asset import Asset
from .channel import Messageable from .asset import Asset, PartialAsset
from .embed import Embed from .channel import DMChannel, GroupDMChannel, TextChannel, SavedMessageChannel
from .embed import Embed, SendableEmbed, to_embed
from .utils import Ulid, parse_timestamp
if TYPE_CHECKING: if TYPE_CHECKING:
from .server import Server
from .state import State from .state import State
from .types import Embed as EmbedPayload
from .types import Interactions as InteractionsPayload
from .types import Masquerade as MasqueradePayload
from .types import Message as MessagePayload from .types import Message as MessagePayload
from .types import MessageReplyPayload, SystemMessageContent
from .user import User
from .member import Member
__all__ = (
"Message",
"MessageReply",
"Masquerade",
"MessageInteractions"
)
__all__ = ("Message",) class Message(Ulid):
class Message:
"""Represents a message """Represents a message
Attributes Attributes
----------- -----------
id: :class:`str` id: :class:`str`
@@ -25,62 +38,273 @@ class Message:
The content of the message, this will not include system message's content The content of the message, this will not include system message's content
attachments: list[:class:`Asset`] attachments: list[:class:`Asset`]
The attachments of the message The attachments of the message
embeds: list[:class:`Embed`] embeds: list[Union[:class:`WebsiteEmbed`, :class:`ImageEmbed`, :class:`TextEmbed`, :class:`NoneEmbed`]]
The embeds of the message The embeds of the message
channel: :class:`Messageable` channel: :class:`Messageable`
The channel the message was sent in The channel the message was sent in
server: :class:`Server`
The server the message was sent in
author: Union[:class:`Member`, :class:`User`] author: Union[:class:`Member`, :class:`User`]
The author of the message, will be :class:`User` in DMs The author of the message, will be :class:`User` in DMs
edited_at: Optional[:class:`datetime.datetime`] edited_at: Optional[:class:`datetime.datetime`]
The time at which the message was edited, will be None if the message has not been edited The time at which the message was edited, will be None if the message has not been edited
raw_mentions: list[:class:`str`]
A list of ids of the mentions in this message
replies: list[:class:`Message`]
The message's this message has replied to, this may not contain all the messages if they are outside the cache
reply_ids: list[:class:`str`]
The message's ids this message has replies to
reactions: dict[str, list[:class:`User`]]
The reactions on the message
interactions: Optional[:class:`MessageInteractions`]
The interactions on the message, if any
""" """
__slots__ = ("state", "id", "content", "attachments", "embeds", "channel", "server", "author", "edited_at") __slots__ = ("state", "id", "content", "attachments", "embeds", "channel", "author", "edited_at", "replies", "reply_ids", "reactions", "interactions")
def __init__(self, data: MessagePayload, state: State): def __init__(self, data: MessagePayload, state: State):
self.state = state self.state: State = state
self.id = data["_id"] self.id: str = data["_id"]
self.content = data["content"] self.content: str = data.get("content", "")
self.attachments = [Asset(attachment, state) for attachment in data.get("attachments", [])]
self.embeds = [Embed.from_dict(embed) for embed in data.get("embeds", [])] self.system_content: SystemMessageContent | None = data.get("system")
self.attachments: list[Asset] = [Asset(attachment, state) for attachment in data.get("attachments", [])]
self.embeds: list[Embed] = [to_embed(embed, state) for embed in data.get("embeds", [])]
channel = state.get_channel(data["channel"]) channel = state.get_channel(data["channel"])
assert isinstance(channel, Messageable) assert isinstance(channel, (TextChannel, GroupDMChannel, DMChannel, SavedMessageChannel))
self.channel = channel self.channel: TextChannel | GroupDMChannel | DMChannel | SavedMessageChannel = channel
self.server = self.channel and self.channel.server self.server_id: str | None = self.channel.server_id
if self.server: self.raw_mentions: list[str] = data.get("mentions", [])
author = state.get_member(self.server.id, data["author"])
if self.system_content:
author_id: str = self.system_content.get("id", data["author"])
else: else:
author = state.get_user(data["author"]) author_id = data["author"]
assert author if self.server_id:
self.author = author author = state.get_member(self.server_id, author_id)
self.edited_at: Optional[datetime.datetime] = None else:
author = state.get_user(author_id)
def _update(self, *, content: Optional[str] = None, edited_at: Optional[str] = None) -> Message: self.author: Member | User = author
if content:
if masquerade := data.get("masquerade"):
if name := masquerade.get("name"):
self.author.masquerade_name = name
if avatar := masquerade.get("avatar"):
self.author.masquerade_avatar = PartialAsset(avatar, state)
if edited_at := data.get("edited"):
self.edited_at: Optional[datetime.datetime] = parse_timestamp(edited_at)
self.replies: list[Message] = []
self.reply_ids: list[str] = []
for reply in data.get("replies", []):
try:
message = state.get_message(reply)
self.replies.append(message)
except LookupError:
pass
self.reply_ids.append(reply)
reactions = data.get("reactions", {})
self.reactions: dict[str, list[User]] = {}
for emoji, users in reactions.items():
self.reactions[emoji] = [self.state.get_user(user_id) for user_id in users]
self.interactions: MessageInteractions | None
if interactions := data.get("interactions"):
self.interactions = MessageInteractions(reactions=interactions.get("reactions"), restrict_reactions=interactions.get("restrict_reactions", False))
else:
self.interactions = None
def _update(self, *, content: Optional[str] = None, embeds: Optional[list[EmbedPayload]] = None, edited: Optional[Union[str, int]] = None):
if content is not None:
self.content = content self.content = content
if edited_at: if embeds is not None:
self.edited_at = datetime.datetime.strptime(edited_at, "%Y-%m-%dT%H:%M:%S.%f%z") self.embeds = [to_embed(embed, self.state) for embed in embeds]
# strptime is used here instead of fromisoformat because of its inability to parse `Z` (Zulu or UTC time) in the RFCC 3339 format provided by API
return self if edited is not None:
self.edited_at = parse_timestamp(edited)
async def edit(self, *, content: str) -> None: @property
def mentions(self) -> list[User | Member]:
"""The users or members that where mentioned in the message
Returns: list[Union[:class:`Member`, :class:`User`]]
"""
mentions: list[User | Member] = []
if self.server_id:
for mention in self.raw_mentions:
try:
mentions.append(self.server.get_member(mention))
except LookupError:
pass
else:
for mention in self.raw_mentions:
try:
mentions.append(self.state.get_user(mention))
except LookupError:
pass
return mentions
async def edit(self, *, content: Optional[str] = None, embeds: Optional[list[SendableEmbed]] = None) -> None:
"""Edits the message. The bot can only edit its own message """Edits the message. The bot can only edit its own message
Parameters Parameters
----------- -----------
content: :class:`str` content: :class:`str`
The new content of the message The new content of the message
embeds: list[:class:`SendableEmbed`]
The new embeds of the message
""" """
await self.state.http.edit_message(self.channel.id, self.id, content)
new_embeds = [embed.to_dict() for embed in embeds] if embeds else None
await self.state.http.edit_message(self.channel.id, self.id, content, new_embeds)
async def delete(self) -> None: async def delete(self) -> None:
"""Deletes the message. The bot can only delete its own messages and messages it has permission to delete """ """Deletes the message. The bot can only delete its own messages and messages it has permission to delete """
await self.state.http.delete_message(self.channel.id, self.id) await self.state.http.delete_message(self.channel.id, self.id)
def reply(self, *args: Any, mention: bool = False, **kwargs: Any) -> Coroutine[Any, Any, Message]:
"""Replies to this message, equivilant to:
.. code-block:: python
await channel.send(..., replies=[MessageReply(message, mention)])
"""
return self.channel.send(*args, **kwargs, replies=[MessageReply(self, mention)])
async def add_reaction(self, emoji: str) -> None:
"""Adds a reaction to the message
Parameters
-----------
emoji: :class:`str`
The emoji to add as a reaction
"""
await self.state.http.add_reaction(self.channel.id, self.id, emoji)
async def remove_reaction(self, emoji: str, user: Optional[User] = None, remove_all: bool = False) -> None:
"""Removes a reaction from the message, this can remove either a specific users, the current users reaction or all of a specific emoji
Parameters
-----------
emoji: :class:`str`
The emoji to remove
user: Optional[:class:`User`]
The user to use for removing a reaction from
remove_all: bool
Whether or not to remove all reactions for that specific emoji
"""
await self.state.http.remove_reaction(self.channel.id, self.id, emoji, user.id if user else None, remove_all)
async def remove_all_reactions(self) -> None:
"""Removes all reactions from the message"""
await self.state.http.remove_all_reactions(self.channel.id, self.id)
@property
def server(self) -> Server:
""":class:`Server` The server this voice channel belongs too
Raises
-------
:class:`LookupError`
Raises if the channel is not part of a server
"""
return self.channel.server
class MessageReply:
"""represents a reply to a message.
Parameters
-----------
message: :class:`Message`
The message being replied to.
mention: :class:`bool`
Whether the reply should mention the author of the message. Defaults to false.
"""
__slots__ = ("message", "mention")
def __init__(self, message: Ulid, mention: bool = False):
self.message: Ulid = message
self.mention: bool = mention
def to_dict(self) -> MessageReplyPayload:
return {"id": self.message.id, "mention": self.mention}
class Masquerade:
"""represents a message's masquerade.
Parameters
-----------
name: Optional[:class:`str`]
The name to display for the message
avatar: Optional[:class:`str`]
The avatar's url to display for the message
colour: Optional[:class:`str`]
The colour of the name, similar to role colours
"""
__slots__ = ("name", "avatar", "colour")
def __init__(self, name: Optional[str] = None, avatar: Optional[str] = None, colour: Optional[str] = None):
self.name: str | None = name
self.avatar: str | None = avatar
self.colour: str | None = colour
def to_dict(self) -> MasqueradePayload:
output: MasqueradePayload = {}
if name := self.name:
output["name"] = name
if avatar := self.avatar:
output["avatar"] = avatar
if colour := self.colour:
output["colour"] = colour
return output
class MessageInteractions:
"""Represents a message's interactions, this is for allowing preset reactions and restricting adding reactions to only those.
Parameters
-----------
reactions: Optional[list[:class:`str`]]
The preset reactions on the message
restrict_reactions: bool
Whether or not users can only react to the interaction's reactions
"""
__slots__ = ("reactions", "restrict_reactions")
def __init__(self, *, reactions: Optional[list[str]] = None, restrict_reactions: bool = False):
self.reactions: list[str] | None = reactions
self.restrict_reactions: bool = restrict_reactions
def to_dict(self) -> InteractionsPayload:
output: InteractionsPayload = {}
if reactions := self.reactions:
output["reactions"] = reactions
if restrict_reactions := self.restrict_reactions:
output["restrict_reactions"] = restrict_reactions
return output
+128 -9
View File
@@ -2,29 +2,34 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional
from .enums import SortType
if TYPE_CHECKING: if TYPE_CHECKING:
from .embed import Embed from .embed import SendableEmbed
from .file import File from .file import File
from .message import Message from .message import Masquerade, Message, MessageInteractions, MessageReply
from .state import State from .state import State
from .types.http import MessageWithUserData
__all__ = ("Messageable",) __all__ = ("Messageable",)
class Messageable: class Messageable:
"""Base class for all channels that you can send messages in """Base class for all channels that you can send messages in
Attributes Attributes
----------- -----------
id: :class:`str` id: :class:`str`
The id of the channel The id of the channel
""" """
id: str
state: State state: State
__slots__ = () __slots__ = ()
async def send(self, content: Optional[str] = None, *, embeds: Optional[list[Embed]] = None, embed: Optional[Embed] = None, attachments: Optional[list[File]] = None) -> Message: async def _get_channel_id(self) -> str:
raise NotImplementedError
async def send(self, content: Optional[str] = None, *, embeds: Optional[list[SendableEmbed]] = None, embed: Optional[SendableEmbed] = None, attachments: Optional[list[File]] = None, replies: Optional[list[MessageReply]] = None, reply: Optional[MessageReply] = None, masquerade: Optional[Masquerade] = None, interactions: Optional[MessageInteractions] = None) -> Message:
"""Sends a message in a channel, you must send at least one of either `content`, `embeds` or `attachments` """Sends a message in a channel, you must send at least one of either `content`, `embeds` or `attachments`
Parameters Parameters
@@ -33,8 +38,16 @@ class Messageable:
The content of the message, this will not include system message's content The content of the message, this will not include system message's content
attachments: Optional[list[:class:`File`]] attachments: Optional[list[:class:`File`]]
The attachments of the message The attachments of the message
embeds: Optional[list[:class:`Embed`]] embed: Optional[:class:`SendableEmbed`]
The embeds of the message The embed to send with the message
embeds: Optional[list[:class:`SendableEmbed`]]
The embeds to send with the message
replies: Optional[list[:class:`MessageReply`]]
The list of messages to reply to.
masquerade: Optional[:class:`Masquerade`]
The masquerade for the message, this can overwrite the username and avatar shown
interactions: Optional[:class:`MessageInteractions`]
The interactions for the message
Returns Returns
-------- --------
@@ -44,7 +57,113 @@ class Messageable:
if embed: if embed:
embeds = [embed] embeds = [embed]
embed_payload = [embed.to_dict() for embed in embeds] if embeds else None if reply:
replies = [reply]
message = await self.state.http.send_message(self.id, content, embed_payload, attachments) embed_payload = [embed.to_dict() for embed in embeds] if embeds else None
reply_payload = [reply.to_dict() for reply in replies] if replies else None
masquerade_payload = masquerade.to_dict() if masquerade else None
interactions_payload = interactions.to_dict() if interactions else None
message = await self.state.http.send_message(await self._get_channel_id(), content, embed_payload, attachments, reply_payload, masquerade_payload, interactions_payload)
return self.state.add_message(message) return self.state.add_message(message)
async def fetch_message(self, message_id: str) -> Message:
"""Fetches a message from the channel
Parameters
-----------
message_id: :class:`str`
The id of the message you want to fetch
Returns
--------
:class:`Message`
The message with the matching id
"""
from .message import Message
payload = await self.state.http.fetch_message(await self._get_channel_id(), message_id)
return Message(payload, self.state)
def _add_missing_users(self, payload: MessageWithUserData):
for user in payload["users"]:
if user["_id"] not in self.state.users:
self.state.add_user(user)
if members := payload.get("members", []):
server = self.state.get_server(members[0]["_id"]["server"])
for member in members:
if member["_id"]["user"] not in server._members:
server._add_member(member)
async def history(self, *, sort: SortType = SortType.latest, limit: int = 100, before: Optional[str] = None, after: Optional[str] = None, nearby: Optional[str] = None) -> list[Message]:
"""Fetches multiple messages from the channel's history
Parameters
-----------
sort: :class:`SortType`
The order to sort the messages in
limit: :class:`int`
How many messages to fetch
before: Optional[:class:`str`]
The id of the message which should come *before* all the messages to be fetched
after: Optional[:class:`str`]
The id of the message which should come *after* all the messages to be fetched
nearby: Optional[:class:`str`]
The id of the message which should be nearby all the messages to be fetched
Returns
--------
list[:class:`Message`]
The messages found in order of the sort parameter
"""
from .message import Message
payload = await self.state.http.fetch_messages(await self._get_channel_id(), sort=sort, limit=limit, before=before, after=after, nearby=nearby, include_users=True)
self._add_missing_users(payload)
return [Message(msg, self.state) for msg in payload["messages"]]
async def search(self, query: str, *, sort: SortType = SortType.latest, limit: int = 100, before: Optional[str] = None, after: Optional[str] = None) -> list[Message]:
"""searches the channel for a query
Parameters
-----------
query: :class:`str`
The query to search for in the channel
sort: :class:`SortType`
The order to sort the messages in
limit: :class:`int`
How many messages to fetch
before: Optional[:class:`str`]
The id of the message which should come *before* all the messages to be fetched
after: Optional[:class:`str`]
The id of the message which should come *after* all the messages to be fetched
Returns
--------
list[:class:`Message`]
The messages found in order of the sort parameter
"""
from .message import Message
payload = await self.state.http.search_messages(await self._get_channel_id(), query, sort=sort, limit=limit, before=before, after=after, include_users=True)
self._add_missing_users(payload)
return [Message(msg, self.state) for msg in payload["messages"]]
async def delete_messages(self, messages: list[Message]) -> None:
"""Bulk deletes messages from the channel
.. note:: The messages must have been sent in the last 7 days.
Parameters
-----------
messages: list[:class:`Message`]
The messages for deletion, this can be up to 100 messages
"""
await self.state.http.delete_messages(await self._get_channel_id(), [message.id for message in messages])
+210 -328
View File
@@ -1,351 +1,233 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Any, Optional
if TYPE_CHECKING: from typing_extensions import Self
from .types import Permission as PermissionTuple
from .flags import Flag, Flags
from .types.permissions import Overwrite
__all__ = ( __all__ = ("Permissions", "PermissionsOverwrite", "UserPermissions")
"ChannelPermissions",
"ServerPermissions"
)
# Channel permissions class UserPermissions(Flags):
# """Permissions for users"""
# View = 0b00000000000000000000000000000001 // 1
# SendMessage = 0b00000000000000000000000000000010 // 2
# ManageMessages = 0b00000000000000000000000000000100 // 4
# ManageChannel = 0b00000000000000000000000000001000 // 8
# VoiceCall = 0b00000000000000000000000000010000 // 16
# InviteOthers = 0b00000000000000000000000000100000 // 32
# EmbedLinks = 0b00000000000000000000000001000000 // 64
# UploadFiles = 0b00000000000000000000000010000000 // 128
@Flag
def access() -> int:
return 1 << 0
# Server permissions @Flag
# def view_profile() -> int:
# View = 0b00000000000000000000000000000001 // 1 return 1 << 1
# ManageRoles = 0b00000000000000000000000000000010 // 2
# ManageChannels = 0b00000000000000000000000000000100 // 4
# ManageServer = 0b00000000000000000000000000001000 // 8
# KickMembers = 0b00000000000000000000000000010000 // 16
# BanMembers = 0b00000000000000000000000000100000 // 32
# ChangeNickname = 0b00000000000000000001000000000000 // 4096 @Flag
# ManageNicknames = 0b00000000000000000010000000000000 // 8192 def send_message() -> int:
# ChangeAvatar = 0b00000000000000000100000000000000 // 16382 return 1 << 2
# RemoveAvatars = 0b00000000000000001000000000000000 // 32768
class _Permission: @Flag
def invite() -> int:
__slots__ = ("_value",) return 1 << 3
def __init__(self, value: int):
self._value = value
def __or__(self, p: _Permission):
self._value |= p._value
return self
def __and__(self, p: _Permission):
self._value &= p._value
return self
def __invert__(self):
self._value = ~self._value
return self
def __add__(self, p: _Permission):
# For example we have a bit array `0011` representing permissions
# To allow a permission we would convert the bit for that permission from 0 to 1
# Let's say we want to allow permissions with bit array `0101`
# Our desired result is `0111` which can be achieved by simple OR (A+B)
return self | p
def __sub__(self, p: _Permission):
# For example we have a bit array `0011` representing permissions
# To deny a permission we would convert the bit for that permission from 1 to 0
# Let's say we want to deny permissions with bit array `0101`
# Our desired result is `0010` which can be achieved by Selective Clear Algorithm (A.B')
return self & ~p
def __lt__(self, p: _Permission):
return self._value < p._value
def __gt__(self, p: _Permission):
return self._value > p._value
def __eq__(self, p: _Permission):
return self._value == p._value
def __repr__(self):
return f"{self.__class__.__name__}<{self._value:032b}>"
def _check(self, bit_pos: int):
# To check if a permission is present we would need to compare the bit for that position in our bit array
# For example if we have permission `0100` and we need to check if it is present in our bit array `0101`
# We need to compare the 3rd bit from the left, so we shift it 2 places (n-1) and perform AND with 1
# If the result is 1 the bit was 1 and the permission is present
return (self._value >> bit_pos) & 1 == 1
class ChannelPermissions(_Permission):
"""Represents the channel permissions for a role as seen in channel settings."""
__slots__ = ()
@property
def value(self) -> int:
return self._value
@classmethod @classmethod
def none(cls) -> ChannelPermissions: def all(cls) -> Self:
return cls(0) return cls(access=True, view_profile=True, send_message=True, invite=True)
class Permissions(Flags):
"""Server permissions for members and roles"""
@Flag
def manage_channel() -> int:
return 1 << 0
@Flag
def manage_server() -> int:
return 1 << 1
@Flag
def manage_permissions() -> int:
return 1 << 2
@Flag
def manage_role() -> int:
return 1 << 3
@Flag
def kick_members() -> int:
return 1 << 6
@Flag
def ban_members() -> int:
return 1 << 7
@Flag
def timeout_members() -> int:
return 1 << 8
@Flag
def asign_roles() -> int:
return 1 << 9
@Flag
def change_nickname() -> int:
return 1 << 10
@Flag
def manage_nicknames() -> int:
return 1 << 11
@Flag
def change_avatars() -> int:
return 1 << 12
@Flag
def remove_avatars() -> int:
return 1 << 13
@Flag
def view_channel() -> int:
return 1 << 20
@Flag
def read_message_history() -> int:
return 1 << 21
@Flag
def send_messages() -> int:
return 1 << 22
@Flag
def manage_messages() -> int:
return 1 << 23
@Flag
def manage_webhooks() -> int:
return 1 << 24
@Flag
def invite_others() -> int:
return 1 << 25
@Flag
def send_embeds() -> int:
return 1 << 26
@Flag
def upload_files() -> int:
return 1 << 27
@Flag
def masquerade() -> int:
return 1 << 28
@Flag
def connect() -> int:
return 1 << 30
@Flag
def speak() -> int:
return 1 << 31
@Flag
def video() -> int:
return 1 << 32
@Flag
def mute_members() -> int:
return 1 << 33
@Flag
def deafen_members() -> int:
return 1 << 34
@Flag
def move_members() -> int:
return 1 << 35
@classmethod @classmethod
def all(cls) -> ChannelPermissions: def all(cls) -> Self:
# channel permissions here do not have manage messages permission return cls(0x000F_FFFF_FFFF_FFFF)
return cls(0b00000000000000000000000011111011)
@classmethod @classmethod
def view(cls) -> ChannelPermissions: def default_view_only(cls) -> Self:
return cls(0b00000000000000000000000000000001) return cls(view_channel=True, read_message_history=True)
# view permission is always present and therefore is included in every permission below
@classmethod @classmethod
def send_message(cls) -> ChannelPermissions: def default(cls) -> Self:
return cls(0b00000000000000000000000000000011) return cls.default_view_only() | cls(send_messages=True, invite_others=True, send_embeds=True, upload_files=True, connect=True, speak=True)
@classmethod @classmethod
def manage_channel(cls) -> ChannelPermissions: def default_direct_message(cls) -> Self:
return cls(0b00000000000000000000000000001001) return cls.default_view_only() | cls(react=True, manage_channel=True)
class PermissionsOverwrite:
"""A permissions overwrite in a channel"""
def __init__(self, allow: Permissions, deny: Permissions):
self._allow = allow
self._deny = deny
for perm in Permissions.FLAG_NAMES:
if getattr(allow, perm):
value = True
elif getattr(deny, perm):
value = False
else:
value = None
super().__setattr__(perm, value)
def __setattr__(self, key: str, value: Any) -> None:
if key in Permissions.FLAG_NAMES:
if value is True:
setattr(self._allow, key, True)
super().__setattr__(key, True)
elif value is False:
setattr(self._deny, key, True)
super().__setattr__(key, False)
else:
setattr(self._allow, key, False)
setattr(self._deny, key, False)
super().__setattr__(key, None)
else:
super().__setattr__(key, value)
if TYPE_CHECKING:
manage_channel: Optional[bool]
manage_server: Optional[bool]
manage_permissions: Optional[bool]
manage_role: Optional[bool]
kick_members: Optional[bool]
ban_members: Optional[bool]
timeout_members: Optional[bool]
asign_roles: Optional[bool]
change_nickname: Optional[bool]
manage_nicknames: Optional[bool]
change_avatars: Optional[bool]
remove_avatars: Optional[bool]
view_channel: Optional[bool]
read_message_history: Optional[bool]
send_messages: Optional[bool]
manage_messages: Optional[bool]
manage_webhooks: Optional[bool]
invite_others: Optional[bool]
send_embeds: Optional[bool]
upload_files: Optional[bool]
masquerade: Optional[bool]
connect: Optional[bool]
speak: Optional[bool]
video: Optional[bool]
mute_members: Optional[bool]
deafen_members: Optional[bool]
move_members: Optional[bool]
def to_pair(self) -> tuple[Permissions, Permissions]:
return self._allow, self._deny
@classmethod @classmethod
def voice_call(cls) -> ChannelPermissions: def _from_overwrite(cls, overwrite: Overwrite) -> Self:
return cls(0b00000000000000000000000000010001) allow = Permissions(overwrite["a"])
deny = Permissions(overwrite["d"])
@classmethod return cls(allow, deny)
def invite_others(cls) -> ChannelPermissions:
return cls(0b00000000000000000000000000100001)
@classmethod
def embed_links(cls) -> ChannelPermissions:
return cls(0b00000000000000000000000001000001)
@classmethod
def upload_files(cls) -> ChannelPermissions:
return cls(0b00000000000000000000000010000001)
@property
def can_view(self) -> bool:
return self._check(0)
@property
def can_send_message(self) -> bool:
return self._check(1)
@property
def can_manage_channel(self) -> bool:
return self._check(3)
@property
def can_voice_call(self) -> bool:
return self._check(4)
@property
def can_invite_others(self) -> bool:
return self._check(5)
@property
def can_embed_links(self) -> bool:
return self._check(6)
@property
def can_upload_files(self) -> bool:
return self._check(7)
class ServerPermissions:
"""Represents the server permissions for a role as seen in server settings."""
__slots__ = ("_server_perms", "_channel_perms", )
def __init__(self, server_perms: int, channel_perms: int) -> None:
self._server_perms = _Permission(server_perms)
self._channel_perms = _Permission(channel_perms)
def __add__(self, p: ServerPermissions) -> ServerPermissions:
self._server_perms += p._server_perms
self._channel_perms += p._channel_perms
return self
def __sub__(self, p: ServerPermissions) -> ServerPermissions:
self._server_perms -= p._server_perms
self._channel_perms -= p._channel_perms
return self
def __lt__(self, p: ServerPermissions) -> bool:
return self._server_perms < p._server_perms and self._channel_perms < p._channel_perms
def __gt__(self, p: ServerPermissions) -> bool:
return self._server_perms > p._server_perms and self._channel_perms > p._channel_perms
def __eq__(self, p: ServerPermissions) -> bool:
return self._server_perms == p._server_perms and self._channel_perms == p._channel_perms
def __repr__(self) -> str:
return f"{self.__class__.__name__}<Server: {self._server_perms._value:032b} Channel: {self._channel_perms._value:032b}>"
@property
def value(self) -> PermissionTuple:
return self._server_perms._value, self._channel_perms._value
@classmethod
def none(cls) -> ServerPermissions:
return cls(0, 0)
@classmethod
def all(cls) -> ServerPermissions:
# channel permissions here do not have manage channel permission
return cls(0b00000000000000001111000000111111, 0b00000000000000000000000011110111)
# view server and view channel are always present and therefore are included in every permission below
# server permissions
@classmethod
def view_server(cls) -> ServerPermissions:
return cls(0b00000000000000000000000000000001, 1)
@classmethod
def manage_roles(cls) -> ServerPermissions:
return cls(0b00000000000000000000000000000011, 1)
@classmethod
def manage_channels(cls) -> ServerPermissions:
return cls(0b00000000000000000000000000000101, 1)
@classmethod
def manage_server(cls) -> ServerPermissions:
return cls(0b00000000000000000000000000001001, 1)
@classmethod
def kick_members(cls) -> ServerPermissions:
return cls(0b00000000000000000000000000010001, 1)
@classmethod
def ban_members(cls) -> ServerPermissions:
return cls(0b00000000000000000000000000100001, 1)
@classmethod
def change_nicknames(cls) -> ServerPermissions:
return cls(0b00000000000000000001000000000001, 1)
@classmethod
def manage_nicknames(cls) -> ServerPermissions:
return cls(0b00000000000000000010000000000001, 1)
@classmethod
def change_avatar(cls) -> ServerPermissions:
return cls(0b00000000000000000100000000000001, 1)
@classmethod
def remove_avatars(cls) -> ServerPermissions:
return cls(0b00000000000000001000000000000001, 1)
@property
def can_view_server(self) -> bool:
return self._server_perms._check(0)
@property
def can_manage_roles(self) -> bool:
return self._server_perms._check(1)
@property
def can_manage_channels(self) -> bool:
return self._server_perms._check(2)
@property
def can_manage_server(self) -> bool:
return self._server_perms._check(3)
@property
def can_kick_members(self) -> bool:
return self._server_perms._check(4)
@property
def can_ban_members(self) -> bool:
return self._server_perms._check(5)
@property
def can_change_nicknames(self) -> bool:
return self._server_perms._check(12)
@property
def can_manage_nicknames(self) -> bool:
return self._server_perms._check(13)
@property
def can_change_avatar(self) -> bool:
return self._server_perms._check(14)
@property
def can_remove_avatars(self) -> bool:
return self._server_perms._check(15)
# channel permissions
@classmethod
def view_channels(cls) -> ServerPermissions:
return cls(1, 0b00000000000000000000000000000001)
@classmethod
def send_message(cls) -> ServerPermissions:
return cls(1, 0b00000000000000000000000000000011)
@classmethod
def manage_messages(cls) -> ServerPermissions:
return cls(1, 0b00000000000000000000000000000101)
@classmethod
def voice_call(cls) -> ServerPermissions:
return cls(1, 0b00000000000000000000000000010001)
@classmethod
def invite_others(cls) -> ServerPermissions:
return cls(1, 0b00000000000000000000000000100001)
@classmethod
def embed_links(cls) -> ServerPermissions:
return cls(1, 0b00000000000000000000000001000001)
@classmethod
def upload_files(cls) -> ServerPermissions:
return cls(1, 0b00000000000000000000000010000001)
@property
def can_view_channel(self) -> bool:
return self._channel_perms._check(0)
@property
def can_send_message(self) -> bool:
return self._channel_perms._check(1)
@property
def can_manage_messages(self) -> bool:
return self._channel_perms._check(2)
@property
def can_voice_call(self) -> bool:
return self._channel_perms._check(4)
@property
def can_invite_others(self) -> bool:
return self._channel_perms._check(5)
@property
def can_embed_links(self) -> bool:
return self._channel_perms._check(6)
@property
def can_upload_files(self) -> bool:
return self._channel_perms._check(7)
+82
View File
@@ -0,0 +1,82 @@
from __future__ import annotations
from datetime import datetime, timezone
from typing import TYPE_CHECKING, cast
from revolt.enums import ChannelType
from .permissions import Permissions
if TYPE_CHECKING:
from .channel import Channel, DMChannel, GroupDMChannel, ServerChannel
from .member import Member
from .server import Server
def calculate_permissions(member: Member, target: Server | Channel) -> Permissions:
if member.privileged:
return Permissions.all()
from .server import Server
if isinstance(target, Server):
if target.owner_id == member.id:
return Permissions.all()
permissions = target.default_permissions
for role in member.roles:
permissions = (permissions | role.permissions._allow) & (~role.permissions._deny)
if member.current_timeout and member.current_timeout > datetime.now(timezone.utc):
permissions = permissions & Permissions.default_view_only()
return permissions
else:
channel_type = target.channel_type
if channel_type is ChannelType.saved_messages:
return Permissions.all()
elif channel_type is ChannelType.direct_message:
target = cast("DMChannel", target)
user_permissions = target.recipient.get_permissions()
if user_permissions.send_message:
return Permissions.default_direct_message()
else:
return Permissions.default_view_only()
elif channel_type is ChannelType.group:
target = cast("GroupDMChannel", target)
if target.owner.id != member.id:
return Permissions.default_direct_message()
else:
if target.permissions.value == 0:
return Permissions.default_direct_message()
else:
return target.permissions
else:
target = cast("ServerChannel", target)
server = target.server
if server.owner_id == member.id:
return Permissions.all()
else:
perms = calculate_permissions(member, server)
perms = (perms | target.default_permissions._allow) & (~target.default_permissions._deny)
for role in server.roles[::-1]:
if overwrite :=target.permissions.get(role.id):
perms = (perms | overwrite._allow) & (~overwrite._deny)
if member.current_timeout and member.current_timeout > datetime.now():
perms = perms & Permissions(view_channel=True, read_message_history=True)
return perms
View File
+69 -22
View File
@@ -1,8 +1,9 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Any, Optional
from .permissions import ServerPermissions from .permissions import Overwrite, PermissionsOverwrite
from .utils import Missing, Ulid
if TYPE_CHECKING: if TYPE_CHECKING:
from .server import Server from .server import Server
@@ -12,16 +13,16 @@ if TYPE_CHECKING:
__all__ = ("Role",) __all__ = ("Role",)
class Role: class Role(Ulid):
"""Represents a role """Represents a role
Attributes Attributes
----------- -----------
id: :class:`str` id: :class:`str`
The id of the role The id of the role
name: :class:`str` name: :class:`str`
The name of the role The name of the role
colour: :class:`str` colour: Optional[:class:`str`]
The colour of the role The colour of the role
hoist: :class:`bool` hoist: :class:`bool`
Whether members with the role will display seperate from everyone else Whether members with the role will display seperate from everyone else
@@ -29,30 +30,76 @@ class Role:
The position of the role in the role heirarchy The position of the role in the role heirarchy
server: :class:`Server` server: :class:`Server`
The server the role belongs to The server the role belongs to
permissions: :class:`ServerPermissions` server_permissions: :class:`ServerPermissions`
The server permissions for the role The server permissions for the role
channel_permissions: :class:`ChannelPermissions`
The channel permissions for the role
""" """
__slots__ = ("id", "name", "colour", "hoist", "rank", "state", "server", "permissions") __slots__: tuple[str, ...] = ("id", "name", "colour", "hoist", "rank", "state", "server", "permissions")
def __init__(self, data: RolePayload, role_id: str, state: State, server: Server): def __init__(self, data: RolePayload, role_id: str, server: Server, state: State):
self.state = state self.state: State = state
self.id = role_id self.id: str = role_id
self.name = data["name"] self.name: str = data["name"]
self.colour = data.get("colour") self.colour: str | None = data.get("colour", None)
self.hoist = data.get("hoist", False) self.hoist: bool = data.get("hoist", False)
self.rank = data.get("rank", 0) self.rank: int = data["rank"]
self.server = server self.server: Server = server
self.permissions = ServerPermissions(*data.get("permissions")) self.permissions: PermissionsOverwrite = PermissionsOverwrite._from_overwrite(data.get("permissions", {"a": 0, "d": 0}))
@property @property
def color(self): def color(self) -> str | None:
return self.colour return self.colour
async def set_permissions(self, permissions: ServerPermissions) -> None: async def set_permissions_overwrite(self, *, permissions: PermissionsOverwrite) -> None:
"""Sets the permissions for a role in a server. """Sets the permissions for a role in a server.
Parameters Parameters
----------- -----------
permissions: :class:`ServerPermissions` server_permissions: Optional[:class:`ServerPermissions`]
The new permissions for the role The new server permissions for the role
channel_permissions: Optional[:class:`ChannelPermissions`]
The new channel permissions for the role
""" """
await self.state.http.set_role_permissions(self.server.id, self.id, *permissions.value) allow, deny = permissions.to_pair()
await self.state.http.set_server_role_permissions(self.server.id, self.id, allow.value, deny.value)
def _update(self, *, name: Optional[str] = None, colour: Optional[str] = None, hoist: Optional[bool] = None, rank: Optional[int] = None, permissions: Optional[Overwrite] = None) -> None:
if name is not None:
self.name = name
if colour is not None:
self.colour = colour
if hoist is not None:
self.hoist = hoist
if rank is not None:
self.rank = rank
if permissions is not None:
self.permissions = PermissionsOverwrite._from_overwrite(permissions)
async def delete(self) -> None:
"""Deletes the role"""
await self.state.http.delete_role(self.server.id, self.id)
async def edit(self, **kwargs: Any) -> None:
"""Edits the role
Parameters
-----------
name: str
The name of the role
colour: str
The colour of the role
hoist: bool
Whether the role should make the member display seperately in the member list
rank: int
The position of the role
"""
if kwargs.get("colour", Missing) is None:
remove = ["Colour"]
else:
remove = None
await self.state.http.edit_role(self.server.id, self.id, remove, kwargs)
+393 -41
View File
@@ -2,49 +2,200 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Optional, cast from typing import TYPE_CHECKING, Optional, cast
from .channel import Channel from .asset import Asset
from .permissions import ServerPermissions from .category import Category
from .invite import Invite
from .permissions import Permissions
from .role import Role from .role import Role
from .utils import Ulid
from .channel import Channel, TextChannel, VoiceChannel
from .member import Member
if TYPE_CHECKING: if TYPE_CHECKING:
from .member import Member from .emoji import Emoji
from .file import File
from .state import State from .state import State
from .types import Ban
from .types import Category as CategoryPayload
from .types import File as FilePayload
from .types import Server as ServerPayload from .types import Server as ServerPayload
from .types import SystemMessagesConfig
from .types import Member as MemberPayload
__all__ = ("Server", "SystemMessages", "ServerBan")
__all__ = ("Server",) class SystemMessages:
"""Holds all the configuration for the server's system message channels"""
class Server: def __init__(self, data: SystemMessagesConfig, state: State):
self.state: State = state
self.user_joined_id: str | None = data.get("user_joined")
self.user_left_id: str | None = data.get("user_left")
self.user_kicked_id: str | None = data.get("user_kicked")
self.user_banned_id: str | None = data.get("user_banned")
@property
def user_joined(self) -> Optional[TextChannel]:
"""The channel which user join messages get sent in
Returns
--------
Optional[:class:`TextChannel`]
The channel
"""
if not self.user_joined_id:
return
channel = self.state.get_channel(self.user_joined_id)
assert isinstance(channel, TextChannel)
return channel
@property
def user_left(self) -> Optional[TextChannel]:
"""The channel which user leave messages get sent in
Returns
--------
Optional[:class:`TextChannel`]
The channel
"""
if not self.user_left_id:
return
channel = self.state.get_channel(self.user_left_id)
assert isinstance(channel, TextChannel)
return channel
@property
def user_kicked(self) -> Optional[TextChannel]:
"""The channel which user kick messages get sent in
Returns
--------
Optional[:class:`TextChannel`]
The channel
"""
if not self.user_kicked_id:
return
channel = self.state.get_channel(self.user_kicked_id)
assert isinstance(channel, TextChannel)
return channel
@property
def user_banned(self) -> Optional[TextChannel]:
"""The channel which user ban messages get sent in
Returns
--------
Optional[:class:`TextChannel`]
The channel
"""
if not self.user_banned_id:
return
channel = self.state.get_channel(self.user_banned_id)
assert isinstance(channel, TextChannel)
return channel
class Server(Ulid):
"""Represents a server """Represents a server
Attributes Attributes
----------- -----------
id: :class:`str` id: :class:`str`
The id of the server The id of the server
name: :class:`str` name: :class:`str`
The name of the server The name of the server
owner: Optional[:class:`Member`] owner_id: :class:`str`
The owner of the server The owner's id of the server
description: Optional[:class:`str`]
The servers description
nsfw: :class:`bool`
Whether the server is nsfw or not
system_messages: :class:`SystemMessages`
The system message config for the server
icon: Optional[:class:`Asset`]
The servers icon
banner: Optional[:class:`Asset`]
The servers banner
default_permissions: :class:`Permissions`
The permissions for the default role
""" """
__slots__ = ("state", "id", "name", "owner_id", "default_permissions", "_members", "_roles", "_channels") __slots__ = ("state", "id", "name", "owner_id", "default_permissions", "_members", "_roles", "_channels", "description", "icon", "banner", "nsfw", "system_messages", "_categories", "_emojis")
def __init__(self, data: ServerPayload, state: State): def __init__(self, data: ServerPayload, state: State):
self.state = state self.state: State = state
self.id = data["_id"] self.id: str = data["_id"]
self.name = data["name"] self.name: str = data["name"]
self.owner_id = data["owner"] self.owner_id: str = data["owner"]
self.default_permissions = ServerPermissions(*data["default_permissions"]) self.description: str | None = data.get("description") or None
self.nsfw: bool = data.get("nsfw", False)
self.system_messages: SystemMessages = SystemMessages(data.get("system_messages", cast("SystemMessagesConfig", {})), state)
self._categories: dict[str, Category] = {data["id"]: Category(data, state) for data in data.get("categories", [])}
self.default_permissions: Permissions = Permissions(data["default_permissions"])
self.icon: Asset | None
if icon := data.get("icon"):
self.icon = Asset(icon, state)
else:
self.icon = None
self.banner: Asset | None
if banner := data.get("banner"):
self.banner = Asset(banner, state)
else:
self.banner = None
self._members: dict[str, Member] = {} self._members: dict[str, Member] = {}
self._roles: dict[str, Role] = {role_id: Role(role, role_id, state, self) for role_id, role in data.get("roles", {}).items()} self._roles: dict[str, Role] = {role_id: Role(role, role_id, self, state) for role_id, role in data.get("roles", {}).items()}
channels = cast(list[Channel], list(filter(bool, [state.get_channel(channel_id) for channel_id in data["channels"]])))
self._channels: dict[str, Channel] = {channel.id: channel for channel in channels} self._channels: dict[str, Channel] = {}
# The api doesnt send us all the channels but sends us all the ids, this is because channels we dont have permissions to see are not sent
# this causes get_channel to error so we have to first check ourself if its in the cache.
for channel_id in data["channels"]:
if channel := state.channels.get(channel_id):
self._channels[channel_id] = channel
self._emojis: dict[str, Emoji] = {}
def _update(self, *, owner: Optional[str] = None, name: Optional[str] = None, description: Optional[str] = None, icon: Optional[FilePayload] = None, banner: Optional[FilePayload] = None, default_permissions: Optional[int] = None, nsfw: Optional[bool] = None, system_messages: Optional[SystemMessagesConfig] = None, categories: Optional[list[CategoryPayload]] = None, channels: Optional[list[str]] = None):
if owner is not None:
self.owner_id = owner
if name is not None:
self.name = name
if description is not None:
self.description = description or None
if icon is not None:
self.icon = Asset(icon, self.state)
if banner is not None:
self.banner = Asset(banner, self.state)
if default_permissions is not None:
self.default_permissions = Permissions(default_permissions)
if nsfw is not None:
self.nsfw = nsfw
if system_messages is not None:
self.system_messages = SystemMessages(system_messages, self.state)
if categories is not None:
self._categories = {data["id"]: Category(data, self.state) for data in categories}
if channels is not None:
self._channels = {channel_id: self.state.get_channel(channel_id) for channel_id in channels}
def _add_member(self, payload: MemberPayload) -> Member:
member = Member(payload, self, self.state)
self._members[member.id] = member
return member
@property @property
def roles(self) -> list[Role]: def roles(self) -> list[Role]:
"""list[:class:`Role`] Gets all roles in the server in decending order""" """list[:class:`Role`] Gets all roles in the server in decending order"""
return list(self._roles.values()) return list(self._roles.values())
@property @property
def members(self) -> list[Member]: def members(self) -> list[Member]:
"""list[:class:`Member`] Gets all members in the server""" """list[:class:`Member`] Gets all members in the server"""
@@ -52,17 +203,27 @@ class Server:
@property @property
def channels(self) -> list[Channel]: def channels(self) -> list[Channel]:
"""list[:class:`Member`] Gets all channels in the server""" """list[:class:`Channel`] Gets all channels in the server"""
return list(self._channels.values()) return list(self._channels.values())
@property
def categories(self) -> list[Category]:
"""list[:class:`Category`] Gets all categories in the server"""
return list(self._categories.values())
@property
def emojis(self) -> list[Emoji]:
"""list[:class:`Emoji`] Gets all emojis in the server"""
return list(self._emojis.values())
def get_role(self, role_id: str) -> Role: def get_role(self, role_id: str) -> Role:
"""Gets a role from the cache """Gets a role from the cache
Parameters Parameters
----------- -----------
id: :class:`str` id: :class:`str`
The id of the role The id of the role
Returns Returns
-------- --------
:class:`Role` :class:`Role`
@@ -72,48 +233,239 @@ class Server:
def get_member(self, member_id: str) -> Member: def get_member(self, member_id: str) -> Member:
"""Gets a member from the cache """Gets a member from the cache
Parameters Parameters
----------- -----------
id: :class:`str` id: :class:`str`
The id of the member The id of the member
Returns Returns
-------- --------
:class:`Member` :class:`Member`
The member The member
""" """
return self._members[member_id] try:
return self._members[member_id]
except KeyError:
raise LookupError from None
def get_channel(self, channel_id: str) -> Channel: def get_channel(self, channel_id: str) -> Channel:
"""Gets a channel from the cache """Gets a channel from the cache
Parameters Parameters
----------- -----------
id: :class:`str` id: :class:`str`
The id of the channel The id of the channel
Returns Returns
-------- --------
:class:`Channel` :class:`Channel`
The channel The channel
""" """
return self._channels[channel_id] try:
return self._channels[channel_id]
except KeyError:
raise LookupError from None
@property def get_category(self, category_id: str) -> Category:
def owner(self) -> Optional[Member]: """Gets a category from the cache
owner_id = self.owner_id
if not owner_id:
return
return self.get_member(owner_id)
async def set_default_permissions(self, permissions: ServerPermissions) -> None:
"""Sets the default server permissions.
Parameters Parameters
----------- -----------
permissions: :class:`ServerPermissions` id: :class:`str`
The id of the category
Returns
--------
:class:`Category`
The category
"""
try:
return self._categories[category_id]
except KeyError:
raise LookupError from None
def get_emoji(self, emoji_id: str) -> Emoji:
"""Gets a emoji from the cache
Parameters
-----------
id: :class:`str`
The id of the emoji
Returns
--------
:class:`Emoji`
The emoji
"""
try:
return self._emojis[emoji_id]
except KeyError as e:
raise LookupError from e
@property
def owner(self) -> Member:
""":class:`Member` The owner of the server"""
return self.get_member(self.owner_id)
async def set_default_permissions(self, permissions: Permissions) -> None:
"""Sets the default server permissions.
Parameters
-----------
permissions: :class:`Permissions`
The new default server permissions The new default server permissions
""" """
await self.state.http.set_default_permissions(self.id, *permissions.value)
await self.state.http.set_server_default_permissions(self.id, permissions.value)
async def leave_server(self) -> None:
"""Leaves or deletes the server"""
await self.state.http.delete_leave_server(self.id)
async def delete_server(self) -> None:
"""Leaves or deletes a server, alias to :meth`Server.leave_server`"""
await self.leave_server()
async def create_text_channel(self, *, name: str, description: Optional[str] = None) -> TextChannel:
"""Creates a text channel in the server
Parameters
-----------
name: :class:`str`
The name of the channel
description: Optional[:class:`str`]
The channel's description
Returns
--------
:class:`TextChannel`
The text channel that was just created
"""
payload = await self.state.http.create_channel(self.id, "Text", name, description)
channel = TextChannel(payload, self.state)
self._channels[channel.id] = channel
return channel
async def create_voice_channel(self, *, name: str, description: Optional[str] = None) -> VoiceChannel:
"""Creates a voice channel in the server
Parameters
-----------
name: :class:`str`
The name of the channel
description: Optional[:class:`str`]
The channel's description
Returns
--------
:class:`VoiceChannel`
The voice channel that was just created
"""
payload = await self.state.http.create_channel(self.id, "Voice", name, description)
channel = self.state.add_channel(payload)
self._channels[channel.id] = channel
return cast(VoiceChannel, channel)
async def fetch_invites(self) -> list[Invite]:
"""Fetches all invites in the server
Returns
--------
list[:class:`Invite`]
"""
invite_payloads = await self.state.http.fetch_server_invites(self.id)
return [Invite._from_partial(payload["_id"], payload["server"], payload["creator"], payload["channel"], self.state) for payload in invite_payloads]
async def fetch_member(self, member_id: str) -> Member:
"""Fetches a member from this server
Parameters
-----------
member_id: :class:`str`
The id of the member you are fetching
Returns
--------
:class:`Member`
The member with the matching id
"""
payload = await self.state.http.fetch_member(self.id, member_id)
return Member(payload, self, self.state)
async def fetch_bans(self) -> list[ServerBan]:
"""Fetches all bans in the server
Returns
--------
list[:class:`ServerBan`]
"""
payload = await self.state.http.fetch_bans(self.id)
return [ServerBan(ban, self.state) for ban in payload["bans"]]
async def create_role(self, name: str) -> Role:
"""Creates a role in the server
Parameters
-----------
name: :class:`str`
The name of the role
Returns
--------
:class:`Role`
The role that was just created
"""
payload = await self.state.http.create_role(self.id, name)
return Role(payload["role"], payload["id"], self, self.state)
async def create_emoji(self, name: str, file: File, *, nsfw: bool = False) -> Emoji:
"""Creates an emoji
Parameters
-----------
name: :class:`str`
The name for the emoji
file: :class:`File`
The image for the emoji
nsfw: :class:`bool`
Whether or not the emoji is nsfw
"""
payload = await self.state.http.create_emoji(name, file, nsfw, {"type": "Server", "id": self.id})
return self.state.add_emoji(payload)
class ServerBan:
"""Represents a server ban
Attributes
-----------
reason: Optional[:class:`str`]
The reason the user was banned
server: :class:`Server`
The server the user was banned in
user_id: :class:`str`
The id of the user who was banned
"""
__slots__ = ("reason", "server", "user_id", "state")
def __init__(self, ban: Ban, state: State):
self.reason: str | None = ban.get("reason")
self.server: Server = state.get_server(ban["_id"]["server"])
self.user_id: str = ban["_id"]["user"]
self.state: State = state
async def unban(self) -> None:
"""Unbans the user"""
await self.state.http.unban_member(self.server.id, self.user_id)
+53 -24
View File
@@ -1,9 +1,10 @@
from __future__ import annotations from __future__ import annotations
from collections import deque from collections import deque
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING
from .channel import Channel, channel_factory from .channel import Channel, channel_factory
from .emoji import Emoji
from .member import Member from .member import Member
from .message import Message from .message import Message
from .server import Server from .server import Server
@@ -13,51 +14,65 @@ if TYPE_CHECKING:
from .http import HttpClient from .http import HttpClient
from .types import ApiInfo from .types import ApiInfo
from .types import Channel as ChannelPayload from .types import Channel as ChannelPayload
from .types import Emoji as EmojiPayload
from .types import Member as MemberPayload from .types import Member as MemberPayload
from .types import Message as MessagePayload from .types import Message as MessagePayload
from .types import Server as ServerPayload from .types import Server as ServerPayload
from .types import User as UserPayload from .types import User as UserPayload
__all__ = ("State",) __all__ = ("State",)
class State: class State:
__slots__ = ("http", "api_info", "max_messages", "users", "channels", "servers", "messages") __slots__ = ("http", "api_info", "max_messages", "users", "channels", "servers", "messages", "global_emojis", "user_id", "me")
def __init__(self, http: HttpClient, api_info: ApiInfo, max_messages: int): def __init__(self, http: HttpClient, api_info: ApiInfo, max_messages: int):
self.http = http self.http: HttpClient = http
self.api_info = api_info self.api_info: ApiInfo = api_info
self.max_messages = max_messages self.max_messages: int = max_messages
self.me: User
self.users: dict[str, User] = {} self.users: dict[str, User] = {}
self.channels: dict[str, Channel] = {} self.channels: dict[str, Channel] = {}
self.servers: dict[str, Server] = {} self.servers: dict[str, Server] = {}
self.messages: deque[Message] = deque() self.messages: deque[Message] = deque()
self.global_emojis: list[Emoji] = []
def get_user(self, id: str) -> User: def get_user(self, id: str) -> User:
return self.users[id] try:
return self.users[id]
except KeyError:
raise LookupError from None
def get_member(self, server_id: str, member_id: str) -> Member: def get_member(self, server_id: str, member_id: str) -> Member:
server = self.servers[server_id] server = self.servers[server_id]
return server.get_member(member_id) return server.get_member(member_id)
def get_channel(self, id: str) -> Channel: def get_channel(self, id: str) -> Channel:
return self.channels[id] try:
return self.channels[id]
except KeyError:
raise LookupError from None
def get_server(self, id: str) -> Server: def get_server(self, id: str) -> Server:
return self.servers[id] try:
return self.servers[id]
except KeyError:
raise LookupError from None
def add_user(self, payload: UserPayload) -> User: def add_user(self, payload: UserPayload) -> User:
user = User(payload, self) user = User(payload, self)
if payload.get("relationship") == "User":
self.me = user
self.users[user.id] = user self.users[user.id] = user
return user return user
def add_member(self, server_id: str, payload: MemberPayload) -> Member: def add_member(self, server_id: str, payload: MemberPayload) -> Member:
server = self.get_server(server_id) server = self.get_server(server_id)
member = Member(payload, server, self)
server._members[member.id] = member
return member return server._add_member(payload)
def add_channel(self, payload: ChannelPayload) -> Channel: def add_channel(self, payload: ChannelPayload) -> Channel:
channel = channel_factory(payload, self) channel = channel_factory(payload, self)
@@ -73,23 +88,37 @@ class State:
message = Message(payload, self) message = Message(payload, self)
if len(self.messages) >= self.max_messages: if len(self.messages) >= self.max_messages:
self.messages.pop() self.messages.pop()
self.messages.appendleft(message) self.messages.appendleft(message)
return message return message
def add_emoji(self, payload: EmojiPayload) -> Emoji:
emoji = Emoji(payload, self)
if server_id := emoji.server_id:
server = self.get_server(server_id)
server._emojis[emoji.id] = emoji
else:
self.global_emojis.append(emoji)
return emoji
def get_message(self, message_id: str) -> Message: def get_message(self, message_id: str) -> Message:
for msg in self.messages: for msg in self.messages:
if msg.id == message_id: if msg.id == message_id:
return msg return msg
raise KeyError
async def fetch_all_server_members(self): raise LookupError
for server_id in self.servers.keys():
data = await self.http.fetch_members(server_id) async def fetch_server_members(self, server_id: str) -> None:
data = await self.http.fetch_members(server_id)
for user in data["users"]:
self.add_user(user) for user in data["users"]:
self.add_user(user)
for member in data["members"]:
self.add_member(server_id, member) for member in data["members"]:
self.add_member(server_id, member)
async def fetch_all_server_members(self) -> None:
for server_id in self.servers:
await self.fetch_server_members(server_id)
+3
View File
@@ -1,11 +1,14 @@
from .category import *
from .channel import * from .channel import *
from .embed import * from .embed import *
from .emoji import *
from .file import * from .file import *
from .gateway import * from .gateway import *
from .http import * from .http import *
from .invite import * from .invite import *
from .member import * from .member import *
from .message import * from .message import *
from .permissions import *
from .role import * from .role import *
from .server import * from .server import *
from .user import * from .user import *
+2 -6
View File
@@ -1,12 +1,8 @@
from typing import TYPE_CHECKING, TypedDict from typing import TypedDict
if TYPE_CHECKING:
from .channel import Channel
__all__ = ("Category",) __all__ = ("Category",)
class Category(TypedDict): class Category(TypedDict):
id: str id: str
title: str title: str
channels: list[Channel] channels: list[str]
+29 -30
View File
@@ -2,68 +2,67 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Literal, TypedDict, Union from typing import TYPE_CHECKING, Literal, TypedDict, Union
from typing_extensions import NotRequired
if TYPE_CHECKING: if TYPE_CHECKING:
from .file import File from .file import File
from .message import Message from .permissions import Overwrite
__all__ = ( __all__ = (
"SavedMessages", "SavedMessages",
"DMChannel", "DMChannel",
"Group", "GroupDMChannel",
"TextChannel", "TextChannel",
"VoiceChannel", "VoiceChannel",
"ServerChannel",
"Channel", "Channel",
) )
class _NonceChannel(TypedDict, total=False):
nonce: str
class BaseChannel(TypedDict): class BaseChannel(TypedDict):
_id: str _id: str
nonce: str
class SavedMessages(_NonceChannel, BaseChannel): class SavedMessages(BaseChannel):
user: str user: str
channel_type: Literal["SavedMessage"] channel_type: Literal["SavedMessages"]
class DMChannel(_NonceChannel, BaseChannel): class DMChannel(BaseChannel):
active: bool active: bool
recipients: list[str] recipients: list[str]
last_message: Message last_message_id: NotRequired[str]
channel_type: Literal["DirectMessage"] channel_type: Literal["DirectMessage"]
class _GroupOptional(TypedDict): class GroupDMChannel(BaseChannel):
icon: File
permissions: int
description: str
class Group(_NonceChannel, _GroupOptional, BaseChannel):
recipients: list[str] recipients: list[str]
name: str name: str
owner: str owner: str
channel_type: Literal["Group"] channel_type: Literal["Group"]
icon: NotRequired[File]
permissions: NotRequired[int]
description: NotRequired[str]
nsfw: NotRequired[bool]
last_message_id: NotRequired[str]
class _TextChannelOptional(TypedDict, total=False): class TextChannel(BaseChannel):
icon: File
default_permissions: int
role_permissions: dict[str, int]
class TextChannel(_NonceChannel, _TextChannelOptional, BaseChannel):
server: str server: str
name: str name: str
description: str description: str
last_message: str
channel_type: Literal["TextChannel"] channel_type: Literal["TextChannel"]
icon: NotRequired[File]
default_permissions: NotRequired[Overwrite]
role_permissions: NotRequired[dict[str, Overwrite]]
nsfw: NotRequired[bool]
last_message_id: NotRequired[str]
class _VoiceChannelOptional(TypedDict, total=False): class VoiceChannel(BaseChannel):
icon: File
default_permissions: int
role_permissions: int
class VoiceChannel(_NonceChannel, _TextChannelOptional, BaseChannel):
server: str server: str
name: str name: str
description: str description: str
channel_type: Literal["VoiceChannel"] channel_type: Literal["VoiceChannel"]
icon: NotRequired[File]
default_permissions: NotRequired[Overwrite]
role_permissions: NotRequired[dict[str, Overwrite]]
nsfw: NotRequired[bool]
Channel = Union[SavedMessages, DMChannel, Group, TextChannel, VoiceChannel] ServerChannel = Union[TextChannel, VoiceChannel]
Channel = Union[SavedMessages, DMChannel, GroupDMChannel, TextChannel, VoiceChannel]
+81 -4
View File
@@ -1,8 +1,85 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, TypedDict from typing import TYPE_CHECKING, Literal, TypedDict, Union
__all__ = ("Embed",) from typing_extensions import NotRequired
if TYPE_CHECKING:
from .file import File
__all__ = ("Embed", "SendableEmbed", "WebsiteEmbed", "ImageEmbed", "TextEmbed", "NoneEmbed", "YoutubeSpecial", "TwitchSpecial", "SpotifySpecial", "SoundcloudSpecial", "BandcampSpecial", "WebsiteSpecial", "JanuaryImage", "JanuaryVideo")
class YoutubeSpecial(TypedDict):
type: Literal["Youtube"]
id: str
timestamp: NotRequired[str]
class TwitchSpecial(TypedDict):
type: Literal["Twitch"]
content_type: Literal["Channel", "Video", "Clip"]
id: str
class SpotifySpecial(TypedDict):
type: Literal["Spotify"]
content_type: str
id: str
class SoundcloudSpecial(TypedDict):
type: Literal["Soundcloud"]
class BandcampSpecial(TypedDict):
type: Literal["Bandcamp"]
content_type: Literal["Album", "Track"]
id: str
WebsiteSpecial = Union[YoutubeSpecial, TwitchSpecial, SpotifySpecial, SoundcloudSpecial, BandcampSpecial]
class JanuaryImage(TypedDict):
url: str
width: int
height: int
size: Literal["Large", "Preview"]
class JanuaryVideo(TypedDict):
url: str
width: int
height: int
class WebsiteEmbed(TypedDict):
type: Literal["Website"]
url: NotRequired[str]
special: NotRequired[WebsiteSpecial]
title: NotRequired[str]
description: NotRequired[str]
image: NotRequired[JanuaryImage]
video: NotRequired[JanuaryVideo]
site_name: NotRequired[str]
icon_url: NotRequired[str]
colour: NotRequired[str]
class ImageEmbed(JanuaryImage):
type: Literal["Image"]
class TextEmbed(TypedDict):
type: Literal["Text"]
icon_url: NotRequired[str]
url: NotRequired[str]
title: NotRequired[str]
description: NotRequired[str]
media: NotRequired[File]
colour: NotRequired[str]
class NoneEmbed(TypedDict):
type: Literal["None"]
Embed = Union[WebsiteEmbed, ImageEmbed, TextEmbed, NoneEmbed]
class SendableEmbed(TypedDict):
type: Literal["Text"]
icon_url: NotRequired[str]
url: NotRequired[str]
title: NotRequired[str]
description: NotRequired[str]
media: NotRequired[str]
colour: NotRequired[str]
class Embed(TypedDict):
pass # TODO
+21
View File
@@ -0,0 +1,21 @@
from typing import Literal, TypedDict, Union
from typing_extensions import NotRequired
class EmojiParentServer(TypedDict):
type: Literal["Server"]
id: str
class EmojiParentDetached(TypedDict):
type: Literal["Detached"]
EmojiParent = Union[EmojiParentServer, EmojiParentDetached]
class Emoji(TypedDict):
_id: str
parent: EmojiParent
creator_id: str
name: str
animated: NotRequired[bool]
nsfw: NotRequired[bool]
+1 -1
View File
@@ -1,6 +1,6 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, Literal, TypedDict, Union from typing import Literal, TypedDict, Union
__all__ = ("File",) __all__ = ("File",)
+140 -15
View File
@@ -1,23 +1,27 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, TypedDict, Union from typing import TYPE_CHECKING, Literal, TypedDict, Union
from .channel import (Channel, DMChannel, Group, SavedMessages, TextChannel, from typing_extensions import NotRequired
VoiceChannel)
from .channel import Channel, DMChannel, GroupDMChannel, SavedMessages, TextChannel, VoiceChannel
from .message import Message from .message import Message
from .permissions import Overwrite
if TYPE_CHECKING: if TYPE_CHECKING:
from .member import Member from .category import Category
from .server import Server from .embed import Embed
from .user import User from .emoji import Emoji
from .file import File
from .member import Member, MemberID
from .server import Server, SystemMessagesConfig
from .user import Status, User, UserProfile, UserRelation
__all__ = ( __all__ = (
"BasePayload", "BasePayload",
"AuthenticatePayload", "AuthenticatePayload",
"ReadyEventPayload", "ReadyEventPayload",
"MessageEventPayload", "MessageEventPayload",
"MessageUpdateEditedData",
"MessageUpdateData", "MessageUpdateData",
"MessageUpdateEventPayload", "MessageUpdateEventPayload",
"MessageDeleteEventPayload", "MessageDeleteEventPayload",
@@ -25,7 +29,21 @@ __all__ = (
"ChannelUpdateEventPayload", "ChannelUpdateEventPayload",
"ChannelDeleteEventPayload", "ChannelDeleteEventPayload",
"ChannelStartTypingEventPayload", "ChannelStartTypingEventPayload",
"ChannelDeleteTypingEventPayload" "ChannelDeleteTypingEventPayload",
"ServerUpdateEventPayload",
"ServerDeleteEventPayload",
"ServerMemberUpdateEventPayload",
"ServerMemberJoinEventPayload",
"ServerMemberLeaveEventPayload",
"ServerRoleUpdateEventPayload",
"ServerRoleDeleteEventPayload",
"UserUpdateEventPayload",
"UserRelationshipEventPayload",
"ServerCreateEventPayload",
"MessageReactEventPayload",
"MessageUnreactEventPayload",
"MessageRemoveReactionEventPayload",
"BulkMessageDeleteEventPayload"
) )
class BasePayload(TypedDict): class BasePayload(TypedDict):
@@ -39,15 +57,15 @@ class ReadyEventPayload(BasePayload):
servers: list[Server] servers: list[Server]
channels: list[Channel] channels: list[Channel]
members: list[Member] members: list[Member]
emojis: list[Emoji]
class MessageEventPayload(BasePayload, Message): class MessageEventPayload(BasePayload, Message):
pass pass
MessageUpdateEditedData = TypedDict("MessageUpdateEditedData", {"$date": str})
class MessageUpdateData(TypedDict): class MessageUpdateData(TypedDict):
content: str content: str
edited: MessageUpdateEditedData embeds: list[Embed]
edited: Union[str, int]
class MessageUpdateEventPayload(BasePayload): class MessageUpdateEventPayload(BasePayload):
channel: str channel: str
@@ -61,7 +79,7 @@ class MessageDeleteEventPayload(BasePayload):
class ChannelCreateEventPayload_SavedMessages(BasePayload, SavedMessages): class ChannelCreateEventPayload_SavedMessages(BasePayload, SavedMessages):
pass pass
class ChannelCreateEventPayload_Group(BasePayload, Group): class ChannelCreateEventPayload_Group(BasePayload, GroupDMChannel):
pass pass
class ChannelCreateEventPayload_TextChannel(BasePayload, TextChannel): class ChannelCreateEventPayload_TextChannel(BasePayload, TextChannel):
@@ -75,10 +93,19 @@ class ChannelCreateEventPayload_DMChannel(BasePayload, DMChannel):
ChannelCreateEventPayload = Union[ChannelCreateEventPayload_Group, ChannelCreateEventPayload_Group, ChannelCreateEventPayload_TextChannel, ChannelCreateEventPayload_VoiceChannel, ChannelCreateEventPayload_DMChannel] ChannelCreateEventPayload = Union[ChannelCreateEventPayload_Group, ChannelCreateEventPayload_Group, ChannelCreateEventPayload_TextChannel, ChannelCreateEventPayload_VoiceChannel, ChannelCreateEventPayload_DMChannel]
class ChannelUpdateEventPayloadData(TypedDict, total=False):
name: str
description: str
icon: File
nsfw: bool
active: bool
role_permissions: dict[str, Overwrite]
default_permissions: Overwrite
class ChannelUpdateEventPayload(BasePayload): class ChannelUpdateEventPayload(BasePayload):
id: str id: str
data: ... data: ChannelUpdateEventPayloadData
clear: ... clear: Literal["Icon", "Description"]
class ChannelDeleteEventPayload(BasePayload): class ChannelDeleteEventPayload(BasePayload):
id: str id: str
@@ -88,3 +115,101 @@ class ChannelStartTypingEventPayload(BasePayload):
user: str user: str
ChannelDeleteTypingEventPayload = ChannelStartTypingEventPayload ChannelDeleteTypingEventPayload = ChannelStartTypingEventPayload
class ServerUpdateEventPayloadData(TypedDict, total=False):
owner: str
name: str
description: str
icon: File
banner: File
default_permissions: int
nsfw: bool
system_messages: SystemMessagesConfig
categories: list[Category]
class ServerUpdateEventPayload(BasePayload):
id: str
data: ServerUpdateEventPayloadData
clear: Literal["Icon", "Banner", "Description"]
class ServerDeleteEventPayload(BasePayload):
id: str
class ServerCreateEventPayload(BasePayload):
id: str
server: Server
channels: list[Channel]
class ServerMemberUpdateEventPayloadData(TypedDict, total=False):
nickname: str
avatar: File
roles: list[str]
timeout: str | int
class ServerMemberUpdateEventPayload(BasePayload):
id: MemberID
data: ServerMemberUpdateEventPayloadData
clear: Literal["Nickname", "Avatar"]
class ServerMemberJoinEventPayload(BasePayload):
id: str
user: str
ServerMemberLeaveEventPayload = ServerMemberJoinEventPayload
class ServerRoleUpdateEventPayloadData(TypedDict, total=False):
name: str
colour: str
hoist: bool
rank: int
class ServerRoleUpdateEventPayload(BasePayload):
id: str
role_id: str
data: ServerRoleUpdateEventPayloadData
clear: Literal["Colour"]
class ServerRoleDeleteEventPayload(BasePayload):
id: str
role_id: str
class UserUpdateEventPayloadData(TypedDict):
status: NotRequired[Status]
avatar: NotRequired[File]
online: NotRequired[bool]
profile: NotRequired[UserProfile]
username: NotRequired[str]
display_name: NotRequired[str]
relations: NotRequired[list[UserRelation]]
badges: NotRequired[int]
online: NotRequired[bool]
flags: NotRequired[int]
discriminator: NotRequired[str]
privileged: NotRequired[bool]
class UserUpdateEventPayload(BasePayload):
id: str
data: UserUpdateEventPayloadData
clear: Literal["ProfileContent", "ProfileBackground", "StatusText", "Avatar"]
class UserRelationshipEventPayload(BasePayload):
id: str
user: str
status: Status
class MessageReactEventPayload(BasePayload):
id: str
channel_id: str
user_id: str
emoji_id: str
MessageUnreactEventPayload = MessageReactEventPayload
class MessageRemoveReactionEventPayload(BasePayload):
id: str
channel_id: str
emoji_id: str
class BulkMessageDeleteEventPayload(BasePayload):
channel: str
ids: list[str]
+9 -2
View File
@@ -1,19 +1,22 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, TypedDict from typing import TYPE_CHECKING, TypedDict
from typing_extensions import NotRequired
if TYPE_CHECKING: if TYPE_CHECKING:
from .member import Member from .member import Member
from .message import Message from .message import Message
from .user import User from .user import User
from .role import Role
__all__ = ( __all__ = (
"VosoFeature", "VosoFeature",
"ApiInfo", "ApiInfo",
"Autumn", "Autumn",
"GetServerMembers", "GetServerMembers",
"MessageWithUserData", "MessageWithUserData",
"CreateRole",
) )
@@ -48,5 +51,9 @@ class GetServerMembers(TypedDict):
class MessageWithUserData(TypedDict): class MessageWithUserData(TypedDict):
messages: list[Message] messages: list[Message]
members: list[Member] members: NotRequired[list[Member]]
users: list[User] users: list[User]
class CreateRole(TypedDict):
id: str
role: Role
+23 -15
View File
@@ -1,22 +1,30 @@
from typing import Literal, TypedDict, Union from __future__ import annotations
__all__ = ( from typing import TYPE_CHECKING, Literal, TypedDict
"GroupInvite",
"ServerInvite",
"Invite",
)
class GroupInvite(TypedDict): from typing_extensions import NotRequired
type: Literal["Group"]
_id: str
creator: str
channel: str
class ServerInvite(TypedDict): if TYPE_CHECKING:
from .file import File
__all__ = ("Invite", "PartialInvite")
class Invite(TypedDict):
type: Literal["Server"] type: Literal["Server"]
server_id: str
server_name: str
server_icon: NotRequired[str]
server_banner: NotRequired[str]
channel_id: str
channel_name: str
channel_description: NotRequired[str]
user_name: str
user_avatar: NotRequired[File]
member_count: int
class PartialInvite(TypedDict):
_id: str _id: str
server: str server: str
creator: str
channel: str channel: str
creator: str
Invite = Union[ServerInvite, GroupInvite]
+9 -7
View File
@@ -2,20 +2,22 @@ from __future__ import annotations
from typing import TYPE_CHECKING, TypedDict from typing import TYPE_CHECKING, TypedDict
from typing_extensions import NotRequired
if TYPE_CHECKING: if TYPE_CHECKING:
from .file import File from .file import File
__all__ = ("Member",) __all__ = ("Member", "MemberID")
class _MemberOptional(TypedDict, total=False):
nickname: str
avatar: File
roles: list[str]
class MemberID(TypedDict): class MemberID(TypedDict):
server: str server: str
user: str user: str
class Member(_MemberOptional): class Member(TypedDict):
_id: MemberID _id: MemberID
nickname: NotRequired[str]
avatar: NotRequired[File]
roles: NotRequired[list[str]]
joined_at: int | str
timeout: NotRequired[str | int]
+43 -6
View File
@@ -2,12 +2,29 @@ from __future__ import annotations
from typing import TYPE_CHECKING, TypedDict, Union from typing import TYPE_CHECKING, TypedDict, Union
from typing_extensions import NotRequired
if TYPE_CHECKING: if TYPE_CHECKING:
from .embed import Embed from .embed import Embed
from .file import File from .file import File
__all__ = ("Message",) __all__ = (
"UserAddContent",
"UserRemoveContent",
"UserJoinedContent",
"UserLeftContent",
"UserKickedContent",
"UserBannedContent",
"ChannelRenameContent",
"ChannelDescriptionChangeContent",
"ChannelIconChangeContent",
"Masquerade",
"Interactions",
"Message",
"MessageReplyPayload",
"SystemMessageContent",
)
class UserAddContent(TypedDict): class UserAddContent(TypedDict):
id: str id: str
@@ -40,12 +57,32 @@ class ChannelDescriptionChangeContent(TypedDict):
class ChannelIconChangeContent(TypedDict): class ChannelIconChangeContent(TypedDict):
by: str by: str
class _OptionalMessage(TypedDict): class Masquerade(TypedDict, total=False):
attachments: list[File] name: str
embeds: list[Embed] avatar: str
colour: str
class Message(_OptionalMessage): class Interactions(TypedDict):
reactions: NotRequired[list[str]]
restrict_reactions: NotRequired[bool]
SystemMessageContent = Union[UserAddContent, UserRemoveContent, UserJoinedContent, UserLeftContent, UserKickedContent, UserBannedContent, ChannelRenameContent, ChannelDescriptionChangeContent, ChannelIconChangeContent]
class Message(TypedDict):
_id: str _id: str
channel: str channel: str
author: str author: str
content: Union[str, UserAddContent, UserRemoveContent, UserJoinedContent, UserLeftContent, UserKickedContent, UserBannedContent, ChannelRenameContent, ChannelDescriptionChangeContent, ChannelIconChangeContent] content: str
system: NotRequired[SystemMessageContent]
attachments: NotRequired[list[File]]
embeds: NotRequired[list[Embed]]
mentions: NotRequired[list[str]]
replies: NotRequired[list[str]]
edited: NotRequired[str | int]
masquerade: NotRequired[Masquerade]
interactions: NotRequired[Interactions]
reactions: dict[str, list[str]]
class MessageReplyPayload(TypedDict):
id: str
mention: bool
+8
View File
@@ -0,0 +1,8 @@
from __future__ import annotations
from typing import TypedDict
class Overwrite(TypedDict):
a: int
d: int
+11 -11
View File
@@ -1,19 +1,19 @@
from __future__ import annotations from __future__ import annotations
from typing import TypedDict from typing import TYPE_CHECKING, TypedDict
from typing_extensions import NotRequired
if TYPE_CHECKING:
from .permissions import Overwrite
__all__ = ( __all__ = (
"Permission",
"Role", "Role",
) )
Permission = tuple[int, int] class Role(TypedDict):
class _RoleOptional(TypedDict, total=False):
colour: str
hoist: bool
rank: int
class Role(_RoleOptional):
name: str name: str
permissions: Permission permissions: Overwrite
colour: NotRequired[str]
hoist: NotRequired[bool]
rank: int
+21 -19
View File
@@ -2,17 +2,19 @@ from __future__ import annotations
from typing import TYPE_CHECKING, TypedDict from typing import TYPE_CHECKING, TypedDict
from typing_extensions import NotRequired
if TYPE_CHECKING: if TYPE_CHECKING:
from .category import Category from .category import Category
from .channel import Channel
from .file import File from .file import File
from .role import Permission, Role from .role import Role
__all__ = ( __all__ = (
"Server", "Server",
"BannedUser", "BannedUser",
"Ban", "Ban",
"ServerBans", "ServerBans",
"SystemMessagesConfig"
) )
class SystemMessagesConfig(TypedDict, total=False): class SystemMessagesConfig(TypedDict, total=False):
@@ -21,34 +23,34 @@ class SystemMessagesConfig(TypedDict, total=False):
user_kicked: str user_kicked: str
user_banned: str user_banned: str
class _ServerOptional(TypedDict, total=False):
nonce: str
description: str
categories: list[Category]
system_messages: SystemMessagesConfig
roles: list[Role]
icon: File
banner: File
class Server(TypedDict): class Server(TypedDict):
_id: str _id: str
owner: str owner: str
name: str name: str
channels: list[str] channels: list[str]
default_permissions: Permission default_permissions: int
nonce: NotRequired[str]
description: NotRequired[str]
categories: NotRequired[list[Category]]
system_messages: NotRequired[SystemMessagesConfig]
roles: NotRequired[dict[str, Role]]
icon: NotRequired[File]
banner: NotRequired[File]
nsfw: NotRequired[bool]
class _OptionalBannedUser(TypedDict, total=False): class BannedUser(TypedDict):
avatar: File
class BannedUser(_OptionalBannedUser):
_id: str _id: str
username: str username: str
avatar: NotRequired[File]
class _OptionalBan(TypedDict, total=False): class BanId(TypedDict):
reason: str server: str
user: str
class Ban(_OptionalBan): class Ban(TypedDict):
_id: str _id: BanId
reason: NotRequired[str]
class ServerBans(TypedDict): class ServerBans(TypedDict):
users: list[BannedUser] users: list[BannedUser]
+14 -11
View File
@@ -2,6 +2,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Literal, TypedDict from typing import TYPE_CHECKING, Literal, TypedDict
from typing_extensions import NotRequired
if TYPE_CHECKING: if TYPE_CHECKING:
from .file import File from .file import File
@@ -27,19 +29,20 @@ class UserRelation(TypedDict):
status: Relation status: Relation
_id: str _id: str
class _OptionalUser(TypedDict, total=False): class User(TypedDict):
avatar: File
relations: list[UserRelation]
badges: int
status: Status
relationship: Relation
online: bool
flags: int
bot: UserBot
class User(_OptionalUser):
_id: str _id: str
username: str username: str
discriminator: str
display_name: NotRequired[str]
avatar: NotRequired[File]
relations: NotRequired[list[UserRelation]]
badges: NotRequired[int]
status: NotRequired[Status]
relationship: NotRequired[Relation]
online: NotRequired[bool]
flags: NotRequired[int]
bot: NotRequired[UserBot]
privileged: NotRequired[bool]
class UserProfile(TypedDict, total=False): class UserProfile(TypedDict, total=False):
content: str content: str
+297 -32
View File
@@ -1,17 +1,28 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, NamedTuple, Optional from typing import TYPE_CHECKING, NamedTuple, Optional, Union
from weakref import WeakValueDictionary
from .asset import Asset from revolt.types.user import UserRelation
from .asset import Asset, PartialAsset
from .channel import DMChannel, GroupDMChannel, SavedMessageChannel
from .enums import PresenceType, RelationshipType from .enums import PresenceType, RelationshipType
from .flags import UserBadges
from .messageable import Messageable
from .permissions import UserPermissions
from .utils import Ulid
if TYPE_CHECKING: if TYPE_CHECKING:
from .member import Member
from .state import State from .state import State
from .types import File
from .types import Status as StatusPayload
from .types import User as UserPayload from .types import User as UserPayload
from .types import UserRelation from .types import UserProfile as UserProfileData
from .server import Server
__all__ = ("User", "Status", "Relation", "UserProfile")
__all__ = ("User",)
class Relation(NamedTuple): class Relation(NamedTuple):
"""A namedtuple representing a relation between the bot and a user""" """A namedtuple representing a relation between the bot and a user"""
@@ -23,43 +34,60 @@ class Status(NamedTuple):
text: Optional[str] text: Optional[str]
presence: Optional[PresenceType] presence: Optional[PresenceType]
class User: class UserProfile(NamedTuple):
"""A namedtuple representing a users profile"""
content: Optional[str]
background: Optional[Asset]
class User(Messageable, Ulid):
"""Represents a user """Represents a user
Attributes Attributes
----------- -----------
id: :class:`str` id: :class:`str`
The users id The user's id
name: :class:`str` discriminator: :class:`str`
The users name The user's discriminator
display_name: Optional[:class:`str`]
The user's display name if they have one
bot: :class:`bool` bot: :class:`bool`
Whether or not the user is a bot Whether or not the user is a bot
owner: Optional[:class:`User`] owner_id: Optional[:class:`str`]
The bot's owner if the user is a bot The bot's owner id if the user is a bot
badges: :class:`int` badges: :class:`UserBadges`
The users badges The users badges
online: :class:`bool` online: :class:`bool`
Whether or not the user is online Whether or not the user is online
flags: :class:`int` flags: :class:`int`
The user flags The user flags
avatar: :class:`Asset`
The users avatar if they have one
relations: list[:class:`Relation`] relations: list[:class:`Relation`]
A list of the users relations A list of the users relations
relationship: Optional[:class:`RelationshipType`] relationship: Optional[:class:`RelationshipType`]
The relationship between the user and the bot The relationship between the user and the bot
status: Optional[:class:`Status`] status: Optional[:class:`Status`]
The users status The users status
dm_channel: Optional[:class:`DMChannel`]
The dm channel between the client and the user, this will only be set if the client has dm'ed the user or :meth:`User.open_dm` was run
privileged: :class:`bool`
Whether the user is privileged
""" """
__flattern_attributes__ = ("id", "name", "bot", "owner_id", "badges", "online", "flags", "avatar", "relations", "relationship", "status") __flattern_attributes__: tuple[str, ...] = ("id", "discriminator", "display_name", "bot", "owner_id", "badges", "online", "flags", "relations", "relationship", "status", "masquerade_avatar", "masquerade_name", "original_name", "original_avatar", "profile", "dm_channel", "privileged")
__slots__ = (*__flattern_attributes__, "state") __slots__: tuple[str, ...] = (*__flattern_attributes__, "state", "_members")
def __init__(self, data: UserPayload, state: State): def __init__(self, data: UserPayload, state: State):
self.state = state self.state = state
self.id = data["_id"] self._members: WeakValueDictionary[str, Member] = WeakValueDictionary() # we store all member versions of this user to avoid having to check every guild when needing to update.
self.name = data["username"] self.id: str = data["_id"]
self.discriminator: str = data["discriminator"]
self.display_name: str | None = data.get("display_name")
self.original_name: str = data["username"]
self.dm_channel: DMChannel | SavedMessageChannel | None = None
bot = data.get("bot") bot = data.get("bot")
self.bot: bool
self.owner_id: str | None
if bot: if bot:
self.bot = True self.bot = True
self.owner_id = bot["owner"] self.owner_id = bot["owner"]
@@ -67,36 +95,273 @@ class User:
self.bot = False self.bot = False
self.owner_id = None self.owner_id = None
self.badges = data.get("badges", 0) self.badges: UserBadges = UserBadges._from_value(data.get("badges", 0))
self.online = data.get("online", False) self.online: bool = data.get("online", False)
self.flags = data.get("flags", 0) self.flags: int = data.get("flags", 0)
self.privileged: bool = data.get("privileged", False)
avatar = data.get("avatar") avatar = data.get("avatar")
self.avatar = Asset(avatar, state) if avatar else None self.original_avatar: Asset | None = Asset(avatar, state) if avatar else None
relations = [] relations: list[Relation] = []
for relation in data.get("relations", []): for relation in data.get("relations", []):
user = state.get_user(relation["_id"]) user = state.get_user(relation["_id"])
if user: if user:
relations.append(Relation(RelationshipType(relation["status"]), user)) relations.append(Relation(RelationshipType(relation["status"]), user))
self.relations = relations
self.relations: list[Relation] = relations
relationship = data.get("relationship") relationship = data.get("relationship")
self.relationship = RelationshipType(relationship) if relationship else None self.relationship: RelationshipType | None = RelationshipType(relationship) if relationship else None
status = data.get("status") status = data.get("status")
self.status: Status | None
if status: if status:
presence = status.get("presence") presence = status.get("presence")
self.status = Status(status.get("text"), PresenceType(presence) if presence else None) if status else None self.status = Status(status.get("text"), PresenceType(presence) if presence else None) if status else None
else: else:
self.status = None self.status = None
self.profile: Optional[UserProfile] = None
self.masquerade_avatar: Optional[PartialAsset] = None
self.masquerade_name: Optional[str] = None
def get_permissions(self) -> UserPermissions:
"""Gets the permissions for the user
Returns
--------
:class:`UserPermissions`
The users permissions
"""
permissions = UserPermissions()
if self.relationship in [RelationshipType.friend, RelationshipType.user]:
return UserPermissions.all()
elif self.relationship in [RelationshipType.blocked, RelationshipType.blocked_other]:
return UserPermissions(access=True)
elif self.relationship in [RelationshipType.incoming_friend_request, RelationshipType.outgoing_friend_request]:
permissions.access = True
for channel in self.state.channels.values():
if (isinstance(channel, (GroupDMChannel, DMChannel)) and self.id in channel.recipient_ids) or any(self.id in (m.id for m in server.members) for server in self.state.servers.values()):
if self.state.me.bot or self.bot:
permissions.send_message = True
permissions.access = True
permissions.view_profile = True
return permissions
def has_permissions(self, **permissions: bool) -> bool:
"""Computes if the user has the specified permissions
Parameters
-----------
permissions: :class:`bool`
The permissions to check, this also accepted `False` if you need to check if the user does not have the permission
Returns
--------
:class:`bool`
Whether or not they have the permissions
"""
perms = self.get_permissions()
return all([getattr(perms, key, False) == value for key, value in permissions.items()])
async def _get_channel_id(self):
if not self.dm_channel:
payload = await self.state.http.open_dm(self.id)
if payload["channel_type"] == "SavedMessages":
self.dm_channel = SavedMessageChannel(payload, self.state)
else:
self.dm_channel = DMChannel(payload, self.state)
return self.dm_channel.id
@property @property
def owner(self) -> Optional[User]: def owner(self) -> User:
owner_id = self.owner_id """:class:`User` the owner of the bot account"""
if not owner_id: if not self.owner_id:
return raise LookupError
return self.state.get_user(owner_id) return self.state.get_user(self.owner_id)
@property
def name(self) -> str:
""":class:`str` The name the user is displaying, this includes (in order) their masqueraded name, display name and orginal name"""
return self.display_name or self.masquerade_name or self.original_name
@property
def avatar(self) -> Union[Asset, PartialAsset, None]:
"""Optional[:class:`Asset`] The avatar the member is displaying, this includes there orginal avatar and masqueraded avatar"""
return self.masquerade_avatar or self.original_avatar
@property
def mention(self) -> str:
""":class:`str`: Returns a string that allows you to mention the given user."""
return f"<@{self.id}>"
def _update(
self,
*,
status: Optional[StatusPayload] = None,
profile: Optional[UserProfileData] = None,
avatar: Optional[File] = None,
online: Optional[bool] = None,
display_name: Optional[str] = None,
relations: Optional[list[UserRelation]] = None,
badges: Optional[int] = None,
flags: Optional[int] = None,
discriminator: Optional[str] = None,
privileged: Optional[bool] = None,
username: Optional[str] = None
) -> None:
if status is not None:
presence = status.get("presence")
self.status = Status(status.get("text"), PresenceType(presence) if presence else None)
if profile is not None:
if background_file := profile.get("background"):
background = Asset(background_file, self.state)
else:
background = None
self.profile = UserProfile(profile.get("content"), background)
if avatar is not None:
self.original_avatar = Asset(avatar, self.state)
if online is not None:
self.online = online
if display_name is not None:
self.display_name = display_name
if relations is not None:
new_relations: list[Relation] = []
for relation in relations:
user = self.state.get_user(relation["_id"])
if user:
new_relations.append(Relation(RelationshipType(relation["status"]), user))
self.relations = new_relations
if badges is not None:
self.badges = UserBadges(badges)
if flags is not None:
self.flags = flags
if discriminator is not None:
self.discriminator = discriminator
if privileged is not None:
self.privileged = privileged
if username is not None:
self.original_name = username
# update user infomation for all members
if self.__class__ is User:
for member in self._members.values():
User._update(
member,
status=status,
profile=profile,
avatar=avatar,
online=online,
display_name=display_name,
relations=relations,
badges=badges,
flags=flags,
discriminator=discriminator,
privileged=privileged,
username=username
)
async def default_avatar(self) -> bytes:
"""Returns the default avatar for this user
Returns
--------
:class:`bytes`
The bytes of the image
"""
return await self.state.http.fetch_default_avatar(self.id)
async def fetch_profile(self) -> UserProfile:
"""Fetches the user's profile
Returns
--------
:class:`UserProfile`
The user's profile
"""
if profile := self.profile:
return profile
payload = await self.state.http.fetch_profile(self.id)
if file := payload.get("background"):
background = Asset(file, self.state)
else:
background = None
self.profile = UserProfile(payload.get("content"), background)
return self.profile
def to_member(self, server: Server) -> Member:
"""Gets the member instance for this user for a specific server.
Roughly equivelent to:
.. code-block:: python
member = server.get_member(user.id)
Parameters
-----------
server: :class:`Server`
The server to get the member for
Returns
--------
:class:`Member`
The member
Raises
-------
:class:`LookupError`
"""
try:
return self._members[server.id]
except IndexError:
raise LookupError from None
async def open_dm(self) -> DMChannel | SavedMessageChannel:
"""Opens a dm with the user, if this user is the current user this will return :class:`SavedMessageChannel`
.. note:: using this function is discouraged as :meth:`User.send` does this implicitally.
Returns
--------
Union[:class:`DMChannel`, :class:`SavedMessageChannel`]
"""
await self._get_channel_id()
assert self.dm_channel
return self.dm_channel
+140 -5
View File
@@ -1,12 +1,25 @@
from typing import Callable, TypeVar from __future__ import annotations
__all__ = ("Missing", "copy_doc") import datetime
import inspect
from contextlib import asynccontextmanager
from operator import attrgetter
from typing import Any, Callable, Coroutine, Iterable, Literal, TypeVar, Union
import ulid
from aiohttp import ClientSession
from typing_extensions import ParamSpec
__all__ = ("_Missing", "Missing", "copy_doc", "maybe_coroutine", "get", "client_session", "parse_timestamp")
class _Missing: class _Missing:
def __repr__(self): def __repr__(self) -> str:
return "<Missing>" return "<Missing>"
Missing = _Missing() def __bool__(self) -> Literal[False]:
return False
Missing: _Missing = _Missing()
T = TypeVar("T") T = TypeVar("T")
@@ -14,5 +27,127 @@ def copy_doc(from_t: T) -> Callable[[T], T]:
def inner(to_t: T) -> T: def inner(to_t: T) -> T:
to_t.__doc__ = from_t.__doc__ to_t.__doc__ = from_t.__doc__
return to_t return to_t
return inner return inner
R_T = TypeVar("R_T")
P = ParamSpec("P")
# it is impossible to type this function correctly as typeguard does not narrow for the negative case,
# so `value` would stay being a union even after the if statement (PEP 647 - "The type is not narrowed in the negative case")
# see typing#926, typing#930, typing#996
async def maybe_coroutine(func: Callable[P, Union[R_T, Coroutine[Any, Any, R_T]]], *args: P.args, **kwargs: P.kwargs) -> R_T:
value = func(*args, **kwargs)
if inspect.isawaitable(value):
value = await value
return value # type: ignore
class Ulid:
"""Base class for any revolt object with an id
Attributes
-----------
id: :class:`str`
The id of the object
"""
id: str
@property
def created_at(self) -> datetime.datetime:
"""Returns a datetime for when the object was created according to the id
Returns
--------
:class:`datetime.datetime`
The datetime of the creation date and time
"""
return ulid.from_str(self.id).timestamp().datetime
class Object(Ulid):
"""Class to mock objects with an id
.. note::
This does not validate or guarantee the id is correct, you must handle this yourself
Parameters
-----------
id: :class:`str`
The ULID id to mock
"""
def __init__(self, id: str):
self.id = id
def get(iterable: Iterable[T], **attrs: Any) -> T:
"""A convenience function to help get a value from an iterable with a specific attribute
Examples
---------
.. code-block:: python
:emphasize-lines: 3
from revolt import utils
channel = utils.get(server.channels, name="General")
await channel.send("Hello general chat.")
Parameters
-----------
iterable: Iterable
The values to search though
**attrs: Any
The attributes to check
Returns
--------
Any
The value from the iterable with the met attributes
Raises
-------
LookupError
Raises when none of the values in the iterable matches the attributes
"""
converted = [(attrgetter(attr.replace('__', '.')), value) for attr, value in attrs.items()]
for elem in iterable:
if all(pred(elem) == value for pred, value in converted):
return elem
raise LookupError
@asynccontextmanager
async def client_session():
"""A context manager that creates a new aiohttp.ClientSession() and closes it when exiting the context.
Examples
---------
.. code-block:: python
:emphasize-lines: 3
async def main():
async with client_session() as session:
client = revolt.Client(session, "TOKEN")
await client.start()
asyncio.run(main())
"""
session = ClientSession()
try:
yield session
finally:
await session.close()
def parse_timestamp(timestamp: int | str) -> datetime.datetime:
if isinstance(timestamp, int):
return datetime.datetime.fromtimestamp(timestamp / 1000, tz=datetime.timezone.utc)
else:
return datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f%z")
+374 -64
View File
@@ -2,20 +2,43 @@ from __future__ import annotations
import asyncio import asyncio
import logging import logging
import time
from copy import copy from copy import copy
from typing import TYPE_CHECKING, Callable, cast from typing import TYPE_CHECKING, Callable, NamedTuple, cast
from .types import (ChannelCreateEventPayload, ChannelDeleteEventPayload, from .errors import RevoltError
ChannelDeleteTypingEventPayload, from . import utils
from .channel import GroupDMChannel, TextChannel, VoiceChannel
from .enums import RelationshipType
from .role import Role
from .types import (BulkMessageDeleteEventPayload, ChannelCreateEventPayload,
ChannelDeleteEventPayload, ChannelDeleteTypingEventPayload,
ChannelStartTypingEventPayload, ChannelUpdateEventPayload) ChannelStartTypingEventPayload, ChannelUpdateEventPayload)
from .types import Member as MemberPayload
from .types import MemberID as MemberIDPayload
from .types import Message as MessagePayload from .types import Message as MessagePayload
from .types import MessageDeleteEventPayload, MessageUpdateEventPayload from .types import (MessageDeleteEventPayload, MessageReactEventPayload,
MessageRemoveReactionEventPayload,
MessageUnreactEventPayload, MessageUpdateEventPayload)
from .types import Role as RolePayload
from .types import (ServerCreateEventPayload, ServerDeleteEventPayload,
ServerMemberJoinEventPayload,
ServerMemberLeaveEventPayload,
ServerMemberUpdateEventPayload,
ServerRoleDeleteEventPayload, ServerRoleUpdateEventPayload,
ServerUpdateEventPayload, UserRelationshipEventPayload,
UserUpdateEventPayload)
from .user import Status, User, UserProfile
import aiohttp
try: try:
import ujson as json import ujson as json
except ImportError: except ImportError:
import json import json
use_msgpack: bool
try: try:
import msgpack import msgpack
use_msgpack = True use_msgpack = True
@@ -26,40 +49,50 @@ if TYPE_CHECKING:
import aiohttp import aiohttp
from .state import State from .state import State
from .types import AuthenticatePayload, BasePayload from .types import (AuthenticatePayload, BasePayload, MessageEventPayload,
from .types import Member as MemberPayload ReadyEventPayload)
from .types import MessageEventPayload, ReadyEventPayload from .message import Message
class WSMessage(NamedTuple):
type: aiohttp.WSMsgType
data: str | bytes | aiohttp.WSCloseCode
__all__ = ("WebsocketHandler",) __all__: tuple[str, ...] = ("WebsocketHandler",)
logger = logging.getLogger("revolt") logger: logging.Logger = logging.getLogger("revolt")
class WebsocketHandler: class WebsocketHandler:
__slots__ = ("session", "token", "ws_url", "dispatch", "state", "websocket", "loop") __slots__ = ("session", "token", "ws_url", "dispatch", "state", "websocket", "loop", "user", "ready", "server_events")
def __init__(self, session: aiohttp.ClientSession, token: str, ws_url: str, dispatch: Callable[..., None], state: State): def __init__(self, session: aiohttp.ClientSession, token: str, ws_url: str, dispatch: Callable[..., None], state: State):
self.session = session self.session: aiohttp.ClientSession = session
self.token = token self.token: str = token
self.ws_url = ws_url self.ws_url: str = ws_url
self.dispatch = dispatch self.dispatch: Callable[..., None] = dispatch
self.state = state self.state: State = state
self.websocket: aiohttp.ClientWebSocketResponse self.websocket: aiohttp.ClientWebSocketResponse
self.loop = asyncio.get_running_loop() self.loop: asyncio.AbstractEventLoop = asyncio.get_running_loop()
self.user: User | None = None
self.ready: asyncio.Event = asyncio.Event()
self.server_events: dict[str, asyncio.Event] = {}
async def send_payload(self, payload: BasePayload): async def _wait_for_server_ready(self, server_id: str) -> None:
if event := self.server_events.get(server_id):
await event.wait()
async def send_payload(self, payload: BasePayload) -> None:
if use_msgpack: if use_msgpack:
await self.websocket.send_bytes(msgpack.packb(payload)) # type: ignore await self.websocket.send_bytes(msgpack.packb(payload)) # type: ignore
else: else:
await self.websocket.send_str(json.dumps(payload)) await self.websocket.send_str(json.dumps(payload))
async def heartbeat(self): async def heartbeat(self) -> None:
while not self.websocket.closed: while not self.websocket.closed:
logger.info("Sending hearbeat") logger.info("Sending hearbeat")
await self.websocket.ping() await self.websocket.ping()
await asyncio.sleep(15) await asyncio.sleep(15)
async def send_authenticate(self): async def send_authenticate(self) -> None:
payload: AuthenticatePayload = { payload: AuthenticatePayload = {
"type": "Authenticate", "type": "Authenticate",
"token": self.token "token": self.token
@@ -67,23 +100,32 @@ class WebsocketHandler:
await self.send_payload(payload) await self.send_payload(payload)
async def handle_event(self, payload: BasePayload): async def handle_event(self, payload: BasePayload) -> None:
event_type = payload["type"].lower() event_type = payload["type"].lower()
logger.debug("Recieved event %s %s", event_type, payload) logger.debug("Recieved event %s %s", event_type, payload)
try: try:
if event_type not in ["ready", "notfound"]:
await self.ready.wait()
func = getattr(self, f"handle_{event_type}") func = getattr(self, f"handle_{event_type}")
except: except AttributeError:
logger.debug("Unknown event '%s'", event_type) return logger.debug("Unknown event '%s'", event_type)
return
await func(payload) await func(payload)
async def handle_authenticated(self, _): async def handle_authenticated(self, _: BasePayload) -> None:
logger.info("Successfully authenticated") logger.info("Successfully authenticated")
async def handle_ready(self, payload: ReadyEventPayload): async def handle_notfound(self, _: BasePayload) -> None:
for user in payload["users"]: raise RevoltError("Invalid token")
self.state.add_user(user)
async def handle_ready(self, payload: ReadyEventPayload) -> None:
for user_payload in payload["users"]:
user = self.state.add_user(user_payload)
if user.relationship == RelationshipType.user:
self.user = user
for channel in payload["channels"]: for channel in payload["channels"]:
self.state.add_channel(channel) self.state.add_channel(channel)
@@ -95,47 +137,71 @@ class WebsocketHandler:
for member in payload["members"]: for member in payload["members"]:
self.state.add_member(member["_id"]["server"], member) self.state.add_member(member["_id"]["server"], member)
for emoji in payload["emojis"]:
emoji = self.state.add_emoji(emoji)
await self.state.fetch_all_server_members() await self.state.fetch_all_server_members()
self.ready.set()
self.dispatch("ready") self.dispatch("ready")
async def handle_message(self, payload: MessageEventPayload): async def handle_message(self, payload: MessageEventPayload) -> None:
if server := self.state.get_channel(payload["channel"]).server_id:
await self._wait_for_server_ready(server)
message = self.state.add_message(cast(MessagePayload, payload)) message = self.state.add_message(cast(MessagePayload, payload))
self.dispatch("message", message) self.dispatch("message", message)
async def handle_messageupdate(self, payload: MessageUpdateEventPayload): async def handle_messageupdate(self, payload: MessageUpdateEventPayload) -> None:
self.dispatch("raw_message_update", payload) self.dispatch("raw_message_update", payload)
message = self.state.get_message(payload["id"]) try:
message = self.state.get_message(payload["id"])
except LookupError:
return
data = payload["data"] if server_id := message.channel.server_id:
kwargs = {} await self._wait_for_server_ready(server_id)
if data["content"]: before = copy(message)
kwargs["content"] = data["content"] message._update(**payload["data"])
if data["edited"]["$date"]: self.dispatch("message_update", before, message)
kwargs["edited_at"] = data["edited"]["$date"]
message._update(**kwargs) async def handle_messagedelete(self, payload: MessageDeleteEventPayload) -> None:
self.dispatch("message_update", message)
async def handle_messagedelete(self, payload: MessageDeleteEventPayload):
self.dispatch("raw_message_delete", payload) self.dispatch("raw_message_delete", payload)
message = self.state.get_message(payload["id"]) try:
if message: message = self.state.get_message(payload["id"])
self.state.messages.remove(message) except LookupError:
self.dispatch("message_delete", message) return
async def handle_channelcreate(self, payload: ChannelCreateEventPayload): if server_id := message.channel.server_id:
await self._wait_for_server_ready(server_id)
self.state.messages.remove(message)
self.dispatch("message_delete", message)
async def handle_channelcreate(self, payload: ChannelCreateEventPayload) -> None:
channel = self.state.add_channel(payload) channel = self.state.add_channel(payload)
if server_id := channel.server_id:
await self._wait_for_server_ready(server_id)
self.dispatch("channel_create", channel) self.dispatch("channel_create", channel)
async def handle_channelupdate(self, payload: ChannelUpdateEventPayload): async def handle_channelupdate(self, payload: ChannelUpdateEventPayload) -> None:
channel = self.state.get_channel(payload["id"]) # Revolt sends channel updates for channels we dont have permissions to see, a bug, but still can cause issues as its not in the cache
if not (channel := self.state.channels.get(payload["id"], None)):
return
if server_id := channel.server_id:
await self._wait_for_server_ready(server_id)
old_channel = copy(channel) old_channel = copy(channel)
@@ -143,44 +209,288 @@ class WebsocketHandler:
if clear := payload.get("clear"): if clear := payload.get("clear"):
if clear == "Icon": if clear == "Icon":
pass # TODO if isinstance(channel, (TextChannel, VoiceChannel, GroupDMChannel)):
channel.icon = None
elif clear == "Description": elif clear == "Description":
channel.description = None # type: ignore if isinstance(channel, (TextChannel, VoiceChannel, GroupDMChannel)):
channel.description = None
self.dispatch("channel_update", old_channel, channel) self.dispatch("channel_update", old_channel, channel)
async def handle_channeldelete(self, payload: ChannelDeleteEventPayload): async def handle_channeldelete(self, payload: ChannelDeleteEventPayload) -> None:
channel = self.state.channels.pop(payload["id"]) channel = self.state.channels.pop(payload["id"])
if server_id := channel.server_id:
await self._wait_for_server_ready(server_id)
self.dispatch("channel_delete", channel) self.dispatch("channel_delete", channel)
async def handle_channelstarttyping(self, payload: ChannelStartTypingEventPayload): async def handle_channelstarttyping(self, payload: ChannelStartTypingEventPayload) -> None:
channel = self.state.get_channel(payload["id"]) channel = self.state.get_channel(payload["id"])
if server_id := channel.server_id:
await self._wait_for_server_ready(server_id)
user = self.state.get_user(payload["user"]) user = self.state.get_user(payload["user"])
self.dispatch("typing_start", channel, user) self.dispatch("typing_start", channel, user)
async def handle_channeldeletetyping(self, payload: ChannelDeleteTypingEventPayload): async def handle_channelstoptyping(self, payload: ChannelDeleteTypingEventPayload) -> None:
channel = self.state.get_channel(payload["id"]) channel = self.state.get_channel(payload["id"])
if server_id := channel.server_id:
await self._wait_for_server_ready(server_id)
user = self.state.get_user(payload["user"]) user = self.state.get_user(payload["user"])
self.dispatch("typing_delete", channel, user) self.dispatch("typing_stop", channel, user)
async def start(self): async def handle_serverupdate(self, payload: ServerUpdateEventPayload) -> None:
await self._wait_for_server_ready(payload["id"])
server = self.state.get_server(payload["id"])
old_server = copy(server)
server._update(**payload["data"])
if clear := payload.get("clear"):
if clear == "Icon":
server.icon = None
elif clear == "Banner":
server.banner = None
elif clear == "Description":
server.description = None
self.dispatch("server_update", old_server, server)
async def handle_serverdelete(self, payload: ServerDeleteEventPayload) -> None:
server = self.state.servers.pop(payload["id"])
for channel in server.channels:
del self.state.channels[channel.id]
await self._wait_for_server_ready(server.id)
self.dispatch("server_delete", server)
async def handle_servercreate(self, payload: ServerCreateEventPayload) -> None:
for channel in payload["channels"]:
self.state.add_channel(channel)
server = self.state.add_server(payload["server"])
# lock all server events until we fetch all the members, otherwise the cache will be incomplete
self.server_events[server.id] = asyncio.Event()
await self.state.fetch_server_members(server.id)
self.server_events.pop(server.id).set()
self.dispatch("server_join", server)
async def handle_servermemberupdate(self, payload: ServerMemberUpdateEventPayload) -> None:
await self._wait_for_server_ready(payload["id"]["server"])
member = self.state.get_member(payload["id"]["server"], payload["id"]["user"])
old_member = copy(member)
if clear := payload.get("clear"):
if clear == "Nickname":
member.nickname = None
elif clear == "Avatar":
member.guild_avatar = None
member._update(**payload["data"])
self.dispatch("member_update", old_member, member)
async def handle_servermemberjoin(self, payload: ServerMemberJoinEventPayload) -> None:
# avoid an api request if possible
if payload["user"] not in self.state.users:
user = await self.state.http.fetch_user(payload["user"])
self.state.add_user(user)
member = self.state.add_member(payload["id"], MemberPayload(_id=MemberIDPayload(server=payload["id"], user=payload["user"]), joined_at=int(time.time()))) # revolt doesnt give us the joined at time
self.dispatch("member_join", member)
async def handle_servermemberleave(self, payload: ServerMemberLeaveEventPayload) -> None:
await self._wait_for_server_ready(payload["id"])
server = self.state.get_server(payload["id"])
member = server._members.pop(payload["user"])
# remove the member from the user
user = self.state.get_user(payload["user"])
user._members.pop(server.id)
self.dispatch("member_leave", member)
async def handle_serverroleupdate(self, payload: ServerRoleUpdateEventPayload) -> None:
server = self.state.get_server(payload["id"])
await self._wait_for_server_ready(server.id)
try:
role = server.get_role(payload["role_id"])
except LookupError:
# the role wasnt found meaning it was just created
role = Role(cast(RolePayload, payload["data"]), payload["role_id"], server, self.state)
server._roles[role.id] = role
self.dispatch("role_create", role)
else:
old_role = copy(role)
if clear := payload.get("clear"):
if clear == "Colour":
role.colour = None
role._update(**payload["data"])
self.dispatch("role_update", old_role, role)
async def handle_serverroledelete(self, payload: ServerRoleDeleteEventPayload) -> None:
server = self.state.get_server(payload["id"])
role = server._roles.pop(payload["role_id"])
await self._wait_for_server_ready(server.id)
self.dispatch("role_delete", role)
async def handle_userupdate(self, payload: UserUpdateEventPayload) -> None:
user = self.state.get_user(payload["id"])
old_user = copy(user)
if clear := payload.get("clear"):
if clear == "ProfileContent":
if profile := user.profile:
user.profile = UserProfile(None, profile.background)
elif clear == "ProfileBackground":
if profile := user.profile:
user.profile = UserProfile(profile.content, None)
elif clear == "StatusText":
user.status = Status(None, user.status.presence if user.status else None)
elif clear == "Avatar":
user.original_avatar = None
user._update(**payload["data"])
self.dispatch("user_update", old_user, user)
async def handle_userrelationship(self, payload: UserRelationshipEventPayload) -> None:
user = self.state.get_user(payload["user"])
old_relationship = user.relationship
user.relationship = RelationshipType(payload["status"])
self.dispatch("user_relationship_update", user, old_relationship, user.relationship)
async def handle_messagereact(self, payload: MessageReactEventPayload) -> None:
if server := self.state.get_channel(payload["channel_id"]).server_id:
await self._wait_for_server_ready(server)
self.dispatch("raw_reaction_add", payload)
try:
message = utils.get(self.state.messages, id=payload["id"])
except LookupError:
return
user = self.state.get_user(payload["user_id"])
message.reactions.setdefault(payload["emoji_id"], []).append(user)
emoji_id = payload["emoji_id"]
self.dispatch("reaction_add", message, user, emoji_id)
async def handle_messageunreact(self, payload: MessageUnreactEventPayload) -> None:
if server := self.state.get_channel(payload["channel_id"]).server_id:
await self._wait_for_server_ready(server)
self.dispatch("raw_reaction_remove", payload)
try:
message = utils.get(self.state.messages, id=payload["id"])
except LookupError:
return
user = self.state.get_user(payload["user_id"])
message.reactions[payload["emoji_id"]].remove(user)
self.dispatch("reaction_remove", message, user, payload["emoji_id"])
async def handle_messageremovereaction(self, payload: MessageRemoveReactionEventPayload) -> None:
if server := self.state.get_channel(payload["channel_id"]).server_id:
await self._wait_for_server_ready(server)
self.dispatch("raw_reaction_clear", payload)
try:
message = utils.get(self.state.messages, id=payload["id"])
except LookupError:
return
users = message.reactions.pop(payload["emoji_id"])
self.dispatch("reaction_clear", message, users, payload["emoji_id"])
async def handle_bulkmessagedelete(self, payload: BulkMessageDeleteEventPayload) -> None:
channel = self.state.get_channel(payload["channel"])
self.dispatch("raw_bulk_message_delete", payload)
messages: list[Message] = []
for message_id in payload["ids"]:
if server_id := channel.server_id:
await self._wait_for_server_ready(server_id)
self.dispatch("raw_message_delete", MessageDeleteEventPayload(type="messagedelete", channel=payload["channel"], id=message_id))
try:
message = self.state.get_message(message_id)
except LookupError:
pass
else:
self.state.messages.remove(message)
self.dispatch("message_delete", message)
messages.append(message)
self.dispatch("bulk_message_delete", messages)
async def start(self, reconnect: bool) -> None:
if use_msgpack: if use_msgpack:
url = f"{self.ws_url}?format=msgpack" url = f"{self.ws_url}?format=msgpack"
else: else:
url = f"{self.ws_url}?format=json" url = f"{self.ws_url}?format=json"
self.websocket = await self.session.ws_connect(url) while True:
await self.send_authenticate() self.websocket = await self.session.ws_connect(url) # type: ignore
asyncio.create_task(self.heartbeat()) await self.send_authenticate()
hb = asyncio.create_task(self.heartbeat())
async for msg in self.websocket: async for msg in self.websocket:
if use_msgpack: msg = cast(WSMessage, msg) # aiohttp doesnt use NamedTuple so the type info is missing
payload = msgpack.unpackb(msg.data)
else:
payload = json.loads(msg.data)
self.loop.create_task(self.handle_event(payload)) if use_msgpack:
data = cast(bytes, msg.data)
payload = msgpack.unpackb(data) # type: ignore
else:
data = cast(str, msg.data)
payload = json.loads(data)
self.loop.create_task(self.handle_event(payload))
hb.cancel()
if not reconnect:
return
-39
View File
@@ -1,39 +0,0 @@
import pathlib
from setuptools import find_packages, setup
here = pathlib.Path(__file__).parent.resolve()
long_description = (here / "README.md").read_text(encoding="utf-8")
setup(
name="revolt.py",
version="0.1.1",
description="Python wrapper around revolt.chat",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/Zomatree/revolt.py",
author="Zomatree",
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3 :: Only",
],
keywords="wrapper, async, api, websockets, http",
packages=find_packages(),
python_requires=">=3.9",
extras_require={
"speedups": ["ujson", "aiohttp[speedups]==3.7.4.post0", "msgpack==1.0.2"],
},
project_urls={
"Bug Reports": "https://github.com/Zomatree/revolt.py/issues",
"Source": "https://github.com/Zomatree/revolt.py/",
},
)
+40
View File
@@ -0,0 +1,40 @@
from __future__ import annotations
from typing import Any, Callable, Dict, List, Optional, Tuple
from typing_extensions import Protocol
class _FileLike(Protocol):
def read(self, n: int) -> bytes: ...
def unpackb(
packed: bytes,
file_like: Optional[_FileLike] = ...,
read_size: int = ...,
use_list: bool = ...,
raw: bool = ...,
timestamp: int = ...,
strict_map_key: bool = ...,
object_hook: Optional[Callable[[Dict[Any, Any]], Any]] = ...,
object_pairs_hook: Optional[Callable[[List[Tuple[Any, Any]]], Any]] = ...,
list_hook: Optional[Callable[[List[Any]], Any]] = ...,
unicode_errors: Optional[str] = ...,
max_buffer_size: int = ...,
ext_hook: Callable[[int, bytes], Any] = ...,
max_str_len: int = ...,
max_bin_len: int = ...,
max_array_len: int = ...,
max_map_len: int = ...,
max_ext_len: int = ...,
) -> Any: ...
def packb(
o: Any,
default: Optional[Callable[[Any], Any]] = ...,
use_single_float: bool = ...,
autoreset: bool = ...,
use_bin_type: bool = ...,
strict_types: bool = ...,
datetime: bool = ...,
unicode_errors: Optional[str] = ...,
) -> bytes: ...
+1
View File
@@ -0,0 +1 @@
def get_html_theme_path() -> str: ...