diff --git a/CompatBot/Commands/CompatList.cs b/CompatBot/Commands/CompatList.cs index 4b8cb2b5..18dc4b11 100644 --- a/CompatBot/Commands/CompatList.cs +++ b/CompatBot/Commands/CompatList.cs @@ -8,12 +8,14 @@ using CompatApiClient; using CompatApiClient.POCOs; using CompatApiClient.Utils; using CompatBot.Commands.Attributes; +using CompatBot.Database; using CompatBot.Utils; using CompatBot.Utils.ResultFormatters; using DSharpPlus; using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext.Attributes; using DSharpPlus.Entities; +using Microsoft.EntityFrameworkCore; namespace CompatBot.Commands { @@ -22,6 +24,13 @@ namespace CompatBot.Commands private static readonly Client client = new Client(); private static SemaphoreSlim updateCheck = new SemaphoreSlim(1, 1); private static string lastUpdateInfo = null; + private const string Rpcs3UpdateStateKey = "Rpcs3UpdateState"; + + static CompatList() + { + using (var db = new BotDb()) + lastUpdateInfo = db.BotState.FirstOrDefault(k => k.Key == Rpcs3UpdateStateKey)?.Value; + } [Command("compat"), Aliases("c")] [Description("Searches the compatibility database, USE: !compat searchterm")] @@ -115,8 +124,17 @@ Example usage: try { var compatChannel = await discordClient.GetChannelAsync(Config.BotChannelId).ConfigureAwait(false); - lastUpdateInfo = updateLinks; await compatChannel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false); + lastUpdateInfo = updateLinks; + using (var db = new BotDb()) + { + var currentState = await db.BotState.FirstOrDefaultAsync(k => k.Key == Rpcs3UpdateStateKey).ConfigureAwait(false); + if (currentState == null) + db.BotState.Add(new BotState {Key = Rpcs3UpdateStateKey, Value = updateLinks}); + else + currentState.Value = updateLinks; + await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false); + } } finally { diff --git a/CompatBot/Database/BotDb.cs b/CompatBot/Database/BotDb.cs index 4e008db1..afe136fb 100644 --- a/CompatBot/Database/BotDb.cs +++ b/CompatBot/Database/BotDb.cs @@ -7,6 +7,7 @@ namespace CompatBot.Database { internal class BotDb: DbContext { + public DbSet BotState { get; set; } public DbSet Moderator { get; set; } public DbSet Piracystring { get; set; } public DbSet Warning { get; set; } @@ -22,6 +23,7 @@ namespace CompatBot.Database protected override void OnModelCreating(ModelBuilder modelBuilder) { //configure indices + modelBuilder.Entity().HasIndex(m => m.Key).IsUnique().HasName("bot_state_key"); modelBuilder.Entity().HasIndex(m => m.DiscordId).IsUnique().HasName("moderator_discord_id"); modelBuilder.Entity().HasIndex(ps => ps.String).IsUnique().HasName("piracystring_string"); modelBuilder.Entity().HasIndex(w => w.DiscordId).HasName("warning_discord_id"); @@ -37,6 +39,13 @@ namespace CompatBot.Database } } + internal class BotState + { + public int Id { get; set; } + public string Key { get; set; } + public string Value { get; set; } + } + internal class Moderator { public int Id { get; set; } diff --git a/CompatBot/Database/Migrations/BotDb/20180908150603_BotState.Designer.cs b/CompatBot/Database/Migrations/BotDb/20180908150603_BotState.Designer.cs new file mode 100644 index 00000000..a7b2d25b --- /dev/null +++ b/CompatBot/Database/Migrations/BotDb/20180908150603_BotState.Designer.cs @@ -0,0 +1,189 @@ +// +using System; +using CompatBot.Database; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace CompatBot.Database.Migrations +{ + [DbContext(typeof(BotDb))] + [Migration("20180908150603_BotState")] + partial class BotState + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "2.1.2-rtm-30932"); + + modelBuilder.Entity("CompatBot.Database.BotState", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id"); + + b.Property("Key") + .HasColumnName("key"); + + b.Property("Value") + .HasColumnName("value"); + + b.HasKey("Id") + .HasName("id"); + + b.HasIndex("Key") + .IsUnique() + .HasName("bot_state_key"); + + b.ToTable("bot_state"); + }); + + modelBuilder.Entity("CompatBot.Database.DisabledCommand", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id"); + + b.Property("Command") + .IsRequired() + .HasColumnName("command"); + + b.HasKey("Id") + .HasName("id"); + + b.HasIndex("Command") + .IsUnique() + .HasName("disabled_command_command"); + + b.ToTable("disabled_commands"); + }); + + modelBuilder.Entity("CompatBot.Database.Explanation", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id"); + + b.Property("Keyword") + .IsRequired() + .HasColumnName("keyword"); + + b.Property("Text") + .IsRequired() + .HasColumnName("text"); + + b.HasKey("Id") + .HasName("id"); + + b.HasIndex("Keyword") + .IsUnique() + .HasName("explanation_keyword"); + + b.ToTable("explanation"); + }); + + modelBuilder.Entity("CompatBot.Database.Moderator", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id"); + + b.Property("DiscordId") + .HasColumnName("discord_id"); + + b.Property("Sudoer") + .HasColumnName("sudoer"); + + b.HasKey("Id") + .HasName("id"); + + b.HasIndex("DiscordId") + .IsUnique() + .HasName("moderator_discord_id"); + + b.ToTable("moderator"); + }); + + modelBuilder.Entity("CompatBot.Database.Piracystring", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id"); + + b.Property("String") + .IsRequired() + .HasColumnName("string") + .HasColumnType("varchar(255)"); + + b.HasKey("Id") + .HasName("id"); + + b.HasIndex("String") + .IsUnique() + .HasName("piracystring_string"); + + b.ToTable("piracystring"); + }); + + modelBuilder.Entity("CompatBot.Database.Warning", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id"); + + b.Property("DiscordId") + .HasColumnName("discord_id"); + + b.Property("FullReason") + .IsRequired() + .HasColumnName("full_reason"); + + b.Property("IssuerId") + .HasColumnName("issuer_id"); + + b.Property("Reason") + .IsRequired() + .HasColumnName("reason"); + + b.Property("Timestamp") + .HasColumnName("timestamp"); + + b.HasKey("Id") + .HasName("id"); + + b.HasIndex("DiscordId") + .HasName("warning_discord_id"); + + b.ToTable("warning"); + }); + + modelBuilder.Entity("CompatBot.Database.WhitelistedInvite", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id"); + + b.Property("GuildId") + .HasColumnName("guild_id"); + + b.Property("InviteCode") + .HasColumnName("invite_code"); + + b.Property("Name") + .HasColumnName("name"); + + b.HasKey("Id") + .HasName("id"); + + b.HasIndex("GuildId") + .IsUnique() + .HasName("whitelisted_invite_guild_id"); + + b.ToTable("whitelisted_invites"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CompatBot/Database/Migrations/BotDb/20180908150603_BotState.cs b/CompatBot/Database/Migrations/BotDb/20180908150603_BotState.cs new file mode 100644 index 00000000..c6576c6a --- /dev/null +++ b/CompatBot/Database/Migrations/BotDb/20180908150603_BotState.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace CompatBot.Database.Migrations +{ + public partial class BotState : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "bot_state", + columns: table => new + { + id = table.Column(nullable: false) + .Annotation("Sqlite:Autoincrement", true), + key = table.Column(nullable: true), + value = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("id", x => x.id); + }); + + migrationBuilder.CreateIndex( + name: "bot_state_key", + table: "bot_state", + column: "key", + unique: true); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "bot_state"); + } + } +} diff --git a/CompatBot/Database/Migrations/BotDb/BotDbModelSnapshot.cs b/CompatBot/Database/Migrations/BotDb/BotDbModelSnapshot.cs index ae46ca3c..718f3860 100644 --- a/CompatBot/Database/Migrations/BotDb/BotDbModelSnapshot.cs +++ b/CompatBot/Database/Migrations/BotDb/BotDbModelSnapshot.cs @@ -14,7 +14,29 @@ namespace CompatBot.Database.Migrations { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "2.1.1-rtm-30846"); + .HasAnnotation("ProductVersion", "2.1.2-rtm-30932"); + + modelBuilder.Entity("CompatBot.Database.BotState", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id"); + + b.Property("Key") + .HasColumnName("key"); + + b.Property("Value") + .HasColumnName("value"); + + b.HasKey("Id") + .HasName("id"); + + b.HasIndex("Key") + .IsUnique() + .HasName("bot_state_key"); + + b.ToTable("bot_state"); + }); modelBuilder.Entity("CompatBot.Database.DisabledCommand", b => {