diff --git a/ModularAssistentForDiscordServer/Commands/Slash/BotStats.cs b/ModularAssistentForDiscordServer/Commands/Slash/BotStats.cs index f4a5cc7..77891a7 100644 --- a/ModularAssistentForDiscordServer/Commands/Slash/BotStats.cs +++ b/ModularAssistentForDiscordServer/Commands/Slash/BotStats.cs @@ -17,7 +17,6 @@ using DSharpPlus.Entities; using DSharpPlus.SlashCommands; using Humanizer; -using Humanizer.Localisation; using MADS.Entities; using MADS.Extensions; using Microsoft.EntityFrameworkCore; diff --git a/ModularAssistentForDiscordServer/Commands/Slash/Ping.cs b/ModularAssistentForDiscordServer/Commands/Slash/Ping.cs index 8500198..7677b4a 100644 --- a/ModularAssistentForDiscordServer/Commands/Slash/Ping.cs +++ b/ModularAssistentForDiscordServer/Commands/Slash/Ping.cs @@ -16,7 +16,6 @@ using DSharpPlus.Entities; using DSharpPlus.SlashCommands; using Humanizer; -using Humanizer.Localisation; using MADS.Extensions; namespace MADS.Commands.Slash; diff --git a/ModularAssistentForDiscordServer/Commands/Slash/Reminder.cs b/ModularAssistentForDiscordServer/Commands/Slash/Reminder.cs index ef189f5..274bca3 100644 --- a/ModularAssistentForDiscordServer/Commands/Slash/Reminder.cs +++ b/ModularAssistentForDiscordServer/Commands/Slash/Reminder.cs @@ -27,7 +27,7 @@ namespace MADS.Commands.Slash; [SlashCommandGroup("reminder", "mangage reminders")] public sealed class Reminder : MadsBaseApplicationCommand { - private ReminderService _reminderService; + private readonly ReminderService _reminderService; public Reminder(ReminderService reminderService) { @@ -63,9 +63,9 @@ public async Task AddReminder IsPrivate = isPrivate }; - await _reminderService.AddReminder(newReminder); + ReminderDbEntity reminder = await _reminderService.AddReminder(newReminder); - await EditResponse_Success($"Reminder created. I will remind you in {Formatter.Timestamp(timeSpan.Value)}"); + await EditResponse_Success($"Reminder created with id {reminder.Id}. I will remind you in {Formatter.Timestamp(timeSpan.Value)}"); } [SlashCommand("list", "list your Reminder")] diff --git a/ModularAssistentForDiscordServer/Entities/Configs/GuildConfigConfig.cs b/ModularAssistentForDiscordServer/Entities/Configs/GuildConfigConfig.cs deleted file mode 100644 index 0d6ba55..0000000 --- a/ModularAssistentForDiscordServer/Entities/Configs/GuildConfigConfig.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2023 Plerx2493 -// -// Licensed under the Apache License, Version 2.0 (the "License") -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; - -namespace MADS.Entities.Configs; - -public class GuildConfigConfig : IEntityTypeConfiguration -{ - public void Configure(EntityTypeBuilder builder) - { - builder.HasOne(x => x.Guild) - .WithOne(x => x.Settings); - } -} \ No newline at end of file diff --git a/ModularAssistentForDiscordServer/Entities/Configs/GuildEntityConfig.cs b/ModularAssistentForDiscordServer/Entities/Configs/GuildEntityConfig.cs deleted file mode 100644 index 202175b..0000000 --- a/ModularAssistentForDiscordServer/Entities/Configs/GuildEntityConfig.cs +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2023 Plerx2493 -// -// Licensed under the Apache License, Version 2.0 (the "License") -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; - -namespace MADS.Entities.Configs; - -public class GuildEntityConfig : IEntityTypeConfiguration -{ - public void Configure(EntityTypeBuilder builder) - { - builder.HasMany(u => u.Incidents) - .WithOne(i => i.Guild) - .HasForeignKey(i => i.GuildId) - .HasPrincipalKey(x => x.DiscordId) - .OnDelete(DeleteBehavior.Cascade); - - builder.HasOne(a => a.Settings) - .WithOne(b => b.Guild) - .HasForeignKey(b => b.DiscordId) - .HasPrincipalKey(x => x.DiscordGuildId) - .OnDelete(DeleteBehavior.Cascade); - - builder.HasMany(x => x.Quotes) - .WithOne(x => x.Guild) - .HasForeignKey(x => x.DiscordGuildId) - .HasPrincipalKey(x => x.DiscordId) - .OnDelete(DeleteBehavior.Cascade); - } -} \ No newline at end of file diff --git a/ModularAssistentForDiscordServer/Entities/Configs/UserEntityConfig.cs b/ModularAssistentForDiscordServer/Entities/Configs/UserEntityConfig.cs deleted file mode 100644 index e92d7ed..0000000 --- a/ModularAssistentForDiscordServer/Entities/Configs/UserEntityConfig.cs +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2023 Plerx2493 -// -// Licensed under the Apache License, Version 2.0 (the "License") -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; - -namespace MADS.Entities.Configs; - -public class UserEntityConfig : IEntityTypeConfiguration -{ - public void Configure(EntityTypeBuilder builder) - { - builder.HasKey(u => u.Id); - - builder.HasMany(u => u.Reminders) - .WithOne(x => x.User) - .HasPrincipalKey(x => x.Id) - .HasForeignKey(x => x.UserId) - .OnDelete(DeleteBehavior.Cascade); - - builder.HasMany(u => u.VoiceAlerts) - .WithOne(x => x.User) - .HasPrincipalKey(x => x.Id) - .HasForeignKey(x => x.UserId) - .OnDelete(DeleteBehavior.Cascade); - - builder.HasMany(u => u.Incidents) - .WithOne(x => x.TargetUser) - .HasPrincipalKey(x => x.Id) - .HasForeignKey(x => x.TargetId); - } -} \ No newline at end of file diff --git a/ModularAssistentForDiscordServer/EventListeners/EventListener.GuildDownload.cs b/ModularAssistentForDiscordServer/EventListeners/EventListener.GuildDownload.cs index 72bb280..03cc62a 100644 --- a/ModularAssistentForDiscordServer/EventListeners/EventListener.GuildDownload.cs +++ b/ModularAssistentForDiscordServer/EventListeners/EventListener.GuildDownload.cs @@ -18,21 +18,20 @@ using MADS.Entities; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace MADS.EventListeners; internal static partial class EventListener { - public static void GuildDownload(DiscordClient client) - => client.GuildDownloadCompleted += async (_, args) => { await UpdateDb(args); }; - - private static async Task UpdateDb( GuildDownloadCompletedEventArgs args) + public static async Task UpdateDb(DiscordClient client, GuildDownloadCompletedEventArgs args) { IDbContextFactory dbFactory = ModularDiscordBot.Services.GetRequiredService>(); Task updateGuilds = UpdateGuilds(args, dbFactory); Task updateUsers = UpdateUsersDb(args, dbFactory); await Task.WhenAll(updateGuilds, updateUsers); + client.Logger.LogInformation("Database updated!"); } private static async Task UpdateUsersDb diff --git a/ModularAssistentForDiscordServer/Migrations/20240405003023_RemoveUserDeletionConstraints.Designer.cs b/ModularAssistentForDiscordServer/Migrations/20240405003023_RemoveUserDeletionConstraints.Designer.cs new file mode 100644 index 0000000..8377539 --- /dev/null +++ b/ModularAssistentForDiscordServer/Migrations/20240405003023_RemoveUserDeletionConstraints.Designer.cs @@ -0,0 +1,401 @@ +// +using System; +using MADS.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MADS.Migrations +{ + [DbContext(typeof(MadsContext))] + [Migration("20240405003023_RemoveUserDeletionConstraints")] + partial class RemoveUserDeletionConstraints + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("MADS.Entities.GuildConfigDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordId"); + + b.Property("Prefix") + .IsRequired() + .HasColumnType("longtext") + .HasColumnName("prefix"); + + b.Property("StarboardActive") + .HasColumnType("tinyint(1)") + .HasColumnName("starboardEnabled"); + + b.Property("StarboardChannelId") + .HasColumnType("bigint unsigned") + .HasColumnName("starboardChannel"); + + b.Property("StarboardEmojiId") + .HasColumnType("bigint unsigned") + .HasColumnName("starboardEmojiId"); + + b.Property("StarboardEmojiName") + .HasColumnType("longtext") + .HasColumnName("starboardEmojiName"); + + b.Property("StarboardThreshold") + .HasColumnType("int") + .HasColumnName("starboardThreshold"); + + b.HasKey("Id"); + + b.ToTable("Configs"); + }); + + modelBuilder.Entity("MADS.Entities.GuildDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("DiscordId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordId"); + + b.HasKey("Id"); + + b.ToTable("Guilds"); + }); + + modelBuilder.Entity("MADS.Entities.IncidentDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("CreationTimeStamp") + .HasColumnType("datetime(6)"); + + b.Property("GuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("guild_id"); + + b.Property("ModeratorId") + .HasColumnType("bigint unsigned") + .HasColumnName("moderator_id"); + + b.Property("Reason") + .IsRequired() + .HasColumnType("longtext") + .HasColumnName("reason"); + + b.Property("TargetId") + .HasColumnType("bigint unsigned") + .HasColumnName("target_id"); + + b.Property("TargetUserId") + .HasColumnType("bigint unsigned"); + + b.HasKey("Id"); + + b.HasIndex("GuildId"); + + b.HasIndex("TargetUserId"); + + b.ToTable("Incidents"); + }); + + modelBuilder.Entity("MADS.Entities.QuoteDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("Content") + .IsRequired() + .HasMaxLength(1000) + .HasColumnType("varchar(1000)") + .HasColumnName("content"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnName("timestamp"); + + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordGuildId"); + + b.Property("QuotedUserId") + .HasColumnType("bigint unsigned") + .HasColumnName("quotedUserId"); + + b.Property("UserId") + .HasColumnType("bigint unsigned") + .HasColumnName("UserId"); + + b.HasKey("Id"); + + b.HasIndex("DiscordGuildId"); + + b.ToTable("Quotes"); + }); + + modelBuilder.Entity("MADS.Entities.ReminderDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("ChannelId") + .HasColumnType("bigint unsigned") + .HasColumnName("channelId"); + + b.Property("CreationTime") + .HasColumnType("datetime(6)") + .HasColumnName("creationTime"); + + b.Property("ExecutionTime") + .HasColumnType("datetime(6)") + .HasColumnName("executionTime"); + + b.Property("IsPrivate") + .HasColumnType("tinyint(1)") + .HasColumnName("isPrivate"); + + b.Property("MentionedChannel") + .HasColumnType("bigint unsigned") + .HasColumnName("mentionedChannel"); + + b.Property("MentionedMessage") + .HasColumnType("bigint unsigned") + .HasColumnName("MentionedMessage"); + + b.Property("MessageId") + .HasColumnType("bigint unsigned") + .HasColumnName("messageId"); + + b.Property("ReminderText") + .IsRequired() + .HasColumnType("longtext") + .HasColumnName("reminderText"); + + b.Property("UserId") + .HasColumnType("bigint unsigned") + .HasColumnName("userId"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Reminders"); + }); + + modelBuilder.Entity("MADS.Entities.StarboardMessageDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("DiscordChannelId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordChannelId"); + + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordGuildId"); + + b.Property("DiscordMessageId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordMessageId"); + + b.Property("StarboardChannelId") + .HasColumnType("bigint unsigned") + .HasColumnName("starboardChannelId"); + + b.Property("StarboardGuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("starboardGuildId"); + + b.Property("StarboardMessageId") + .HasColumnType("bigint unsigned") + .HasColumnName("starboardMessageId"); + + b.Property("Stars") + .HasColumnType("int") + .HasColumnName("starCount"); + + b.HasKey("Id"); + + b.ToTable("Starboard"); + }); + + modelBuilder.Entity("MADS.Entities.UserDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("Discriminator") + .HasColumnType("int") + .HasColumnName("discriminator"); + + b.Property("PreferedLanguage") + .IsRequired() + .HasColumnType("longtext") + .HasColumnName("prefered_language"); + + b.Property("Username") + .IsRequired() + .HasColumnType("longtext") + .HasColumnName("username"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("MADS.Entities.VoiceAlert", b => + { + b.Property("AlertId") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("ChannelId") + .HasColumnType("bigint unsigned") + .HasColumnName("channel_id"); + + b.Property("GuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("guild_id"); + + b.Property("IsRepeatable") + .HasColumnType("tinyint(1)") + .HasColumnName("is_repeatable"); + + b.Property("LastAlert") + .HasColumnType("datetime(6)") + .HasColumnName("last_alert"); + + b.Property("MinTimeBetweenAlerts") + .HasColumnType("time(6)") + .HasColumnName("time_between"); + + b.Property("UserId") + .HasColumnType("bigint unsigned") + .HasColumnName("user_id"); + + b.HasKey("AlertId"); + + b.HasIndex("UserId"); + + b.ToTable("VoiceAlerts"); + }); + + modelBuilder.Entity("MADS.Entities.GuildDbEntity", b => + { + b.HasOne("MADS.Entities.GuildConfigDbEntity", "Settings") + .WithOne("Guild") + .HasForeignKey("MADS.Entities.GuildDbEntity", "DiscordId") + .HasPrincipalKey("MADS.Entities.GuildConfigDbEntity", "DiscordGuildId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Settings"); + }); + + modelBuilder.Entity("MADS.Entities.IncidentDbEntity", b => + { + b.HasOne("MADS.Entities.GuildDbEntity", "Guild") + .WithMany("Incidents") + .HasForeignKey("GuildId") + .HasPrincipalKey("DiscordId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MADS.Entities.UserDbEntity", "TargetUser") + .WithMany("Incidents") + .HasForeignKey("TargetUserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Guild"); + + b.Navigation("TargetUser"); + }); + + modelBuilder.Entity("MADS.Entities.QuoteDbEntity", b => + { + b.HasOne("MADS.Entities.GuildDbEntity", "Guild") + .WithMany("Quotes") + .HasForeignKey("DiscordGuildId") + .HasPrincipalKey("DiscordId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Guild"); + }); + + modelBuilder.Entity("MADS.Entities.ReminderDbEntity", b => + { + b.HasOne("MADS.Entities.UserDbEntity", "User") + .WithMany("Reminders") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("MADS.Entities.VoiceAlert", b => + { + b.HasOne("MADS.Entities.UserDbEntity", "User") + .WithMany("VoiceAlerts") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("MADS.Entities.GuildConfigDbEntity", b => + { + b.Navigation("Guild"); + }); + + modelBuilder.Entity("MADS.Entities.GuildDbEntity", b => + { + b.Navigation("Incidents"); + + b.Navigation("Quotes"); + }); + + modelBuilder.Entity("MADS.Entities.UserDbEntity", b => + { + b.Navigation("Incidents"); + + b.Navigation("Reminders"); + + b.Navigation("VoiceAlerts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ModularAssistentForDiscordServer/Migrations/20240405003023_RemoveUserDeletionConstraints.cs b/ModularAssistentForDiscordServer/Migrations/20240405003023_RemoveUserDeletionConstraints.cs new file mode 100644 index 0000000..e0ab019 --- /dev/null +++ b/ModularAssistentForDiscordServer/Migrations/20240405003023_RemoveUserDeletionConstraints.cs @@ -0,0 +1,71 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MADS.Migrations +{ + /// + public partial class RemoveUserDeletionConstraints : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Incidents_Users_target_id", + table: "Incidents"); + + migrationBuilder.DropIndex( + name: "IX_Incidents_target_id", + table: "Incidents"); + + migrationBuilder.AddColumn( + name: "TargetUserId", + table: "Incidents", + type: "bigint unsigned", + nullable: false, + defaultValue: 0ul); + + migrationBuilder.CreateIndex( + name: "IX_Incidents_TargetUserId", + table: "Incidents", + column: "TargetUserId"); + + migrationBuilder.AddForeignKey( + name: "FK_Incidents_Users_TargetUserId", + table: "Incidents", + column: "TargetUserId", + principalTable: "Users", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Incidents_Users_TargetUserId", + table: "Incidents"); + + migrationBuilder.DropIndex( + name: "IX_Incidents_TargetUserId", + table: "Incidents"); + + migrationBuilder.DropColumn( + name: "TargetUserId", + table: "Incidents"); + + migrationBuilder.CreateIndex( + name: "IX_Incidents_target_id", + table: "Incidents", + column: "target_id"); + + migrationBuilder.AddForeignKey( + name: "FK_Incidents_Users_target_id", + table: "Incidents", + column: "target_id", + principalTable: "Users", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/ModularAssistentForDiscordServer/Migrations/20240405004119_RemoveConfigs.Designer.cs b/ModularAssistentForDiscordServer/Migrations/20240405004119_RemoveConfigs.Designer.cs new file mode 100644 index 0000000..deee33c --- /dev/null +++ b/ModularAssistentForDiscordServer/Migrations/20240405004119_RemoveConfigs.Designer.cs @@ -0,0 +1,401 @@ +// +using System; +using MADS.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MADS.Migrations +{ + [DbContext(typeof(MadsContext))] + [Migration("20240405004119_RemoveConfigs")] + partial class RemoveConfigs + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("MADS.Entities.GuildConfigDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordId"); + + b.Property("Prefix") + .IsRequired() + .HasColumnType("longtext") + .HasColumnName("prefix"); + + b.Property("StarboardActive") + .HasColumnType("tinyint(1)") + .HasColumnName("starboardEnabled"); + + b.Property("StarboardChannelId") + .HasColumnType("bigint unsigned") + .HasColumnName("starboardChannel"); + + b.Property("StarboardEmojiId") + .HasColumnType("bigint unsigned") + .HasColumnName("starboardEmojiId"); + + b.Property("StarboardEmojiName") + .HasColumnType("longtext") + .HasColumnName("starboardEmojiName"); + + b.Property("StarboardThreshold") + .HasColumnType("int") + .HasColumnName("starboardThreshold"); + + b.HasKey("Id"); + + b.ToTable("Configs"); + }); + + modelBuilder.Entity("MADS.Entities.GuildDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("DiscordId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordId"); + + b.Property("SettingsId") + .HasColumnType("bigint unsigned"); + + b.HasKey("Id"); + + b.HasIndex("SettingsId"); + + b.ToTable("Guilds"); + }); + + modelBuilder.Entity("MADS.Entities.IncidentDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("CreationTimeStamp") + .HasColumnType("datetime(6)"); + + b.Property("GuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("guild_id"); + + b.Property("ModeratorId") + .HasColumnType("bigint unsigned") + .HasColumnName("moderator_id"); + + b.Property("Reason") + .IsRequired() + .HasColumnType("longtext") + .HasColumnName("reason"); + + b.Property("TargetId") + .HasColumnType("bigint unsigned") + .HasColumnName("target_id"); + + b.Property("TargetUserId") + .HasColumnType("bigint unsigned"); + + b.HasKey("Id"); + + b.HasIndex("GuildId"); + + b.HasIndex("TargetUserId"); + + b.ToTable("Incidents"); + }); + + modelBuilder.Entity("MADS.Entities.QuoteDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("Content") + .IsRequired() + .HasMaxLength(1000) + .HasColumnType("varchar(1000)") + .HasColumnName("content"); + + b.Property("CreatedAt") + .HasColumnType("datetime(6)") + .HasColumnName("timestamp"); + + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordGuildId"); + + b.Property("GuildId") + .HasColumnType("bigint unsigned"); + + b.Property("QuotedUserId") + .HasColumnType("bigint unsigned") + .HasColumnName("quotedUserId"); + + b.Property("UserId") + .HasColumnType("bigint unsigned") + .HasColumnName("UserId"); + + b.HasKey("Id"); + + b.HasIndex("GuildId"); + + b.ToTable("Quotes"); + }); + + modelBuilder.Entity("MADS.Entities.ReminderDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("ChannelId") + .HasColumnType("bigint unsigned") + .HasColumnName("channelId"); + + b.Property("CreationTime") + .HasColumnType("datetime(6)") + .HasColumnName("creationTime"); + + b.Property("ExecutionTime") + .HasColumnType("datetime(6)") + .HasColumnName("executionTime"); + + b.Property("IsPrivate") + .HasColumnType("tinyint(1)") + .HasColumnName("isPrivate"); + + b.Property("MentionedChannel") + .HasColumnType("bigint unsigned") + .HasColumnName("mentionedChannel"); + + b.Property("MentionedMessage") + .HasColumnType("bigint unsigned") + .HasColumnName("MentionedMessage"); + + b.Property("MessageId") + .HasColumnType("bigint unsigned") + .HasColumnName("messageId"); + + b.Property("ReminderText") + .IsRequired() + .HasColumnType("longtext") + .HasColumnName("reminderText"); + + b.Property("UserId") + .HasColumnType("bigint unsigned") + .HasColumnName("userId"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Reminders"); + }); + + modelBuilder.Entity("MADS.Entities.StarboardMessageDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("DiscordChannelId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordChannelId"); + + b.Property("DiscordGuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordGuildId"); + + b.Property("DiscordMessageId") + .HasColumnType("bigint unsigned") + .HasColumnName("discordMessageId"); + + b.Property("StarboardChannelId") + .HasColumnType("bigint unsigned") + .HasColumnName("starboardChannelId"); + + b.Property("StarboardGuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("starboardGuildId"); + + b.Property("StarboardMessageId") + .HasColumnType("bigint unsigned") + .HasColumnName("starboardMessageId"); + + b.Property("Stars") + .HasColumnType("int") + .HasColumnName("starCount"); + + b.HasKey("Id"); + + b.ToTable("Starboard"); + }); + + modelBuilder.Entity("MADS.Entities.UserDbEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("Discriminator") + .HasColumnType("int") + .HasColumnName("discriminator"); + + b.Property("PreferedLanguage") + .IsRequired() + .HasColumnType("longtext") + .HasColumnName("prefered_language"); + + b.Property("Username") + .IsRequired() + .HasColumnType("longtext") + .HasColumnName("username"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("MADS.Entities.VoiceAlert", b => + { + b.Property("AlertId") + .ValueGeneratedOnAdd() + .HasColumnType("bigint unsigned") + .HasColumnName("id"); + + b.Property("ChannelId") + .HasColumnType("bigint unsigned") + .HasColumnName("channel_id"); + + b.Property("GuildId") + .HasColumnType("bigint unsigned") + .HasColumnName("guild_id"); + + b.Property("IsRepeatable") + .HasColumnType("tinyint(1)") + .HasColumnName("is_repeatable"); + + b.Property("LastAlert") + .HasColumnType("datetime(6)") + .HasColumnName("last_alert"); + + b.Property("MinTimeBetweenAlerts") + .HasColumnType("time(6)") + .HasColumnName("time_between"); + + b.Property("UserId") + .HasColumnType("bigint unsigned") + .HasColumnName("user_id"); + + b.HasKey("AlertId"); + + b.HasIndex("UserId"); + + b.ToTable("VoiceAlerts"); + }); + + modelBuilder.Entity("MADS.Entities.GuildDbEntity", b => + { + b.HasOne("MADS.Entities.GuildConfigDbEntity", "Settings") + .WithMany() + .HasForeignKey("SettingsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Settings"); + }); + + modelBuilder.Entity("MADS.Entities.IncidentDbEntity", b => + { + b.HasOne("MADS.Entities.GuildDbEntity", "Guild") + .WithMany("Incidents") + .HasForeignKey("GuildId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MADS.Entities.UserDbEntity", "TargetUser") + .WithMany("Incidents") + .HasForeignKey("TargetUserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Guild"); + + b.Navigation("TargetUser"); + }); + + modelBuilder.Entity("MADS.Entities.QuoteDbEntity", b => + { + b.HasOne("MADS.Entities.GuildDbEntity", "Guild") + .WithMany("Quotes") + .HasForeignKey("GuildId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Guild"); + }); + + modelBuilder.Entity("MADS.Entities.ReminderDbEntity", b => + { + b.HasOne("MADS.Entities.UserDbEntity", "User") + .WithMany("Reminders") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("MADS.Entities.VoiceAlert", b => + { + b.HasOne("MADS.Entities.UserDbEntity", "User") + .WithMany("VoiceAlerts") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("MADS.Entities.GuildDbEntity", b => + { + b.Navigation("Incidents"); + + b.Navigation("Quotes"); + }); + + modelBuilder.Entity("MADS.Entities.UserDbEntity", b => + { + b.Navigation("Incidents"); + + b.Navigation("Reminders"); + + b.Navigation("VoiceAlerts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ModularAssistentForDiscordServer/Migrations/20240405004119_RemoveConfigs.cs b/ModularAssistentForDiscordServer/Migrations/20240405004119_RemoveConfigs.cs new file mode 100644 index 0000000..2972a1e --- /dev/null +++ b/ModularAssistentForDiscordServer/Migrations/20240405004119_RemoveConfigs.cs @@ -0,0 +1,157 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MADS.Migrations +{ + /// + public partial class RemoveConfigs : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Guilds_Configs_discordId", + table: "Guilds"); + + migrationBuilder.DropForeignKey( + name: "FK_Incidents_Guilds_guild_id", + table: "Incidents"); + + migrationBuilder.DropForeignKey( + name: "FK_Quotes_Guilds_discordGuildId", + table: "Quotes"); + + migrationBuilder.DropIndex( + name: "IX_Quotes_discordGuildId", + table: "Quotes"); + + migrationBuilder.DropUniqueConstraint( + name: "AK_Guilds_discordId", + table: "Guilds"); + + migrationBuilder.DropUniqueConstraint( + name: "AK_Configs_discordId", + table: "Configs"); + + migrationBuilder.AddColumn( + name: "GuildId", + table: "Quotes", + type: "bigint unsigned", + nullable: false, + defaultValue: 0ul); + + migrationBuilder.AddColumn( + name: "SettingsId", + table: "Guilds", + type: "bigint unsigned", + nullable: false, + defaultValue: 0ul); + + migrationBuilder.CreateIndex( + name: "IX_Quotes_GuildId", + table: "Quotes", + column: "GuildId"); + + migrationBuilder.CreateIndex( + name: "IX_Guilds_SettingsId", + table: "Guilds", + column: "SettingsId"); + + migrationBuilder.AddForeignKey( + name: "FK_Guilds_Configs_SettingsId", + table: "Guilds", + column: "SettingsId", + principalTable: "Configs", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Incidents_Guilds_guild_id", + table: "Incidents", + column: "guild_id", + principalTable: "Guilds", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Quotes_Guilds_GuildId", + table: "Quotes", + column: "GuildId", + principalTable: "Guilds", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Guilds_Configs_SettingsId", + table: "Guilds"); + + migrationBuilder.DropForeignKey( + name: "FK_Incidents_Guilds_guild_id", + table: "Incidents"); + + migrationBuilder.DropForeignKey( + name: "FK_Quotes_Guilds_GuildId", + table: "Quotes"); + + migrationBuilder.DropIndex( + name: "IX_Quotes_GuildId", + table: "Quotes"); + + migrationBuilder.DropIndex( + name: "IX_Guilds_SettingsId", + table: "Guilds"); + + migrationBuilder.DropColumn( + name: "GuildId", + table: "Quotes"); + + migrationBuilder.DropColumn( + name: "SettingsId", + table: "Guilds"); + + migrationBuilder.AddUniqueConstraint( + name: "AK_Guilds_discordId", + table: "Guilds", + column: "discordId"); + + migrationBuilder.AddUniqueConstraint( + name: "AK_Configs_discordId", + table: "Configs", + column: "discordId"); + + migrationBuilder.CreateIndex( + name: "IX_Quotes_discordGuildId", + table: "Quotes", + column: "discordGuildId"); + + migrationBuilder.AddForeignKey( + name: "FK_Guilds_Configs_discordId", + table: "Guilds", + column: "discordId", + principalTable: "Configs", + principalColumn: "discordId", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Incidents_Guilds_guild_id", + table: "Incidents", + column: "guild_id", + principalTable: "Guilds", + principalColumn: "discordId", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Quotes_Guilds_discordGuildId", + table: "Quotes", + column: "discordGuildId", + principalTable: "Guilds", + principalColumn: "discordId", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/ModularAssistentForDiscordServer/Migrations/MadsContextModelSnapshot.cs b/ModularAssistentForDiscordServer/Migrations/MadsContextModelSnapshot.cs index 8f49ecd..0d77eaa 100644 --- a/ModularAssistentForDiscordServer/Migrations/MadsContextModelSnapshot.cs +++ b/ModularAssistentForDiscordServer/Migrations/MadsContextModelSnapshot.cs @@ -16,7 +16,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("ProductVersion", "8.0.1") .HasAnnotation("Relational:MaxIdentifierLength", 64); modelBuilder.Entity("MADS.Entities.GuildConfigDbEntity", b => @@ -31,6 +31,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnName("discordId"); b.Property("Prefix") + .IsRequired() .HasColumnType("longtext") .HasColumnName("prefix"); @@ -70,8 +71,13 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("bigint unsigned") .HasColumnName("discordId"); + b.Property("SettingsId") + .HasColumnType("bigint unsigned"); + b.HasKey("Id"); + b.HasIndex("SettingsId"); + b.ToTable("Guilds"); }); @@ -102,11 +108,14 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("bigint unsigned") .HasColumnName("target_id"); + b.Property("TargetUserId") + .HasColumnType("bigint unsigned"); + b.HasKey("Id"); b.HasIndex("GuildId"); - b.HasIndex("TargetId"); + b.HasIndex("TargetUserId"); b.ToTable("Incidents"); }); @@ -132,6 +141,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("bigint unsigned") .HasColumnName("discordGuildId"); + b.Property("GuildId") + .HasColumnType("bigint unsigned"); + b.Property("QuotedUserId") .HasColumnType("bigint unsigned") .HasColumnName("quotedUserId"); @@ -142,7 +154,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); - b.HasIndex("DiscordGuildId"); + b.HasIndex("GuildId"); b.ToTable("Quotes"); }); @@ -305,9 +317,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("MADS.Entities.GuildDbEntity", b => { b.HasOne("MADS.Entities.GuildConfigDbEntity", "Settings") - .WithOne("Guild") - .HasForeignKey("MADS.Entities.GuildDbEntity", "DiscordId") - .HasPrincipalKey("MADS.Entities.GuildConfigDbEntity", "DiscordGuildId") + .WithMany() + .HasForeignKey("SettingsId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -319,13 +330,12 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasOne("MADS.Entities.GuildDbEntity", "Guild") .WithMany("Incidents") .HasForeignKey("GuildId") - .HasPrincipalKey("DiscordId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); b.HasOne("MADS.Entities.UserDbEntity", "TargetUser") .WithMany("Incidents") - .HasForeignKey("TargetId") + .HasForeignKey("TargetUserId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -338,8 +348,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) { b.HasOne("MADS.Entities.GuildDbEntity", "Guild") .WithMany("Quotes") - .HasForeignKey("DiscordGuildId") - .HasPrincipalKey("DiscordId") + .HasForeignKey("GuildId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -368,11 +377,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("User"); }); - modelBuilder.Entity("MADS.Entities.GuildConfigDbEntity", b => - { - b.Navigation("Guild"); - }); - modelBuilder.Entity("MADS.Entities.GuildDbEntity", b => { b.Navigation("Incidents"); diff --git a/ModularAssistentForDiscordServer/ModularAssistentForDiscordServer.csproj b/ModularAssistentForDiscordServer/ModularAssistentForDiscordServer.csproj index b220812..e1e7ee1 100644 --- a/ModularAssistentForDiscordServer/ModularAssistentForDiscordServer.csproj +++ b/ModularAssistentForDiscordServer/ModularAssistentForDiscordServer.csproj @@ -8,6 +8,7 @@ MADS latest false + en-US @@ -23,20 +24,20 @@ - - - - - - - + + + + + + + - - - - - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -48,10 +49,11 @@ - + - + + diff --git a/ModularAssistentForDiscordServer/ModularDiscordBot.cs b/ModularAssistentForDiscordServer/ModularDiscordBot.cs index b9abb12..19c8b48 100644 --- a/ModularAssistentForDiscordServer/ModularDiscordBot.cs +++ b/ModularAssistentForDiscordServer/ModularDiscordBot.cs @@ -72,11 +72,12 @@ await Host.CreateDefaultBuilder() }); x.InterruptJobsOnShutdownWithWait = true; x.UseSimpleTypeLoader(); - x.SchedulerName = "reminder-scheduler"; }) .AddQuartzHostedService(options => { options.WaitForJobsToComplete = true; + options.StartDelay = TimeSpan.FromSeconds(10); + options.AwaitApplicationStarted = true; }) .AddSingleton() .AddHostedService(s => s.GetRequiredService()) diff --git a/ModularAssistentForDiscordServer/Services/DiscordClientService.cs b/ModularAssistentForDiscordServer/Services/DiscordClientService.cs index 44abc31..f0ed9f3 100644 --- a/ModularAssistentForDiscordServer/Services/DiscordClientService.cs +++ b/ModularAssistentForDiscordServer/Services/DiscordClientService.cs @@ -69,7 +69,7 @@ LoggingService loggingService }; DiscordClient = new DiscordClient(discordConfig); - EventListener.GuildDownload(DiscordClient); + DiscordClient.GuildDownloadCompleted += EventListener.UpdateDb; DiscordClient.GuildCreated += EventListener.OnGuildCreated; DiscordClient.GuildDeleted += EventListener.OnGuildDeleted; DiscordClient.GuildAvailable += EventListener.OnGuildAvailable; diff --git a/ModularAssistentForDiscordServer/Services/LoggingService.cs b/ModularAssistentForDiscordServer/Services/LoggingService.cs index e14a4ca..e9d5716 100644 --- a/ModularAssistentForDiscordServer/Services/LoggingService.cs +++ b/ModularAssistentForDiscordServer/Services/LoggingService.cs @@ -41,7 +41,7 @@ public class LoggingService private static readonly Serilog.ILogger _logger = Log.ForContext(); - internal LoggingService() + public LoggingService() { Directory.CreateDirectory(_dirPath); diff --git a/ModularAssistentForDiscordServer/Services/MessageSnipeService.cs b/ModularAssistentForDiscordServer/Services/MessageSnipeService.cs index dea24a4..5603ac8 100644 --- a/ModularAssistentForDiscordServer/Services/MessageSnipeService.cs +++ b/ModularAssistentForDiscordServer/Services/MessageSnipeService.cs @@ -71,7 +71,7 @@ MessageDeleteEventArgs e return Task.CompletedTask; } - if (e.Message.WebhookMessage ?? false) + if (e.Message.WebhookMessage) { return Task.CompletedTask; } @@ -93,7 +93,7 @@ private Task MessageSniperEdited MessageUpdateEventArgs e ) { - if (e.Message.WebhookMessage ?? false) + if (e.Message.WebhookMessage) { return Task.CompletedTask; } diff --git a/ModularAssistentForDiscordServer/Services/QuotesService.cs b/ModularAssistentForDiscordServer/Services/QuotesService.cs index 50b0578..5d15fab 100644 --- a/ModularAssistentForDiscordServer/Services/QuotesService.cs +++ b/ModularAssistentForDiscordServer/Services/QuotesService.cs @@ -29,14 +29,14 @@ public QuotesService(IDbContextFactory factory) public async Task> GetQuotesGuildAsync(ulong guildId) { await using MadsContext db = await _dbContextFactory.CreateDbContextAsync(); - return db.Quotes.Where(x => x.DiscordGuildId == guildId).ToList(); + return db.Quotes.AsNoTracking().Where(x => x.DiscordGuildId == guildId).ToList(); } public async Task GetRndGuildAsync(ulong guildId) { await using MadsContext db = await _dbContextFactory.CreateDbContextAsync(); Random rnd = new(); - QuoteDbEntity[] quotes = db.Quotes.Where(x => x.DiscordGuildId == guildId).ToArray(); + QuoteDbEntity[] quotes = db.Quotes.AsNoTracking().Where(x => x.DiscordGuildId == guildId).ToArray(); return quotes[rnd.Next(0, quotes.Length)]; } diff --git a/ModularAssistentForDiscordServer/Services/ReminderService.cs b/ModularAssistentForDiscordServer/Services/ReminderService.cs index 068205d..cb6ad4d 100644 --- a/ModularAssistentForDiscordServer/Services/ReminderService.cs +++ b/ModularAssistentForDiscordServer/Services/ReminderService.cs @@ -46,8 +46,6 @@ public ReminderService(IDbContextFactory dbContextFactory, ISchedul _restClient = rest; } - private IScheduler? _reminderScheduler; - public async Task StartAsync(CancellationToken cancellationToken) { if (_isRunning) @@ -60,13 +58,13 @@ public async Task StartAsync(CancellationToken cancellationToken) throw new UnreachableException(); } - _reminderScheduler = await _schedulerFactory.GetScheduler("reminder-scheduler", cancellationToken); - if (_reminderScheduler == null) + IScheduler scheduler = await _schedulerFactory.GetScheduler(cancellationToken); + if (scheduler == null) { throw new NullReferenceException(); } - await _reminderScheduler.Start(cancellationToken); + await scheduler.Start(cancellationToken); _isRunning = true; _logger.Information("Reminders active"); } @@ -85,11 +83,6 @@ public async Task StopAsync(CancellationToken cancellationToken) _isDisposed = true; _isRunning = false; - if (_reminderScheduler is not null) - { - await _reminderScheduler.Shutdown(cancellationToken); - } - _logger.Information("Reminders stopped"); } @@ -113,16 +106,12 @@ private async Task DispatchReminder(ReminderDbEntity reminder) public async Task AddReminder(ReminderDbEntity reminder) { - if (_reminderScheduler is null) - { - throw new NullReferenceException(); - } - if (reminder.ExecutionTime < DateTimeOffset.Now) { throw new ArgumentException("Execution time cannot be in the past"); } + IScheduler scheduler = await _schedulerFactory.GetScheduler(); await using MadsContext db = await _dbContextFactory.CreateDbContextAsync(); db.Reminders.Add(reminder); @@ -147,7 +136,7 @@ public async Task AddReminder(ReminderDbEntity reminder) .StartAt(reminder.ExecutionTime) .Build(); - await _reminderScheduler.ScheduleJob(job, trigger); + await scheduler.ScheduleJob(job, trigger); return dbEntity; } @@ -159,11 +148,7 @@ public async Task> GetByUserAsync(ulong userId) public async Task TryDeleteById(ulong reminderId) { - if (_reminderScheduler is null) - { - throw new NullReferenceException(); - } - + IScheduler scheduler = await _schedulerFactory.GetScheduler(); await using MadsContext db = await _dbContextFactory.CreateDbContextAsync(); ReminderDbEntity? reminder = db.Reminders.FirstOrDefault(x => x.Id == reminderId); @@ -174,7 +159,7 @@ public async Task TryDeleteById(ulong reminderId) } JobKey jobKey = new($"reminder-{reminder.Id}", "reminders"); - await _reminderScheduler.DeleteJob(jobKey); + await scheduler.DeleteJob(jobKey); db.Reminders.Remove(reminder); await db.SaveChangesAsync(); @@ -185,7 +170,7 @@ public async Task TryDeleteById(ulong reminderId) { await using MadsContext db = await _dbContextFactory.CreateDbContextAsync(); - ReminderDbEntity? reminder = db.Reminders.FirstOrDefault(x => x.Id == id); + ReminderDbEntity? reminder = db.Reminders.AsNoTracking().FirstOrDefault(x => x.Id == id); return reminder; } diff --git a/ModularAssistentForDiscordServer/Services/StarboardService.cs b/ModularAssistentForDiscordServer/Services/StarboardService.cs index d1e46e0..1938a84 100644 --- a/ModularAssistentForDiscordServer/Services/StarboardService.cs +++ b/ModularAssistentForDiscordServer/Services/StarboardService.cs @@ -158,7 +158,7 @@ private async Task HandleEvent(DiscordReactionUpdateEvent e) await using MadsContext db = await _dbFactory.CreateDbContextAsync(); GuildConfigDbEntity? guildSettings = - db.Configs.FirstOrDefault(x => x.DiscordGuildId == e.Message.Channel!.GuildId); + db.Configs.AsNoTracking().FirstOrDefault(x => x.DiscordGuildId == e.Message.Channel!.GuildId); if (guildSettings is null) { diff --git a/ModularAssistentForDiscordServer/Services/TranslateInformationService.cs b/ModularAssistentForDiscordServer/Services/TranslateInformationService.cs index c0abeae..6137ca0 100644 --- a/ModularAssistentForDiscordServer/Services/TranslateInformationService.cs +++ b/ModularAssistentForDiscordServer/Services/TranslateInformationService.cs @@ -42,7 +42,7 @@ public async void SetPreferredLanguage(ulong userId, string language) public async Task GetPreferredLanguage(ulong userId) { await using MadsContext db = await _dbContextFactory.CreateDbContextAsync(); - UserDbEntity? user = db.Users.FirstOrDefault(x => x.Id == userId); + UserDbEntity? user = db.Users.AsNoTracking().FirstOrDefault(x => x.Id == userId); return user?.PreferedLanguage; } diff --git a/ModularAssistentForDiscordServer/Services/VoiceAlertService.cs b/ModularAssistentForDiscordServer/Services/VoiceAlertService.cs index a47b78e..ab188e3 100644 --- a/ModularAssistentForDiscordServer/Services/VoiceAlertService.cs +++ b/ModularAssistentForDiscordServer/Services/VoiceAlertService.cs @@ -97,6 +97,7 @@ private async Task HandleEvent(VoiceStateUpdateEventArgs e) await using MadsContext context = await _contextFactory.CreateDbContextAsync(); List alerts = await context.VoiceAlerts + .AsNoTracking() .Where(x => x.ChannelId == e.After.Channel.Id && e.User.Id != x.UserId) .ToListAsync(); @@ -153,6 +154,7 @@ private async Task HandleEvent(VoiceStateUpdateEventArgs e) { await using MadsContext context = await _contextFactory.CreateDbContextAsync(); UserDbEntity? user = await context.Users + .AsNoTracking() .Include(x => x.VoiceAlerts) .FirstOrDefaultAsync(x => x.Id == userId); if (user == null) @@ -189,6 +191,7 @@ public async Task RemoveVoiceAlert(ulong userId, ulong channelId, ulong guildId) { await using MadsContext context = await _contextFactory.CreateDbContextAsync(); UserDbEntity? user = await context.Users + .AsNoTracking() .Include(x => x.VoiceAlerts) .FirstOrDefaultAsync(x => x.Id == userId); if (user == null) diff --git a/QuartzNetDocker/tables_mysql_innodb.sql b/QuartzNetDocker/tables_mysql_innodb.sql index 4380653..51805c3 100644 --- a/QuartzNetDocker/tables_mysql_innodb.sql +++ b/QuartzNetDocker/tables_mysql_innodb.sql @@ -1,7 +1,7 @@ # By: Ron Cordell - roncordell # I didn't see this anywhere, so I thought I'd post it here. This is the script from Quartz to create the tables in a MySQL database, modified to use INNODB instead of MYISAM. -USE quartznet; +USE quartz; DROP TABLE IF EXISTS QRTZ_FIRED_TRIGGERS; DROP TABLE IF EXISTS QRTZ_PAUSED_TRIGGER_GRPS;