-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
DiscordRestClient.cs
2333 lines (2088 loc) · 110 KB
/
DiscordRestClient.cs
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using DSharpPlus.Net.Abstractions;
using DSharpPlus.Net.Models;
namespace DSharpPlus;
public class DiscordRestClient : BaseDiscordClient
{
/// <summary>
/// Gets the dictionary of guilds cached by this client.
/// </summary>
public override IReadOnlyDictionary<ulong, DiscordGuild> Guilds
=> this.guilds;
internal Dictionary<ulong, DiscordGuild> guilds = [];
private bool disposedValue;
public DiscordRestClient(DiscordConfiguration config) : base(config) { }
/// <summary>
/// Initializes cache
/// </summary>
/// <returns></returns>
public async Task InitializeCacheAsync()
{
await base.InitializeAsync();
IReadOnlyList<DiscordGuild> currentUserGuilds = await this.ApiClient.GetCurrentUserGuildsAsync();
foreach (DiscordGuild guild in currentUserGuilds)
{
this.guilds[guild.Id] = guild;
}
}
#region Scheduled Guild Events
/// <summary>
/// Creates a new scheduled guild event.
/// </summary>
/// <param name="guildId">The guild to create an event on.</param>
/// <param name="name">The name of the event, up to 100 characters.</param>
/// <param name="description">The description of the event, up to 1000 characters.</param>
/// <param name="channelId">The channel the event will take place in, if applicable.</param>
/// <param name="type">The type of event. If <see cref="DiscordScheduledGuildEventType.External"/>, a end time must be specified.</param>
/// <param name="image">The image of event.</param>
/// <param name="privacyLevel">The privacy level of the event.</param>
/// <param name="start">When the event starts. Must be in the future and before the end date, if specified.</param>
/// <param name="end">When the event ends. Required for <see cref="DiscordScheduledGuildEventType.External"/></param>
/// <param name="location">Where this location takes place.</param>
/// <returns>The created event.</returns>
public async Task<DiscordScheduledGuildEvent> CreateScheduledGuildEventAsync(ulong guildId, string name, string description, ulong? channelId, DiscordScheduledGuildEventType type, DiscordScheduledGuildEventPrivacyLevel privacyLevel, DateTimeOffset start, DateTimeOffset? end, Stream? image = null, string location = null)
=> await this.ApiClient.CreateScheduledGuildEventAsync(guildId, name, description, start, type, privacyLevel, new DiscordScheduledGuildEventMetadata(location), end, channelId, image);
/// <summary>
/// Delete a scheduled guild event.
/// </summary>
/// <param name="guildId">The ID the guild the event resides on.</param>
/// <param name="eventId">The ID of the event to delete.</param>
public async Task DeleteScheduledGuildEventAsync(ulong guildId, ulong eventId)
=> await this.ApiClient.DeleteScheduledGuildEventAsync(guildId, eventId);
/// <summary>
/// Gets a specific scheduled guild event.
/// </summary>
/// <param name="guildId">The ID of the guild the event resides on.</param>
/// <param name="eventId">The ID of the event to get</param>
/// <returns>The requested event.</returns>
public async Task<DiscordScheduledGuildEvent> GetScheduledGuildEventAsync(ulong guildId, ulong eventId)
=> await this.ApiClient.GetScheduledGuildEventAsync(guildId, eventId);
/// <summary>
/// Gets all available scheduled guild events.
/// </summary>
/// <param name="guildId">The ID of the guild to query.</param>
/// <returns>All active and scheduled events.</returns>
public async Task<IReadOnlyList<DiscordScheduledGuildEvent>> GetScheduledGuildEventsAsync(ulong guildId)
=> await this.ApiClient.GetScheduledGuildEventsAsync(guildId);
/// <summary>
/// Modify a scheduled guild event.
/// </summary>
/// <param name="guildId">The ID of the guild the event resides on.</param>
/// <param name="eventId">The ID of the event to modify.</param>
/// <param name="mdl">The action to apply to the event.</param>
/// <returns>The modified event.</returns>
public async Task<DiscordScheduledGuildEvent> ModifyScheduledGuildEventAsync(ulong guildId, ulong eventId, Action<ScheduledGuildEventEditModel> mdl)
{
ScheduledGuildEventEditModel model = new();
mdl(model);
if (model.Type.HasValue && model.Type.Value is DiscordScheduledGuildEventType.StageInstance or DiscordScheduledGuildEventType.VoiceChannel)
{
if (!model.Channel.HasValue)
{
throw new ArgumentException("Channel must be supplied if the event is a stage instance or voice channel event.");
}
}
if (model.Type.HasValue && model.Type.Value is DiscordScheduledGuildEventType.External)
{
if (!model.EndTime.HasValue)
{
throw new ArgumentException("End must be supplied if the event is an external event.");
}
if (!model.Metadata.HasValue || string.IsNullOrEmpty(model.Metadata.Value.Location))
{
throw new ArgumentException("Location must be supplied if the event is an external event.");
}
if (model.Channel.HasValue && model.Channel.Value is not null)
{
throw new ArgumentException("Channel must not be supplied if the event is an external event.");
}
}
// We only have an ID to work off of, so we have no validation as to the current state of the event.
return model.Status.HasValue && model.Status.Value is DiscordScheduledGuildEventStatus.Scheduled
? throw new ArgumentException("Status cannot be set to scheduled.")
: await this.ApiClient.ModifyScheduledGuildEventAsync(
guildId, eventId,
model.Name, model.Description,
model.Channel.IfPresent(c => c?.Id),
model.StartTime, model.EndTime,
model.Type, model.PrivacyLevel,
model.Metadata, model.Status);
}
/// <summary>
/// Gets the users interested in the guild event.
/// </summary>
/// <param name="guildId">The ID of the guild the event resides on.</param>
/// <param name="eventId">The ID of the event.</param>
/// <param name="limit">How many users to query.</param>
/// <param name="after">Fetch users after this ID.</param>
/// <param name="before">Fetch users before this ID.</param>
/// <returns>The users interested in the event.</returns>
public async Task<IReadOnlyList<DiscordUser>> GetScheduledGuildEventUsersAsync(ulong guildId, ulong eventId, int limit = 100, ulong? after = null, ulong? before = null)
{
int remaining = limit;
ulong? last = null;
bool isAfter = after is not null;
List<DiscordUser> users = [];
int lastCount;
do
{
int fetchSize = remaining > 100 ? 100 : remaining;
IReadOnlyList<DiscordUser> fetch = await this.ApiClient.GetScheduledGuildEventUsersAsync(guildId, eventId, true, fetchSize, !isAfter ? last ?? before : null, isAfter ? last ?? after : null);
lastCount = fetch.Count;
remaining -= lastCount;
if (!isAfter)
{
users.AddRange(fetch);
last = fetch.LastOrDefault()?.Id;
}
else
{
users.InsertRange(0, fetch);
last = fetch.FirstOrDefault()?.Id;
}
}
while (remaining > 0 && lastCount > 0);
return users.AsReadOnly();
}
#endregion
#region Guild
/// <summary>
/// Searches the given guild for members who's display name start with the specified name.
/// </summary>
/// <param name="guildId">The ID of the guild to search.</param>
/// <param name="name">The name to search for.</param>
/// <param name="limit">The maximum amount of members to return. Max 1000. Defaults to 1.</param>
/// <returns>The members found, if any.</returns>
public async Task<IReadOnlyList<DiscordMember>> SearchMembersAsync(ulong guildId, string name, int? limit = 1)
=> await this.ApiClient.SearchMembersAsync(guildId, name, limit);
/// <summary>
/// Creates a new guild
/// </summary>
/// <param name="name">New guild's name</param>
/// <param name="regionId">New guild's region ID</param>
/// <param name="iconb64">New guild's icon (base64)</param>
/// <param name="verificationLevel">New guild's verification level</param>
/// <param name="defaultMessageNotifications">New guild's default message notification level</param>
/// <param name="systemChannelFlags">New guild's system channel flags</param>
/// <returns></returns>
public async Task<DiscordGuild> CreateGuildAsync(string name, string regionId, string iconb64, DiscordVerificationLevel? verificationLevel, DiscordDefaultMessageNotifications? defaultMessageNotifications, DiscordSystemChannelFlags? systemChannelFlags)
=> await this.ApiClient.CreateGuildAsync(name, regionId, iconb64, verificationLevel, defaultMessageNotifications, systemChannelFlags);
/// <summary>
/// Creates a guild from a template. This requires the bot to be in less than 10 guilds total.
/// </summary>
/// <param name="code">The template code.</param>
/// <param name="name">Name of the guild.</param>
/// <param name="icon">Stream containing the icon for the guild.</param>
/// <returns>The created guild.</returns>
public async Task<DiscordGuild> CreateGuildFromTemplateAsync(string code, string name, string icon)
=> await this.ApiClient.CreateGuildFromTemplateAsync(code, name, icon);
/// <summary>
/// Deletes a guild
/// </summary>
/// <param name="id">Guild ID</param>
/// <returns></returns>
public async Task DeleteGuildAsync(ulong id)
=> await this.ApiClient.DeleteGuildAsync(id);
/// <summary>
/// Modifies a guild
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="name">New guild Name</param>
/// <param name="region">New guild voice region</param>
/// <param name="verificationLevel">New guild verification level</param>
/// <param name="defaultMessageNotifications">New guild default message notification level</param>
/// <param name="mfaLevel">New guild MFA level</param>
/// <param name="explicitContentFilter">New guild explicit content filter level</param>
/// <param name="afkChannelId">New guild AFK channel ID</param>
/// <param name="afkTimeout">New guild AFK timeout in seconds</param>
/// <param name="iconb64">New guild icon (base64)</param>
/// <param name="ownerId">New guild owner ID</param>
/// <param name="splashb64">New guild splash (base64)</param>
/// <param name="systemChannelId">New guild system channel ID</param>
/// <param name="banner">New guild banner</param>
/// <param name="description">New guild description</param>
/// <param name="discorverySplash">New guild Discovery splash</param>
/// <param name="features">List of new <see href="https://discord.com/developers/docs/resources/guild#guild-object-guild-features">guild features</see></param>
/// <param name="preferredLocale">New preferred locale</param>
/// <param name="publicUpdatesChannelId">New updates channel ID</param>
/// <param name="rulesChannelId">New rules channel ID</param>
/// <param name="systemChannelFlags">New system channel flags</param>
/// <param name="reason">Modify reason</param>
/// <returns></returns>
public async Task<DiscordGuild> ModifyGuildAsync(ulong guildId, Optional<string> name,
Optional<string> region, Optional<DiscordVerificationLevel> verificationLevel,
Optional<DiscordDefaultMessageNotifications> defaultMessageNotifications, Optional<DiscordMfaLevel> mfaLevel,
Optional<DiscordExplicitContentFilter> explicitContentFilter, Optional<ulong?> afkChannelId,
Optional<int> afkTimeout, Optional<string> iconb64, Optional<ulong> ownerId, Optional<string> splashb64,
Optional<ulong?> systemChannelId, Optional<string> banner, Optional<string> description,
Optional<string> discorverySplash, Optional<IEnumerable<string>> features, Optional<string> preferredLocale,
Optional<ulong?> publicUpdatesChannelId, Optional<ulong?> rulesChannelId, Optional<DiscordSystemChannelFlags> systemChannelFlags,
string reason)
=> await this.ApiClient.ModifyGuildAsync(guildId, name, region, verificationLevel, defaultMessageNotifications, mfaLevel, explicitContentFilter, afkChannelId, afkTimeout, iconb64,
ownerId, splashb64, systemChannelId, banner, description, discorverySplash, features, preferredLocale, publicUpdatesChannelId, rulesChannelId, systemChannelFlags, reason);
/// <summary>
/// Modifies a guild
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="action">Guild modifications</param>
/// <returns></returns>
public async Task<DiscordGuild> ModifyGuildAsync(ulong guildId, Action<GuildEditModel> action)
{
GuildEditModel mdl = new();
action(mdl);
if (mdl.AfkChannel.HasValue)
{
if (mdl.AfkChannel.Value.Type != DiscordChannelType.Voice)
{
throw new ArgumentException("AFK channel needs to be a voice channel!");
}
}
Optional<string> iconb64 = Optional.FromNoValue<string>();
if (mdl.Icon.HasValue && mdl.Icon.Value is not null)
{
using ImageTool imgtool = new(mdl.Icon.Value);
iconb64 = imgtool.GetBase64();
}
else if (mdl.Icon.HasValue)
{
iconb64 = null;
}
Optional<string> splashb64 = Optional.FromNoValue<string>();
if (mdl.Splash.HasValue && mdl.Splash.Value is not null)
{
using ImageTool imgtool = new(mdl.Splash.Value);
splashb64 = imgtool.GetBase64();
}
else if (mdl.Splash.HasValue)
{
splashb64 = null;
}
Optional<string> bannerb64 = Optional.FromNoValue<string>();
if (mdl.Banner.HasValue && mdl.Banner.Value is not null)
{
using ImageTool imgtool = new(mdl.Banner.Value);
bannerb64 = imgtool.GetBase64();
}
else if (mdl.Banner.HasValue)
{
bannerb64 = null;
}
return await this.ApiClient.ModifyGuildAsync(guildId, mdl.Name, mdl.Region.IfPresent(x => x.Id), mdl.VerificationLevel, mdl.DefaultMessageNotifications,
mdl.MfaLevel, mdl.ExplicitContentFilter, mdl.AfkChannel.IfPresent(x => x?.Id), mdl.AfkTimeout, iconb64, mdl.Owner.IfPresent(x => x.Id),
splashb64, mdl.SystemChannel.IfPresent(x => x?.Id), bannerb64, mdl.Description, mdl.DiscoverySplash, mdl.Features, mdl.PreferredLocale,
mdl.PublicUpdatesChannel.IfPresent(e => e?.Id), mdl.RulesChannel.IfPresent(e => e?.Id), mdl.SystemChannelFlags, mdl.AuditLogReason);
}
/// <summary>
/// Gets guild bans.
/// </summary>
/// <param name="guildId">The ID of the guild to get the bans from.</param>
/// <param name="limit">The number of users to return (up to maximum 1000, default 1000).</param>
/// <param name="before">Consider only users before the given user ID.</param>
/// <param name="after">Consider only users after the given user ID.</param>
/// <returns>A collection of the guild's bans.</returns>
public async Task<IReadOnlyList<DiscordBan>> GetGuildBansAsync(ulong guildId, int? limit = null, ulong? before = null, ulong? after = null)
=> await this.ApiClient.GetGuildBansAsync(guildId, limit, before, after);
/// <summary>
/// Gets the ban of the specified user. Requires Ban Members permission.
/// </summary>
/// <param name="guildId">The ID of the guild to get the ban from.</param>
/// <param name="userId">The ID of the user to get the ban for.</param>
/// <returns>A guild ban object.</returns>
public async Task<DiscordBan> GetGuildBanAsync(ulong guildId, ulong userId)
=> await this.ApiClient.GetGuildBanAsync(guildId, userId);
/// <summary>
/// Creates guild ban
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="userId">User ID</param>
/// <param name="deleteMessageDays">Days to delete messages</param>
/// <param name="reason">Reason why this member was banned</param>
/// <returns></returns>
public async Task CreateGuildBanAsync(ulong guildId, ulong userId, int deleteMessageDays, string reason)
=> await this.ApiClient.CreateGuildBanAsync(guildId, userId, deleteMessageDays, reason);
/// <summary>
/// Creates multiple guild bans
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="userIds">Collection of user ids to ban</param>
/// <param name="deleteMessageSeconds">Timespan in seconds to delete messages from the banned users</param>
/// <param name="reason">Auditlog reason</param>
/// <returns></returns>
public async Task<DiscordBulkBan> CreateGuildBansAsync(ulong guildId, IEnumerable<ulong> userIds, int deleteMessageSeconds, string reason)
=> await this.ApiClient.CreateGuildBulkBanAsync(guildId, userIds, deleteMessageSeconds, reason);
/// <summary>
/// Removes a guild ban
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="userId">User to unban</param>
/// <param name="reason">Reason why this member was unbanned</param>
/// <returns></returns>
public async Task RemoveGuildBanAsync(ulong guildId, ulong userId, string reason)
=> await this.ApiClient.RemoveGuildBanAsync(guildId, userId, reason);
/// <summary>
/// Leaves a guild
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <returns></returns>
public async Task LeaveGuildAsync(ulong guildId)
=> await this.ApiClient.LeaveGuildAsync(guildId);
/// <summary>
/// Adds a member to a guild
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="userId">User ID</param>
/// <param name="accessToken">Access token</param>
/// <param name="nick">User nickname</param>
/// <param name="roleIds">Ids of roles to add to the new member.</param>
/// <param name="muted">Whether this user should be muted on join</param>
/// <param name="deafened">Whether this user should be deafened on join</param>
/// <returns>Only returns the member if they were not already in the guild</returns>
public async Task<DiscordMember?> AddGuildMemberAsync(ulong guildId, ulong userId, string accessToken, string nick, IEnumerable<ulong> roleIds, bool muted, bool deafened)
=> await this.ApiClient.AddGuildMemberAsync(guildId, userId, accessToken, muted, deafened, nick, roleIds);
/// <summary>
/// Gets all guild members
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="limit">Member download limit</param>
/// <param name="after">Gets members after this ID</param>
/// <returns></returns>
public async Task<IReadOnlyList<DiscordMember>> ListGuildMembersAsync(ulong guildId, int? limit, ulong? after)
{
List<DiscordMember> recmbr = [];
int recd = limit ?? 1000;
int lim = limit ?? 1000;
ulong? last = after;
while (recd == lim)
{
IReadOnlyList<TransportMember> tms = await this.ApiClient.ListGuildMembersAsync(guildId, lim, last == 0 ? null : last);
recd = tms.Count;
foreach (TransportMember xtm in tms)
{
last = xtm.User.Id;
if (this.UserCache.ContainsKey(xtm.User.Id))
{
continue;
}
DiscordUser usr = new(xtm.User)
{
Discord = this
};
UpdateUserCache(usr);
}
recmbr.AddRange(tms.Select(xtm => new DiscordMember(xtm) { Discord = this, guild_id = guildId }));
}
return new ReadOnlyCollection<DiscordMember>(recmbr);
}
/// <summary>
/// Add role to guild member
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="userId">User ID</param>
/// <param name="roleId">Role ID</param>
/// <param name="reason">Reason this role gets added</param>
/// <returns></returns>
public async Task AddGuildMemberRoleAsync(ulong guildId, ulong userId, ulong roleId, string reason)
=> await this.ApiClient.AddGuildMemberRoleAsync(guildId, userId, roleId, reason);
/// <summary>
/// Remove role from member
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="userId">User ID</param>
/// <param name="roleId">Role ID</param>
/// <param name="reason">Reason this role gets removed</param>
/// <returns></returns>
public async Task RemoveGuildMemberRoleAsync(ulong guildId, ulong userId, ulong roleId, string reason)
=> await this.ApiClient.RemoveGuildMemberRoleAsync(guildId, userId, roleId, reason);
/// <summary>
/// Updates a role's position
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="roleId">Role ID</param>
/// <param name="reason">Reason this position was modified</param>
/// <returns></returns>
public async Task UpdateRolePositionAsync(ulong guildId, ulong roleId, string reason = null)
{
List<RestGuildRoleReorderPayload> rgrrps =
[
new()
{
RoleId = roleId
}
];
await this.ApiClient.ModifyGuildRolePositionsAsync(guildId, rgrrps, reason);
}
/// <summary>
/// Updates a channel's position
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="channelId">Channel ID</param>
/// <param name="position">Channel position</param>
/// <param name="reason">Reason this position was modified</param>
/// <param name="lockPermissions">Whether to sync channel permissions with the parent, if moving to a new category.</param>
/// <param name="parentId">The new parent id if the channel is to be moved to a new category.</param>
/// <returns></returns>
public async Task UpdateChannelPositionAsync(ulong guildId, ulong channelId, int position, string reason, bool? lockPermissions = null, ulong? parentId = null)
{
List<RestGuildChannelReorderPayload> rgcrps =
[
new()
{
ChannelId = channelId,
Position = position,
LockPermissions = lockPermissions,
ParentId = parentId
}
];
await this.ApiClient.ModifyGuildChannelPositionAsync(guildId, rgcrps, reason);
}
/// <summary>
/// Gets a guild's widget
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <returns></returns>
public async Task<DiscordWidget> GetGuildWidgetAsync(ulong guildId)
=> await this.ApiClient.GetGuildWidgetAsync(guildId);
/// <summary>
/// Gets a guild's widget settings
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <returns></returns>
public async Task<DiscordWidgetSettings> GetGuildWidgetSettingsAsync(ulong guildId)
=> await this.ApiClient.GetGuildWidgetSettingsAsync(guildId);
/// <summary>
/// Modifies a guild's widget settings
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="enabled">If the widget is enabled or not</param>
/// <param name="channelId">Widget channel ID</param>
/// <param name="reason">Reason the widget settings were modified</param>
/// <returns></returns>
public async Task<DiscordWidgetSettings> ModifyGuildWidgetSettingsAsync(ulong guildId, bool? enabled = null, ulong? channelId = null, string reason = null)
=> await this.ApiClient.ModifyGuildWidgetSettingsAsync(guildId, enabled, channelId, reason);
/// <summary>
/// Gets a guild's membership screening form.
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <returns>The guild's membership screening form.</returns>
public async Task<DiscordGuildMembershipScreening> GetGuildMembershipScreeningFormAsync(ulong guildId)
=> await this.ApiClient.GetGuildMembershipScreeningFormAsync(guildId);
/// <summary>
/// Modifies a guild's membership screening form.
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="action">Action to perform</param>
/// <returns>The modified screening form.</returns>
public async Task<DiscordGuildMembershipScreening> ModifyGuildMembershipScreeningFormAsync(ulong guildId, Action<MembershipScreeningEditModel> action)
{
MembershipScreeningEditModel mdl = new();
action(mdl);
return await this.ApiClient.ModifyGuildMembershipScreeningFormAsync(guildId, mdl.Enabled, mdl.Fields, mdl.Description);
}
/// <summary>
/// Gets a guild's vanity url
/// </summary>
/// <param name="guildId">The ID of the guild.</param>
/// <returns>The guild's vanity url.</returns>
public async Task<DiscordInvite> GetGuildVanityUrlAsync(ulong guildId)
=> await this.ApiClient.GetGuildVanityUrlAsync(guildId);
/// <summary>
/// Updates the current user's suppress state in a stage channel.
/// </summary>
/// <param name="guildId">The ID of the guild.</param>
/// <param name="channelId">The ID of the channel.</param>
/// <param name="suppress">Toggles the suppress state.</param>
/// <param name="requestToSpeakTimestamp">Sets the time the user requested to speak.</param>
public async Task UpdateCurrentUserVoiceStateAsync(ulong guildId, ulong channelId, bool? suppress, DateTimeOffset? requestToSpeakTimestamp = null)
=> await this.ApiClient.UpdateCurrentUserVoiceStateAsync(guildId, channelId, suppress, requestToSpeakTimestamp);
/// <summary>
/// Updates a member's suppress state in a stage channel.
/// </summary>
/// <param name="guildId">The ID of the guild.</param>
/// <param name="userId">The ID of the member.</param>
/// <param name="channelId">The ID of the stage channel.</param>
/// <param name="suppress">Toggles the member's suppress state.</param>
/// <returns></returns>
public async Task UpdateUserVoiceStateAsync(ulong guildId, ulong userId, ulong channelId, bool? suppress)
=> await this.ApiClient.UpdateUserVoiceStateAsync(guildId, userId, channelId, suppress);
#endregion
#region Channel
/// <summary>
/// Creates a guild channel
/// </summary>
/// <param name="id">Channel ID</param>
/// <param name="name">Channel name</param>
/// <param name="type">Channel type</param>
/// <param name="parent">Channel parent ID</param>
/// <param name="topic">Channel topic</param>
/// <param name="bitrate">Voice channel bitrate</param>
/// <param name="userLimit">Voice channel user limit</param>
/// <param name="overwrites">Channel overwrites</param>
/// <param name="nsfw">Whether this channel should be marked as NSFW</param>
/// <param name="perUserRateLimit">Slow mode timeout for users.</param>
/// <param name="qualityMode">Voice channel video quality mode.</param>
/// <param name="position">Sorting position of the channel.</param>
/// <param name="reason">Reason this channel was created</param>
/// <param name="defaultAutoArchiveDuration">Default duration for newly created forum posts in the channel.</param>
/// <param name="defaultReactionEmoji">Default emoji used for reacting to forum posts.</param>
/// <param name="availableTags">Tags available for use by forum posts in the channel.</param>
/// <param name="defaultSortOrder">Default sorting order for forum posts in the channel.</param>
/// <returns></returns>
public async Task<DiscordChannel> CreateGuildChannelAsync
(
ulong id,
string name,
DiscordChannelType type,
ulong? parent,
Optional<string> topic,
int? bitrate,
int? userLimit,
IEnumerable<DiscordOverwriteBuilder> overwrites,
bool? nsfw,
Optional<int?> perUserRateLimit,
DiscordVideoQualityMode? qualityMode,
int? position,
string reason,
DiscordAutoArchiveDuration? defaultAutoArchiveDuration = null,
DefaultReaction? defaultReactionEmoji = null,
IEnumerable<DiscordForumTagBuilder> availableTags = null,
DiscordDefaultSortOrder? defaultSortOrder = null
) => type is not (DiscordChannelType.Text or DiscordChannelType.Voice or DiscordChannelType.Category or DiscordChannelType.News or DiscordChannelType.Stage or DiscordChannelType.GuildForum)
? throw new ArgumentException("Channel type must be text, voice, stage, category, or a forum.", nameof(type))
: await this.ApiClient.CreateGuildChannelAsync
(
id,
name,
type,
parent,
topic,
bitrate,
userLimit,
overwrites,
nsfw,
perUserRateLimit,
qualityMode,
position,
reason,
defaultAutoArchiveDuration,
defaultReactionEmoji,
availableTags,
defaultSortOrder
);
/// <summary>
/// Modifies a channel
/// </summary>
/// <param name="id">Channel ID</param>
/// <param name="name">New channel name</param>
/// <param name="position">New channel position</param>
/// <param name="topic">New channel topic</param>
/// <param name="nsfw">Whether this channel should be marked as NSFW</param>
/// <param name="parent">New channel parent</param>
/// <param name="bitrate">New voice channel bitrate</param>
/// <param name="userLimit">New voice channel user limit</param>
/// <param name="perUserRateLimit">Slow mode timeout for users.</param>
/// <param name="rtcRegion">New region override.</param>
/// <param name="qualityMode">New video quality mode.</param>
/// <param name="type">New channel type.</param>
/// <param name="permissionOverwrites">New channel permission overwrites.</param>
/// <param name="reason">Reason why this channel was modified</param>
/// <param name="flags">Channel flags.</param>
/// <param name="defaultAutoArchiveDuration">Default duration for newly created forum posts in the channel.</param>
/// <param name="defaultReactionEmoji">Default emoji used for reacting to forum posts.</param>
/// <param name="availableTags">Tags available for use by forum posts in the channel.</param>
/// <param name="defaultPerUserRatelimit">Default per-user ratelimit for forum posts in the channel.</param>
/// <param name="defaultSortOrder">Default sorting order for forum posts in the channel.</param>
/// <param name="defaultForumLayout">Default layout for forum posts in the channel.</param>
/// <returns></returns>
public async Task ModifyChannelAsync
(
ulong id,
string name,
int? position,
Optional<string> topic,
bool? nsfw,
Optional<ulong?> parent,
int? bitrate,
int? userLimit,
Optional<int?> perUserRateLimit,
Optional<DiscordVoiceRegion> rtcRegion,
DiscordVideoQualityMode? qualityMode,
Optional<DiscordChannelType> type,
IEnumerable<DiscordOverwriteBuilder> permissionOverwrites,
string reason,
Optional<DiscordChannelFlags> flags,
IEnumerable<DiscordForumTagBuilder>? availableTags,
Optional<DiscordAutoArchiveDuration?> defaultAutoArchiveDuration,
Optional<DefaultReaction?> defaultReactionEmoji,
Optional<int> defaultPerUserRatelimit,
Optional<DiscordDefaultSortOrder?> defaultSortOrder,
Optional<DiscordDefaultForumLayout> defaultForumLayout
)
=> await this.ApiClient.ModifyChannelAsync
(
id,
name,
position,
topic,
nsfw,
parent,
bitrate,
userLimit,
perUserRateLimit,
rtcRegion.IfPresent(e => e?.Id),
qualityMode,
type,
permissionOverwrites,
flags,
availableTags,
defaultAutoArchiveDuration,
defaultReactionEmoji,
defaultPerUserRatelimit,
defaultSortOrder,
defaultForumLayout,
reason
);
/// <summary>
/// Modifies a channel
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="action">Channel modifications</param>
/// <returns></returns>
public async Task ModifyChannelAsync(ulong channelId, Action<ChannelEditModel> action)
{
ChannelEditModel mdl = new();
action(mdl);
await this.ApiClient.ModifyChannelAsync
(
channelId, mdl.Name,
mdl.Position,
mdl.Topic,
mdl.Nsfw,
mdl.Parent.HasValue ? mdl.Parent.Value?.Id : default(Optional<ulong?>),
mdl.Bitrate,
mdl.Userlimit,
mdl.PerUserRateLimit,
mdl.RtcRegion.IfPresent(r => r?.Id),
mdl.QualityMode,
mdl.Type,
mdl.PermissionOverwrites,
mdl.Flags,
mdl.AvailableTags,
mdl.DefaultAutoArchiveDuration,
mdl.DefaultReaction,
mdl.DefaultThreadRateLimit,
mdl.DefaultSortOrder,
mdl.DefaultForumLayout,
mdl.AuditLogReason
);
}
/// <summary>
/// Gets a channel object
/// </summary>
/// <param name="id">Channel ID</param>
/// <returns></returns>
public async Task<DiscordChannel> GetChannelAsync(ulong id)
=> await this.ApiClient.GetChannelAsync(id);
/// <summary>
/// Deletes a channel
/// </summary>
/// <param name="id">Channel ID</param>
/// <param name="reason">Reason why this channel was deleted</param>
/// <returns></returns>
public async Task DeleteChannelAsync(ulong id, string reason)
=> await this.ApiClient.DeleteChannelAsync(id, reason);
/// <summary>
/// Gets message in a channel
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="messageId">Message ID</param>
/// <returns></returns>
public async Task<DiscordMessage> GetMessageAsync(ulong channelId, ulong messageId)
=> await this.ApiClient.GetMessageAsync(channelId, messageId);
/// <summary>
/// Sends a message
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="content">Message (text) content</param>
/// <returns></returns>
public async Task<DiscordMessage> CreateMessageAsync(ulong channelId, string content)
=> await this.ApiClient.CreateMessageAsync(channelId, content, null, replyMessageId: null, mentionReply: false, failOnInvalidReply: false, suppressNotifications: false);
/// <summary>
/// Sends a message
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="embed">Embed to attach</param>
/// <returns></returns>
public async Task<DiscordMessage> CreateMessageAsync(ulong channelId, DiscordEmbed embed)
=> await this.ApiClient.CreateMessageAsync(channelId, null, embed is not null ? new[] { embed } : null, replyMessageId: null, mentionReply: false, failOnInvalidReply: false, suppressNotifications: false);
/// <summary>
/// Sends a message
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="content">Message (text) content</param>
/// <param name="embed">Embed to attach</param>
/// <returns></returns>
public async Task<DiscordMessage> CreateMessageAsync(ulong channelId, string content, DiscordEmbed embed)
=> await this.ApiClient.CreateMessageAsync(channelId, content, embed is not null ? new[] { embed } : null, replyMessageId: null, mentionReply: false, failOnInvalidReply: false, suppressNotifications: false);
/// <summary>
/// Sends a message
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="builder">The Discord Message builder.</param>
/// <returns></returns>
public async Task<DiscordMessage> CreateMessageAsync(ulong channelId, DiscordMessageBuilder builder)
=> await this.ApiClient.CreateMessageAsync(channelId, builder);
/// <summary>
/// Sends a message
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="action">The Discord Message builder.</param>
/// <returns></returns>
public async Task<DiscordMessage> CreateMessageAsync(ulong channelId, Action<DiscordMessageBuilder> action)
{
DiscordMessageBuilder builder = new();
action(builder);
return await this.ApiClient.CreateMessageAsync(channelId, builder);
}
/// <summary>
/// Gets channels from a guild
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <returns></returns>
public async Task<IReadOnlyList<DiscordChannel>> GetGuildChannelsAsync(ulong guildId)
=> await this.ApiClient.GetGuildChannelsAsync(guildId);
/// <summary>
/// Gets messages from a channel
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="limit">Limit of messages to get</param>
/// <param name="before">Gets messages before this ID</param>
/// <param name="after">Gets messages after this ID</param>
/// <param name="around">Gets messages around this ID</param>
/// <returns></returns>
public async Task<IReadOnlyList<DiscordMessage>> GetChannelMessagesAsync(ulong channelId, int limit, ulong? before, ulong? after, ulong? around)
=> await this.ApiClient.GetChannelMessagesAsync(channelId, limit, before, after, around);
/// <summary>
/// Gets a message from a channel
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="messageId">Message ID</param>
/// <returns></returns>
public async Task<DiscordMessage> GetChannelMessageAsync(ulong channelId, ulong messageId)
=> await this.ApiClient.GetChannelMessageAsync(channelId, messageId);
/// <summary>
/// Edits a message
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="messageId">Message ID</param>
/// <param name="content">New message content</param>
/// <returns></returns>
public async Task<DiscordMessage> EditMessageAsync(ulong channelId, ulong messageId, Optional<string> content)
=> await this.ApiClient.EditMessageAsync(channelId, messageId, content, default, default, default, Array.Empty<DiscordMessageFile>());
/// <summary>
/// Edits a message
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="messageId">Message ID</param>
/// <param name="embed">New message embed</param>
/// <returns></returns>
public async Task<DiscordMessage> EditMessageAsync(ulong channelId, ulong messageId, Optional<DiscordEmbed> embed)
=> await this.ApiClient.EditMessageAsync(channelId, messageId, default, embed.HasValue ? [embed.Value] : Array.Empty<DiscordEmbed>(), default, default, Array.Empty<DiscordMessageFile>());
/// <summary>
/// Edits a message
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="messageId">Message ID</param>
/// <param name="builder">The builder of the message to edit.</param>
/// <param name="suppressEmbeds">Whether to suppress embeds on the message.</param>
/// <param name="attachments">Attached files to keep.</param>
/// <returns></returns>
public async Task<DiscordMessage> EditMessageAsync(ulong channelId, ulong messageId, DiscordMessageBuilder builder, bool suppressEmbeds = false, IEnumerable<DiscordAttachment> attachments = default)
{
builder.Validate();
return await this.ApiClient.EditMessageAsync(channelId, messageId, builder.Content, new Optional<IEnumerable<DiscordEmbed>>(builder.Embeds), builder.mentions, builder.Components, builder.Files, suppressEmbeds ? DiscordMessageFlags.SuppressedEmbeds : null, attachments);
}
/// <summary>
/// Modifies the visibility of embeds in a message.
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="messageId">Message ID</param>
/// <param name="hideEmbeds">Whether to hide all embeds.</param>
public async Task ModifyEmbedSuppressionAsync(ulong channelId, ulong messageId, bool hideEmbeds)
=> await this.ApiClient.EditMessageAsync(channelId, messageId, default, default, default, default, Array.Empty<DiscordMessageFile>(), hideEmbeds ? DiscordMessageFlags.SuppressedEmbeds : null);
/// <summary>
/// Deletes a message
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="messageId">Message ID</param>
/// <param name="reason">Why this message was deleted</param>
/// <returns></returns>
public async Task DeleteMessageAsync(ulong channelId, ulong messageId, string reason)
=> await this.ApiClient.DeleteMessageAsync(channelId, messageId, reason);
/// <summary>
/// Deletes multiple messages
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="messageIds">Message IDs</param>
/// <param name="reason">Reason these messages were deleted</param>
/// <returns></returns>
public async Task DeleteMessagesAsync(ulong channelId, IEnumerable<ulong> messageIds, string reason)
=> await this.ApiClient.DeleteMessagesAsync(channelId, messageIds, reason);
/// <summary>
/// Gets a channel's invites
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <returns></returns>
public async Task<IReadOnlyList<DiscordInvite>> GetChannelInvitesAsync(ulong channelId)
=> await this.ApiClient.GetChannelInvitesAsync(channelId);
/// <summary>
/// Creates a channel invite
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="maxAge">For how long the invite should exist</param>
/// <param name="maxUses">How often the invite may be used</param>
/// <param name="temporary">Whether this invite should be temporary</param>
/// <param name="unique">Whether this invite should be unique (false might return an existing invite)</param>
/// <param name="reason">Why you made an invite</param>
/// <param name="targetType">The target type of the invite, for stream and embedded application invites.</param>
/// <param name="targetUserId">The ID of the target user.</param>
/// <param name="targetApplicationId">The ID of the target application.</param>
/// <returns></returns>
public async Task<DiscordInvite> CreateChannelInviteAsync(ulong channelId, int maxAge, int maxUses, bool temporary, bool unique, string reason, DiscordInviteTargetType? targetType = null, ulong? targetUserId = null, ulong? targetApplicationId = null)
=> await this.ApiClient.CreateChannelInviteAsync(channelId, maxAge, maxUses, temporary, unique, reason, targetType, targetUserId, targetApplicationId);
/// <summary>
/// Deletes channel overwrite
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="overwriteId">Overwrite ID</param>
/// <param name="reason">Reason it was deleted</param>
/// <returns></returns>
public async Task DeleteChannelPermissionAsync(ulong channelId, ulong overwriteId, string reason)
=> await this.ApiClient.DeleteChannelPermissionAsync(channelId, overwriteId, reason);
/// <summary>
/// Edits channel overwrite
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="overwriteId">Overwrite ID</param>
/// <param name="allow">Permissions to allow</param>
/// <param name="deny">Permissions to deny</param>
/// <param name="type">Overwrite type</param>
/// <param name="reason">Reason this overwrite was created</param>
/// <returns></returns>
public async Task EditChannelPermissionsAsync(ulong channelId, ulong overwriteId, DiscordPermissions allow, DiscordPermissions deny, string type, string reason)
=> await this.ApiClient.EditChannelPermissionsAsync(channelId, overwriteId, allow, deny, type, reason);
/// <summary>
/// Send a typing indicator to a channel
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <returns></returns>
public async Task TriggerTypingAsync(ulong channelId)
=> await this.ApiClient.TriggerTypingAsync(channelId);
/// <summary>
/// Gets pinned messages
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <returns></returns>
public async Task<IReadOnlyList<DiscordMessage>> GetPinnedMessagesAsync(ulong channelId)
=> await this.ApiClient.GetPinnedMessagesAsync(channelId);
/// <summary>
/// Unpins a message
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="messageId">Message ID</param>
/// <returns></returns>
public async Task UnpinMessageAsync(ulong channelId, ulong messageId)
=> await this.ApiClient.UnpinMessageAsync(channelId, messageId);
/// <summary>
/// Joins a group DM
/// </summary>
/// <param name="channelId">Channel ID</param>
/// <param name="nickname">DM nickname</param>
/// <returns></returns>