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
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
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
|
use serde_json::builder::ObjectBuilder;
use std::collections::HashMap;
use std::fmt::Write as FmtWrite;
use std::io::Read;
use std::sync::{Arc, Mutex};
use super::gateway::Shard;
use super::rest::{self, GuildPagination};
use super::login_type::LoginType;
use typemap::ShareMap;
use ::utils::builder::{
CreateEmbed,
CreateInvite,
CreateMessage,
EditChannel,
EditGuild,
EditMember,
EditProfile,
EditRole,
GetMessages,
Search,
};
use ::internal::prelude::*;
use ::model::*;
use ::utils;
#[cfg(feature="extras")]
use std::ops::ShlAssign;
#[cfg(feature="cache")]
use super::CACHE;
/// The context is a general utility struct provided on event dispatches, which
/// helps with dealing with the current "context" of the event dispatch,
/// and providing helper methods where possible. The context also acts as a
/// general high-level interface over the associated [`Shard`] which
/// received the event, or the low-level [`rest`] module.
///
/// For example, when the [`Client::on_message`] handler is dispatched to, the
/// context will contain the Id of the [`Channel`] that the message was created
/// for. This allows for using shortcuts like [`say`], which will
/// post its given argument to the associated channel for you as a [`Message`].
///
/// Additionally, the context contains "shortcuts", like for interacting with
/// the shard. Methods like [`set_game`] will unlock the shard and perform an
/// update for you to save a bit of work.
///
/// A context will only live for the event it was dispatched for. After the
/// event handler finished, it is destroyed and will not be re-used.
///
/// # Automatically using the Cache
///
/// The context makes use of the [`Cache`] being global, and will first check
/// the cache for associated data before hitting the REST API. This is to save
/// Discord requests, and ultimately save your bot bandwidth and time. This also
/// acts as a clean interface for retrieving from the cache without needing to
/// check it yourself first, and then performing a request if it does not exist.
/// The context ultimately acts as a means to simplify these two operations into
/// one.
///
/// For example, if you are needing information about a
/// [channel][`GuildChannel`] within a [guild][`Guild`], then you can
/// use [`get_channel`] to retrieve it. Under most circumstances, the guild and
/// its channels will be cached within the cache, and `get_channel` will just
/// pull from the cache. If it does not exist, it will make a request to the
/// REST API, and then insert a clone of the channel into the cache, returning
/// you the channel.
///
/// In this scenario, now that the cache has the channel, performing the same
/// request to `get_channel` will instead pull from the cache, as it is now
/// cached.
///
/// [`Channel`]: ../model/enum.Channel.html
/// [`Client::on_message`]: struct.Client.html#method.on_message
/// [`Guild`]: ../model/struct.Guild.html
/// [`Message`]: ../model/struct.Message.html
/// [`GuildChannel`]: ../model/struct.GuildChannel.html
/// [`Shard`]: gateway/struct.Shard.html
/// [`Cache`]: ../ext/cache/struct.Cache.html
/// [`get_channel`]: #method.get_channel
/// [`rest`]: rest/index.html
/// [`say`]: #method.say
/// [`set_game`]: #method.set_game
#[derive(Clone)]
pub struct Context {
/// The Id of the relevant channel, if there is one. This is present on the
/// [`on_message`] handler, for example.
///
/// [`on_message`]: struct.Client.html#method.on_message
pub channel_id: Option<ChannelId>,
/// A clone of [`Client::data`]. Refer to its documentation for more
/// information.
///
/// [`Client::data`]: struct.Client.html#method.data
pub data: Arc<Mutex<ShareMap>>,
/// The associated shard which dispatched the event handler.
///
/// Note that if you are sharding, in relevant terms, this is the shard
/// which received the event being dispatched.
pub shard: Arc<Mutex<Shard>>,
/// The queue of messages that are sent after context goes out of scope.
pub queue: String,
login_type: LoginType,
}
impl Context {
/// Create a new Context to be passed to an event handler.
///
/// There's no real reason to use this yourself. But the option is there.
/// Highly re-consider _not_ using this if you're tempted.
///
/// Or don't do what I say. I'm just a comment hidden from the generated
/// documentation.
#[doc(hidden)]
pub fn new(channel_id: Option<ChannelId>,
shard: Arc<Mutex<Shard>>,
data: Arc<Mutex<ShareMap>>,
login_type: LoginType) -> Context {
Context {
channel_id: channel_id,
data: data,
shard: shard,
login_type: login_type,
queue: String::new(),
}
}
/// Accepts the given invite.
///
/// Refer to the documentation for [`rest::accept_invite`] for restrictions
/// on accepting an invite.
///
/// **Note**: Requires that the current user be a user account.
///
/// # Errors
///
/// Returns a [`ClientError::InvalidOperationAsBot`] if the current user is
/// a bot user.
///
/// [`ClientError::InvalidOperationAsBot`]: enum.ClientError.html#variant.InvalidOperationAsBot
/// [`rest::accept_invite`]: rest/fn.accept_invite.html
pub fn accept_invite(&self, invite: &str) -> Result<Invite> {
if self.login_type == LoginType::Bot {
return Err(Error::Client(ClientError::InvalidOperationAsBot));
}
rest::accept_invite(utils::parse_invite(invite))
}
/// Marks a [`Channel`] as being read up to a certain [`Message`].
///
/// Refer to the documentation for [`rest::ack_message`] for more
/// information.
///
/// # Errors
///
/// Returns a [`ClientError::InvalidOperationAsBot`] if the current user is
/// a bot user.
///
/// [`Channel`]: ../../model/enum.Channel.html
/// [`ClientError::InvalidOperationAsBot`]: ../enum.ClientError.html#variant.InvalidOperationAsUser
/// [`Message`]: ../../model/struct.Message.html
/// [`rest::ack_message`]: rest/fn.ack_message.html
pub fn ack<C, M>(&self, channel_id: C, message_id: M) -> Result<()>
where C: Into<ChannelId>, M: Into<MessageId> {
if self.login_type == LoginType::User {
return Err(Error::Client(ClientError::InvalidOperationAsUser));
}
rest::ack_message(channel_id.into().0, message_id.into().0)
}
/// Bans a [`User`] from a [`Guild`], removing their messages sent in the
/// last X number of days.
///
/// Refer to the documentation for [`rest::ban_user`] for more information.
///
/// Requires the [Ban Members] permission.
///
/// # Examples
///
/// Ban the user that sent a message for `7` days:
///
/// ```rust,ignore
/// // assuming you are in a context
/// context.ban_user(context.guild_id, context.message.author, 7);
/// ```
///
/// # Errors
///
/// Returns a [`ClientError::DeleteMessageDaysAmount`] if the number of days
/// given is over the maximum allowed.
///
/// [`ClientError::DeleteMessageDaysAmount`]: enum.ClientError.html#variant.DeleteMessageDaysAmount
/// [`Guild`]: ../model/struct.Guild.html
/// [`User`]: ../model/struct.User.html
/// [`rest::ban_user`]: rest/fn.ban_user.html
/// [Ban Members]: ../model/permissions/constant.BAN_MEMBERS.html
pub fn ban<G, U>(&self, guild_id: G, user_id: U, delete_message_days: u8)
-> Result<()> where G: Into<GuildId>, U: Into<UserId> {
if delete_message_days > 7 {
return Err(Error::Client(ClientError::DeleteMessageDaysAmount(delete_message_days)));
}
rest::ban_user(guild_id.into().0, user_id.into().0, delete_message_days)
}
/// Broadcasts that you are typing to a channel for the next 5 seconds.
///
/// After 5 seconds, another request must be made to continue broadcasting
/// that you are typing.
///
/// This should rarely be used for bots, and should likely only be used for
/// signifying that a long-running command is still being executed.
///
/// Requires the [Send Messages] permission.
///
/// # Examples
///
/// ```rust,ignore
/// // assuming you are in a context
/// context.broadcast_typing(context.channel_id);
/// ```
///
/// [Send Messages]: ../model/permissions/constant.SEND_MESSAGES.html
pub fn broadcast_typing<C>(&self, channel_id: C) -> Result<()>
where C: Into<ChannelId> {
rest::broadcast_typing(channel_id.into().0)
}
/// Creates a [`GuildChannel`] in the given [`Guild`].
///
/// Refer to [`rest::create_channel`] for more information.
///
/// Requires the [Manage Channels] permission.
///
/// # Examples
///
/// Create a voice channel in a guild with the name `test`:
///
/// ```rust,ignore
/// use serenity::model::ChannelType;
///
/// context.create_channel(context.guild_id, "test", ChannelType::Voice);
/// ```
///
/// [`Guild`]: ../model/struct.Guild.html
/// [`GuildChannel`]: ../model/struct.GuildChannel.html
/// [`rest::create_channel`]: rest/fn.create_channel.html
/// [Manage Channels]: ../model/permissions/constant.MANAGE_CHANNELS.html
pub fn create_channel<G>(&self, guild_id: G, name: &str, kind: ChannelType)
-> Result<Channel> where G: Into<GuildId> {
let map = ObjectBuilder::new()
.insert("name", name)
.insert("type", kind.name())
.build();
rest::create_channel(guild_id.into().0, map)
}
/// Creates an emoji in the given guild with a name and base64-encoded
/// image. The [`utils::read_image`] function is provided for you as a
/// simple method to read an image and encode it into base64, if you are
/// reading from the filesystem.
///
/// The name of the emoji must be at least 2 characters long and can only
/// contain alphanumeric characters and underscores.
///
/// Requires the [Manage Emojis] 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.
///
/// [`EditProfile::avatar`]: ../utils/builder/struct.EditProfile.html#method.avatar
/// [`utils::read_image`]: ../utils/fn.read_image.html
/// [Manage Emojis]: ../model/permissions/constant.MANAGE_EMOJIS.html
pub fn create_emoji<G>(&self, guild_id: G, name: &str, image: &str)
-> Result<Emoji> where G: Into<GuildId> {
let map = ObjectBuilder::new()
.insert("name", name)
.insert("image", image)
.build();
rest::create_emoji(guild_id.into().0, map)
}
/// Creates a guild with the data provided.
///
/// Only a [`PartialGuild`] will be immediately returned, and a full
/// [`Guild`] will be received over a [`Shard`].
///
/// **Note**: This endpoint is usually only available for user accounts.
/// Refer to Discord's information for the endpoint [here][whitelist] for
/// more information. If you require this as a bot, re-think what you are
/// doing and if it _really_ needs to be doing this.
///
/// # Examples
///
/// Create a guild called `"test"` in the [US West region] with no icon:
///
/// ```rust,ignore
/// use serenity::model::Region;
///
/// context.create_guild("test", Region::UsWest, None);
/// ```
///
/// [`Guild`]: ../model/struct.Guild.html
/// [`PartialGuild`]: ../model/struct.PartialGuild.html
/// [`Shard`]: ../gateway/struct.Shard.html
/// [US West region]: ../model/enum.Region.html#variant.UsWest
/// [whitelist]: https://discordapp.com/developers/docs/resources/guild#create-guild
pub fn create_guild(&self, name: &str, region: Region, icon: Option<&str>)
-> Result<PartialGuild> {
let map = ObjectBuilder::new()
.insert("icon", icon)
.insert("name", name)
.insert("region", region.name())
.build();
rest::create_guild(map)
}
/// Creates an [`Integration`] for a [`Guild`].
///
/// Requires the [Manage Guild] permission.
///
/// [`Guild`]: ../model/struct.Guild.html
/// [`Integration`]: ../model/struct.Integration.html
/// [Manage Guild]: ../model/permissions/constant.MANAGE_GUILD.html
pub fn create_integration<G, I>(&self,
guild_id: G,
integration_id: I,
kind: &str)
-> Result<()> where G: Into<GuildId>,
I: Into<IntegrationId> {
let integration_id = integration_id.into();
let map = ObjectBuilder::new()
.insert("id", integration_id.0)
.insert("type", kind)
.build();
rest::create_guild_integration(guild_id.into().0, integration_id.0, map)
}
/// Creates an invite for the channel, providing a builder so that fields
/// may optionally be set.
///
/// See the documentation for the [`CreateInvite`] builder for information
/// on how to use this and the default values that it provides.
///
/// Requires the [Create Invite] permission.
///
/// [`CreateInvite`]: ../utils/builder/struct.CreateInvite.html
/// [Create Invite]: ../model/permissions/constant.CREATE_INVITE.html
pub fn create_invite<C, F>(&self, channel_id: C, f: F) -> Result<RichInvite>
where C: Into<ChannelId>, F: FnOnce(CreateInvite) -> CreateInvite {
let map = f(CreateInvite::default()).0.build();
rest::create_invite(channel_id.into().0, map)
}
/// Creates a [permission overwrite][`PermissionOverwrite`] for either a
/// single [`Member`] or [`Role`] within a [`Channel`].
///
/// Refer to the documentation for [`PermissionOverwrite`]s for more
/// information.
///
/// Requires the [Manage Channels] permission.
///
/// # Examples
///
/// Creating a permission overwrite for a member by specifying the
/// [`PermissionOverwrite::Member`] variant, allowing it the [Send Messages]
/// permission, but denying the [Send TTS Messages] and [Attach Files]
/// permissions:
///
/// ```rust,ignore
/// use serenity::model::{ChannelId, PermissionOverwrite, permissions};
///
/// // assuming you are in a context
///
/// let channel_id = 7;
/// let user_id = 8;
///
/// let allow = permissions::SEND_MESSAGES;
/// let deny = permissions::SEND_TTS_MESSAGES | permissions::ATTACH_FILES;
/// let overwrite = PermissionOverwrite {
/// allow: allow,
/// deny: deny,
/// kind: PermissionOverwriteType::Member(user_id),
/// };
///
/// let _result = context.create_permission(channel_id, overwrite);
/// ```
///
/// Creating a permission overwrite for a role by specifying the
/// [`PermissionOverwrite::Role`] variant, allowing it the [Manage Webhooks]
/// permission, but denying the [Send TTS Messages] and [Attach Files]
/// permissions:
///
/// ```rust,ignore
/// use serenity::model::{ChannelId, PermissionOverwrite, permissions};
///
/// // assuming you are in a context
///
/// let channel_id = 7;
/// let user_id = 8;
///
/// let allow = permissions::SEND_MESSAGES;
/// let deny = permissions::SEND_TTS_MESSAGES | permissions::ATTACH_FILES;
/// let overwrite = PermissionOverwrite {
/// allow: allow,
/// deny: deny,
/// kind: PermissionOverwriteType::Member(user_id),
/// };
///
/// let _result = context.create_permission(channel_id, overwrite);
/// ```
///
/// [`Channel`]: ../model/enum.Channel.html
/// [`Member`]: ../model/struct.Member.html
/// [`PermissionOverwrite`]: ../model/struct.PermissionOverWrite.html
/// [`PermissionOverwrite::Member`]: ../model/struct.PermissionOverwrite.html#variant.Member
/// [`Role`]: ../model/struct.Role.html
/// [Attach Files]: ../model/permissions/constant.ATTACH_FILES.html
/// [Manage Channels]: ../model/permissions/constant.MANAGE_CHANNELS.html
/// [Manage Webhooks]: ../model/permissions/constant.MANAGE_WEBHOOKS.html
/// [Send TTS Messages]: ../model/permissions/constant.SEND_TTS_MESSAGES.html
pub fn create_permission<C>(&self,
channel_id: C,
target: PermissionOverwrite)
-> Result<()> where C: Into<ChannelId> {
let (id, kind) = match target.kind {
PermissionOverwriteType::Member(id) => (id.0, "member"),
PermissionOverwriteType::Role(id) => (id.0, "role"),
};
let map = ObjectBuilder::new()
.insert("allow", target.allow.bits())
.insert("deny", target.deny.bits())
.insert("id", id)
.insert("type", kind)
.build();
rest::create_permission(channel_id.into().0, id, map)
}
/// Creates a direct message channel between the [current user] and another
/// [`User`]. This can also retrieve the channel if one already exists.
///
/// [`User`]: ../model/struct.User.html
/// [current user]: ../model/struct.CurrentUser.html
pub fn create_direct_message_channel<U>(&self, user_id: U)
-> Result<PrivateChannel> where U: Into<UserId> {
let map = ObjectBuilder::new()
.insert("recipient_id", user_id.into().0)
.build();
rest::create_private_channel(map)
}
/// React to a [`Message`] with a custom [`Emoji`] or unicode character.
///
/// [`Message::react`] may be a more suited method of reacting in most
/// cases.
///
/// Requires the [Add Reactions] permission, _if_ the current user is the
/// first user to perform a react with a certain emoji.
///
/// [`Emoji`]: ../model/struct.Emoji.html
/// [`Message`]: ../model/struct.Message.html
/// [`Message::react`]: ../model/struct.Message.html#method.react
/// [Add Reactions]: ../model/permissions/constant.ADD_REACTIONS.html
pub fn create_reaction<C, M, R>(&self,
channel_id: C,
message_id: M,
reaction_type: R)
-> Result<()>
where C: Into<ChannelId>,
M: Into<MessageId>,
R: Into<ReactionType> {
rest::create_reaction(channel_id.into().0,
message_id.into().0,
reaction_type.into())
}
/// Creates a [`Role`] in guild with given Id. Second argument is a
/// closure, and you can use it to automatically configure role.
///
/// Requires the [Manage Roles] permission.
///
/// # Examples
///
/// Create a role which can be mentioned, with the name 'test':
///
/// ```rust,ignore
/// let role = context.create_role(guild_id, |r| r
/// .hoist(true)
/// .name("role"));
/// ```
///
/// [`Role`]: ../model/struct.Role.html
/// [Manage Roles]: ../model/permissions/constant.MANAGE_ROLES.html
pub fn create_role<F, G>(&self, guild_id: G, f: F) -> Result<Role>
where F: FnOnce(EditRole) -> EditRole, G: Into<GuildId> {
let id = guild_id.into().0;
// The API only allows creating an empty role, which must then be
// edited.
//
// Note to self: [this] issue/proposal may make this not require an
// edit.
//
// [this]: http://github.com/hammerandchisel/discord-api-docs/issues/156
let role = rest::create_role(id)?;
let map = f(EditRole::default()).0.build();
rest::edit_role(id, role.id.0, map)
}
/// Deletes a [`Channel`] based on the Id given.
///
/// If the channel being deleted is a [`GuildChannel`] (a [`Guild`]'s
/// channel), then the [Manage Channels] permission is required.
///
/// [`Channel`]: ../model/enum.Channel.html
/// [`Guild`]: ../model/struct.Guild.html
/// [`GuildChannel`]: ../model/struct.GuildChannel.html
/// [Manage Channels]: ../model/permissions/constant.MANAGE_CHANNELS.html
pub fn delete_channel<C>(&self, channel_id: C) -> Result<Channel>
where C: Into<ChannelId> {
rest::delete_channel(channel_id.into().0)
}
/// Deletes an emoji in a [`Guild`] given its Id.
///
/// Requires the [Manage Emojis] permission.
///
/// [`Guild`]: ../model/struct.Guild.html
/// [Manage Emojis]: ../model/permissions/constant.MANAGE_EMOJIS.html
pub fn delete_emoji<E, G>(&self, guild_id: G, emoji_id: E) -> Result<()>
where E: Into<EmojiId>, G: Into<GuildId> {
rest::delete_emoji(guild_id.into().0, emoji_id.into().0)
}
/// Deletes a [`Guild`]. The current user must be the guild owner to be able
/// to delete it.
///
/// Only a [`PartialGuild`] will be immediately returned.
///
/// [`Guild`]: ../model/struct.Guild.html
/// [`PartialGuild`]: ../model/struct.PartialGuild.html
pub fn delete_guild<G: Into<GuildId>>(&self, guild_id: G)
-> Result<PartialGuild> {
rest::delete_guild(guild_id.into().0)
}
/// Deletes an integration by Id from a guild which Id was given.
///
/// Requires the [Manage Guild] permission.
///
/// [Manage Guild]: ../model/permissions/constant.MANAGE_GUILD.html
pub fn delete_integration<G, I>(&self, guild_id: G, integration_id: I)
-> Result<()> where G: Into<GuildId>, I: Into<IntegrationId> {
rest::delete_guild_integration(guild_id.into().0,
integration_id.into().0)
}
/// Deletes the given invite.
///
/// Refer to the documentation for [`Invite::delete`] for restrictions on
/// deleting an invite.
///
/// Requires the [Manage Guild] permission.
///
/// # Errors
///
/// Returns a [`ClientError::InvalidPermissions`] if the current user does
/// not have the required [permission].
///
/// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions
/// [`Invite::delete`]: ../model/struct.Invite.html#method.delete
/// [Manage Guild]: permissions/constant.MANAGE_GUILD.html
pub fn delete_invite(&self, invite: &str) -> Result<Invite> {
rest::delete_invite(utils::parse_invite(invite))
}
/// Deletes a [`Message`] given its Id.
///
/// Also see [`Message::delete`] if you have the `methods` feature enabled.
///
/// Requires the [Manage Messages] permission, if the current user is not
/// the author of the message.
///
/// # Examples
///
/// Deleting every message that is received:
///
/// ```rust,ignore
/// use serenity::Client;
/// use std::env;
///
/// let client = Client::login_bot(&env::var("DISCORD_BOT_TOKEN").unwrap());
/// client.on_message(|context, message| {
/// context.delete_message(message);
/// });
/// ```
///
/// (in practice, please do not do this)
///
/// [`Message`]: ../model/struct.Message.html
/// [`Message::delete`]: ../model/struct.Message.html#method.delete
/// [Manage Messages]: ../model/permissions/constant.MANAGE_MESSAGES.html
pub fn delete_message<C, M>(&self, channel_id: C, message_id: M)
-> Result<()> where C: Into<ChannelId>, M: Into<MessageId> {
rest::delete_message(channel_id.into().0, message_id.into().0)
}
/// Deletes all messages by Ids from the given vector in the given channel.
///
/// The minimum amount of messages is 2 and the maximum amount is 100.
///
/// Requires the [Manage Messages] permission.
///
/// **Note**: This uses bulk delete endpoint which is not available
/// for user accounts.
///
/// **Note**: Messages that are older than 2 weeks can't be deleted using this method.
///
/// [Manage Messages]: ../model/permissions/constant.MANAGE_MESSAGES.html
pub fn delete_messages<C>(&self, channel_id: C, message_ids: &[MessageId])
-> Result<()> where C: Into<ChannelId> {
if self.login_type == LoginType::User {
return Err(Error::Client(ClientError::InvalidOperationAsUser))
}
let ids = message_ids.into_iter()
.map(|message_id| message_id.0)
.collect::<Vec<u64>>();
let map = ObjectBuilder::new()
.insert("messages", ids)
.build();
rest::delete_messages(channel_id.into().0, map)
}
/// Deletes a profile note from a user.
pub fn delete_note<U: Into<UserId>>(&self, user_id: U) -> Result<()> {
let map = ObjectBuilder::new()
.insert("note", "")
.build();
rest::edit_note(user_id.into().0, map)
}
/// Deletes all permission overrides in a channel from a member or
/// a role.
///
/// Requires the [Manage Channel] permission.
///
/// [Manage Channel]: ../model/permissions/constant.MANAGE_CHANNELS.html
pub fn delete_permission<C>(&self,
channel_id: C,
permission_type: PermissionOverwriteType)
-> Result<()> where C: Into<ChannelId> {
let id = match permission_type {
PermissionOverwriteType::Member(id) => id.0,
PermissionOverwriteType::Role(id) => id.0,
};
rest::delete_permission(channel_id.into().0, id)
}
/// Deletes the given [`Reaction`].
///
/// **Note**: Requires the [Manage Messages] permission, _if_ the current
/// user did not perform the reaction.
///
/// [`Reaction`]: ../model/struct.Reaction.html
/// [Manage Messages]: ../model/permissions/constant.MANAGE_MESSAGES.html
pub fn delete_reaction<C, M, R>(&self,
channel_id: C,
message_id: M,
user_id: Option<UserId>,
reaction_type: R)
-> Result<()>
where C: Into<ChannelId>,
M: Into<MessageId>,
R: Into<ReactionType> {
rest::delete_reaction(channel_id.into().0,
message_id.into().0,
user_id.map(|uid| uid.0),
reaction_type.into())
}
/// Deletes a [`Role`] by Id from the given [`Guild`].
///
/// Also see [`Role::delete`] if you have the `cache` and `methods` features
/// enabled.
///
/// Requires the [Manage Roles] permission.
///
/// [`Guild`]: ../model/struct.Guild.html
/// [`Role`]: ../model/struct.Role.html
/// [`Role::delete`]: ../model/struct.Role.html#method.delete
/// [Manage Roles]: ../model/permissions/constant.MANAGE_ROLES.html
pub fn delete_role<G, R>(&self, guild_id: G, role_id: R) -> Result<()>
where G: Into<GuildId>, R: Into<RoleId> {
rest::delete_role(guild_id.into().0, role_id.into().0)
}
/// Sends a message to a user through a direct message channel. This is a
/// channel that can only be accessed by you and the recipient.
///
/// # Examples
///
/// There are three ways to send a direct message to someone, the first
/// being an unrelated, although equally helpful method.
///
/// Sending a message via [`User::dm`]:
///
/// ```rust,ignore
/// // assuming you are in a context
/// let _ = context.message.author.dm("Hello!");
/// ```
///
/// Sending a message to a `PrivateChannel`:
///
/// ```rust,ignore
/// assuming you are in a context
/// let private_channel = context.create_private_channel(context.message.author.id);
///
/// let _ = context.direct_message(private_channel, "Test!");
/// ```
///
/// Sending a message to a `PrivateChannel` given its ID:
///
/// ```rust,ignore
/// use serenity::Client;
/// use std::env;
///
/// let mut client = Client::login_bot(&env::var("DISCORD_BOT_TOKEN").unwrap());
///
/// client.on_message(|context, message| {
/// if message.content == "!pm-me" {
/// let channel = context.create_private_channel(message.author.id)
/// .unwrap();
///
/// let _ = channel.send_message("test!");
/// }
/// });
/// ```
///
/// [`PrivateChannel`]: ../model/struct.PrivateChannel.html
/// [`User::dm`]: ../model/struct.User.html#method.dm
pub fn dm<C: Into<ChannelId>>(&self, target_id: C, content: &str)
-> Result<Message> {
self.send_message(target_id.into(), |m| m.content(content))
}
/// Edits the settings of a [`Channel`], optionally setting new values.
///
/// Refer to `EditChannel`'s documentation for its methods.
///
/// Requires the [Manage Channel] permission.
///
/// # Examples
///
/// Change a voice channel's name and bitrate:
///
/// ```rust,ignore
/// context.edit_channel(channel_id, |c| c
/// .name("test")
/// .bitrate(64000));
/// ```
///
/// [`Channel`]: ../model/enum.Channel.html
pub fn edit_channel<C, F>(&self, channel_id: C, f: F)
-> Result<GuildChannel> where C: Into<ChannelId>,
F: FnOnce(EditChannel) -> EditChannel {
let channel_id = channel_id.into();
let map = match self.get_channel(channel_id)? {
Channel::Guild(channel) => {
let map = ObjectBuilder::new()
.insert("name", channel.name)
.insert("position", channel.position);
match channel.kind {
ChannelType::Text => map.insert("topic", channel.topic),
ChannelType::Voice => {
map.insert("bitrate", channel.bitrate)
.insert("user_limit", channel.user_limit)
},
kind => return Err(Error::Client(ClientError::UnexpectedChannelType(kind))),
}
},
Channel::Private(channel) => {
return Err(Error::Client(ClientError::UnexpectedChannelType(channel.kind)));
},
Channel::Group(_group) => {
return Err(Error::Client(ClientError::UnexpectedChannelType(ChannelType::Group)));
},
};
let edited = f(EditChannel(map)).0.build();
rest::edit_channel(channel_id.0, edited)
}
/// Edits an [`Emoji`]'s name.
///
/// Also see [`Emoji::edit`] if you have the `cache` and `methods` features
/// enabled.
///
/// Requires the [Manage Emojis] permission.
///
/// [`Emoji`]: ../model/struct.Emoji.html
/// [`Emoji::edit`]: ../model/struct.Emoji.html#method.edit
/// [Manage Emojis]: ../model/permissions/constant.MANAGE_EMOJIS.html
pub fn edit_emoji<E, G>(&self, guild_id: G, emoji_id: E, name: &str)
-> Result<Emoji> where E: Into<EmojiId>, G: Into<GuildId> {
let map = ObjectBuilder::new()
.insert("name", name)
.build();
rest::edit_emoji(guild_id.into().0, emoji_id.into().0, map)
}
/// Edits the settings of a [`Guild`], optionally setting new values.
///
/// Refer to `EditGuild`'s documentation for a full list of methods.
///
/// Also see [`Guild::edit`] if you have the `methods` feature enabled.
///
/// Requires the [Manage Guild] permission.
///
/// # Examples
///
/// Change a guild's icon using a file name "icon.png":
///
/// ```rust,ignore
/// use serenity::utils;
///
/// // We are using read_image helper function from utils.
/// let base64_icon = utils::read_image("./icon.png")
/// .expect("Failed to read image");
///
/// context.edit_guild(guild_id, |g|
/// g.icon(base64_icon));
/// ```
///
/// [`Guild`]: ../model/struct.Guild.html
/// [`Guild::edit`]: ../model/struct.Guild.html
/// [Manage Guild]: ../model/permissions/constant.MANAGE_GUILD.html
pub fn edit_guild<F, G>(&self, guild_id: G, f: F) -> Result<PartialGuild>
where F: FnOnce(EditGuild) -> EditGuild, G: Into<GuildId> {
let map = f(EditGuild::default()).0.build();
rest::edit_guild(guild_id.into().0, map)
}
/// Edits the properties of member of a guild, such as muting or nicknaming
/// them.
///
/// Refer to `EditMember`'s documentation for a full list of methods and
/// permission restrictions.
///
/// # Examples
///
/// Mute a member and set their roles to just one role with a predefined Id:
///
/// ```rust,ignore
/// context.edit_member(guild_id, user_id, |m| m
/// .mute(true)
/// .roles(&vec![role_id]));
/// ```
pub fn edit_member<F, G, U>(&self, guild_id: G, user_id: U, f: F)
-> Result<()> where F: FnOnce(EditMember) -> EditMember,
G: Into<GuildId>,
U: Into<UserId> {
let map = f(EditMember::default()).0.build();
rest::edit_member(guild_id.into().0, user_id.into().0, map)
}
/// Edits the current user's nickname for the provided [`Guild`] via its Id.
///
/// Pass `None` to reset the nickname.
///
/// Requires the [Change Nickname] permission.
///
/// [`Guild`]: ../../model/struct.Guild.html
/// [Change Nickname]: permissions/constant.CHANGE_NICKNAME.html
#[inline]
pub fn edit_nickname<G>(&self, guild_id: G, new_nickname: Option<&str>)
-> Result<()> where G: Into<GuildId> {
rest::edit_nickname(guild_id.into().0, new_nickname)
}
/// Edits the current user's profile settings.
///
/// Refer to `EditProfile`'s documentation for its methods.
///
/// # Examples
///
/// Change the current user's username:
///
/// ```rust,ignore
/// context.edit_profile(|p| p.username("Hakase"));
/// ```
pub fn edit_profile<F: FnOnce(EditProfile) -> EditProfile>(&self, f: F)
-> Result<CurrentUser> {
let user = rest::get_current_user()?;
let mut map = ObjectBuilder::new()
.insert("avatar", user.avatar)
.insert("username", user.name);
if let Some(email) = user.email.as_ref() {
map = map.insert("email", email);
}
let edited = f(EditProfile(map)).0.build();
rest::edit_profile(edited)
}
/// Edits a [`Role`], optionally setting its new fields.
///
/// Requires the [Manage Roles] permission.
///
/// # Examples
///
/// Make a role hoisted:
///
/// ```rust,ignore
/// context.edit_role(guild_id, role_id, |r| r
/// .hoist(true));
/// ```
///
/// [`Role`]: ../model/struct.Role.html
/// [Manage Roles]: ../model/permissions/constant.MANAGE_ROLES.html
pub fn edit_role<F, G, R>(&self, guild_id: G, role_id: R, f: F)
-> Result<Role> where F: FnOnce(EditRole) -> EditRole,
G: Into<GuildId>,
R: Into<GuildId> {
let guild_id = guild_id.into();
let role_id = role_id.into();
let map = feature_cache! {{
let cache = CACHE.read().unwrap();
let role = if let Some(role) = {
cache.get_role(guild_id.0, role_id.0)
} {
role
} else {
return Err(Error::Client(ClientError::RecordNotFound));
};
f(EditRole::new(role)).0.build()
} else {
f(EditRole::default()).0.build()
}};
rest::edit_role(guild_id.0, role_id.0, map)
}
/// Edits a [`Message`] given its Id and the Id of the channel it belongs
/// to.
///
/// Pass an empty string (`""`) to `text` if you are editing a message with
/// an embed or file but no content. Otherwise, `text` must be given.
///
/// **Note**: Requires that the current user be the author of the message.
///
/// [`Message`]: ../model/struct.Message.html
pub fn edit_message<C, F, M>(&self, channel_id: C, message_id: M, text: &str, f: F)
-> Result<Message> where C: Into<ChannelId>,
F: FnOnce(CreateEmbed) -> CreateEmbed,
M: Into<MessageId> {
let mut map = ObjectBuilder::new()
.insert("content", text);
let embed = f(CreateEmbed::default()).0;
if embed.len() > 1 {
map = map.insert("embed", Value::Object(embed));
}
rest::edit_message(channel_id.into().0, message_id.into().0, map.build())
}
/// Edits the note that the current user has set for another user.
///
/// Use [`delete_note`] to remove a note.
///
/// **Note**: Requires that the current user be a user account.
///
/// # Examples
///
/// Set a note for a message's author:
///
/// ```rust,ignore
/// // assuming a `message` has been bound
/// let _ = context.edit_note(message.author, "test note");
/// ```
///
/// # Errors
///
/// Returns a [`ClientError::InvalidOperationAsBot`] if the current user is
/// a bot user.
///
/// [`ClientError::InvalidOperationAsBot`]: enum.ClientError.html#variant.InvalidOperationAsBot
/// [`delete_note`]: #method.delete_note
pub fn edit_note<U: Into<UserId>>(&self, user_id: U, note: &str)
-> Result<()> {
let map = ObjectBuilder::new()
.insert("note", note)
.build();
rest::edit_note(user_id.into().0, map)
}
/// Gets a list of the given [`Guild`]'s bans.
///
/// Requires the [Ban Members] permission.
///
/// [`Guild`]: ../model/struct.Guild.html
/// [Ban Members]: ../model/permissions/constant.BAN_MEMBERS.html
pub fn get_bans<G: Into<GuildId>>(&self, guild_id: G) -> Result<Vec<Ban>> {
rest::get_bans(guild_id.into().0)
}
/// Gets all of a [`GuildChannel`]'s invites.
///
/// Requires the [Manage Guild] permission.
///
/// [`GuildChannel`]: ../model/struct.GuildChannel.html
/// [Manage Guild]: ../model/permissions/constant.MANAGE_GUILD.html
pub fn get_channel_invites<C: Into<ChannelId>>(&self, channel_id: C)
-> Result<Vec<RichInvite>> {
rest::get_channel_invites(channel_id.into().0)
}
/// Gets a `Channel` by the given Id.
pub fn get_channel<C>(&self, channel_id: C) -> Result<Channel>
where C: Into<ChannelId> {
let channel_id = channel_id.into();
#[cfg(feature="cache")]
{
if let Some(channel) = CACHE.read().unwrap().get_channel(channel_id) {
return Ok(channel.clone_inner());
}
}
rest::get_channel(channel_id.0)
}
/// Gets all of a [`Guild`]'s channels with given Id.
///
/// [`Guild`]: ../model/struct.Guild.html
pub fn get_channels<G>(&self, guild_id: G)
-> Result<HashMap<ChannelId, GuildChannel>> where G: Into<GuildId> {
let guild_id = guild_id.into();
#[cfg(feature="cache")]
{
if let Some(guild) = CACHE.read().unwrap().get_guild(guild_id) {
return Ok(guild.channels.clone());
}
}
let mut channels = HashMap::new();
for channel in rest::get_channels(guild_id.0)? {
channels.insert(channel.id, channel);
}
Ok(channels)
}
/// Gets information about the current user.
///
/// Note this is shorthand for retrieving the current user through the
/// cache, and will perform a clone.
#[cfg(all(feature = "cache", feature = "methods"))]
pub fn get_current_user(&self) -> CurrentUser {
CACHE.read().unwrap().user.clone()
}
/// Gets an [`Guild`]'s emoji by Id.
///
/// Requires the [Manage Emojis] permission.
///
/// [`Guild`]: ../model/struct.Guild.html
/// [Manage Emojis]: ../model/permissions/constant.MANAGE_EMOJIS.html
pub fn get_emoji<E, G>(&self, guild_id: G, emoji_id: E) -> Result<Emoji>
where E: Into<EmojiId>, G: Into<GuildId> {
rest::get_emoji(guild_id.into().0, emoji_id.into().0)
}
/// Gets a list of all of a [`Guild`]'s emojis.
///
/// Requires the [Manage Emojis] permission.
///
/// [`Guild`]: ../model/struct.Guild.html
/// [Manage Emojis]: ../model/permissions/constant.MANAGE_EMOJIS.html
pub fn get_emojis<G: Into<GuildId>>(&self, guild_id: G)
-> Result<Vec<Emoji>> {
rest::get_emojis(guild_id.into().0)
}
/// Gets a partial amount of guild data by its Id.
///
/// Requires that the current user be in the guild.
pub fn get_guild<G: Into<GuildId>>(&self, guild_id: G)
-> Result<PartialGuild> {
rest::get_guild(guild_id.into().0)
}
/// Gets all of a guild's invites.
///
/// Requires the [Manage Guild] permission.
///
/// [`RichInvite`]: ../model/struct.RichInvite.html
/// [Manage Guild]: ../model/permissions/struct.MANAGE_GUILD.html
pub fn get_guild_invites<G>(&self, guild_id: G) -> Result<Vec<RichInvite>>
where G: Into<GuildId> {
rest::get_guild_invites(guild_id.into().0)
}
/// Gets the number of [`Member`]s that would be pruned with the given
/// number of days.
///
/// Requires the [Kick Members] permission.
///
/// [`Member`]: ../model/struct.Member.html
/// [Kick Members]: ../model/permissions/constant.KICK_MEMBERS.html
pub fn get_guild_prune_count<G>(&self, guild_id: G, days: u16)
-> Result<GuildPrune> where G: Into<GuildId> {
let map = ObjectBuilder::new()
.insert("days", days)
.build();
rest::get_guild_prune_count(guild_id.into().0, map)
}
/// Gets a paginated list of guilds that the current user is in.
///
/// The `limit` has a maximum value of 100.
///
/// See also: [`CurrentUser::guilds`].
///
/// # Examples
///
/// Get the first 10 guilds after the current [`Message`]'s guild's Id:
///
/// ```rust,ignore
/// use serenity::client::rest::GuildPagination;
///
/// // assuming you are in a context
///
/// let guild_id = message.guild_id.unwrap();
/// context.get_guilds(GuildPagination::After(guild_id, 10)).unwrap();
/// ```
///
/// [`CurrentUser::guilds`]: ../model/struct.CurrentUser.html#method.guilds
/// [`Message`]: ../model/struct.Message.html
pub fn get_guilds(&self, target: GuildPagination, limit: u8) -> Result<Vec<GuildInfo>> {
rest::get_guilds(target, limit as u64)
}
/// Gets all integrations of a guild via the given Id.
pub fn get_integrations<G: Into<GuildId>>(&self, guild_id: G)
-> Result<Vec<Integration>> {
rest::get_guild_integrations(guild_id.into().0)
}
/// Gets the information about an invite.
pub fn get_invite(&self, invite: &str) -> Result<Invite> {
let code = utils::parse_invite(invite);
rest::get_invite(code)
}
/// Gets a user's [`Member`] instance for a [`Guild`], given by Id.
///
/// If the `cache` feature is enabled, then the instance will be cloned from
/// the cache if it exists.
///
/// [`Guild`]: ../model/struct.Guild.html
/// [`Member`]: ../model/struct.Member.html
pub fn get_member<G, U>(&self, guild_id: G, user_id: U) -> Result<Member>
where G: Into<GuildId>, U: Into<UserId> {
let guild_id = guild_id.into();
let user_id = user_id.into();
#[cfg(feature="cache")]
{
let cache = CACHE.read().unwrap();
if let Some(member) = cache.get_member(guild_id, user_id) {
return Ok(member.clone());
}
}
rest::get_member(guild_id.0, user_id.0)
}
/// Gets a list of a [`Guild`]'s members.
///
/// Optionally pass in the `limit` to limit the number of results. Maximum
/// value is 1000. Optionally pass in `after` to offset the results by a
/// [`User`]'s Id.
///
/// [`Guild`]: ../model/struct.Guild.html
/// [`User`]: ../model/struct.User.html
pub fn get_members<G, U>(&self, guild_id: G, limit: Option<u64>, after: Option<U>)
-> Result<Vec<Member>> where G: Into<GuildId>, U: Into<UserId> {
rest::get_guild_members(guild_id.into().0,
limit,
after.map(|x| x.into().0))
}
/// Gets a single [`Message`] from a [`Channel`].
///
/// Requires the [Read Message History] permission.
///
/// # Errors
///
/// Returns a [`ClientError::InvalidOperationAsUser`] if the current user is
/// not a user account.
///
/// [`Channel`]: ../model/struct.Channel.html
/// [`ClientError::InvalidOperationAsUser`]: ../enum.ClientError.html#variant.InvalidOperationAsUser
/// [`Message`]: ../model/struct.Message.html
/// [Read Message History]: ../model/permissions/constant.READ_MESSAGE_HISTORY.html
pub fn get_message<C, M>(&self, channel_id: C, message_id: M)
-> Result<Message> where C: Into<ChannelId>, M: Into<MessageId> {
if self.login_type == LoginType::User {
return Err(Error::Client(ClientError::InvalidOperationAsUser))
}
rest::get_message(channel_id.into().0, message_id.into().0)
}
/// Gets messages from a specific channel.
///
/// Requires the [Read Message History] permission.
///
/// # Examples
///
/// ```rust,ignore
/// let role = context.get_messages(channel_id, |g| g
/// .before(20)
/// .after(100)); // Maximum is 100.
/// ```
///
/// [Read Message History]: ../model/permission/constant.READ_MESSAGE_HISTORY.html
pub fn get_messages<C, F>(&self, channel_id: C, f: F) -> Result<Vec<Message>>
where C: Into<ChannelId>, F: FnOnce(GetMessages) -> GetMessages {
let mut map = f(GetMessages::default()).0;
let mut query = format!("?limit={}", map.remove("limit").unwrap_or(50));
if let Some(after) = map.remove("after") {
write!(query, "&after={}", after)?;
}
if let Some(around) = map.remove("around") {
write!(query, "&around={}", around)?;
}
if let Some(before) = map.remove("before") {
write!(query, "&before={}", before)?;
}
rest::get_messages(channel_id.into().0, &query)
}
/// Gets the list of [`User`]s who have reacted to a [`Message`] with a
/// certain [`Emoji`].
///
/// The default `limit` is `50` - specify otherwise to receive a different
/// maximum number of users. The maximum that may be retrieve at a time is
/// `100`, if a greater number is provided then it is automatically reduced.
///
/// The optional `after` attribute is to retrieve the users after a certain
/// user. This is useful for pagination.
///
/// **Note**: Requires the [Read Message History] permission.
///
/// [`Emoji`]: struct.Emoji.html
/// [`Message`]: struct.Message.html
/// [`User`]: struct.User.html
/// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html
pub fn get_reaction_users<C, M, R, U>(&self,
channel_id: C,
message_id: M,
reaction_type: R,
limit: Option<u8>,
after: Option<U>)
-> Result<Vec<User>>
where C: Into<ChannelId>,
M: Into<MessageId>,
R: Into<ReactionType>,
U: Into<UserId> {
let limit = limit.map_or(50, |x| if x > 100 { 100 } else { x });
rest::get_reaction_users(channel_id.into().0,
message_id.into().0,
reaction_type.into(),
limit,
after.map(|u| u.into().0))
}
/// Gets a [`User`] by its Id.
///
/// [`User`]: ../model/struct.User.html
///
/// # Errors
///
/// Returns a [`ClientError::InvalidOperationAsUser`] if the current user is
/// not a bot user.
///
/// [`ClientError::InvalidOperationAsUser`]: enum.ClientError.html#variant.InvalidOperationAsUser
#[inline]
pub fn get_user<U: Into<UserId>>(&self, user_id: U) -> Result<User> {
#[cfg(feature="cache")]
{
if !CACHE.read().unwrap().user.bot {
return Err(Error::Client(ClientError::InvalidOperationAsUser));
}
}
rest::get_user(user_id.into().0)
}
/// Kicks a [`Member`] from the specified [`Guild`] if they are in it.
///
/// Requires the [Kick Members] permission.
///
/// [`Guild`]: ../model/struct.Guild.html
/// [`Member`]: ../model/struct.Member.html
/// [Kick Members]: ../model/permissions/constant.KICK_MEMBERS.html
pub fn kick_member<G, U>(&self, guild_id: G, user_id: U) -> Result<()>
where G: Into<GuildId>, U: Into<UserId> {
rest::kick_member(guild_id.into().0, user_id.into().0)
}
/// Leaves a [`Guild`] by its Id.
///
/// [`Guild`]: ../model/struct.Guild.html
pub fn leave_guild<G: Into<GuildId>>(&self, guild_id: G)
-> Result<PartialGuild> {
rest::leave_guild(guild_id.into().0)
}
/// Moves a member to a specific voice channel.
///
/// Requires the [Move Members] permission.
///
/// [Move Members]: ../model/permissions/constant.MOVE_MEMBERS.html
pub fn move_member<C, G, U>(&self, guild_id: G, user_id: U, channel_id: C)
-> Result<()> where C: Into<ChannelId>,
G: Into<GuildId>,
U: Into<UserId> {
let map = ObjectBuilder::new()
.insert("channel_id", channel_id.into().0)
.build();
rest::edit_member(guild_id.into().0, user_id.into().0, map)
}
/// Gets the list of [`Message`]s which are pinned to the specified
/// [`Channel`].
///
/// [`Channel`]: ../model/enum.Channel.html
/// [`Message`]: ../model/struct.Message.html
pub fn get_pins<C>(&self, channel_id: C) -> Result<Vec<Message>>
where C: Into<ChannelId> {
rest::get_pins(channel_id.into().0)
}
/// Pins a [`Message`] in the specified [`Channel`] by its Id.
///
/// Requires the [Manage Messages] permission.
///
/// [`Channel`]: ../model/enum.Channel.html
/// [`Message`]: ../model/struct.Message.html
///
/// [Manage Messages]: ../model/permissions/constant.MANAGE_MESSAGES.html
pub fn pin<C, M>(&self, channel_id: C, message_id: M) -> Result<()>
where C: Into<ChannelId>, M: Into<MessageId> {
rest::pin_message(channel_id.into().0, message_id.into().0)
}
/// Sends a message with just the given message content in the channel that
/// a message was received from.
///
/// # Supported Events
///
/// This will only work through the context of one of the following event
/// dispatches:
///
/// - [`ChannelCreate`][`Event::ChannelCreate`]
/// - [`ChannelPinsAck`][`Event::ChannelPinsAck`]
/// - [`ChannelPinsUpdate`][`Event::ChannelPinsUpdate`]
/// - [`ChannelRecipientAdd`][`Event::ChannelRecipientAdd`]
/// - [`ChannelRecipientRemove`][`Event::ChannelRecipientRemove`]
/// - [`ChannelUpdate`][`Event::ChannelUpdate`]
/// - [`MessageAck`][`Event::MessageAck`]
/// - [`MessageDelete`][`Event::MessageDelete`]
/// - [`MessageDeleteBulk`][`Event::MessageDeleteBulk`]
/// - [`MessageUpdate`][`Event::MessageUpdate`]
/// - [`ReactionAdd`][`Event::ReactionAdd`]
/// - [`ReactionRemove`][`Event::ReactionRemove`]
/// - [`ReactionRemoveAll`][`Event::ReactionRemoveAll`]
///
/// # Errors
///
/// Returns a [`ClientError::MessageTooLong`] if the content of the message
/// is over the above limit, containing the number of unicode code points
/// over the limit.
///
/// Returns a [`ClientError::NoChannelId`] when there is no [`ChannelId`]
/// directly available; i.e. when not under the context of one of the above
/// events.
///
/// [`ChannelId`]: ../../model/struct.ChannelId.html
/// [`ClientError::MessageTooLong`]: enum.ClientError.html#variant.MessageTooLong
/// [`ClientError::NoChannelId`]: ../enum.ClientError.html#NoChannelId
/// [`Event::ChannelCreate`]: ../model/event/enum.Event.html#variant.ChannelCreate
/// [`Event::ChannelPinsAck`]: ../model/event/enum.Event.html#variant.ChannelPinsAck
/// [`Event::ChannelPinsUpdate`]: ../model/event/enum.Event.html#variant.ChannelPinsUpdate
/// [`Event::ChannelRecipientAdd`]: ../model/event/enum.Event.html#variant.ChannelRecipientAdd
/// [`Event::ChannelRecipientRemove`]: ../model/event/enum.Event.html#variant.ChannelRecipientRemove
/// [`Event::ChannelUpdate`]: ../model/event/enum.Event.html#variant.ChannelUpdate
/// [`Event::MessageAck`]: ../model/event/enum.Event.html#variant.MessageAck
/// [`Event::MessageDelete`]: ../model/event/enum.Event.html#variant.MessageDelete
/// [`Event::MessageDeleteBulk`]: ../model/event/enum.Event.html#variant.MessageDeleteBulk
/// [`Event::MessageUpdate`]: ../model/event/enum.Event.html#variant.MessageUpdate
/// [`Event::ReactionAdd`]: ../model/event/enum.Event.html#variant.ReactionAdd
/// [`Event::ReactionRemove`]: ../model/event/enum.Event.html#variant.ReactionRemove
/// [`Event::ReactionRemoveAll`]: ../model/event/enum.Event.html#variant.ReactionRemoveAll
/// [`Message`]: ../model/struct.Message.html
pub fn say(&self, content: &str) -> Result<Message> {
if let Some(channel_id) = self.channel_id {
self.send_message(channel_id, |m| m.content(content))
} else {
Err(Error::Client(ClientError::NoChannelId))
}
}
/// Adds a string to message queue, which is sent joined by a newline
/// when context goes out of scope.
///
/// **Note**: Only works in a context where a channel is present. Refer to
/// [`say`] for a list of events where this is applicable.
///
/// [`say`]: #method.say
pub fn queue(&mut self, content: &str) -> &mut Self {
self.queue.push('\n');
self.queue.push_str(content);
self
}
/// Searches a [`Channel`]'s messages by providing query parameters via the
/// search builder.
///
/// Refer to the documentation for the [`Search`] builder for restrictions
/// and defaults parameters, as well as potentially advanced usage.
///
/// **Note**: Bot users can not search.
///
/// # Examples
///
/// Refer to the [`Search`] builder's documentation for examples,
/// specifically the section on [searching a channel][search channel].
///
/// # Errors
///
/// If the `cache` is enabled, returns a
/// [`ClientError::InvalidOperationAsBot`] if the current user is a bot.
///
/// [`ClientError::InvalidOperationAsBot`]: ../client/enum.ClientError.html#variant.InvalidOperationAsBot
/// [`Channel`]: ../model/enum.Channel.html
/// [`Search`]: ../utils/builder/struct.Search.html
/// [search channel]: ../../utils/builder/struct.Search.html#searching-a-channel
pub fn search_channel<C, F>(&self, channel_id: C, f: F)
-> Result<SearchResult> where C: Into<ChannelId>,
F: FnOnce(Search) -> Search {
#[cfg(feature="cache")]
{
if CACHE.read().unwrap().user.bot {
return Err(Error::Client(ClientError::InvalidOperationAsBot));
}
}
let map = f(Search::default()).0;
rest::search_channel_messages(channel_id.into().0, map)
}
/// Searches a [`Guild`]'s messages by providing query parameters via the
/// search builder, with the ability to narrow down channels to search.
///
/// Refer to the documentation for the [`Search`] builder for restrictions
/// and default parameters, as well as potentially advanced usage.
///
/// **Note**: Bot users can not search.
///
/// # Examples
///
/// Refer to the [`Search`] builder's documentation for more examples,
/// specifically the section on
/// [searching a guild's channels][search guild].
///
/// # Errors
///
/// If the `cache` is enabled, returns a
/// [`ClientError::InvalidOperationAsBot`] if the current user is a bot.
///
/// [`ClientError::InvalidOperationAsBot`]: ../client/enum.ClientError.html#variant.InvalidOperationAsBot
/// [`Guild`]: ../model/struct.Guild.html
/// [`Search`]: ../utils/builder/struct.Search.html
/// [search guild]: ../../utils/builder/struct.Search.html#searching-a-guilds-channels
pub fn search_guild<F, G>(&self,
guild_id: G,
channel_ids: Vec<ChannelId>,
f: F)
-> Result<SearchResult>
where F: FnOnce(Search) -> Search,
G: Into<GuildId> {
#[cfg(feature="cache")]
{
if CACHE.read().unwrap().user.bot {
return Err(Error::Client(ClientError::InvalidOperationAsBot));
}
}
let map = f(Search::default()).0;
let ids = channel_ids.iter().map(|ch| ch.0).collect::<Vec<u64>>();
rest::search_guild_messages(guild_id.into().0, &ids, map)
}
/// Sends a file along with optional message contents. The filename _must_
/// be specified.
///
/// Message contents may be passed by using the [`CreateMessage::content`]
/// method.
///
/// An embed can _not_ be sent when sending a file. If you set one, it will
/// be automatically removed.
///
/// Requires the [Attach Files] and [Send Messages] permissions are required.
///
/// **Note**: Message contents must be under 2000 unicode code points.
///
/// # Errors
///
/// If the content of the message is over the above limit, then a
/// [`ClientError::MessageTooLong`] will be returned, containing the number
/// of unicode code points over the limit.
///
/// [`ClientError::MessageTooLong`]: enum.ClientError.html#variant.MessageTooLong
/// [`CreateMessage::content`]: ../utils/builder/struct.CreateMessage.html#method.content
/// [`GuildChannel`]: ../model/struct.GuildChannel.html
/// [Attach Files]: ../model/permissions/constant.ATTACH_FILES.html
/// [Send Messages]: ../model/permissions/constant.SEND_MESSAGES.html
pub fn send_file<C, F, R>(&self, channel_id: C, file: R, filename: &str, f: F)
-> Result<Message> where C: Into<ChannelId>,
F: FnOnce(CreateMessage) -> CreateMessage,
R: Read {
let mut map = f(CreateMessage::default()).0;
if let Some(content) = map.get("content") {
if let Value::String(ref content) = *content {
if let Some(length_over) = Message::overflow_length(content) {
return Err(Error::Client(ClientError::MessageTooLong(length_over)));
}
}
}
let _ = map.remove("embed");
rest::send_file(channel_id.into().0, file, filename, map)
}
/// Sends a message to a [`Channel`].
///
/// Refer to the documentation for [`CreateMessage`] for more information
/// regarding message restrictions and requirements.
///
/// **Note**: Message contents must be under 2000 unicode code points.
///
/// Requires the [Send Messages] permission is required.
///
/// # Example
///
/// Send a message with just the content `test`:
///
/// ```rust,ignore
/// // assuming you are in a context
/// let _ = context.send_message(message.channel_id, |f| f.content("test"));
/// ```
///
/// Send a message on `!ping` with a very descriptive [`Embed`]. This sends
/// a message content of `"Pong! Here's some info"`, with an embed with the
/// following attributes:
///
/// - Dark gold in colour;
/// - A description of `"Information about the message just posted"`;
/// - A title of `"Message Information"`;
/// - A URL of `"https://rust-lang.org"`;
/// - An [author structure] containing an icon and the user's name;
/// - An inline [field structure] containing the message's content with a
/// label;
/// - An inline field containing the channel's name with a label;
/// - A footer containing the current user's icon and name, saying that the
/// information was generated by them.
///
/// ```rust,ignore
/// use serenity::client::{CACHE, Client, Context};
/// use serenity::model::{Channel, Message};
/// use serenity::utils::Colour;
/// use std::env;
///
/// let mut client = Client::login_bot(&env::var("DISCORD_TOKEN").unwrap());
/// client.with_framework(|f| f
/// .configure(|c| c.prefix("~"))
/// .on("ping", ping));
///
/// client.on_ready(|_context, ready| {
/// println!("{} is connected!", ready.user.name);
/// });
///
/// let _ = client.start();
///
/// command!(ping(context, message) {
/// let cache = CACHE.read().unwrap();
/// let channel = cache.get_guild_channel(message.channel_id);
///
/// let _ = context.send_message(message.channel_id, |m| m
/// .content("Pong! Here's some info")
/// .embed(|e| e
/// .colour(Colour::dark_gold())
/// .description("Information about the message just posted")
/// .title("Message information")
/// .url("https://rust-lang.org")
/// .author(|mut a| {
/// a = a.name(&message.author.name);
///
/// if let Some(avatar) = message.author.avatar_url() {
/// a = a.icon_url(&avatar);
/// }
///
/// a
/// })
/// .field(|f| f
/// .inline(true)
/// .name("Message content:")
/// .value(&message.content))
/// .field(|f| f
/// .inline(true)
/// .name("Channel name:")
/// .value(&channel.map_or_else(|| "Unknown", |c| &c.name)))
/// .footer(|mut f| {
/// f = f.text(&format!("Generated by {}", cache.user.name));
///
/// if let Some(avatar) = cache.user.avatar_url() {
/// f = f.icon_url(&avatar);
/// }
///
/// f
/// })));
///
/// Ok(())
/// });
/// ```
///
/// Note that for most use cases, your embed layout will _not_ be this ugly.
/// This is an example of a very involved and conditional embed.
///
/// # Errors
///
/// Returns a [`ClientError::MessageTooLong`] if the content of the message
/// is over the above limit, containing the number of unicode code points
/// over the limit.
///
/// [`Channel`]: ../model/enum.Channel.html
/// [`ClientError::MessageTooLong`]: enum.ClientError.html#variant.MessageTooLong
/// [`CreateMessage`]: ../utils/builder/struct.CreateMessage.html
/// [`Embed`]: ../model/struct.Embed.html
/// [`GuildChannel`]: ../model/struct.GuildChannel.html
/// [Send Messages]: ../model/permissions/constant.SEND_MESSAGES.html
/// [author structure]: ../utils/builder/struct.CreateEmbedAuthor.html
/// [field structure]: ../utils/builder/struct.CreateEmbedField.html
pub fn send_message<C, F>(&self, channel_id: C, f: F) -> Result<Message>
where C: Into<ChannelId>, F: FnOnce(CreateMessage) -> CreateMessage {
let map = f(CreateMessage::default()).0;
if let Some(content) = map.get(&"content".to_owned()) {
if let Value::String(ref content) = *content {
if let Some(length_over) = Message::overflow_length(content) {
return Err(Error::Client(ClientError::MessageTooLong(length_over)));
}
}
}
rest::send_message(channel_id.into().0, Value::Object(map))
}
/// Sets the current user as being [`Online`]. This maintains the current
/// game and `afk` setting.
///
/// [`Online`]: ../model/enum.OnlineStatus.html#variant.Online
pub fn online(&self) {
self.shard.lock().unwrap().set_status(OnlineStatus::Online);
}
/// Sets the current user as being [`Idle`]. This maintains the current
/// game and `afk` setting.
///
/// [`Idle`]: ../model/enum.OnlineStatus.html#variant.Idle
pub fn idle(&self) {
self.shard.lock().unwrap().set_status(OnlineStatus::Idle);
}
/// Sets the current user as being [`DoNotDisturb`]. This maintains the
/// current game and `afk` setting.
///
/// [`DoNotDisturb`]: ../model/enum.OnlineStatus.html#variant.DoNotDisturb
pub fn dnd(&self) {
self.shard.lock().unwrap().set_status(OnlineStatus::DoNotDisturb);
}
/// Sets the current user as being [`Invisible`]. This maintains the current
/// game and `afk` setting.
///
/// [`Invisible`]: ../model/enum.OnlineStatus.html#variant.Invisible
pub fn invisible(&self) {
self.shard.lock().unwrap().set_status(OnlineStatus::Invisible);
}
/// "Resets" the current user's presence, by setting the game to `None`,
/// the online status to [`Online`], and `afk` to `false`.
///
/// Use [`set_presence`] for fine-grained control over individual details.
///
/// [`Online`]: ../model/enum.OnlineStatus.html#variant.Online
/// [`set_presence`]: #method.set_presence
pub fn reset_presence(&self) {
self.shard.lock()
.unwrap()
.set_presence(None, OnlineStatus::Online, false)
}
/// Sets the current game, defaulting to an online status of [`Online`], and
/// setting `afk` to `false`.
///
/// # Examples
///
/// Set the current user as playing "Heroes of the Storm":
///
/// ```rust,ignore
/// use serenity::model::Game;
///
/// // assuming you are in a context
///
/// context.set_game(Game::playing("Heroes of the Storm"));
/// ```
///
/// [`Online`]: ../model/enum.OnlineStatus.html#variant.Online
pub fn set_game(&self, game: Game) {
self.shard.lock()
.unwrap()
.set_presence(Some(game), OnlineStatus::Online, false);
}
/// Sets the current game, passing in only its name. This will automatically
/// set the current user's [`OnlineStatus`] to [`Online`], and its
/// [`GameType`] as [`Playing`].
///
/// Use [`reset_presence`] to clear the current game, or [`set_presence`]
/// for more fine-grained control.
///
/// **Note**: Maximum length is 128.
///
/// [`GameType`]: ../model/enum.GameType.html
/// [`Online`]: ../model/enum.OnlineStatus.html#variant.Online
/// [`OnlineStatus`]: ../model/enum.OnlineStatus.html
/// [`Playing`]: ../model/enum.GameType.html#variant.Playing
/// [`reset_presence`]: #method.reset_presence
/// [`set_presence`]: #method.set_presence
pub fn set_game_name(&self, game_name: &str) {
let game = Game {
kind: GameType::Playing,
name: game_name.to_owned(),
url: None,
};
self.shard.lock()
.unwrap()
.set_presence(Some(game), OnlineStatus::Online, false);
}
/// Sets the current user's presence, providing all fields to be passed.
///
/// # Examples
///
/// Setting the current user as having no game, being [`Idle`],
/// and setting `afk` to `true`:
///
/// ```rust,ignore
/// use serenity::model::OnlineStatus;
///
/// // assuming you are in a context
///
/// context.set_game(None, OnlineStatus::Idle, true);
/// ```
///
/// Setting the current user as playing "Heroes of the Storm", being
/// [`DoNotDisturb`], and setting `afk` to `false`:
///
/// ```rust,ignore
/// use serenity::model::{Game, OnlineStatus};
///
/// // assuming you are in a context
///
/// let game = Game::playing("Heroes of the Storm");
/// let status = OnlineStatus::DoNotDisturb;
///
/// context.set_game(Some(game), status, false);
/// ```
///
/// [`DoNotDisturb`]: ../model/enum.OnlineStatus.html#variant.DoNotDisturb
/// [`Idle`]: ../model/enum.OnlineStatus.html#variant.Idle
pub fn set_presence(&self,
game: Option<Game>,
status: OnlineStatus,
afk: bool) {
self.shard.lock()
.unwrap()
.set_presence(game, status, afk)
}
/// Deletes an undefined amount of members from the given guild
/// based on the amount of days they've been offline for.
///
/// **Note**: This will trigger [`GuildMemberRemove`] events.
///
/// **Note**: Requires the [Kick Members] permission.
///
/// [`GuildMemberRemove`]: ../model/event/enum.Event.html#variant.GuildMemberRemove
/// [Kick Members]: ../model/permissions/constant.KICK_MEMBERS.html
pub fn start_guild_prune<G>(&self, guild_id: G, days: u16)
-> Result<GuildPrune> where G: Into<GuildId> {
let map = ObjectBuilder::new()
.insert("days", days)
.build();
rest::start_guild_prune(guild_id.into().0, map)
}
/// Starts integration synchronization by the given integration Id.
///
/// Requires the [Manage Guild] permission.
///
/// [Manage Guild]: ../model/permissions/constant.MANAGE_GUILD.html
pub fn start_integration_sync<G, I>(&self, guild_id: G, integration_id: I)
-> Result<()> where G: Into<GuildId>, I: Into<IntegrationId> {
rest::start_integration_sync(guild_id.into().0, integration_id.into().0)
}
/// Unbans a [`User`] from a [`Guild`].
///
/// Requires the [Ban Members] permission.
///
/// [`Guild`]: ../model/struct.Guild.html
/// [`User`]: ../model/struct.User.html
/// [Ban Members]: ../model/permissions/constant.BAN_MEMBERS.html
pub fn unban<G, U>(&self, guild_id: G, user_id: U) -> Result<()>
where G: Into<GuildId>, U: Into<UserId> {
rest::remove_ban(guild_id.into().0, user_id.into().0)
}
/// Unpins a [`Message`] in the specified [`Channel`] given each Id.
///
/// Requires the [Manage Messages] permission.
///
/// [`Channel`]: ../model/enum.Channel.html
/// [`Message`]: ../model/struct.Message.html
///
/// [Manage Messages]: ../model/permissions/constant.MANAGE_MESSAGES.html
pub fn unpin<C, M>(&self, channel_id: C, message_id: M) -> Result<()>
where C: Into<ChannelId>, M: Into<MessageId> {
rest::unpin_message(channel_id.into().0, message_id.into().0)
}
}
impl Drop for Context {
/// Combines and sends all queued messages.
fn drop(&mut self) {
if !self.queue.is_empty() {
let _ = self.say(&self.queue);
}
}
}
/// Allows the `<<=` operator to be used to queue messages.
#[cfg(feature="extras")]
impl<'a> ShlAssign<&'a str> for &'a mut Context {
fn shl_assign(&mut self, rhs: &str) {
self.queue(rhs);
}
}
|