persist update check state

This commit is contained in:
13xforever 2018-09-08 20:17:03 +05:00 committed by Roberto Anić Banić
parent 3be1ce44bf
commit f9836f56d7
5 changed files with 276 additions and 2 deletions

View File

@ -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
{

View File

@ -7,6 +7,7 @@ namespace CompatBot.Database
{
internal class BotDb: DbContext
{
public DbSet<BotState> BotState { get; set; }
public DbSet<Moderator> Moderator { get; set; }
public DbSet<Piracystring> Piracystring { get; set; }
public DbSet<Warning> Warning { get; set; }
@ -22,6 +23,7 @@ namespace CompatBot.Database
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//configure indices
modelBuilder.Entity<BotState>().HasIndex(m => m.Key).IsUnique().HasName("bot_state_key");
modelBuilder.Entity<Moderator>().HasIndex(m => m.DiscordId).IsUnique().HasName("moderator_discord_id");
modelBuilder.Entity<Piracystring>().HasIndex(ps => ps.String).IsUnique().HasName("piracystring_string");
modelBuilder.Entity<Warning>().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; }

View File

@ -0,0 +1,189 @@
// <auto-generated />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id");
b.Property<string>("Key")
.HasColumnName("key");
b.Property<string>("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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id");
b.Property<string>("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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id");
b.Property<string>("Keyword")
.IsRequired()
.HasColumnName("keyword");
b.Property<string>("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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id");
b.Property<ulong>("DiscordId")
.HasColumnName("discord_id");
b.Property<bool>("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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id");
b.Property<string>("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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id");
b.Property<ulong>("DiscordId")
.HasColumnName("discord_id");
b.Property<string>("FullReason")
.IsRequired()
.HasColumnName("full_reason");
b.Property<ulong>("IssuerId")
.HasColumnName("issuer_id");
b.Property<string>("Reason")
.IsRequired()
.HasColumnName("reason");
b.Property<long?>("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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id");
b.Property<ulong>("GuildId")
.HasColumnName("guild_id");
b.Property<string>("InviteCode")
.HasColumnName("invite_code");
b.Property<string>("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
}
}
}

View File

@ -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<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
key = table.Column<string>(nullable: true),
value = table.Column<string>(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");
}
}
}

View File

@ -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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id");
b.Property<string>("Key")
.HasColumnName("key");
b.Property<string>("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 =>
{