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.Migrations.cs 2.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Threading.Tasks;
  3. using Discord.Rest;
  4. namespace Discord
  5. {
  6. public partial class TestsFixture
  7. {
  8. public const uint MigrationCount = 3;
  9. public async Task MigrateAsync()
  10. {
  11. DiscordRestClient client = null;
  12. RestGuild guild = null;
  13. await _cache.LoadInfoAsync(_config.GuildId).ConfigureAwait(false);
  14. while (_cache.Info.Version != MigrationCount)
  15. {
  16. if (client == null)
  17. {
  18. client = new DiscordRestClient();
  19. await client.LoginAsync(TokenType.Bot, _config.Token, false).ConfigureAwait(false);
  20. guild = await client.GetGuildAsync(_config.GuildId);
  21. }
  22. uint nextVer = _cache.Info.Version + 1;
  23. try
  24. {
  25. await DoMigrateAsync(client, guild, nextVer).ConfigureAwait(false);
  26. _cache.Info.Version = nextVer;
  27. await _cache.SaveInfoAsync().ConfigureAwait(false);
  28. }
  29. catch
  30. {
  31. await _cache.ClearAsync().ConfigureAwait(false);
  32. throw;
  33. }
  34. }
  35. }
  36. private static Task DoMigrateAsync(DiscordRestClient client, RestGuild guild, uint toVersion)
  37. {
  38. switch (toVersion)
  39. {
  40. case 1: return Migration_WipeGuild(client, guild);
  41. case 2: return Tests.Migration_CreateTextChannels(client, guild);
  42. case 3: return Tests.Migration_CreateVoiceChannels(client, guild);
  43. default: throw new InvalidOperationException("Unknown migration: " + toVersion);
  44. }
  45. }
  46. private static async Task Migration_WipeGuild(DiscordRestClient client, RestGuild guild)
  47. {
  48. var textChannels = await guild.GetTextChannelsAsync();
  49. var voiceChannels = await guild.GetVoiceChannelsAsync();
  50. var roles = guild.Roles;
  51. foreach (var channel in textChannels)
  52. {
  53. if (channel.Id != guild.DefaultChannelId)
  54. await channel.DeleteAsync();
  55. }
  56. foreach (var channel in voiceChannels)
  57. await channel.DeleteAsync();
  58. foreach (var role in roles)
  59. {
  60. if (role.Id != guild.EveryoneRole.Id)
  61. await role.DeleteAsync();
  62. }
  63. }
  64. }
  65. }