Skip to content

Client

Client

Source code in gd/client.py
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
@define(slots=False)
class Client:
    session: Session = field(factory=Session)
    """The session of the client."""

    credentials: Credentials = field(factory=Credentials)
    """The credentials of the client."""

    cache: Cache = field(factory=Cache, repr=False)
    """The cache of the client."""

    database_unchecked: Optional[Database] = field(default=None, repr=False)
    """The database of the client."""

    load_after_post: bool = field(default=DEFAULT_LOAD_AFTER_POST)
    """Whether to load items after posting them."""

    _listeners: DynamicTuple[Listener] = field(default=(), repr=False, init=False)
    _controller: Optional[Controller] = field(default=None, repr=False, init=False)

    def apply_items(
        self,
        credentials: Optional[Credentials] = None,
        database: Optional[Database] = None,
        credentials_type: Type[Credentials] = Credentials,
    ) -> Self:
        if credentials is None:
            self.credentials = credentials_type()

        else:
            self.credentials = credentials

        self.database_unchecked = database

        return self

    @property
    def database(self) -> Database:
        database = self.database_unchecked

        if database is None:
            raise ValueError(NO_DATABASE)

        return database

    @database.setter
    def database(self, database: Database) -> None:
        self.database_unchecked = database

    @database.deleter
    def database(self) -> None:
        self.database_unchecked = None

    def reset_items(self) -> Self:
        return self.apply_items()

    def is_logged_in(self) -> bool:
        """Checks if the client is logged in.

        Returns:
            Whether the client is logged in.
        """
        return self.credentials.is_loaded()

    def run(self, awaitable: Awaitable[T]) -> T:
        return run(awaiting(awaitable))

    @property
    def account_id(self) -> int:
        """The account ID of the client."""
        return self.credentials.account_id

    @property
    def id(self) -> int:
        """The user ID of the client."""
        return self.credentials.id

    @property
    def name(self) -> str:
        """The name of the client."""
        return self.credentials.name

    @property
    def hashed_password(self) -> str:
        """The hashed password of the client."""
        return self.credentials.hashed_password

    @property
    def http(self) -> HTTPClient:
        """The [`HTTPClient`][gd.http.HTTPClient] used by the client session."""
        return self.session.http

    @property
    @check_login
    def user(self) -> UserReference:
        """The user representing the client."""
        return UserReference(id=self.id, name=self.name, account_id=self.account_id).attach_client(
            self
        )

    async def ping(self) -> Duration:
        """Pings the Geometry Dash server.

        Returns:
            The [`Duration`][gd.date_time.Duration] representing the latency.
        """
        return await self.ping_url(self.http.url)

    async def ping_url(self, url: URLString) -> Duration:
        """Pings the given `url`.

        Returns:
            The [`Duration`][gd.date_time.Duration] representing the latency.
        """
        return await self.session.ping(url)

    async def logout(self) -> None:
        """Performs the logout."""
        self.reset_items()

    def login(self, name: str, hashed_password: str) -> LoginContextManager[Self]:
        """Performs the login.

        This function returns a context manager that can be used for temporarily logging in:

        ```python
        async with client.login(name, password):
            assert client.is_logged_in()

        assert not client.is_logged_in()
        ```

        Returns:
            The [`LoginContextManager`][gd.client.LoginContextManager] for handling login process.
        """
        return LoginContextManager(self, name, hashed_password)

    async def try_login(self, name: str, hashed_password: str) -> None:
        model = await self.session.login(name, hashed_password)

        self.apply_items(Credentials(model.account_id, model.id, name, hashed_password))

    def unsafe_login(self, name: str, hashed_password: str) -> UnsafeLoginContextManager[Self]:
        """Performs the *unsafe* login.

        *Unsafe* means that the credentials are not confirmed.

        This function returns a context manager that can be used for temporarily logging in:

        ```python
        async with client.unsafe_login(name, password):
            assert client.is_logged_in()

        assert not client.is_logged_in()
        ```

        Returns:
            The [`UnsafeLoginContextManager`][gd.client.UnsafeLoginContextManager]
                for handling login process.
        """
        return UnsafeLoginContextManager(self, name, hashed_password)

    async def try_unsafe_login(self, name: str, hashed_password: str) -> None:
        user = await self.search_user(query(name), simple=True)

        self.apply_items(Credentials(user.account_id, user.id, name, hashed_password))

    @check_login
    async def load(self) -> Database:
        """Loads the save from the cloud, saving it in the
        [`database`][gd.client.Client.database] attribute.

        Note:
            This function requires the client to be logged in.

        Returns:
            The [`Database`][gd.api.database.Database] loaded.
        """
        database = await self.session.load(
            account_id=self.account_id,
            name=self.name,
            hashed_password=self.hashed_password,
        )

        self.database = database

        return database

    @check_login
    async def save(self, database: Optional[Database] = None) -> None:
        """Saves the the `database` to the cloud, optionally fetching it from the
        [`database`][gd.client.Client.database] attribute.

        Note:
            This function requires the client to be logged in.
        """
        if database is None:
            database = self.database

        await self.session.save(
            database,
            account_id=self.account_id,
            name=self.name,
            hashed_password=self.hashed_password,
        )

    async def get_account_url(self, account_id: int, type: AccountURLType) -> URL:
        return await self.session.get_account_url(account_id=account_id, type=type)

    @check_login
    async def update_profile(
        self,
        stars: Optional[int] = None,
        diamonds: Optional[int] = None,
        secret_coins: Optional[int] = None,
        user_coins: Optional[int] = None,
        demons: Optional[int] = None,
        icon_type: Optional[IconType] = None,
        icon_id: Optional[int] = None,
        color_1_id: Optional[int] = None,
        color_2_id: Optional[int] = None,
        color_3_id: Optional[int] = None,
        glow: Optional[bool] = None,
        cube_id: Optional[int] = None,
        ship_id: Optional[int] = None,
        ball_id: Optional[int] = None,
        ufo_id: Optional[int] = None,
        wave_id: Optional[int] = None,
        robot_id: Optional[int] = None,
        spider_id: Optional[int] = None,
        swing_id: Optional[int] = None,
        jetpack_id: Optional[int] = None,
        explosion_id: Optional[int] = None,
        streak_id: Optional[int] = None,
        special: int = DEFAULT_SPECIAL,
        *,
        set_as_user: Optional[User] = None,
    ) -> None:
        if set_as_user is None:
            user = await self.get_self()

        else:
            user = set_as_user

        statistics = user.statistics
        cosmetics = user.cosmetics

        if statistics is None:
            raise InternalError  # TODO: message?

        if cosmetics is None:
            raise InternalError  # TODO: message?

        await self.session.update_profile(
            stars=switch_none(stars, statistics.stars),
            diamonds=switch_none(diamonds, statistics.diamonds),
            secret_coins=switch_none(secret_coins, statistics.secret_coins),
            user_coins=switch_none(user_coins, statistics.user_coins),
            demons=switch_none(demons, statistics.demons),
            icon_type=switch_none(icon_type, cosmetics.icon_type),
            icon_id=switch_none(icon_id, cosmetics.icon_id),
            color_1_id=switch_none(color_1_id, cosmetics.color_1_id),
            color_2_id=switch_none(color_2_id, cosmetics.color_2_id),
            color_3_id=switch_none(color_3_id, cosmetics.color_3_id),
            glow=switch_none(glow, cosmetics.glow),
            cube_id=switch_none(cube_id, cosmetics.cube_id),
            ship_id=switch_none(ship_id, cosmetics.ship_id),
            ball_id=switch_none(ball_id, cosmetics.ball_id),
            ufo_id=switch_none(ufo_id, cosmetics.ufo_id),
            wave_id=switch_none(wave_id, cosmetics.wave_id),
            robot_id=switch_none(robot_id, cosmetics.robot_id),
            spider_id=switch_none(spider_id, cosmetics.spider_id),
            swing_id=switch_none(swing_id, cosmetics.swing_id),
            jetpack_id=switch_none(jetpack_id, cosmetics.jetpack_id),
            explosion_id=switch_none(explosion_id, cosmetics.explosion_id),
            streak_id=switch_none(streak_id, cosmetics.streak_id),
            special=special,
            account_id=self.account_id,
            name=self.name,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def update_settings(
        self,
        message_state: Optional[MessageState] = None,
        friend_request_state: Optional[FriendRequestState] = None,
        comment_state: Optional[CommentState] = None,
        youtube: Optional[str] = None,
        x: Optional[str] = None,
        twitch: Optional[str] = None,
        # discord: Optional[str] = None,
        *,
        set_as_user: Optional[User] = None,
    ) -> None:
        if set_as_user is None:
            user = await self.get_self(simple=True)

        else:
            user = set_as_user

        states = user.states
        socials = user.socials

        if states is None:
            raise InternalError  # TODO: message?

        if socials is None:
            raise InternalError  # TODO: message?

        await self.session.update_settings(
            message_state=switch_none(message_state, states.message_state),
            friend_request_state=switch_none(friend_request_state, states.friend_request_state),
            comment_state=switch_none(comment_state, states.comment_state),
            youtube=switch_none(youtube, socials.youtube),
            x=switch_none(x, socials.x),
            twitch=switch_none(twitch, socials.twitch),
            # discord=switch_none(discord, socials.discord),
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def get_self(self, simple: bool = DEFAULT_SIMPLE) -> User:
        """Gets the user representing this client.

        This is a shorthand for:

        ```python
        await client.user.get(simple=simple)
        ```

        See [`User.get`][gd.user.User.get] for more information.

        Note:
            This function requires the client to be logged in.

        Arguments:
            simple: Whether to fetch simple information.

        Returns:
            The [`User`][gd.user.User] representing the client.
        """
        return await self.user.get(simple=simple)

    async def get_user(
        self,
        account_id: int,
        simple: bool = DEFAULT_SIMPLE,
        friend_state: bool = DEFAULT_FRIEND_STATE,
    ) -> User:
        """Fetches the user by `account_id`.

        Arguments:
            account_id: The account ID of the user to fetch.
            simple: Whether to fetch simple information only.
            friend_state: Whether to fetch friend state.

        Note:
            `friend_state` requires the client to be logged in.

        Returns:
            The [`User`][gd.user.User] fetched.
        """
        if friend_state:  # if we need to fetch friend state
            check_client_login(self)

            profile_model = await self.session.get_user_profile(  # request profile
                account_id=account_id,
                client_account_id=self.account_id,
                hashed_password=self.hashed_password,
            )

        else:  # otherwise, simply request normally
            profile_model = await self.session.get_user_profile(account_id=account_id)

        if simple:  # if only the profile is needed, return right away
            return User.from_profile_model(profile_model).attach_client(self)

        search_model = await self.session.search_user(query(profile_model.id))  # search by ID

        return User.from_search_user_and_profile_models(search_model, profile_model).attach_client(
            self
        )

    async def search_user(
        self,
        query: Query,
        simple: bool = DEFAULT_SIMPLE,
        friend_state: bool = DEFAULT_FRIEND_STATE,
    ) -> User:
        search_user_model = await self.session.search_user(query=query)  # search using query

        if simple:  # if only simple is required, return right away
            return User.from_search_user_model(search_user_model)

        if friend_state:  # if friend state is requested
            check_client_login(self)  # assert client is logged in

            profile_model = await self.session.get_user_profile(  # request profile
                search_user_model.account_id,
                client_account_id=self.account_id,
                hashed_password=self.hashed_password,
            )

        else:  # otherwise, request normally
            profile_model = await self.session.get_user_profile(
                account_id=search_user_model.account_id
            )

        return User.from_search_user_and_profile_models(
            search_user_model, profile_model
        ).attach_client(self)

    @wrap_async_iter
    async def search_users_on_page(
        self, query: Query, page: int = DEFAULT_PAGE
    ) -> AsyncIterator[User]:
        search_users_response_model = await self.session.search_users_on_page(
            query=query, page=page
        )

        for search_user_model in search_users_response_model.users:
            yield User.from_search_user_model(search_user_model).attach_client(self)

    @wrap_async_iter
    def search_users(
        self,
        query: Query,
        pages: Iterable[int] = DEFAULT_PAGES,
    ) -> AsyncIterator[User]:
        return run_iterables(
            (self.search_users_on_page(query=query, page=page).unwrap() for page in pages),
            ClientError,
        )

    @wrap_async_iter
    @check_login
    async def get_relationships(self, type: RelationshipType) -> AsyncIterator[User]:
        try:
            response_model = await self.session.get_relationships(
                type=type,
                account_id=self.account_id,
                hashed_password=self.hashed_password,
            )

        except NothingFound:
            return

        for model in response_model.users:
            yield User.from_relationship_user_model(model).attach_client(self)

    @wrap_async_iter
    @check_login
    def get_friends(self) -> AsyncIterator[User]:
        return self.get_relationships(RelationshipType.FRIEND).unwrap()

    @wrap_async_iter
    @check_login
    def get_blocked(self) -> AsyncIterator[User]:
        return self.get_relationships(RelationshipType.BLOCKED).unwrap()

    @wrap_async_iter
    async def get_leaderboard(
        self,
        strategy: LeaderboardStrategy = LeaderboardStrategy.DEFAULT,
        count: int = DEFAULT_COUNT,
    ) -> AsyncIterator[User]:
        response_model = await self.session.get_leaderboard(
            strategy=strategy,
            count=count,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

        for model in response_model.users:
            yield User.from_leaderboard_user_model(model).attach_client(self)

    def level_models_from_model(
        self, response_model: SearchLevelsResponseModel
    ) -> Iterator[Tuple[LevelModel, UserReference, SongReference]]:
        creators_iterator = (
            UserReference.from_creator_model(model).attach_client(self)
            for model in response_model.creators
        )

        creators = {creator.id: creator for creator in creators_iterator}

        for model in response_model.levels:
            song_id = model.custom_song_id
            custom = True

            if not song_id:
                song_id = model.official_song_id
                custom = False

            song = SongReference(id=song_id, custom=custom).attach_client(self)

            creator = creators.get(model.creator_id)

            if creator is None:
                creator = UserReference.default().attach_client(self)

            yield (model, creator, song)

    async def get_daily(self, use_client: bool = DEFAULT_USE_CLIENT) -> Level:
        return await self.get_timely(TimelyType.DAILY, use_client=use_client)

    async def get_weekly(self, use_client: bool = DEFAULT_USE_CLIENT) -> Level:
        return await self.get_timely(TimelyType.WEEKLY, use_client=use_client)

    async def get_event(self, use_client: bool = DEFAULT_USE_CLIENT) -> Level:
        return await self.get_timely(TimelyType.EVENT, use_client=use_client)

    async def get_timely(self, type: TimelyType, use_client: bool = DEFAULT_USE_CLIENT) -> Level:
        timely_model = await self.session.get_timely_info(type=type)

        level = await self.get_level(type.into_timely_id().value, use_client=use_client)

        return level.update_with_timely_model(timely_model)

    async def get_level(
        self,
        level_id: int,
        get_data: bool = DEFAULT_GET_DATA,
        use_client: bool = DEFAULT_USE_CLIENT,
    ) -> Level:
        get_data = get_data or level_id < 0

        if get_data:
            if use_client:
                check_client_login(self)

                response_model = await self.session.get_level(
                    level_id=level_id,
                    account_id=self.account_id,
                    hashed_password=self.hashed_password,
                )

            else:
                response_model = await self.session.get_level(level_id)

            model = response_model.level

            level_id = model.id

        level = await self.search_levels_on_page(query(level_id)).next().extract()

        if level is None:
            raise InternalError  # TODO: message?

        if get_data:
            return Level.from_model(model, level.creator, level.song).attach_client(self)

        return level

    @wrap_async_iter
    async def search_levels_on_page(
        self,
        query: Query = EMPTY_QUERY,
        page: int = DEFAULT_PAGE,
        filters: Optional[Filters] = None,
        user: Optional[UserReference] = None,
        gauntlet: Optional[int] = None,
    ) -> AsyncIterator[Level]:
        if user is None:
            user_id = None

        else:
            user_id = user.id

        try:
            response_model = await self.session.search_levels_on_page(
                query=query,
                page=page,
                filters=filters,
                user_id=user_id,
                gauntlet=gauntlet,
                client_account_id=self.account_id,
                client_user_id=self.id,
                hashed_password=self.hashed_password,
            )

        except NothingFound:
            return

        for model, creator, song in self.level_models_from_model(response_model):
            yield Level.from_model(model, creator, song).attach_client(self)

    @wrap_async_iter
    def search_levels(
        self,
        query: Query = EMPTY_QUERY,
        pages: Iterable[int] = DEFAULT_PAGES,
        filters: Optional[Filters] = None,
        user: Optional[UserReference] = None,
        gauntlet: Optional[int] = None,
    ) -> AsyncIterator[Level]:
        return run_iterables(
            (
                self.search_levels_on_page(
                    query=query,
                    page=page,
                    filters=filters,
                    user=user,
                    gauntlet=gauntlet,
                ).unwrap()
                for page in pages
            ),
            ClientError,
        )

    @check_login
    async def update_level_description(
        self, level: LevelReference, description: Optional[str]
    ) -> None:
        await self.session.update_level_description(
            level_id=level.id,
            description=description,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    async def upload_level(
        self,
        name: str = UNNAMED,
        id: int = DEFAULT_ID,
        version: int = DEFAULT_VERSION,
        length: LevelLength = LevelLength.DEFAULT,
        official_song_id: int = DEFAULT_ID,
        description: str = EMPTY,
        song_id: int = DEFAULT_ID,
        original_id: int = DEFAULT_ID,
        two_player: bool = DEFAULT_TWO_PLAYER,
        privacy: LevelPrivacy = LevelPrivacy.DEFAULT,
        object_count: int = DEFAULT_OBJECT_COUNT,
        coins: int = DEFAULT_COINS,
        reward: int = DEFAULT_COUNT,
        low_detail: bool = DEFAULT_LOW_DETAIL,
        capacity: Optional[Capacity] = None,
        password: Optional[Password] = None,
        recording: Optional[Recording] = None,
        editor_time: Optional[Duration] = None,
        copies_time: Optional[Duration] = None,
        time_steps: int = DEFAULT_TIME_STEPS,
        data: str = EMPTY,
    ) -> Level:
        level_id = await self.session.upload_level(
            name=name,
            id=id,
            version=version,
            length=length,
            official_song_id=official_song_id,
            song_id=song_id,
            description=description,
            original_id=original_id,
            two_player=two_player,
            object_count=object_count,
            coins=coins,
            reward=reward,
            privacy=privacy,
            low_detail=low_detail,
            capacity=capacity,
            password=password,
            recording=recording,
            editor_time=editor_time,
            copies_time=copies_time,
            time_steps=time_steps,
            data=data,
            account_id=self.account_id,
            account_name=self.name,
            hashed_password=self.hashed_password,
        )

        if self.load_after_post:
            return await self.get_level(level_id)

        level = Level.default(level_id).attach_client(self)

        return level

    async def report_level(self, level: LevelReference) -> None:
        await self.session.report_level(level.id)

    @check_login
    async def delete_level(self, level: LevelReference) -> None:
        await self.session.delete_level(
            level_id=level.id,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def rate_level(self, level: LevelReference, stars: int) -> None:
        await self.session.rate_level(
            level_id=level.id,
            stars=stars,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def rate_demon(self, level: LevelReference, rating: DemonDifficulty) -> None:
        await self.session.rate_demon(
            level_id=level.id,
            rating=rating,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def suggest_demon(self, level: LevelReference, rating: DemonDifficulty) -> None:
        await self.session.suggest_demon(
            level_id=level.id,
            rating=rating,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def suggest_level(self, level: LevelReference, stars: int, feature: bool) -> None:
        return await self.session.suggest_level(
            level_id=level.id,
            stars=stars,
            feature=feature,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @wrap_async_iter
    @check_login
    async def get_level_leaderboard(
        self,
        level: LevelReference,
        strategy: LevelLeaderboardStrategy = LevelLeaderboardStrategy.ALL,
    ) -> AsyncIterator[User]:
        response_model = await self.session.get_level_leaderboard(
            level_id=level.id,
            strategy=strategy,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

        for model in response_model.users:
            yield User.from_level_leaderboard_user_model(model).attach_client(self)

    @check_login
    async def block_user(self, user: UserReference) -> None:
        await self.session.block_user(
            account_id=user.account_id,
            client_account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def unblock_user(self, user: UserReference) -> None:
        await self.session.unblock_user(
            account_id=user.account_id,
            client_account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def unfriend_user(self, user: UserReference) -> None:
        await self.session.unfriend_user(
            account_id=user.account_id,
            client_account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def send_message(
        self,
        user: UserReference,
        subject: Optional[str] = None,
        content: Optional[str] = None,
    ) -> Optional[Message]:
        await self.session.send_message(
            account_id=user.account_id,
            subject=subject,
            content=content,
            client_account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

        if self.load_after_post:
            messages = self.get_messages_on_page(MessageType.OUTGOING)
            message = await messages.find(by_subject_and_user(subject, user)).extract()

            if message is None:
                return message

            if content is None:
                content = EMPTY

            message.content = content

            return message

        return None

    @check_login
    async def get_message(self, message: MessageReference) -> Message:
        model = await self.session.get_message(
            message_id=message.id,
            type=message.type,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

        return Message.from_model(model).attach_client(self)

    @check_login
    async def delete_message(self, message: MessageReference) -> None:
        await self.session.delete_message(
            message_id=message.id,
            type=message.type,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @wrap_async_iter
    @check_login
    async def get_messages_on_page(
        self, type: MessageType = MessageType.DEFAULT, page: int = DEFAULT_PAGE
    ) -> AsyncIterator[Message]:
        try:
            response_model = await self.session.get_messages_on_page(
                type=type,
                page=page,
                account_id=self.account_id,
                hashed_password=self.hashed_password,
            )

        except NothingFound:
            return

        for model in response_model.messages:
            yield Message.from_model(model).attach_client(self)

    @wrap_async_iter
    @check_login
    def get_messages(
        self,
        type: MessageType = MessageType.DEFAULT,
        pages: Iterable[int] = DEFAULT_PAGES,
    ) -> AsyncIterator[Message]:
        return run_iterables(
            (self.get_messages_on_page(type=type, page=page).unwrap() for page in pages),
            ClientError,
        )

    @check_login
    async def send_friend_request(
        self, user: UserReference, message: Optional[str] = None
    ) -> Optional[FriendRequest]:
        await self.session.send_friend_request(
            account_id=user.account_id,
            message=message,
            client_account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

        if self.load_after_post:
            friend_requests = self.get_friend_requests_on_page(FriendRequestType.OUTGOING)

            return await friend_requests.find(by_user(user)).extract()

        return None

    @check_login
    async def delete_friend_request(self, friend_request: FriendRequestReference) -> None:
        await self.session.delete_friend_request(
            account_id=friend_request.user.account_id,
            type=friend_request.type,
            client_account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def accept_friend_request(self, friend_request: FriendRequestReference) -> None:
        await self.session.accept_friend_request(
            account_id=friend_request.user.account_id,
            request_id=friend_request.id,
            type=friend_request.type,
            client_account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def read_friend_request(self, friend_request: FriendRequestReference) -> None:
        await self.session.read_friend_request(
            request_id=friend_request.id,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @wrap_async_iter
    @check_login
    async def get_friend_requests_on_page(
        self,
        type: FriendRequestType = FriendRequestType.DEFAULT,
        page: int = DEFAULT_PAGE,
    ) -> AsyncIterator[FriendRequest]:
        try:
            response_model = await self.session.get_friend_requests_on_page(
                type=type,
                page=page,
                account_id=self.account_id,
                hashed_password=self.hashed_password,
            )

        except NothingFound:
            return

        for model in response_model.friend_requests:
            yield FriendRequest.from_model(model, type).attach_client(self)

    @wrap_async_iter
    @check_login
    def get_friend_requests(
        self,
        type: FriendRequestType = FriendRequestType.DEFAULT,
        pages: Iterable[int] = DEFAULT_PAGES,
    ) -> AsyncIterator[FriendRequest]:
        return run_iterables(
            (self.get_friend_requests_on_page(type=type, page=page).unwrap() for page in pages),
            ClientError,
        )

    @check_login
    async def like_level(self, level: LevelReference) -> None:
        await self.session.like_level(
            level.id,
            dislike=False,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def dislike_level(self, level: LevelReference) -> None:
        await self.session.like_level(
            level.id,
            dislike=True,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def like_user_comment(self, comment: UserCommentReference) -> None:
        await self.session.like_user_comment(
            comment.id,
            dislike=False,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def dislike_user_comment(self, comment: UserCommentReference) -> None:
        await self.session.like_user_comment(
            comment.id,
            dislike=True,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def like_level_comment(self, comment: LevelCommentReference) -> None:
        await self.session.like_level_comment(
            comment.id,
            comment.level.id,
            dislike=False,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def dislike_level_comment(self, comment: LevelCommentReference) -> None:
        await self.session.like_level_comment(
            comment.id,
            comment.level.id,
            dislike=True,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def post_comment(self, content: Optional[str] = None) -> Optional[UserComment]:
        comment_id = await self.session.post_user_comment(
            content=content,
            account_id=self.account_id,
            account_name=self.name,
            hashed_password=self.hashed_password,
        )

        if self.load_after_post:
            comments = self.user.get_comments_on_page()

            return await comments.find(by_id(comment_id)).extract()

        return None

    @check_login
    async def post_level_comment(
        self,
        level: LevelReference,
        content: Optional[str] = None,
        record: int = DEFAULT_RECORD,
    ) -> Optional[LevelComment]:
        comment_id = await self.session.post_level_comment(
            level_id=level.id,
            content=content,
            record=record,
            account_id=self.account_id,
            account_name=self.name,
            hashed_password=self.hashed_password,
        )

        if self.load_after_post:
            comments = level.get_comments_on_page(count=DEFAULT_COUNT)

            return await comments.find(by_id(comment_id)).extract()

        return None

    @check_login
    async def delete_user_comment(self, comment: UserCommentReference) -> None:
        await self.session.delete_user_comment(
            comment_id=comment.id,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @check_login
    async def delete_level_comment(self, comment: LevelCommentReference) -> None:
        await self.session.delete_level_comment(
            comment_id=comment.id,
            level_id=comment.level.id,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    @wrap_async_iter
    async def get_user_comments_on_page(
        self,
        user: UserReference,
        page: int = DEFAULT_PAGE,
    ) -> AsyncIterator[UserComment]:
        response_model = await self.session.get_user_comments_on_page(
            account_id=user.account_id,
            page=page,
        )

        reference = UserReference(id=user.id, name=user.name, account_id=user.account_id)

        for model in response_model.comments:
            yield UserComment.from_model(model, reference).attach_client(self)

    @wrap_async_iter
    def get_user_comments(
        self,
        user: UserReference,
        pages: Iterable[int] = DEFAULT_PAGES,
    ) -> AsyncIterator[UserComment]:
        return run_iterables(
            (self.get_user_comments_on_page(user=user, page=page).unwrap() for page in pages),
            ClientError,
        )

    @wrap_async_iter
    async def get_user_level_comments_on_page(
        self,
        user: UserReference,
        count: int = COMMENT_PAGE_SIZE,
        page: int = DEFAULT_PAGE,
        strategy: CommentStrategy = CommentStrategy.DEFAULT,
    ) -> AsyncIterator[LevelComment]:
        try:
            response_model = await self.session.get_user_level_comments_on_page(
                user_id=user.id,
                count=count,
                page=page,
                strategy=strategy,
            )

        except NothingFound:
            return

        for model in response_model.comments:
            yield LevelComment.from_model(model).attach_client(self)

    @wrap_async_iter
    def get_user_level_comments(
        self,
        user: UserReference,
        count: int = COMMENT_PAGE_SIZE,
        pages: Iterable[int] = DEFAULT_PAGES,
        strategy: CommentStrategy = CommentStrategy.DEFAULT,
    ) -> AsyncIterator[LevelComment]:
        return run_iterables(
            (
                self.get_user_level_comments_on_page(user=user, count=count, page=page).unwrap()
                for page in pages
            ),
            ClientError,
        )

    @wrap_async_iter
    async def get_level_comments_on_page(
        self,
        level: LevelReference,
        count: int = COMMENT_PAGE_SIZE,
        page: int = DEFAULT_PAGE,
        strategy: CommentStrategy = CommentStrategy.DEFAULT,
    ) -> AsyncIterator[LevelComment]:
        try:
            response_model = await self.session.get_level_comments_on_page(
                level_id=level.id,
                count=count,
                page=page,
                strategy=strategy,
            )

        except NothingFound:
            return

        for model in response_model.comments:
            comment = LevelComment.from_model(model, name=level.name).attach_client(self)

            yield comment

    @wrap_async_iter
    def get_level_comments(
        self,
        level: LevelReference,
        count: int = COMMENT_PAGE_SIZE,
        pages: Iterable[int] = DEFAULT_PAGES,
        strategy: CommentStrategy = CommentStrategy.DEFAULT,
    ) -> AsyncIterator[LevelComment]:
        return run_iterables(
            (
                self.get_level_comments_on_page(
                    level=level, count=count, page=page, strategy=strategy
                ).unwrap()
                for page in pages
            ),
            ClientError,
        )

    @wrap_async_iter
    async def get_gauntlets(self) -> AsyncIterator[Gauntlet]:
        response_model = await self.session.get_gauntlets()

        for model in response_model.gauntlets:
            yield Gauntlet.from_model(model).attach_client(self)

    @wrap_async_iter
    async def get_map_packs_on_page(self, page: int = DEFAULT_PAGE) -> AsyncIterator[MapPack]:
        response_model = await self.session.get_map_packs_on_page(page=page)

        for model in response_model.map_packs:
            yield MapPack.from_model(model).attach_client(self)

    @wrap_async_iter
    def get_map_packs(self, pages: Iterable[int] = DEFAULT_PAGES) -> AsyncIterator[MapPack]:
        return run_iterables(
            (self.get_map_packs_on_page(page=page).unwrap() for page in pages),
            ClientError,
        )

    @wrap_async_iter
    @check_login
    async def get_quests(self) -> AsyncIterator[Quest]:
        response_model = await self.session.get_quests(
            account_id=self.account_id, hashed_password=self.hashed_password
        )

        model = response_model.inner

        for quest_model in (model.quest_1, model.quest_2, model.quest_3):
            yield Quest.from_model(quest_model, model.quest_duration).attach_client(self)

    @wrap_async_iter
    @check_login
    async def get_chests(
        self,
        reward_type: RewardType = RewardType.DEFAULT,
        chest_1_count: int = DEFAULT_CHEST_COUNT,
        chest_2_count: int = DEFAULT_CHEST_COUNT,
    ) -> AsyncIterator[Chest]:
        response_model = await self.session.get_chests(
            reward_type=reward_type,
            chest_1_count=chest_1_count,
            chest_2_count=chest_2_count,
            account_id=self.account_id,
            hashed_password=self.hashed_password,
        )
        model = response_model.inner

        for id, chest_model, count, duration in (
            (1, model.chest_1, model.chest_1_count, model.chest_1_duration),
            (2, model.chest_2, model.chest_2_count, model.chest_2_duration),
        ):
            yield Chest.from_model(chest_model, id, count, duration).attach_client(self)

    @wrap_async_iter
    async def get_artists_on_page(self, page: int = DEFAULT_PAGE) -> AsyncIterator[Artist]:
        response_model = await self.session.get_artists_on_page(page=page)

        for model in response_model.artists:
            yield Artist.from_model(model).attach_client(self)

    @wrap_async_iter
    def get_artists(self, pages: Iterable[int] = DEFAULT_PAGES) -> AsyncIterator[Artist]:
        return run_iterables(
            (self.get_artists_on_page(page=page).unwrap() for page in pages),
            ClientError,
        )

    async def get_song(self, song_id: int) -> Song:
        model = await self.session.get_song(song_id=song_id)

        return Song.from_model(model).attach_client(self)

    async def get_newgrounds_song(self, song_id: int) -> Song:
        model = await self.session.get_newgrounds_song(song_id=song_id)

        return Song.from_model(model).attach_client(self)

    @wrap_async_iter
    async def search_newgrounds_songs_on_page(
        self, query: str, page: int = DEFAULT_PAGE
    ) -> AsyncIterator[Song]:
        models = await self.session.search_newgrounds_songs_on_page(query=query, page=page)

        for model in models:
            yield Song.from_model(model).attach_client(self)

    @wrap_async_iter
    def search_newgrounds_songs(
        self, query: str, pages: Iterable[int] = DEFAULT_PAGES
    ) -> AsyncIterator[Song]:
        return run_iterables(
            (self.search_newgrounds_songs_on_page(query=query, page=page) for page in pages),
            ClientError,
        )

    @wrap_async_iter
    async def search_newgrounds_artists_on_page(
        self, query: str, page: int = DEFAULT_PAGE
    ) -> AsyncIterator[Artist]:
        models = await self.session.search_newgrounds_artists_on_page(query=query, page=page)

        for model in models:
            yield Artist.from_model(model).attach_client(self)

    @wrap_async_iter
    def search_newgrounds_artists(
        self, query: str, pages: Iterable[int] = DEFAULT_PAGES
    ) -> AsyncIterator[Artist]:
        return run_iterables(
            (self.search_newgrounds_artists_on_page(query=query, page=page) for page in pages),
            ClientError,
        )

    @wrap_async_iter
    async def get_newgrounds_artist_songs_on_page(
        self, artist: Artist, page: int = DEFAULT_PAGE
    ) -> AsyncIterator[Song]:
        models = await self.session.get_newgrounds_artist_songs_on_page(
            artist_name=artist.name, page=page
        )

        for model in models:
            yield Song.from_model(model).attach_client(self)

    @wrap_async_iter
    def get_newgrounds_artist_songs(
        self, artist: Artist, pages: Iterable[int] = DEFAULT_PAGES
    ) -> AsyncIterator[Song]:
        return run_iterables(
            (
                self.get_newgrounds_artist_songs_on_page(artist=artist, page=page).unwrap()
                for page in pages
            ),
            ClientError,
        )

    # handlers

    async def on_daily(self, daily: Level) -> None:
        """Handles `daily` events.

        Arguments:
            daily: The daily level to handle.
        """

    async def on_weekly(self, weekly: Level) -> None:
        """Handles `weekly` events.

        Arguments:
            weekly: The weekly level to handle.
        """

    async def on_rate(self, level: Level) -> None:
        """Handles `rate` events.

        Arguments:
            level: The rated level to handle.
        """

    async def on_level(self, level: Level) -> None:
        """Handles `level` events.

        Arguments:
            level: The level to handle.
        """

    async def on_user_level(self, user: User, level: Level) -> None:
        """Handles `user_level` events.

        Arguments:
            user: The user to handle.
            level: The level to handle.
        """

    async def on_message(self, message: Message) -> None:
        """Handles `message` events.

        Arguments:
            message: The message to handle.
        """

    async def on_friend_request(self, friend_request: FriendRequest) -> None:
        """Handles `friend_request` events.

        Arguments:
            friend_request: The friend request to handle.
        """

    async def on_level_comment(self, level: Level, comment: LevelComment) -> None:
        """Handles `level_comment` events.

        Arguments:
            level: The level to handle.
            comment: The level comment to handle.
        """

    async def on_daily_comment(self, daily: Level, comment: LevelComment) -> None:
        """Handles `daily_comment` events.

        Arguments:
            daily: The daily level to handle.
            comment: The daily comment to handle.
        """

    async def on_weekly_comment(self, weekly: Level, comment: LevelComment) -> None:
        """Handles `weekly_comment` events.

        Arguments:
            weekly: The weekly level to handle.
            comment: The weekly comment to handle.
        """

    async def on_user_comment(self, user: User, comment: UserComment) -> None:
        """Handles `user_comment` events.

        Arguments:
            user: The user to handle.
            comment: The comment to handle.
        """

    async def on_user_level_comment(self, user: User, comment: LevelComment) -> None:
        """Handles `user_level_comment` events.

        Arguments:
            user: The user to handle.
            comment: The level comment to handle.
        """

    # dispatchers

    async def dispatch_daily(self, daily: Level) -> None:
        """Dispatches `daily` events via calling [`on_daily`][gd.client.Client.on_daily].

        Arguments:
            daily: The daily level to dispatch.
        """
        await self.on_daily(daily)

    async def dispatch_weekly(self, weekly: Level) -> None:
        """Dispatches `weekly` events via calling [`on_weekly`][gd.client.Client.on_weekly].

        Arguments:
            weekly: The weekly level to dispatch.
        """
        await self.on_weekly(weekly)

    async def dispatch_rate(self, level: Level) -> None:
        """Dispatches `rate` events via calling [`on_rate`][gd.client.Client.on_rate].

        Arguments:
            level: The rated level to dispatch.
        """
        await self.on_rate(level)

    async def dispatch_level(self, level: Level) -> None:
        """Dispatches `level` events via calling [`on_level`][gd.client.Client.on_level].

        Arguments:
            level: The level to dispatch.
        """
        await self.on_level(level)

    async def dispatch_user_level(self, user: User, level: Level) -> None:
        """Dispatches `user_level` events via calling
        [`on_user_level`][gd.client.Client.on_user_level].

        Arguments:
            user: The user to dispatch.
            level: The level to dispatch.
        """
        await self.on_user_level(user, level)

    async def dispatch_message(self, message: Message) -> None:
        """Dispatches `message` events via calling [`on_message`][gd.client.Client.on_message].

        Arguments:
            message: The message to dispatch.
        """
        await self.on_message(message)

    async def dispatch_friend_request(self, friend_request: FriendRequest) -> None:
        """Dispatches `friend_request` events via calling
        [`on_friend_request`][gd.client.Client.on_friend_request].

        Arguments:
            friend_request: The friend request to dispatch.
        """
        await self.on_friend_request(friend_request)

    async def dispatch_level_comment(self, level: Level, comment: LevelComment) -> None:
        """Dispatches `level_comment` events via calling
        [`on_level_comment`][gd.client.Client.on_level_comment].

        Arguments:
            level: The level to dispatch.
            comment: The level comment to dispatch.
        """
        await self.on_level_comment(level, comment)

    async def dispatch_daily_comment(self, daily: Level, comment: LevelComment) -> None:
        """Dispatches `level_comment` events via calling
        [`on_level_comment`][gd.client.Client.on_level_comment].

        Arguments:
            daily: The daily level to dispatch.
            comment: The daily comment to dispatch.
        """
        await self.on_daily_comment(daily, comment)

    async def dispatch_weekly_comment(self, weekly: Level, comment: LevelComment) -> None:
        """Dispatches `weekly_comment` events via calling
        [`on_weekly_comment`][gd.client.Client.on_weekly_comment].

        Arguments:
            weekly: The weekly level to dispatch.
            comment: The weekly comment to dispatch.
        """
        await self.on_weekly_comment(weekly, comment)

    async def dispatch_user_comment(self, user: User, comment: UserComment) -> None:
        """Dispatches `user_comment` events via calling
        [`on_user_comment`][gd.client.Client.on_user_comment].

        Arguments:
            user: The user to dispatch.
            comment: The comment to dispatch.
        """
        await self.on_user_comment(user, comment)

    async def dispatch_user_level_comment(self, user: User, comment: LevelComment) -> None:
        """Dispatches `user_level_comment` events via calling
        [`on_user_level_comment`][gd.client.Client.on_user_level_comment].

        Arguments:
            user: The user to dispatch.
            comment: The level comment to dispatch.
        """
        await self.on_user_level_comment(user, comment)

    def event(self, function: F) -> F:
        """Registers an event handler.

        Example:
            ```python
            client = Client()

            DAILY_INFO = "new daily! {daily.name} by {daily.creator.name} (ID: {daily.id})
            daily_info = DAILY_INFO.format

            @client.event
            async def on_daily(daily: Level) -> None:
                print(daily_info(daily=daily))
            ```

        Arguments:
            function: The function to register as an event handler.

        Returns:
            The function passed.
        """
        set_attribute(self, function.__name__, function)

        return function

    def listen_for_daily(
        self, delay: float = DEFAULT_DELAY, reconnect: bool = DEFAULT_RECONNECT
    ) -> DailyListener:
        return self.add_listener(DailyListener(self, delay=delay, reconnect=reconnect))

    def listen_for_weekly(
        self, delay: float = DEFAULT_DELAY, reconnect: bool = DEFAULT_RECONNECT
    ) -> WeeklyListener:
        return self.add_listener(WeeklyListener(self, delay=delay, reconnect=reconnect))

    def listen_for_rate(
        self,
        pages_count: int = DEFAULT_PAGES_COUNT,
        delay: float = DEFAULT_DELAY,
        reconnect: bool = DEFAULT_RECONNECT,
    ) -> RateListener:
        return self.add_listener(
            RateListener(self, delay=delay, reconnect=reconnect, pages_count=pages_count)
        )

    def listen_for_level(
        self,
        pages_count: int = DEFAULT_PAGES_COUNT,
        filters: Optional[Filters] = None,
        delay: float = DEFAULT_DELAY,
        reconnect: bool = DEFAULT_RECONNECT,
    ) -> LevelListener:
        if filters is None:
            filters = Filters()

        return self.add_listener(
            LevelListener(
                self,
                delay=delay,
                reconnect=reconnect,
                pages_count=pages_count,
                filters=filters,
            )
        )

    def listen_for_user_level(
        self,
        account_id: Optional[int] = None,
        id: Optional[int] = None,
        name: Optional[str] = None,
        pages_count: int = DEFAULT_PAGES_COUNT,
        update: bool = DEFAULT_UPDATE,
        delay: float = DEFAULT_DELAY,
        reconnect: bool = DEFAULT_RECONNECT,
    ) -> UserLevelListener:
        return self.add_listener(
            UserLevelListener(
                self,
                delay=delay,
                reconnect=reconnect,
                account_id=account_id,
                id=id,
                name=name,
                pages_count=pages_count,
                update=update,
            )
        )

    def listen_for_message(
        self,
        pages_count: int = DEFAULT_PAGES_COUNT,
        delay: float = DEFAULT_DELAY,
        reconnect: bool = DEFAULT_RECONNECT,
    ) -> MessageListener:
        return self.add_listener(
            MessageListener(self, delay=delay, reconnect=reconnect, pages_count=pages_count)
        )

    def listen_for_friend_request(
        self,
        pages_count: int = DEFAULT_PAGES_COUNT,
        delay: float = DEFAULT_DELAY,
        reconnect: bool = DEFAULT_RECONNECT,
    ) -> FriendRequestListener:
        return self.add_listener(
            FriendRequestListener(self, delay=delay, reconnect=reconnect, pages_count=pages_count)
        )

    def listen_for_level_comment(
        self,
        level_id: int,
        pages_count: int = DEFAULT_PAGES_COUNT,
        count: int = DEFAULT_COUNT,
        update: bool = DEFAULT_UPDATE,
        delay: float = DEFAULT_DELAY,
        reconnect: bool = DEFAULT_RECONNECT,
    ) -> LevelCommentListener:
        return self.add_listener(
            LevelCommentListener(
                self,
                delay=delay,
                reconnect=reconnect,
                level_id=level_id,
                pages_count=pages_count,
                count=count,
                update=update,
            )
        )

    def listen_for_daily_comment(
        self,
        pages_count: int = DEFAULT_PAGES_COUNT,
        count: int = DEFAULT_COUNT,
        update: bool = DEFAULT_UPDATE,
        delay: float = DEFAULT_DELAY,
        reconnect: bool = DEFAULT_RECONNECT,
    ) -> DailyCommentListener:
        return self.add_listener(
            DailyCommentListener(
                self,
                delay=delay,
                reconnect=reconnect,
                pages_count=pages_count,
                count=count,
                update=update,
            )
        )

    def listen_for_weekly_comment(
        self,
        pages_count: int = DEFAULT_PAGES_COUNT,
        count: int = DEFAULT_COUNT,
        update: bool = DEFAULT_UPDATE,
        delay: float = DEFAULT_DELAY,
        reconnect: bool = DEFAULT_RECONNECT,
    ) -> WeeklyCommentListener:
        return self.add_listener(
            WeeklyCommentListener(
                self,
                delay=delay,
                reconnect=reconnect,
                pages_count=pages_count,
                count=count,
                update=update,
            )
        )

    def listen_for_user_comment(
        self,
        account_id: Optional[int] = None,
        id: Optional[int] = None,
        name: Optional[str] = None,
        update: bool = DEFAULT_UPDATE,
        delay: float = DEFAULT_DELAY,
        reconnect: bool = DEFAULT_RECONNECT,
    ) -> UserCommentListener:
        return self.add_listener(
            UserCommentListener(
                self,
                delay=delay,
                reconnect=reconnect,
                account_id=account_id,
                id=id,
                name=name,
                update=update,
            )
        )

    def listen_for_user_level_comment(
        self,
        account_id: Optional[int] = None,
        id: Optional[int] = None,
        name: Optional[str] = None,
        update: bool = DEFAULT_UPDATE,
        delay: float = DEFAULT_DELAY,
        reconnect: bool = DEFAULT_RECONNECT,
    ) -> UserLevelCommentListener:
        return self.add_listener(
            UserLevelCommentListener(
                self,
                delay=delay,
                reconnect=reconnect,
                account_id=account_id,
                id=id,
                name=name,
                update=update,
            )
        )

    # listeners

    def add_listener(self, listener: L) -> L:
        self.check_controller()

        self._listeners = (*self._listeners, listener)

        return listener

    def clear_listeners(self) -> None:
        self.check_controller()

        self._listeners = ()

    def remove_listener(self, listener: Listener) -> bool:
        self.check_controller()

        listeners = self._listeners

        length = len(listeners)

        self._listeners = listeners = tuple(
            present_listener for present_listener in listeners if present_listener is not listener
        )

        return len(listeners) < length

    # controllers

    def check_controller(self) -> None:
        if self._controller is not None:
            raise RuntimeError(CONTROLLER_ALREADY_CREATED)

    def create_controller(self) -> Controller:
        self.check_controller()

        self._controller = controller = Controller(self._listeners)

        return controller

session: Session = field(factory=Session) class-attribute instance-attribute

The session of the client.

credentials: Credentials = field(factory=Credentials) class-attribute instance-attribute

The credentials of the client.

cache: Cache = field(factory=Cache, repr=False) class-attribute instance-attribute

The cache of the client.

database_unchecked: Optional[Database] = field(default=None, repr=False) class-attribute instance-attribute

The database of the client.

load_after_post: bool = field(default=DEFAULT_LOAD_AFTER_POST) class-attribute instance-attribute

Whether to load items after posting them.

account_id: int property

The account ID of the client.

id: int property

The user ID of the client.

name: str property

The name of the client.

hashed_password: str property

The hashed password of the client.

http: HTTPClient property

The [HTTPClient][gd.http.HTTPClient] used by the client session.

user: UserReference property

The user representing the client.

is_logged_in() -> bool

Checks if the client is logged in.

Returns:

Type Description
bool

Whether the client is logged in.

Source code in gd/client.py
226
227
228
229
230
231
232
def is_logged_in(self) -> bool:
    """Checks if the client is logged in.

    Returns:
        Whether the client is logged in.
    """
    return self.credentials.is_loaded()

ping() -> Duration async

Pings the Geometry Dash server.

Returns:

Type Description
Duration

The [Duration][gd.date_time.Duration] representing the latency.

Source code in gd/client.py
270
271
272
273
274
275
276
async def ping(self) -> Duration:
    """Pings the Geometry Dash server.

    Returns:
        The [`Duration`][gd.date_time.Duration] representing the latency.
    """
    return await self.ping_url(self.http.url)

ping_url(url: URLString) -> Duration async

Pings the given url.

Returns:

Type Description
Duration

The [Duration][gd.date_time.Duration] representing the latency.

Source code in gd/client.py
278
279
280
281
282
283
284
async def ping_url(self, url: URLString) -> Duration:
    """Pings the given `url`.

    Returns:
        The [`Duration`][gd.date_time.Duration] representing the latency.
    """
    return await self.session.ping(url)

logout() -> None async

Performs the logout.

Source code in gd/client.py
286
287
288
async def logout(self) -> None:
    """Performs the logout."""
    self.reset_items()

login(name: str, hashed_password: str) -> LoginContextManager[Self]

Performs the login.

This function returns a context manager that can be used for temporarily logging in:

async with client.login(name, password):
    assert client.is_logged_in()

assert not client.is_logged_in()

Returns:

Type Description
LoginContextManager[Self]

The [LoginContextManager][gd.client.LoginContextManager] for handling login process.

Source code in gd/client.py
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
def login(self, name: str, hashed_password: str) -> LoginContextManager[Self]:
    """Performs the login.

    This function returns a context manager that can be used for temporarily logging in:

    ```python
    async with client.login(name, password):
        assert client.is_logged_in()

    assert not client.is_logged_in()
    ```

    Returns:
        The [`LoginContextManager`][gd.client.LoginContextManager] for handling login process.
    """
    return LoginContextManager(self, name, hashed_password)

unsafe_login(name: str, hashed_password: str) -> UnsafeLoginContextManager[Self]

Performs the unsafe login.

Unsafe means that the credentials are not confirmed.

This function returns a context manager that can be used for temporarily logging in:

async with client.unsafe_login(name, password):
    assert client.is_logged_in()

assert not client.is_logged_in()

Returns:

Type Description
UnsafeLoginContextManager[Self]

The [UnsafeLoginContextManager][gd.client.UnsafeLoginContextManager] for handling login process.

Source code in gd/client.py
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
def unsafe_login(self, name: str, hashed_password: str) -> UnsafeLoginContextManager[Self]:
    """Performs the *unsafe* login.

    *Unsafe* means that the credentials are not confirmed.

    This function returns a context manager that can be used for temporarily logging in:

    ```python
    async with client.unsafe_login(name, password):
        assert client.is_logged_in()

    assert not client.is_logged_in()
    ```

    Returns:
        The [`UnsafeLoginContextManager`][gd.client.UnsafeLoginContextManager]
            for handling login process.
    """
    return UnsafeLoginContextManager(self, name, hashed_password)

load() -> Database async

Loads the save from the cloud, saving it in the [database][gd.client.Client.database] attribute.

Note

This function requires the client to be logged in.

Returns:

Type Description
Database

The [Database][gd.api.database.Database] loaded.

Source code in gd/client.py
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
@check_login
async def load(self) -> Database:
    """Loads the save from the cloud, saving it in the
    [`database`][gd.client.Client.database] attribute.

    Note:
        This function requires the client to be logged in.

    Returns:
        The [`Database`][gd.api.database.Database] loaded.
    """
    database = await self.session.load(
        account_id=self.account_id,
        name=self.name,
        hashed_password=self.hashed_password,
    )

    self.database = database

    return database

save(database: Optional[Database] = None) -> None async

Saves the the database to the cloud, optionally fetching it from the [database][gd.client.Client.database] attribute.

Note

This function requires the client to be logged in.

Source code in gd/client.py
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
@check_login
async def save(self, database: Optional[Database] = None) -> None:
    """Saves the the `database` to the cloud, optionally fetching it from the
    [`database`][gd.client.Client.database] attribute.

    Note:
        This function requires the client to be logged in.
    """
    if database is None:
        database = self.database

    await self.session.save(
        database,
        account_id=self.account_id,
        name=self.name,
        hashed_password=self.hashed_password,
    )

get_self(simple: bool = DEFAULT_SIMPLE) -> User async

Gets the user representing this client.

This is a shorthand for:

await client.user.get(simple=simple)

See [User.get][gd.user.User.get] for more information.

Note

This function requires the client to be logged in.

Parameters:

Name Type Description Default
simple bool

Whether to fetch simple information.

DEFAULT_SIMPLE

Returns:

Type Description
User

The [User][gd.user.User] representing the client.

Source code in gd/client.py
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
@check_login
async def get_self(self, simple: bool = DEFAULT_SIMPLE) -> User:
    """Gets the user representing this client.

    This is a shorthand for:

    ```python
    await client.user.get(simple=simple)
    ```

    See [`User.get`][gd.user.User.get] for more information.

    Note:
        This function requires the client to be logged in.

    Arguments:
        simple: Whether to fetch simple information.

    Returns:
        The [`User`][gd.user.User] representing the client.
    """
    return await self.user.get(simple=simple)

get_user(account_id: int, simple: bool = DEFAULT_SIMPLE, friend_state: bool = DEFAULT_FRIEND_STATE) -> User async

Fetches the user by account_id.

Parameters:

Name Type Description Default
account_id int

The account ID of the user to fetch.

required
simple bool

Whether to fetch simple information only.

DEFAULT_SIMPLE
friend_state bool

Whether to fetch friend state.

DEFAULT_FRIEND_STATE
Note

friend_state requires the client to be logged in.

Returns:

Type Description
User

The [User][gd.user.User] fetched.

Source code in gd/client.py
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
async def get_user(
    self,
    account_id: int,
    simple: bool = DEFAULT_SIMPLE,
    friend_state: bool = DEFAULT_FRIEND_STATE,
) -> User:
    """Fetches the user by `account_id`.

    Arguments:
        account_id: The account ID of the user to fetch.
        simple: Whether to fetch simple information only.
        friend_state: Whether to fetch friend state.

    Note:
        `friend_state` requires the client to be logged in.

    Returns:
        The [`User`][gd.user.User] fetched.
    """
    if friend_state:  # if we need to fetch friend state
        check_client_login(self)

        profile_model = await self.session.get_user_profile(  # request profile
            account_id=account_id,
            client_account_id=self.account_id,
            hashed_password=self.hashed_password,
        )

    else:  # otherwise, simply request normally
        profile_model = await self.session.get_user_profile(account_id=account_id)

    if simple:  # if only the profile is needed, return right away
        return User.from_profile_model(profile_model).attach_client(self)

    search_model = await self.session.search_user(query(profile_model.id))  # search by ID

    return User.from_search_user_and_profile_models(search_model, profile_model).attach_client(
        self
    )

on_daily(daily: Level) -> None async

Handles daily events.

Parameters:

Name Type Description Default
daily Level

The daily level to handle.

required
Source code in gd/client.py
1460
1461
1462
1463
1464
1465
async def on_daily(self, daily: Level) -> None:
    """Handles `daily` events.

    Arguments:
        daily: The daily level to handle.
    """

on_weekly(weekly: Level) -> None async

Handles weekly events.

Parameters:

Name Type Description Default
weekly Level

The weekly level to handle.

required
Source code in gd/client.py
1467
1468
1469
1470
1471
1472
async def on_weekly(self, weekly: Level) -> None:
    """Handles `weekly` events.

    Arguments:
        weekly: The weekly level to handle.
    """

on_rate(level: Level) -> None async

Handles rate events.

Parameters:

Name Type Description Default
level Level

The rated level to handle.

required
Source code in gd/client.py
1474
1475
1476
1477
1478
1479
async def on_rate(self, level: Level) -> None:
    """Handles `rate` events.

    Arguments:
        level: The rated level to handle.
    """

on_level(level: Level) -> None async

Handles level events.

Parameters:

Name Type Description Default
level Level

The level to handle.

required
Source code in gd/client.py
1481
1482
1483
1484
1485
1486
async def on_level(self, level: Level) -> None:
    """Handles `level` events.

    Arguments:
        level: The level to handle.
    """

on_user_level(user: User, level: Level) -> None async

Handles user_level events.

Parameters:

Name Type Description Default
user User

The user to handle.

required
level Level

The level to handle.

required
Source code in gd/client.py
1488
1489
1490
1491
1492
1493
1494
async def on_user_level(self, user: User, level: Level) -> None:
    """Handles `user_level` events.

    Arguments:
        user: The user to handle.
        level: The level to handle.
    """

on_message(message: Message) -> None async

Handles message events.

Parameters:

Name Type Description Default
message Message

The message to handle.

required
Source code in gd/client.py
1496
1497
1498
1499
1500
1501
async def on_message(self, message: Message) -> None:
    """Handles `message` events.

    Arguments:
        message: The message to handle.
    """

on_friend_request(friend_request: FriendRequest) -> None async

Handles friend_request events.

Parameters:

Name Type Description Default
friend_request FriendRequest

The friend request to handle.

required
Source code in gd/client.py
1503
1504
1505
1506
1507
1508
async def on_friend_request(self, friend_request: FriendRequest) -> None:
    """Handles `friend_request` events.

    Arguments:
        friend_request: The friend request to handle.
    """

on_level_comment(level: Level, comment: LevelComment) -> None async

Handles level_comment events.

Parameters:

Name Type Description Default
level Level

The level to handle.

required
comment LevelComment

The level comment to handle.

required
Source code in gd/client.py
1510
1511
1512
1513
1514
1515
1516
async def on_level_comment(self, level: Level, comment: LevelComment) -> None:
    """Handles `level_comment` events.

    Arguments:
        level: The level to handle.
        comment: The level comment to handle.
    """

on_daily_comment(daily: Level, comment: LevelComment) -> None async

Handles daily_comment events.

Parameters:

Name Type Description Default
daily Level

The daily level to handle.

required
comment LevelComment

The daily comment to handle.

required
Source code in gd/client.py
1518
1519
1520
1521
1522
1523
1524
async def on_daily_comment(self, daily: Level, comment: LevelComment) -> None:
    """Handles `daily_comment` events.

    Arguments:
        daily: The daily level to handle.
        comment: The daily comment to handle.
    """

on_weekly_comment(weekly: Level, comment: LevelComment) -> None async

Handles weekly_comment events.

Parameters:

Name Type Description Default
weekly Level

The weekly level to handle.

required
comment LevelComment

The weekly comment to handle.

required
Source code in gd/client.py
1526
1527
1528
1529
1530
1531
1532
async def on_weekly_comment(self, weekly: Level, comment: LevelComment) -> None:
    """Handles `weekly_comment` events.

    Arguments:
        weekly: The weekly level to handle.
        comment: The weekly comment to handle.
    """

on_user_comment(user: User, comment: UserComment) -> None async

Handles user_comment events.

Parameters:

Name Type Description Default
user User

The user to handle.

required
comment UserComment

The comment to handle.

required
Source code in gd/client.py
1534
1535
1536
1537
1538
1539
1540
async def on_user_comment(self, user: User, comment: UserComment) -> None:
    """Handles `user_comment` events.

    Arguments:
        user: The user to handle.
        comment: The comment to handle.
    """

on_user_level_comment(user: User, comment: LevelComment) -> None async

Handles user_level_comment events.

Parameters:

Name Type Description Default
user User

The user to handle.

required
comment LevelComment

The level comment to handle.

required
Source code in gd/client.py
1542
1543
1544
1545
1546
1547
1548
async def on_user_level_comment(self, user: User, comment: LevelComment) -> None:
    """Handles `user_level_comment` events.

    Arguments:
        user: The user to handle.
        comment: The level comment to handle.
    """

dispatch_daily(daily: Level) -> None async

Dispatches daily events via calling on_daily.

Parameters:

Name Type Description Default
daily Level

The daily level to dispatch.

required
Source code in gd/client.py
1552
1553
1554
1555
1556
1557
1558
async def dispatch_daily(self, daily: Level) -> None:
    """Dispatches `daily` events via calling [`on_daily`][gd.client.Client.on_daily].

    Arguments:
        daily: The daily level to dispatch.
    """
    await self.on_daily(daily)

dispatch_weekly(weekly: Level) -> None async

Dispatches weekly events via calling on_weekly.

Parameters:

Name Type Description Default
weekly Level

The weekly level to dispatch.

required
Source code in gd/client.py
1560
1561
1562
1563
1564
1565
1566
async def dispatch_weekly(self, weekly: Level) -> None:
    """Dispatches `weekly` events via calling [`on_weekly`][gd.client.Client.on_weekly].

    Arguments:
        weekly: The weekly level to dispatch.
    """
    await self.on_weekly(weekly)

dispatch_rate(level: Level) -> None async

Dispatches rate events via calling on_rate.

Parameters:

Name Type Description Default
level Level

The rated level to dispatch.

required
Source code in gd/client.py
1568
1569
1570
1571
1572
1573
1574
async def dispatch_rate(self, level: Level) -> None:
    """Dispatches `rate` events via calling [`on_rate`][gd.client.Client.on_rate].

    Arguments:
        level: The rated level to dispatch.
    """
    await self.on_rate(level)

dispatch_level(level: Level) -> None async

Dispatches level events via calling on_level.

Parameters:

Name Type Description Default
level Level

The level to dispatch.

required
Source code in gd/client.py
1576
1577
1578
1579
1580
1581
1582
async def dispatch_level(self, level: Level) -> None:
    """Dispatches `level` events via calling [`on_level`][gd.client.Client.on_level].

    Arguments:
        level: The level to dispatch.
    """
    await self.on_level(level)

dispatch_user_level(user: User, level: Level) -> None async

Dispatches user_level events via calling on_user_level.

Parameters:

Name Type Description Default
user User

The user to dispatch.

required
level Level

The level to dispatch.

required
Source code in gd/client.py
1584
1585
1586
1587
1588
1589
1590
1591
1592
async def dispatch_user_level(self, user: User, level: Level) -> None:
    """Dispatches `user_level` events via calling
    [`on_user_level`][gd.client.Client.on_user_level].

    Arguments:
        user: The user to dispatch.
        level: The level to dispatch.
    """
    await self.on_user_level(user, level)

dispatch_message(message: Message) -> None async

Dispatches message events via calling on_message.

Parameters:

Name Type Description Default
message Message

The message to dispatch.

required
Source code in gd/client.py
1594
1595
1596
1597
1598
1599
1600
async def dispatch_message(self, message: Message) -> None:
    """Dispatches `message` events via calling [`on_message`][gd.client.Client.on_message].

    Arguments:
        message: The message to dispatch.
    """
    await self.on_message(message)

dispatch_friend_request(friend_request: FriendRequest) -> None async

Dispatches friend_request events via calling on_friend_request.

Parameters:

Name Type Description Default
friend_request FriendRequest

The friend request to dispatch.

required
Source code in gd/client.py
1602
1603
1604
1605
1606
1607
1608
1609
async def dispatch_friend_request(self, friend_request: FriendRequest) -> None:
    """Dispatches `friend_request` events via calling
    [`on_friend_request`][gd.client.Client.on_friend_request].

    Arguments:
        friend_request: The friend request to dispatch.
    """
    await self.on_friend_request(friend_request)

dispatch_level_comment(level: Level, comment: LevelComment) -> None async

Dispatches level_comment events via calling on_level_comment.

Parameters:

Name Type Description Default
level Level

The level to dispatch.

required
comment LevelComment

The level comment to dispatch.

required
Source code in gd/client.py
1611
1612
1613
1614
1615
1616
1617
1618
1619
async def dispatch_level_comment(self, level: Level, comment: LevelComment) -> None:
    """Dispatches `level_comment` events via calling
    [`on_level_comment`][gd.client.Client.on_level_comment].

    Arguments:
        level: The level to dispatch.
        comment: The level comment to dispatch.
    """
    await self.on_level_comment(level, comment)

dispatch_daily_comment(daily: Level, comment: LevelComment) -> None async

Dispatches level_comment events via calling on_level_comment.

Parameters:

Name Type Description Default
daily Level

The daily level to dispatch.

required
comment LevelComment

The daily comment to dispatch.

required
Source code in gd/client.py
1621
1622
1623
1624
1625
1626
1627
1628
1629
async def dispatch_daily_comment(self, daily: Level, comment: LevelComment) -> None:
    """Dispatches `level_comment` events via calling
    [`on_level_comment`][gd.client.Client.on_level_comment].

    Arguments:
        daily: The daily level to dispatch.
        comment: The daily comment to dispatch.
    """
    await self.on_daily_comment(daily, comment)

dispatch_weekly_comment(weekly: Level, comment: LevelComment) -> None async

Dispatches weekly_comment events via calling on_weekly_comment.

Parameters:

Name Type Description Default
weekly Level

The weekly level to dispatch.

required
comment LevelComment

The weekly comment to dispatch.

required
Source code in gd/client.py
1631
1632
1633
1634
1635
1636
1637
1638
1639
async def dispatch_weekly_comment(self, weekly: Level, comment: LevelComment) -> None:
    """Dispatches `weekly_comment` events via calling
    [`on_weekly_comment`][gd.client.Client.on_weekly_comment].

    Arguments:
        weekly: The weekly level to dispatch.
        comment: The weekly comment to dispatch.
    """
    await self.on_weekly_comment(weekly, comment)

dispatch_user_comment(user: User, comment: UserComment) -> None async

Dispatches user_comment events via calling on_user_comment.

Parameters:

Name Type Description Default
user User

The user to dispatch.

required
comment UserComment

The comment to dispatch.

required
Source code in gd/client.py
1641
1642
1643
1644
1645
1646
1647
1648
1649
async def dispatch_user_comment(self, user: User, comment: UserComment) -> None:
    """Dispatches `user_comment` events via calling
    [`on_user_comment`][gd.client.Client.on_user_comment].

    Arguments:
        user: The user to dispatch.
        comment: The comment to dispatch.
    """
    await self.on_user_comment(user, comment)

dispatch_user_level_comment(user: User, comment: LevelComment) -> None async

Dispatches user_level_comment events via calling on_user_level_comment.

Parameters:

Name Type Description Default
user User

The user to dispatch.

required
comment LevelComment

The level comment to dispatch.

required
Source code in gd/client.py
1651
1652
1653
1654
1655
1656
1657
1658
1659
async def dispatch_user_level_comment(self, user: User, comment: LevelComment) -> None:
    """Dispatches `user_level_comment` events via calling
    [`on_user_level_comment`][gd.client.Client.on_user_level_comment].

    Arguments:
        user: The user to dispatch.
        comment: The level comment to dispatch.
    """
    await self.on_user_level_comment(user, comment)

event(function: F) -> F

Registers an event handler.

Example
client = Client()

DAILY_INFO = "new daily! {daily.name} by {daily.creator.name} (ID: {daily.id})
daily_info = DAILY_INFO.format

@client.event
async def on_daily(daily: Level) -> None:
    print(daily_info(daily=daily))

Parameters:

Name Type Description Default
function F

The function to register as an event handler.

required

Returns:

Type Description
F

The function passed.

Source code in gd/client.py
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
def event(self, function: F) -> F:
    """Registers an event handler.

    Example:
        ```python
        client = Client()

        DAILY_INFO = "new daily! {daily.name} by {daily.creator.name} (ID: {daily.id})
        daily_info = DAILY_INFO.format

        @client.event
        async def on_daily(daily: Level) -> None:
            print(daily_info(daily=daily))
        ```

    Arguments:
        function: The function to register as an event handler.

    Returns:
        The function passed.
    """
    set_attribute(self, function.__name__, function)

    return function