You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tests.Channels.cs 7.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using Discord.Rest;
  2. using System;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Xunit;
  6. namespace Discord
  7. {
  8. public partial class Tests
  9. {
  10. internal static async Task Migration_CreateTextChannels(DiscordRestClient client, RestGuild guild)
  11. {
  12. var text1 = await guild.GetDefaultChannelAsync();
  13. var text2 = await guild.CreateTextChannelAsync("text2");
  14. var text3 = await guild.CreateTextChannelAsync("text3");
  15. var text4 = await guild.CreateTextChannelAsync("text4");
  16. var text5 = await guild.CreateTextChannelAsync("text5");
  17. // create a channel category
  18. var cat1 = await guild.CreateCategoryChannelAsync("cat1");
  19. if (text1 == null)
  20. {
  21. // the guild did not have a default channel, so make a new one
  22. text1 = await guild.CreateTextChannelAsync("default");
  23. }
  24. //Modify #general
  25. await text1.ModifyAsync(x =>
  26. {
  27. x.Name = "text1";
  28. x.Position = 1;
  29. x.Topic = "Topic1";
  30. x.CategoryId = cat1.Id;
  31. });
  32. await text2.ModifyAsync(x =>
  33. {
  34. x.Position = 2;
  35. x.CategoryId = cat1.Id;
  36. });
  37. await text3.ModifyAsync(x =>
  38. {
  39. x.Topic = "Topic2";
  40. });
  41. await text4.ModifyAsync(x =>
  42. {
  43. x.Position = 3;
  44. x.Topic = "Topic2";
  45. });
  46. await text5.ModifyAsync(x =>
  47. {
  48. });
  49. CheckTextChannels(guild, text1, text2, text3, text4, text5);
  50. }
  51. [Fact]
  52. public async Task TestTextChannels()
  53. {
  54. CheckTextChannels(_guild, (await _guild.GetTextChannelsAsync()).ToArray());
  55. }
  56. private static void CheckTextChannels(RestGuild guild, params RestTextChannel[] textChannels)
  57. {
  58. Assert.Equal(5, textChannels.Length);
  59. Assert.All(textChannels, x =>
  60. {
  61. Assert.NotNull(x);
  62. Assert.NotEqual(0UL, x.Id);
  63. Assert.True(x.Position >= 0);
  64. });
  65. var text1 = textChannels.FirstOrDefault(x => x.Name == "text1");
  66. var text2 = textChannels.FirstOrDefault(x => x.Name == "text2");
  67. var text3 = textChannels.FirstOrDefault(x => x.Name == "text3");
  68. var text4 = textChannels.FirstOrDefault(x => x.Name == "text4");
  69. var text5 = textChannels.FirstOrDefault(x => x.Name == "text5");
  70. Assert.NotNull(text1);
  71. //Assert.True(text1.Id == guild.DefaultChannelId);
  72. Assert.Equal(1, text1.Position);
  73. Assert.Equal("Topic1", text1.Topic);
  74. Assert.NotNull(text2);
  75. Assert.Equal(2, text2.Position);
  76. Assert.Null(text2.Topic);
  77. Assert.NotNull(text3);
  78. Assert.Equal("Topic2", text3.Topic);
  79. Assert.NotNull(text4);
  80. Assert.Equal(3, text4.Position);
  81. Assert.Equal("Topic2", text4.Topic);
  82. Assert.NotNull(text5);
  83. Assert.Null(text5.Topic);
  84. }
  85. internal static async Task Migration_CreateVoiceChannels(DiscordRestClient client, RestGuild guild)
  86. {
  87. var voice1 = await guild.CreateVoiceChannelAsync("voice1");
  88. var voice2 = await guild.CreateVoiceChannelAsync("voice2");
  89. var voice3 = await guild.CreateVoiceChannelAsync("voice3");
  90. var cat2 = await guild.CreateCategoryChannelAsync("cat2");
  91. await voice1.ModifyAsync(x =>
  92. {
  93. x.Bitrate = 96000;
  94. x.Position = 1;
  95. x.CategoryId = cat2.Id;
  96. });
  97. await voice2.ModifyAsync(x =>
  98. {
  99. x.UserLimit = null;
  100. });
  101. await voice3.ModifyAsync(x =>
  102. {
  103. x.Bitrate = 8000;
  104. x.Position = 1;
  105. x.UserLimit = 16;
  106. x.CategoryId = cat2.Id;
  107. });
  108. CheckVoiceChannels(voice1, voice2, voice3);
  109. }
  110. [Fact]
  111. public async Task TestVoiceChannels()
  112. {
  113. CheckVoiceChannels((await _guild.GetVoiceChannelsAsync()).ToArray());
  114. }
  115. private static void CheckVoiceChannels(params RestVoiceChannel[] voiceChannels)
  116. {
  117. Assert.Equal(3, voiceChannels.Length);
  118. Assert.All(voiceChannels, x =>
  119. {
  120. Assert.NotNull(x);
  121. Assert.NotEqual(0UL, x.Id);
  122. Assert.NotEqual(0, x.UserLimit);
  123. Assert.True(x.Bitrate > 0);
  124. Assert.True(x.Position >= 0);
  125. });
  126. var voice1 = voiceChannels.FirstOrDefault(x => x.Name == "voice1");
  127. var voice2 = voiceChannels.FirstOrDefault(x => x.Name == "voice2");
  128. var voice3 = voiceChannels.FirstOrDefault(x => x.Name == "voice3");
  129. Assert.NotNull(voice1);
  130. Assert.Equal(96000, voice1.Bitrate);
  131. Assert.Equal(1, voice1.Position);
  132. Assert.NotNull(voice2);
  133. Assert.Null(voice2.UserLimit);
  134. Assert.NotNull(voice3);
  135. Assert.Equal(8000, voice3.Bitrate);
  136. Assert.Equal(1, voice3.Position);
  137. Assert.Equal(16, voice3.UserLimit);
  138. }
  139. [Fact]
  140. public async Task TestChannelCategories()
  141. {
  142. // (await _guild.GetVoiceChannelsAsync()).ToArray()
  143. var channels = await _guild.GetCategoryChannelsAsync();
  144. await CheckChannelCategories(channels.ToArray(), (await _guild.GetChannelsAsync()).ToArray());
  145. }
  146. private async Task CheckChannelCategories(RestCategoryChannel[] categories, RestGuildChannel[] allChannels)
  147. {
  148. // 2 categories
  149. Assert.Equal(2, categories.Length);
  150. var cat1 = categories.Where(x => x.Name == "cat1").FirstOrDefault();
  151. var cat2 = categories.Where(x => x.Name == "cat2").FirstOrDefault();
  152. Assert.NotNull(cat1);
  153. Assert.NotNull(cat2);
  154. // get text1, text2, ensure they have category id == cat1
  155. var text1 = allChannels.Where(x => x.Name == "text1").FirstOrDefault() as RestTextChannel;
  156. var text2 = allChannels.Where(x => x.Name == "text2").FirstOrDefault() as RestTextChannel;
  157. Assert.NotNull(text1);
  158. Assert.NotNull(text2);
  159. // check that CategoryID and .GetCategoryAsync work correctly
  160. // for both of the text channels
  161. Assert.Equal(text1.CategoryId, cat1.Id);
  162. var text1Cat = await text1.GetCategoryAsync();
  163. Assert.Equal(text1Cat.Id, cat1.Id);
  164. Assert.Equal(text1Cat.Name, cat1.Name);
  165. Assert.Equal(text2.CategoryId, cat1.Id);
  166. var text2Cat = await text2.GetCategoryAsync();
  167. Assert.Equal(text2Cat.Id, cat1.Id);
  168. Assert.Equal(text2Cat.Name, cat1.Name);
  169. // do the same for the voice channels
  170. var voice1 = allChannels.Where(x => x.Name == "voice1").FirstOrDefault() as RestVoiceChannel;
  171. var voice3 = allChannels.Where(x => x.Name == "voice3").FirstOrDefault() as RestVoiceChannel;
  172. Assert.NotNull(voice1);
  173. Assert.NotNull(voice3);
  174. Assert.Equal(voice1.CategoryId, cat2.Id);
  175. var voice1Cat = await voice1.GetCategoryAsync();
  176. Assert.Equal(voice1Cat.Id, cat2.Id);
  177. Assert.Equal(voice1Cat.Name, cat2.Name);
  178. Assert.Equal(voice3.CategoryId, cat2.Id);
  179. var voice3Cat = await voice3.GetCategoryAsync();
  180. Assert.Equal(voice3Cat.Id, cat2.Id);
  181. Assert.Equal(voice3Cat.Name, cat2.Name);
  182. }
  183. }
  184. }