Skip to content

Commit

Permalink
Attempt to safely startup from docker even if no migration exists
Browse files Browse the repository at this point in the history
  • Loading branch information
VelvetToroyashi committed Jan 20, 2021
1 parent c17810a commit c8b84d7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/Silk.Core/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Npgsql;
using Silk.Core.Database;
using Silk.Core.Utilities;

Expand Down Expand Up @@ -44,8 +45,12 @@ public Bot(IServiceProvider services, DiscordShardedClient client, ILogger<Bot>
_logger = logger;
_exceptionHelper = exceptionHelper;
_eventSubscriber = eventSubscriber;

SilkDBContext = dbFactory.CreateDbContext();

try { _ = SilkDBContext.Guilds.FirstOrDefault(); }
catch (PostgresException) { SilkDBContext.Database.MigrateAsync().GetAwaiter().GetResult(); }

Instance = this;
Client = client;
}
Expand Down
1 change: 1 addition & 0 deletions src/Silk.Core/Commands/Tests/ReplyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Silk.Core.Commands.Tests
public class ReplyCommand : BaseCommandModule
{
[Command]
[Hidden]
public async Task Reply(CommandContext ctx)
{
var builder = new DiscordMessageBuilder();
Expand Down
1 change: 1 addition & 0 deletions src/Silk.Core/Commands/Tests/RoleMenu/RoleMenuCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class RoleMenuCommand : BaseCommandModule
{

[Command("create")]
[Hidden]
[Description("Create a role menu! \n(Note, if you're adding roles with spaces, you must put them in quotes (`\"\"`)!")]
[RequireFlag(UserFlag.Staff)]
public async Task CreateCommand(CommandContext ctx, [RemainingText] params DiscordRole[] roles)
Expand Down
2 changes: 1 addition & 1 deletion src/Silk.Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Program
MinimumLogLevel = LogLevel.Error
};

public static async Task Main(string[] args) => await CreateHostBuilder(args).RunConsoleAsync().ConfigureAwait(false);
public static async Task Main(string[] args) => await CreateHostBuilder(args).UseConsoleLifetime().StartAsync().ConfigureAwait(false);


public static IHostBuilder CreateHostBuilder(string[] args)
Expand Down
13 changes: 8 additions & 5 deletions src/Silk.Core/Services/DatabaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ public async Task<GuildModel> GetOrCreateGuildAsync(ulong guildId)
{
await using SilkDbContext db = GetContext();
GuildModel? guild = await db.Guilds.FirstOrDefaultAsync(g => g.Id == guildId);

if (guild is not null) return guild;
bool guildWasNull = guild is null;
guild ??= new() {Id = guildId, Users = new(), Prefix = Bot.DefaultCommandPrefix, Configuration = new() { Guild = guild!, GuildId = guildId}};
if (guildWasNull)
{
_logger.LogTrace("Guild was null");
db.Guilds.Add(guild);
await db.SaveChangesAsync();
}

guild = new() {Id = guildId, Users = new(), Prefix = Bot.DefaultCommandPrefix, Configuration = new()};
db.Guilds.Add(guild);
await db.SaveChangesAsync();
return guild;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Silk.Core/Tools/EventHelpers/GuildAddedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class GuildAddedHandler
public Task OnGuildAvailable(DiscordClient c, GuildCreateEventArgs e)
{
if (startupCacheCompleted) return Task.CompletedTask; // Prevent double logging when joining a new guild //
_logger.LogDebug($"Beginning Cache. Shard [{c.ShardId + 1}/{c.ShardCount}] | Guild [{++currentGuild}/{c.Guilds.Count}]");
_logger.LogDebug($"Adding guild to cache queue. Shard [{c.ShardId + 1}/{c.ShardCount}] | Guild [{++currentGuild}/{c.Guilds.Count}]");
cacheQueue.Add((e.Guild.Id, e.Guild.Members.Values));
startupCacheCompleted = currentGuild == c.Guilds.Count;
return Task.CompletedTask;
Expand Down Expand Up @@ -63,7 +63,7 @@ private async Task CacheMembersAsync((ulong id, IEnumerable<DiscordMember> membe
await _dbService.UpdateGuildAsync(guild);
}

private Task CacheGuildAsync(DiscordGuild guild) => _dbService.GetOrCreateGuildAsync(guild.Id);
private async Task CacheGuildAsync(DiscordGuild guild) => await _dbService.GetOrCreateGuildAsync(guild.Id);

// Used in conjunction with OnGuildJoin() //
private async Task SendWelcomeMessage(DiscordClient c, GuildCreateEventArgs e)
Expand Down

0 comments on commit c8b84d7

Please sign in to comment.