1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
use serde::Serialize;
use super::IncidentsData;
#[cfg(feature = "model")]
use crate::builder::{
CreateChannel,
CreateCommand,
CreateSoundboard,
CreateSticker,
EditAutoModRule,
EditCommandPermissions,
EditGuild,
EditGuildWelcomeScreen,
EditGuildWidget,
EditMember,
EditRole,
EditSoundboard,
EditSticker,
};
#[cfg(all(feature = "cache", feature = "utils", feature = "client"))]
use crate::cache::Cache;
#[cfg(feature = "collector")]
use crate::collector::{MessageCollector, ReactionCollector};
#[cfg(feature = "collector")]
use crate::gateway::ShardMessenger;
#[cfg(feature = "model")]
use crate::http::{CacheHttp, Http, UserPagination};
use crate::model::prelude::*;
#[cfg(feature = "model")]
use crate::model::utils::icon_url;
use crate::model::utils::{emojis, roles, stickers};
/// Partial information about a [`Guild`]. This does not include information like member data.
///
/// [Discord docs](https://discord.com/developers/docs/resources/guild#guild-object).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(remote = "Self")]
#[non_exhaustive]
pub struct PartialGuild {
// ======
// These fields are copy-pasted from the top part of Guild, and the omitted fields filled in
// ======
/// The unique Id identifying the guild.
///
/// This is equivalent to the Id of the default role (`@everyone`).
pub id: GuildId,
/// The name of the guild.
pub name: String,
/// The hash of the icon used by the guild.
///
/// In the client, this appears on the guild list on the left-hand side.
pub icon: Option<ImageHash>,
/// Icon hash, returned when in the template object
pub icon_hash: Option<ImageHash>,
/// An identifying hash of the guild's splash icon.
///
/// If the `InviteSplash` feature is enabled, this can be used to generate a URL to a splash
/// image.
pub splash: Option<ImageHash>,
/// An identifying hash of the guild discovery's splash icon.
///
/// **Note**: Only present for guilds with the `DISCOVERABLE` feature.
pub discovery_splash: Option<ImageHash>,
// Omitted `owner` field because only Http::get_guilds uses it, which returns GuildInfo
/// The Id of the [`User`] who owns the guild.
pub owner_id: UserId,
// Omitted `permissions` field because only Http::get_guilds uses it, which returns GuildInfo
// Omitted `region` field because it is deprecated (see Discord docs)
/// Information about the voice afk channel.
#[serde(flatten)]
pub afk_metadata: Option<AfkMetadata>,
/// Whether or not the guild widget is enabled.
pub widget_enabled: Option<bool>,
/// The channel id that the widget will generate an invite to, or null if set to no invite
pub widget_channel_id: Option<ChannelId>,
/// Indicator of the current verification level of the guild.
pub verification_level: VerificationLevel,
/// Indicator of whether notifications for all messages are enabled by
/// default in the guild.
pub default_message_notifications: DefaultMessageNotificationLevel,
/// Default explicit content filter level.
pub explicit_content_filter: ExplicitContentFilter,
/// A mapping of the guild's roles.
#[serde(with = "roles")]
pub roles: HashMap<RoleId, Role>,
/// All of the guild's custom emojis.
#[serde(with = "emojis")]
pub emojis: HashMap<EmojiId, Emoji>,
/// The guild features. More information available at [`discord documentation`].
///
/// The following is a list of known features:
/// - `ANIMATED_ICON`
/// - `BANNER`
/// - `COMMERCE`
/// - `COMMUNITY`
/// - `DISCOVERABLE`
/// - `FEATURABLE`
/// - `INVITE_SPLASH`
/// - `MEMBER_VERIFICATION_GATE_ENABLED`
/// - `MONETIZATION_ENABLED`
/// - `MORE_STICKERS`
/// - `NEWS`
/// - `PARTNERED`
/// - `PREVIEW_ENABLED`
/// - `PRIVATE_THREADS`
/// - `ROLE_ICONS`
/// - `SEVEN_DAY_THREAD_ARCHIVE`
/// - `THREE_DAY_THREAD_ARCHIVE`
/// - `TICKETED_EVENTS_ENABLED`
/// - `VANITY_URL`
/// - `VERIFIED`
/// - `VIP_REGIONS`
/// - `WELCOME_SCREEN_ENABLED`
/// - `THREE_DAY_THREAD_ARCHIVE`
/// - `SEVEN_DAY_THREAD_ARCHIVE`
/// - `PRIVATE_THREADS`
///
///
/// [`discord documentation`]: https://discord.com/developers/docs/resources/guild#guild-object-guild-features
pub features: Vec<String>,
/// Indicator of whether the guild requires multi-factor authentication for [`Role`]s or
/// [`User`]s with moderation permissions.
pub mfa_level: MfaLevel,
/// Application ID of the guild creator if it is bot-created.
pub application_id: Option<ApplicationId>,
/// The ID of the channel to which system messages are sent.
pub system_channel_id: Option<ChannelId>,
/// System channel flags.
pub system_channel_flags: SystemChannelFlags,
/// The id of the channel where rules and/or guidelines are displayed.
///
/// **Note**: Only available on `COMMUNITY` guild, see [`Self::features`].
pub rules_channel_id: Option<ChannelId>,
/// The maximum number of presences for the guild. The default value is currently 25000.
///
/// **Note**: It is in effect when it is `None`.
pub max_presences: Option<u64>,
/// The maximum number of members for the guild.
pub max_members: Option<u64>,
/// The vanity url code for the guild, if it has one.
pub vanity_url_code: Option<String>,
/// The server's description, if it has one.
pub description: Option<String>,
/// The guild's banner, if it has one.
pub banner: Option<String>,
/// The server's premium boosting level.
pub premium_tier: PremiumTier,
/// The total number of users currently boosting this server.
pub premium_subscription_count: Option<u64>,
/// The preferred locale of this guild only set if guild has the "COMMUNITY" feature,
/// defaults to en-US.
pub preferred_locale: String,
/// The id of the channel where admins and moderators of Community guilds receive notices from
/// Discord.
///
/// **Note**: Only available on `COMMUNITY` guild, see [`Self::features`].
pub public_updates_channel_id: Option<ChannelId>,
/// The maximum amount of users in a video channel.
pub max_video_channel_users: Option<u64>,
/// The maximum amount of users in a stage video channel
pub max_stage_video_channel_users: Option<u64>,
/// Approximate number of members in this guild.
pub approximate_member_count: Option<u64>,
/// Approximate number of non-offline members in this guild.
pub approximate_presence_count: Option<u64>,
/// The welcome screen of the guild.
///
/// **Note**: Only available on `COMMUNITY` guild, see [`Self::features`].
pub welcome_screen: Option<GuildWelcomeScreen>,
/// The guild NSFW state. See [`discord support article`].
///
/// [`discord support article`]: https://support.discord.com/hc/en-us/articles/1500005389362-NSFW-Server-Designation
pub nsfw_level: NsfwLevel,
/// All of the guild's custom stickers.
#[serde(with = "stickers")]
pub stickers: HashMap<StickerId, Sticker>,
/// Whether the guild has the boost progress bar enabled
pub premium_progress_bar_enabled: bool,
/// The id of the channel where this guild will recieve safety alerts.
pub safety_alerts_channel_id: Option<ChannelId>,
/// The incidents data for this guild, if any.
pub incidents_data: Option<IncidentsData>,
}
#[cfg(feature = "model")]
impl PartialGuild {
/// Gets all auto moderation [`Rule`]s of this guild via HTTP.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the guild is unavailable.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn automod_rules(self, http: impl AsRef<Http>) -> Result<Vec<Rule>> {
self.id.automod_rules(http).await
}
/// Gets an auto moderation [`Rule`] of this guild by its ID via HTTP.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if a rule with the given ID does not exist.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn automod_rule(
&self,
http: impl AsRef<Http>,
rule_id: impl Into<RuleId>,
) -> Result<Rule> {
self.id.automod_rule(http, rule_id).await
}
/// Creates an auto moderation [`Rule`] in the guild.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Examples
///
/// See [`GuildId::create_automod_rule`] for details.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn create_automod_rule(
&self,
cache_http: impl CacheHttp,
builder: EditAutoModRule<'_>,
) -> Result<Rule> {
self.id.create_automod_rule(cache_http, builder).await
}
/// Edit an auto moderation [`Rule`], given its Id.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn edit_automod_rule(
&self,
cache_http: impl CacheHttp,
rule_id: impl Into<RuleId>,
builder: EditAutoModRule<'_>,
) -> Result<Rule> {
self.id.edit_automod_rule(cache_http, rule_id, builder).await
}
/// Deletes an auto moderation [`Rule`] from the guild.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if a rule with that Id
/// does not exist.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn delete_automod_rule(
&self,
http: impl AsRef<Http>,
rule_id: impl Into<RuleId>,
) -> Result<()> {
self.id.delete_automod_rule(http, rule_id).await
}
/// Ban a [`User`] from the guild, deleting a number of days' worth of messages (`dmd`) between
/// the range 0 and 7.
///
/// **Note**: Requires the [Ban Members] permission.
///
/// # Examples
///
/// Ban a member and remove all messages they've sent in the last 4 days:
///
/// ```rust,ignore
/// // assumes a `user` and `guild` have already been bound
/// let _ = guild.ban(user, 4);
/// ```
///
/// # Errors
///
/// Returns a [`ModelError::DeleteMessageDaysAmount`] if the number of days' worth of messages
/// to delete is over the maximum.
///
/// Also may return [`Error::Http`] if the current user lacks permission.
///
/// [Ban Members]: Permissions::BAN_MEMBERS
#[inline]
pub async fn ban(
&self,
http: impl AsRef<Http>,
user: impl Into<UserId>,
dmd: u8,
) -> Result<()> {
self.ban_with_reason(http, user, dmd, "").await
}
/// Ban a [`User`] from the guild with a reason. Refer to [`Self::ban`] to further
/// documentation.
///
/// # Errors
///
/// In addition to the reasons [`Self::ban`] may return an error, can also return an error if
/// the reason is too long.
#[inline]
pub async fn ban_with_reason(
&self,
http: impl AsRef<Http>,
user: impl Into<UserId>,
dmd: u8,
reason: impl AsRef<str>,
) -> Result<()> {
self.id.ban_with_reason(http, user, dmd, reason).await
}
/// Gets a list of the guild's bans, with additional options and filtering. See
/// [`Http::get_bans`] for details.
///
/// Requires the [Ban Members] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [Ban Members]: Permissions::BAN_MEMBERS
#[inline]
pub async fn bans(
&self,
http: impl AsRef<Http>,
target: Option<UserPagination>,
limit: Option<u8>,
) -> Result<Vec<Ban>> {
self.id.bans(http, target, limit).await
}
/// Gets a user's ban from the guild.
/// See [`Http::get_bans`] for details.
///
/// Requires the [Ban Members] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [Ban Members]: Permissions::BAN_MEMBERS
#[inline]
pub async fn get_ban(&self, http: impl AsRef<Http>, user_id: UserId) -> Result<Option<Ban>> {
self.id.get_ban(http, user_id).await
}
/// Gets a list of the guild's audit log entries
///
/// **Note**: Requires the [View Audit Log] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if an invalid value is
/// given.
///
/// [View Audit Log]: Permissions::VIEW_AUDIT_LOG
#[inline]
pub async fn audit_logs(
&self,
http: impl AsRef<Http>,
action_type: Option<audit_log::Action>,
user_id: Option<UserId>,
before: Option<AuditLogEntryId>,
limit: Option<u8>,
) -> Result<AuditLogs> {
self.id.audit_logs(http, action_type, user_id, before, limit).await
}
/// Gets all of the guild's channels over the REST API.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user is not in the guild or if the guild is
/// otherwise unavailable.
#[inline]
pub async fn channels(
&self,
http: impl AsRef<Http>,
) -> Result<HashMap<ChannelId, GuildChannel>> {
self.id.channels(http).await
}
#[cfg(feature = "cache")]
#[deprecated = "Use Cache::guild and Guild::channels"]
pub fn channel_id_from_name(
&self,
cache: impl AsRef<Cache>,
name: impl AsRef<str>,
) -> Option<ChannelId> {
let cache = cache.as_ref();
let guild = cache.guild(self.id)?;
#[allow(deprecated)]
guild.channel_id_from_name(cache, name)
}
/// Creates a [`GuildChannel`] in the guild.
///
/// Refer to [`Http::create_channel`] for more information.
///
/// **Note**: Requires the [Manage Channels] permission.
///
/// # Examples
///
/// Create a voice channel in a guild with the name `test`:
///
/// ```rust,no_run
/// # use serenity::http::Http;
/// # use serenity::model::guild::PartialGuild;
/// # use serenity::model::id::GuildId;
/// use serenity::builder::CreateChannel;
/// use serenity::model::channel::ChannelType;
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http: Http = unimplemented!();
/// # let guild = PartialGuild::get(&http, GuildId::new(7)).await?;
/// let builder = CreateChannel::new("my-test-channel").kind(ChannelType::Text);
///
/// // assuming a `guild` has already been bound
/// let _channel = guild.create_channel(&http, builder).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
/// lacks permission. Otherwise returns [`Error::Http`], as well as if invalid data is given.
///
/// [Manage Channels]: Permissions::MANAGE_CHANNELS
pub async fn create_channel(
&self,
cache_http: impl CacheHttp,
builder: CreateChannel<'_>,
) -> Result<GuildChannel> {
self.id.create_channel(cache_http, builder).await
}
/// Creates an emoji in the guild with a name and base64-encoded image.
///
/// Refer to the documentation for [`Guild::create_emoji`] for more information.
///
/// Requires the [Create Guild Expressions] permission.
///
/// # Examples
///
/// See the [`EditProfile::avatar`] example for an in-depth example as to how to read an image
/// from the filesystem and encode it as base64. Most of the example can be applied similarly
/// for this method.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, if the emoji name is too
/// long, or if the image is too large.
///
/// [`EditProfile::avatar`]: crate::builder::EditProfile::avatar
/// [Create Guild Expressions]: Permissions::CREATE_GUILD_EXPRESSIONS
#[inline]
pub async fn create_emoji(
&self,
http: impl AsRef<Http>,
name: &str,
image: &str,
) -> Result<Emoji> {
self.id.create_emoji(http, name, image).await
}
/// Creates an integration for the guild.
///
/// Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn create_integration(
&self,
http: impl AsRef<Http>,
integration_id: impl Into<IntegrationId>,
kind: &str,
) -> Result<()> {
self.id.create_integration(http, integration_id, kind).await
}
/// Create a guild specific application [`Command`].
///
/// **Note**: Unlike global commands, guild commands will update instantly.
///
/// # Errors
///
/// See [`CreateCommand::execute`] for a list of possible errors.
///
/// [`CreateCommand::execute`]: ../../builder/struct.CreateCommand.html#method.execute
#[inline]
pub async fn create_command(
&self,
cache_http: impl CacheHttp,
builder: CreateCommand,
) -> Result<Command> {
self.id.create_command(cache_http, builder).await
}
/// Override all guild application commands.
///
/// # Errors
///
/// Returns the same errors as [`Self::create_command`].
pub async fn set_commands(
&self,
http: impl AsRef<Http>,
commands: Vec<CreateCommand>,
) -> Result<Vec<Command>> {
self.id.set_commands(http, commands).await
}
/// Overwrites permissions for a specific command.
///
/// **Note**: It will update instantly.
///
/// # Errors
///
/// See [`CreateCommandPermissionsData::execute`] for a list of possible errors.
///
/// [`CreateCommandPermissionsData::execute`]: ../../builder/struct.CreateCommandPermissionsData.html#method.execute
pub async fn edit_command_permissions(
&self,
cache_http: impl CacheHttp,
command_id: CommandId,
builder: EditCommandPermissions,
) -> Result<CommandPermissions> {
self.id.edit_command_permissions(cache_http, command_id, builder).await
}
/// Get all guild application commands.
///
/// # Errors
///
/// If there is an error, it will be either [`Error::Http`] or [`Error::Json`].
pub async fn get_commands(&self, http: impl AsRef<Http>) -> Result<Vec<Command>> {
self.id.get_commands(http).await
}
/// Get all guild application commands with localizations.
///
/// # Errors
///
/// If there is an error, it will be either [`Error::Http`] or [`Error::Json`].
pub async fn get_commands_with_localizations(
&self,
http: impl AsRef<Http>,
) -> Result<Vec<Command>> {
self.id.get_commands_with_localizations(http).await
}
/// Get a specific guild application command by its Id.
///
/// # Errors
///
/// If there is an error, it will be either [`Error::Http`] or [`Error::Json`].
pub async fn get_command(
&self,
http: impl AsRef<Http>,
command_id: CommandId,
) -> Result<Command> {
self.id.get_command(http, command_id).await
}
/// Edit a guild application command, given its Id.
///
/// # Errors
///
/// See [`CreateCommand::execute`] for a list of possible errors.
///
/// [`CreateCommand::execute`]: ../../builder/struct.CreateCommand.html#method.execute
pub async fn edit_command(
&self,
cache_http: impl CacheHttp,
command_id: CommandId,
builder: CreateCommand,
) -> Result<Command> {
self.id.edit_command(cache_http, command_id, builder).await
}
/// Delete guild application command by its Id.
///
/// # Errors
///
/// If there is an error, it will be either [`Error::Http`] or [`Error::Json`].
pub async fn delete_command(
&self,
http: impl AsRef<Http>,
command_id: CommandId,
) -> Result<()> {
self.id.delete_command(http, command_id).await
}
/// Get all guild application commands permissions only.
///
/// # Errors
///
/// If there is an error, it will be either [`Error::Http`] or [`Error::Json`].
pub async fn get_commands_permissions(
&self,
http: impl AsRef<Http>,
) -> Result<Vec<CommandPermissions>> {
self.id.get_commands_permissions(http).await
}
/// Get permissions for specific guild application command by its Id.
///
/// # Errors
///
/// If there is an error, it will be either [`Error::Http`] or [`Error::Json`].
pub async fn get_command_permissions(
&self,
http: impl AsRef<Http>,
command_id: CommandId,
) -> Result<CommandPermissions> {
self.id.get_command_permissions(http, command_id).await
}
/// Creates a new role in the guild with the data set, if any.
///
/// See the documentation for [`Guild::create_role`] on how to use this.
///
/// **Note**: Requires the [Manage Roles] permission.
///
/// # Errors
///
/// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
/// lacks permission. Otherwise returns [`Error::Http`], as well as if invalid data is given.
///
/// [Manage Roles]: Permissions::MANAGE_ROLES
#[inline]
pub async fn create_role(
&self,
cache_http: impl CacheHttp,
builder: EditRole<'_>,
) -> Result<Role> {
self.id.create_role(cache_http, builder).await
}
/// Creates a new sticker in the guild with the data set, if any.
///
/// **Note**: Requires the [Create Guild Expressions] permission.
///
/// # Errors
///
/// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
/// lacks permission. Otherwise returns [`Error::Http`], as well as if invalid data is given.
///
/// [Create Guild Expressions]: Permissions::CREATE_GUILD_EXPRESSIONS
pub async fn create_sticker(
&self,
cache_http: impl CacheHttp,
builder: CreateSticker<'_>,
) -> Result<Sticker> {
self.id.create_sticker(cache_http, builder).await
}
/// Deletes the current guild if the current user is the owner of the
/// guild.
///
/// **Note**: Requires the current user to be the owner of the guild.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user is not the owner of
/// the guild.
#[inline]
pub async fn delete(&self, http: impl AsRef<Http>) -> Result<()> {
self.id.delete(http).await
}
/// Deletes an [`Emoji`] from the guild.
///
/// **Note**: If the emoji was created by the current user, requires either the [Create Guild
/// Expressions] or the [Manage Guild Expressions] permission. Otherwise, the [Manage Guild
/// Expressions] permission is required.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if an emoji with the given
/// id does not exist in the guild.
///
/// [Create Guild Expressions]: Permissions::CREATE_GUILD_EXPRESSIONS
/// [Manage Guild Expressions]: Permissions::MANAGE_GUILD_EXPRESSIONS
#[inline]
pub async fn delete_emoji(
&self,
http: impl AsRef<Http>,
emoji_id: impl Into<EmojiId>,
) -> Result<()> {
self.id.delete_emoji(http, emoji_id).await
}
/// Deletes an integration by Id from the guild.
///
/// Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if an integration with
/// that Id does not exist in the guild.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn delete_integration(
&self,
http: impl AsRef<Http>,
integration_id: impl Into<IntegrationId>,
) -> Result<()> {
self.id.delete_integration(http, integration_id).await
}
/// Deletes a [`Role`] by Id from the guild.
///
/// Also see [`Role::delete`] if you have the `cache` and `model` features enabled.
///
/// Requires the [Manage Roles] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if a Role with that Id
/// does not exist in the Guild.
///
/// [Manage Roles]: Permissions::MANAGE_ROLES
#[inline]
pub async fn delete_role(
&self,
http: impl AsRef<Http>,
role_id: impl Into<RoleId>,
) -> Result<()> {
self.id.delete_role(http, role_id).await
}
/// Deletes a [`Sticker`] by Id from the guild.
///
/// **Note**: If the sticker was created by the current user, requires either the [Create Guild
/// Expressions] or the [Manage Guild Expressions] permission. Otherwise, the [Manage Guild
/// Expressions] permission is required.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if a sticker with that id
/// does not exist.
///
/// [Create Guild Expressions]: Permissions::CREATE_GUILD_EXPRESSIONS
/// [Manage Guild Expressions]: Permissions::MANAGE_GUILD_EXPRESSIONS
#[inline]
pub async fn delete_sticker(
&self,
http: impl AsRef<Http>,
sticker_id: impl Into<StickerId>,
) -> Result<()> {
self.id.delete_sticker(http, sticker_id).await
}
/// Edits the current guild with new data where specified.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
/// lacks permission. Otherwise returns [`Error::Http`], as well as if invalid data is given.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
pub async fn edit(&mut self, cache_http: impl CacheHttp, builder: EditGuild<'_>) -> Result<()> {
let guild = self.id.edit(cache_http, builder).await?;
self.afk_metadata = guild.afk_metadata;
self.default_message_notifications = guild.default_message_notifications;
self.emojis = guild.emojis;
self.features = guild.features;
self.icon = guild.icon;
self.mfa_level = guild.mfa_level;
self.name = guild.name;
self.owner_id = guild.owner_id;
self.roles = guild.roles;
self.splash = guild.splash;
self.verification_level = guild.verification_level;
Ok(())
}
/// Edits an [`Emoji`]'s name in the guild.
///
/// Also see [`Emoji::edit`] if you have the `cache` and `methods` features enabled.
///
/// **Note**: If the emoji was created by the current user, requires either the [Create Guild
/// Expressions] or the [Manage Guild Expressions] permission. Otherwise, the [Manage Guild
/// Expressions] permission is required.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if an emoji with the given
/// id does not exist.
///
/// [Create Guild Expressions]: Permissions::CREATE_GUILD_EXPRESSIONS
/// [Manage Guild Expressions]: Permissions::MANAGE_GUILD_EXPRESSIONS
#[inline]
pub async fn edit_emoji(
&self,
http: impl AsRef<Http>,
emoji_id: impl Into<EmojiId>,
name: &str,
) -> Result<Emoji> {
self.id.edit_emoji(http, emoji_id, name).await
}
/// Edits the properties a guild member, such as muting or nicknaming them. Returns the new
/// member.
///
/// Refer to the documentation of [`EditMember`] for a full list of methods and permission
/// restrictions.
///
/// # Examples
///
/// See [`GuildId::edit_member`] for details.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
#[inline]
pub async fn edit_member(
&self,
cache_http: impl CacheHttp,
user_id: impl Into<UserId>,
builder: EditMember<'_>,
) -> Result<Member> {
self.id.edit_member(cache_http, user_id, builder).await
}
/// Edits the guild's MFA level. Returns the new level on success.
///
/// Requires guild ownership.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
pub async fn edit_mfa_level(
&self,
http: impl AsRef<Http>,
mfa_level: MfaLevel,
audit_log_reason: Option<&str>,
) -> Result<MfaLevel> {
self.id.edit_mfa_level(http, mfa_level, audit_log_reason).await
}
/// Edits the current user's nickname for the guild.
///
/// Pass [`None`] to reset the nickname.
///
/// **Note**: Requires the [Change Nickname] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission to change their nickname.
///
/// [Change Nickname]: Permissions::CHANGE_NICKNAME
#[inline]
pub async fn edit_nickname(
&self,
http: impl AsRef<Http>,
new_nickname: Option<&str>,
) -> Result<()> {
self.id.edit_nickname(http, new_nickname).await
}
/// Edits a role, optionally setting its fields.
///
/// **Note**: Requires the [Manage Roles] permission.
///
/// # Examples
///
/// See the documentation of [`GuildId::edit_role`] for details.
///
/// # Errors
///
/// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
/// lacks permission. Otherwise returns [`Error::Http`], as well as if invalid data is given.
///
/// [Manage Roles]: Permissions::MANAGE_ROLES
#[inline]
pub async fn edit_role(
&self,
cache_http: impl CacheHttp,
role_id: impl Into<RoleId>,
builder: EditRole<'_>,
) -> Result<Role> {
self.id.edit_role(cache_http, role_id, builder).await
}
/// Edits the order of [`Role`]s. Requires the [Manage Roles] permission.
///
/// # Examples
///
/// Change the order of a role:
///
/// ```rust,ignore
/// use serenity::model::id::RoleId;
/// partial_guild.edit_role_position(&context, RoleId::new(8), 2);
/// ```
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [Manage Roles]: Permissions::MANAGE_ROLES
#[inline]
pub async fn edit_role_position(
&self,
http: impl AsRef<Http>,
role_id: impl Into<RoleId>,
position: u16,
) -> Result<Vec<Role>> {
self.id.edit_role_position(http, role_id, position).await
}
/// Edits a sticker.
///
/// **Note**: If the sticker was created by the current user, requires either the [Create Guild
/// Expressions] or the [Manage Guild Expressions] permission. Otherwise, the [Manage Guild
/// Expressions] permission is required.
///
/// # Examples
///
/// Rename a sticker:
///
/// ```rust,no_run
/// # use serenity::http::Http;
/// # use serenity::model::guild::PartialGuild;
/// # use serenity::model::id::GuildId;
/// use serenity::builder::EditSticker;
/// use serenity::model::id::StickerId;
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http: Http = unimplemented!();
/// # let guild = PartialGuild::get(&http, GuildId::new(7)).await?;
/// let builder = EditSticker::new().name("Bun bun meow");
/// guild.edit_sticker(&http, StickerId::new(7), builder).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Create Guild Expressions]: Permissions::CREATE_GUILD_EXPRESSIONS
/// [Manage Guild Expressions]: Permissions::MANAGE_GUILD_EXPRESSIONS
#[inline]
pub async fn edit_sticker(
&self,
cache_http: impl CacheHttp,
sticker_id: impl Into<StickerId>,
builder: EditSticker<'_>,
) -> Result<Sticker> {
self.id.edit_sticker(cache_http, sticker_id, builder).await
}
/// Edits the guild's welcome screen.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
pub async fn edit_welcome_screen(
&self,
cache_http: impl CacheHttp,
builder: EditGuildWelcomeScreen<'_>,
) -> Result<GuildWelcomeScreen> {
self.id.edit_welcome_screen(cache_http, builder).await
}
/// Edits the guild's widget.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
pub async fn edit_widget(
&self,
cache_http: impl CacheHttp,
builder: EditGuildWidget<'_>,
) -> Result<GuildWidget> {
self.id.edit_widget(cache_http, builder).await
}
/// Gets a partial amount of guild data by its Id.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user is not
/// in the guild.
#[inline]
pub async fn get(
cache_http: impl CacheHttp,
guild_id: impl Into<GuildId>,
) -> Result<PartialGuild> {
guild_id.into().to_partial_guild(cache_http).await
}
/// Returns which of two [`User`]s has a higher [`Member`] hierarchy.
///
/// Hierarchy is essentially who has the [`Role`] with the highest [`position`].
///
/// Returns [`None`] if at least one of the given users' member instances is not present.
/// Returns [`None`] if the users have the same hierarchy, as neither are greater than the
/// other.
///
/// If both user IDs are the same, [`None`] is returned. If one of the users is the guild
/// owner, their ID is returned.
///
/// [`position`]: Role::position
#[cfg(feature = "cache")]
#[inline]
#[deprecated = "Use Cache::guild and Guild::greater_member_hierarchy"]
pub fn greater_member_hierarchy(
&self,
cache: impl AsRef<Cache>,
lhs_id: impl Into<UserId>,
rhs_id: impl Into<UserId>,
) -> Option<UserId> {
let cache = cache.as_ref();
let guild = cache.guild(self.id)?;
guild.greater_member_hierarchy(cache, lhs_id, rhs_id)
}
/// Calculate a [`Member`]'s permissions in the guild.
///
/// You likely want to use PartialGuild::user_permissions_in instead as this function does not
/// consider permission overwrites.
#[inline]
#[must_use]
pub fn member_permissions(&self, member: &Member) -> Permissions {
Guild::user_permissions_in_(
None,
member.user.id,
&member.roles,
self.id,
&self.roles,
self.owner_id,
)
}
/// Calculate a [`PartialMember`]'s permissions in the guild.
///
/// You likely want to use PartialGuild::partial_member_permissions_in instead as this function
/// does not consider permission overwrites.
///
/// # Panics
///
/// Panics if the passed [`UserId`] does not match the [`PartialMember`] id, if user is Some.
#[inline]
#[must_use]
pub fn partial_member_permissions(
&self,
member_id: UserId,
member: &PartialMember,
) -> Permissions {
if let Some(user) = &member.user {
assert_eq!(user.id, member_id, "User::id does not match provided PartialMember");
}
Guild::user_permissions_in_(
None,
member_id,
&member.roles,
self.id,
&self.roles,
self.owner_id,
)
}
/// Calculate a [`PartialMember`]'s permissions in a given channel in a guild.
///
/// # Panics
///
/// Panics if the passed [`UserId`] does not match the [`PartialMember`] id, if user is Some.
#[must_use]
pub fn partial_member_permissions_in(
&self,
channel: &GuildChannel,
member_id: UserId,
member: &PartialMember,
) -> Permissions {
if let Some(user) = &member.user {
assert_eq!(user.id, member_id, "User::id does not match provided PartialMember");
}
Guild::user_permissions_in_(
Some(channel),
member_id,
&member.roles,
self.id,
&self.roles,
self.owner_id,
)
}
/// Re-orders the channels of the guild.
///
/// Although not required, you should specify all channels' positions, regardless of whether
/// they were updated. Otherwise, positioning can sometimes get weird.
///
/// **Note**: Requires the [Manage Channels] permission.
///
/// # Errors
///
/// Returns an [`Error::Http`] if the current user is lacking permission.
///
/// [Manage Channels]: Permissions::MANAGE_CHANNELS
#[inline]
pub async fn reorder_channels(
&self,
http: impl AsRef<Http>,
channels: impl IntoIterator<Item = (ChannelId, u64)>,
) -> Result<()> {
self.id.reorder_channels(http, channels).await
}
/// Returns a list of [`Member`]s in a [`Guild`] whose username or nickname starts with a
/// provided string.
///
/// Optionally pass in the `limit` to limit the number of results. Minimum value is 1, maximum
/// and default value is 1000.
///
/// **Note**: Queries are case insensitive.
///
/// # Errors
///
/// Returns an [`Error::Http`] if the API returns an error.
#[inline]
pub async fn search_members(
&self,
http: impl AsRef<Http>,
query: &str,
limit: Option<u64>,
) -> Result<Vec<Member>> {
self.id.search_members(http, query, limit).await
}
/// Starts a prune of [`Member`]s.
///
/// See the documentation on [`GuildPrune`] for more information.
///
/// **Note**: Requires [Kick Members] and [Manage Guild] permissions.
///
/// # Errors
///
/// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
/// does not have permission to kick members.
///
/// Otherwise will return [`Error::Http`] if the current user does not have permission.
///
/// Can also return an [`Error::Json`] if there is an error deserializing the API response.
///
/// [Kick Members]: Permissions::KICK_MEMBERS
/// [Manage Guild]: Permissions::MANAGE_GUILD
/// [`Error::Http`]: crate::error::Error::Http
/// [`Error::Json`]: crate::error::Error::Json
pub async fn start_prune(&self, cache_http: impl CacheHttp, days: u8) -> Result<GuildPrune> {
self.id.start_prune(cache_http.http(), days).await
}
/// Kicks a [`Member`] from the guild.
///
/// Requires the [Kick Members] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the member cannot be kicked by the current user.
///
/// [Kick Members]: Permissions::KICK_MEMBERS
#[inline]
pub async fn kick(&self, http: impl AsRef<Http>, user_id: impl Into<UserId>) -> Result<()> {
self.id.kick(http, user_id).await
}
/// # Errors
///
/// In addition to the reasons [`Self::kick`] may return an error, can also return an error if
/// the reason is too long.
#[inline]
pub async fn kick_with_reason(
&self,
http: impl AsRef<Http>,
user_id: impl Into<UserId>,
reason: &str,
) -> Result<()> {
self.id.kick_with_reason(http, user_id, reason).await
}
/// Returns a formatted URL of the guild's icon, if the guild has an icon.
#[must_use]
pub fn icon_url(&self) -> Option<String> {
icon_url(self.id, self.icon.as_ref())
}
/// Returns a formatted URL of the guild's banner, if the guild has a banner.
#[must_use]
pub fn banner_url(&self) -> Option<String> {
self.banner.as_ref().map(|banner| cdn!("/banners/{}/{}.webp", self.id, banner))
}
/// Gets all [`Emoji`]s of this guild via HTTP.
///
/// # Errors
///
/// Returns [`Error::Http`] if the guild is unavailable.
#[inline]
pub async fn emojis(&self, http: impl AsRef<Http>) -> Result<Vec<Emoji>> {
self.id.emojis(http).await
}
/// Gets an [`Emoji`] of this guild by its ID via HTTP.
///
/// # Errors
///
/// Returns [`Error::Http`] if an [`Emoji`] with the given Id does not exist for the guild.
#[inline]
pub async fn emoji(&self, http: impl AsRef<Http>, emoji_id: EmojiId) -> Result<Emoji> {
self.id.emoji(http, emoji_id).await
}
/// Gets all integration of the guild.
///
/// Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn integrations(&self, http: impl AsRef<Http>) -> Result<Vec<Integration>> {
self.id.integrations(http).await
}
/// Gets all of the guild's invites.
///
/// Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
#[inline]
pub async fn invites(&self, http: impl AsRef<Http>) -> Result<Vec<RichInvite>> {
self.id.invites(http).await
}
/// Returns a guild [`Member`] object for the current user.
///
/// See [`Http::get_current_user_guild_member`] for more.
///
/// # Errors
///
/// Returns an [`Error::Http`] if the current user is not in the guild or the access token
/// lacks the necessary scope.
#[inline]
pub async fn current_user_member(&self, http: impl AsRef<Http>) -> Result<Member> {
self.id.current_user_member(http).await
}
/// Leaves the guild.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user is unable to leave the Guild, or currently is
/// not in the guild.
#[inline]
pub async fn leave(&self, http: impl AsRef<Http>) -> Result<()> {
self.id.leave(http).await
}
/// Gets a user's [`Member`] for the guild by Id.
///
/// # Errors
///
/// Returns [`Error::Http`] if the member is not in the Guild, or if the Guild is otherwise
/// unavailable.
#[inline]
pub async fn member(
&self,
cache_http: impl CacheHttp,
user_id: impl Into<UserId>,
) -> Result<Member> {
self.id.member(cache_http, user_id).await
}
/// Gets a list of the guild's members.
///
/// Optionally pass in the `limit` to limit the number of results. Minimum value is 1, maximum
/// and default value is 1000.
///
/// Optionally pass in `after` to offset the results by a [`User`]'s Id.
///
/// # Errors
///
/// Returns an [`Error::Http`] if the API returns an error, may also return
/// [`Error::NotInRange`] if the input is not within range.
///
/// [`User`]: crate::model::user::User
#[inline]
pub async fn members(
&self,
http: impl AsRef<Http>,
limit: Option<u64>,
after: impl Into<Option<UserId>>,
) -> Result<Vec<Member>> {
self.id.members(http, limit, after).await
}
/// Moves a member to a specific voice channel.
///
/// Requires the [Move Members] permission.
///
/// # Errors
///
/// Returns an [`Error::Http`] if the current user lacks permission, or if the member is not
/// currently in a voice channel for this Guild.
///
/// [Move Members]: Permissions::MOVE_MEMBERS
#[inline]
pub async fn move_member(
&self,
cache_http: impl CacheHttp,
user_id: impl Into<UserId>,
channel_id: impl Into<ChannelId>,
) -> Result<Member> {
self.id.move_member(cache_http, user_id, channel_id).await
}
/// Calculate a [`Member`]'s permissions in a given channel in the guild.
#[inline]
#[must_use]
pub fn user_permissions_in(&self, channel: &GuildChannel, member: &Member) -> Permissions {
Guild::user_permissions_in_(
Some(channel),
member.user.id,
&member.roles,
self.id,
&self.roles,
self.owner_id,
)
}
/// Calculate a [`Role`]'s permissions in a given channel in the guild.
///
/// # Errors
///
/// Returns [`Error::Model`] if the [`Role`] or [`Channel`] is not from this [`Guild`].
#[inline]
#[deprecated = "this function ignores other roles the user may have as well as user-specific permissions; use user_permissions_in instead"]
pub fn role_permissions_in(&self, channel: &GuildChannel, role: &Role) -> Result<Permissions> {
Guild::role_permissions_in_(channel, role, self.id)
}
/// Gets the number of [`Member`]s that would be pruned with the given number of days.
///
/// Requires the [Kick Members] permission.
///
/// See [`Guild::prune_count`].
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [Kick Members]: Permissions::KICK_MEMBERS
/// [`Guild::prune_count`]: crate::model::guild::Guild::prune_count
#[inline]
pub async fn prune_count(&self, http: impl AsRef<Http>, days: u8) -> Result<GuildPrune> {
self.id.prune_count(http, days).await
}
/// Returns the Id of the shard associated with the guild.
///
/// When the cache is enabled this will automatically retrieve the total number of shards.
///
/// **Note**: When the cache is enabled, this function unlocks the cache to retrieve the total
/// number of shards in use. If you already have the total, consider using [`utils::shard_id`].
///
/// [`utils::shard_id`]: crate::utils::shard_id
#[cfg(all(feature = "cache", feature = "utils"))]
#[inline]
#[must_use]
pub fn shard_id(&self, cache: impl AsRef<Cache>) -> u32 {
self.id.shard_id(cache)
}
/// Returns the Id of the shard associated with the guild.
///
/// When the cache is enabled this will automatically retrieve the total number of shards.
///
/// When the cache is not enabled, the total number of shards being used will need to be
/// passed.
///
/// # Examples
///
/// Retrieve the Id of the shard for a guild with Id `81384788765712384`, using 17 shards:
///
/// ```rust,ignore
/// use serenity::utils;
///
/// // assumes a `guild` has already been bound
///
/// assert_eq!(guild.shard_id(17), 7);
/// ```
#[cfg(all(feature = "utils", not(feature = "cache")))]
#[inline]
#[must_use]
pub fn shard_id(&self, shard_count: u32) -> u32 {
self.id.shard_id(shard_count)
}
/// Returns the formatted URL of the guild's splash image, if one exists.
#[inline]
#[must_use]
pub fn splash_url(&self) -> Option<String> {
self.splash.as_ref().map(|splash| cdn!("/splashes/{}/{}.webp?size=4096", self.id, splash))
}
/// Starts an integration sync for the given integration Id.
///
/// Requires the [Manage Guild] permission.
///
/// # Errors
///
/// See [`Guild::start_integration_sync`].
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
/// [`Guild::start_integration_sync`]: crate::model::guild::Guild::start_integration_sync
#[inline]
pub async fn start_integration_sync(
&self,
http: impl AsRef<Http>,
integration_id: impl Into<IntegrationId>,
) -> Result<()> {
self.id.start_integration_sync(http, integration_id).await
}
/// Unbans a [`User`] from the guild.
///
/// Requires the [Ban Members] permission.
///
/// # Errors
///
/// See [`Guild::unban`].
///
/// [Ban Members]: Permissions::BAN_MEMBERS
/// [`Guild::unban`]: crate::model::guild::Guild::unban
#[inline]
pub async fn unban(&self, http: impl AsRef<Http>, user_id: impl Into<UserId>) -> Result<()> {
self.id.unban(http, user_id).await
}
/// Retrieve's the guild's vanity URL.
///
/// **Note**: Requires the [Manage Guild] permission.
///
/// # Errors
///
/// See [`Guild::vanity_url`].
///
/// [Manage Guild]: Permissions::MANAGE_GUILD
/// [`Guild::vanity_url`]: crate::model::guild::Guild::vanity_url
#[inline]
pub async fn vanity_url(&self, http: impl AsRef<Http>) -> Result<String> {
self.id.vanity_url(http).await
}
/// Retrieves the guild's webhooks.
///
/// **Note**: Requires the [Manage Webhooks] permission.
///
/// # Errors
///
/// See [`Guild::webhooks`].
///
/// [Manage Webhooks]: Permissions::MANAGE_WEBHOOKS
/// [`Guild::webhooks`]: crate::model::guild::Guild::webhooks
#[inline]
pub async fn webhooks(&self, http: impl AsRef<Http>) -> Result<Vec<Webhook>> {
self.id.webhooks(http).await
}
/// Obtain a reference to a role by its name.
///
/// **Note**: If two or more roles have the same name, obtained reference will be one of them.
///
/// # Examples
///
/// Obtain a reference to a [`Role`] by its name.
///
/// ```rust,no_run
/// # use serenity::model::prelude::*;
/// # use serenity::prelude::*;
/// # struct Handler;
///
/// #[serenity::async_trait]
/// #[cfg(all(feature = "cache", feature = "client"))]
/// impl EventHandler for Handler {
/// async fn message(&self, context: Context, msg: Message) {
/// if let Some(guild_id) = msg.guild_id {
/// if let Some(guild) = guild_id.to_guild_cached(&context) {
/// if let Some(role) = guild.role_by_name("role_name") {
/// println!("Obtained role's reference: {:?}", role);
/// }
/// }
/// }
/// }
/// }
/// ```
#[inline]
#[must_use]
pub fn role_by_name(&self, role_name: &str) -> Option<&Role> {
self.roles.values().find(|role| role_name == role.name)
}
/// Returns a builder which can be awaited to obtain a message or stream of messages in this
/// guild.
#[cfg(feature = "collector")]
pub fn await_reply(&self, shard_messenger: impl AsRef<ShardMessenger>) -> MessageCollector {
MessageCollector::new(shard_messenger).guild_id(self.id)
}
/// Same as [`Self::await_reply`].
#[cfg(feature = "collector")]
pub fn await_replies(&self, shard_messenger: impl AsRef<ShardMessenger>) -> MessageCollector {
self.await_reply(shard_messenger)
}
/// Returns a builder which can be awaited to obtain a message or stream of reactions sent in
/// this guild.
#[cfg(feature = "collector")]
pub fn await_reaction(&self, shard_messenger: impl AsRef<ShardMessenger>) -> ReactionCollector {
ReactionCollector::new(shard_messenger).guild_id(self.id)
}
/// Same as [`Self::await_reaction`].
#[cfg(feature = "collector")]
pub fn await_reactions(
&self,
shard_messenger: impl AsRef<ShardMessenger>,
) -> ReactionCollector {
self.await_reaction(shard_messenger)
}
/// Gets the guild active threads.
///
/// # Errors
///
/// Returns [`Error::Http`] if there is an error in the deserialization, or if the bot issuing
/// the request is not in the guild.
pub async fn get_active_threads(&self, http: impl AsRef<Http>) -> Result<ThreadsData> {
self.id.get_active_threads(http).await
}
/// Gets a soundboard sound from the guild.
///
/// # Errors
///
/// Returns [`Error::Http`] if there is an error in the deserialization, or if the bot issuing
/// the request is not in the guild.
pub async fn get_soundboard(
self,
http: impl AsRef<Http>,
sound_id: SoundId,
) -> Result<Soundboard> {
self.id.get_soundboard(http, sound_id).await
}
/// Gets all soundboard sounds from the guild.
///
/// # Errors
///
/// Returns [`Error::Http`] if there is an error in the deserialization, or if the bot issuing
/// the request is not in the guild.
pub async fn get_soundboards(self, http: impl AsRef<Http>) -> Result<Vec<Soundboard>> {
self.id.get_soundboards(http).await
}
/// Creates a soundboard sound for the guild.
///
/// # Errors
///
/// See [`CreateSoundboard::execute`] for a list of possible errors.
///
/// [`CreateSoundboard::execute`]: ../../builder/struct.CreateSoundboard.html#method.execute
pub async fn create_soundboard(
self,
cache_http: impl CacheHttp,
builder: CreateSoundboard<'_>,
) -> Result<Soundboard> {
self.id.create_soundboard(cache_http, builder).await
}
/// Edits a soundboard sound for the guild.
///
/// # Errors
///
/// See [`EditSoundboard::execute`] for a list of possible errors.
///
/// [`EditSoundboard::execute`]: ../../builder/struct.EditSoundboard.html#method.execute
pub async fn edit_soundboard(
self,
cache_http: impl CacheHttp,
sound_id: SoundId,
builder: EditSoundboard<'_>,
) -> Result<Soundboard> {
self.id.edit_soundboard(cache_http, sound_id, builder).await
}
/// Deletes a soundboard sound for the guild.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if a
/// soundboard sound with that Id does not exist.
pub async fn delete_soundboard(
self,
http: impl AsRef<Http>,
sound_id: SoundId,
audit_log_reason: Option<&str>,
) -> Result<()> {
self.id.delete_soundboard(http, sound_id, audit_log_reason).await
}
}
// Manual impl needed to insert guild_id into Role's
impl<'de> Deserialize<'de> for PartialGuild {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> {
let mut guild = Self::deserialize(deserializer)?; // calls #[serde(remote)]-generated inherent method
guild.roles.values_mut().for_each(|r| r.guild_id = guild.id);
Ok(guild)
}
}
impl Serialize for PartialGuild {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> StdResult<S::Ok, S::Error> {
Self::serialize(self, serializer) // calls #[serde(remote)]-generated inherent method
}
}
impl From<Guild> for PartialGuild {
/// Converts this [`Guild`] instance into a [`PartialGuild`]
fn from(guild: Guild) -> Self {
Self {
application_id: guild.application_id,
id: guild.id,
afk_metadata: guild.afk_metadata,
default_message_notifications: guild.default_message_notifications,
widget_enabled: guild.widget_enabled,
widget_channel_id: guild.widget_channel_id,
emojis: guild.emojis,
features: guild.features,
icon: guild.icon,
mfa_level: guild.mfa_level,
name: guild.name,
owner_id: guild.owner_id,
roles: guild.roles,
splash: guild.splash,
discovery_splash: guild.discovery_splash,
system_channel_id: guild.system_channel_id,
system_channel_flags: guild.system_channel_flags,
rules_channel_id: guild.rules_channel_id,
public_updates_channel_id: guild.public_updates_channel_id,
verification_level: guild.verification_level,
description: guild.description,
premium_tier: guild.premium_tier,
premium_subscription_count: guild.premium_subscription_count,
banner: guild.banner,
vanity_url_code: guild.vanity_url_code,
welcome_screen: guild.welcome_screen,
approximate_member_count: guild.approximate_member_count,
approximate_presence_count: guild.approximate_presence_count,
nsfw_level: guild.nsfw_level,
max_video_channel_users: guild.max_video_channel_users,
max_presences: guild.max_presences,
max_members: guild.max_members,
stickers: guild.stickers,
icon_hash: guild.icon_hash,
explicit_content_filter: guild.explicit_content_filter,
preferred_locale: guild.preferred_locale,
max_stage_video_channel_users: guild.max_stage_video_channel_users,
premium_progress_bar_enabled: guild.premium_progress_bar_enabled,
safety_alerts_channel_id: guild.safety_alerts_channel_id,
incidents_data: guild.incidents_data,
}
}
}