Added a generic interface for retrieving images. Where multiple images exist, it'll retrieve the least seen. If there are sufficient images to work with, it'll randomly pick from a subset of the least seen to make it less predictable what's going to show up.

This commit is contained in:
barelyprofessional
2025-02-15 23:31:26 +08:00
parent 5eb2ef62b9
commit 7e9137c35c
10 changed files with 642 additions and 297 deletions

View File

@@ -1,6 +1,7 @@
using System.Text.Json;
using KfChatDotNetBot.Models;
using KfChatDotNetBot.Models.DbModels;
using Microsoft.EntityFrameworkCore;
using NLog;
namespace KfChatDotNetBot.Settings;
@@ -95,6 +96,89 @@ public static class BuiltIn
logger.Info("File renamed");
}
public static async Task MigrateImages()
{
var logger = LogManager.GetCurrentClassLogger();
await using var db = new ApplicationDbContext();
logger.Info("Migrating images to the database table");
if (await db.Images.AnyAsync())
{
logger.Info("Not continuing as there's already images in the database");
return;
}
var imagesToMigrate = await Helpers.GetMultipleValues([
Keys.BotGmKasinoImageRotation, Keys.BotGnKasinoImageRotation, Keys.WinmanjackImgUrl, Keys.BotPraygeImgUrl,
Keys.BotCrackpipeImgUrl
]);
logger.Info("Migrating gmkasino images");
foreach (var image in imagesToMigrate[Keys.BotGmKasinoImageRotation].JsonDeserialize<List<string>>() ?? [])
{
await db.Images.AddAsync(new ImageDbModel {Key = "gmkasino", LastSeen = DateTimeOffset.UtcNow, Url = image});
}
logger.Info("Migrating gnkasino images");
foreach (var image in imagesToMigrate[Keys.BotGnKasinoImageRotation].JsonDeserialize<List<string>>() ?? [])
{
await db.Images.AddAsync(new ImageDbModel {Key = "gnkasino", LastSeen = DateTimeOffset.UtcNow, Url = image});
}
if (imagesToMigrate[Keys.WinmanjackImgUrl].Value != null)
{
logger.Info("Migrating winmanjack");
await db.Images.AddAsync(new ImageDbModel
{
Key = "winmanjack", LastSeen = DateTimeOffset.UtcNow,
Url = imagesToMigrate[Keys.WinmanjackImgUrl].Value!
});
}
if (imagesToMigrate[Keys.BotPraygeImgUrl].Value != null)
{
logger.Info("Migrating prayge");
await db.Images.AddAsync(new ImageDbModel
{
Key = "prayge", LastSeen = DateTimeOffset.UtcNow,
Url = imagesToMigrate[Keys.BotPraygeImgUrl].Value!
});
}
if (imagesToMigrate[Keys.BotCrackpipeImgUrl].Value != null)
{
logger.Info("Migrating crackpipe");
await db.Images.AddAsync(new ImageDbModel
{
Key = "crackpipe", LastSeen = DateTimeOffset.UtcNow,
Url = imagesToMigrate[Keys.BotCrackpipeImgUrl].Value!
});
}
logger.Info("Adding bassmanjack");
await db.Images.AddAsync(new ImageDbModel
{
Key = "bassmanjack", LastSeen = DateTimeOffset.UtcNow,
Url = "https://i.postimg.cc/SRstzMQt/boss-soy-koi.gif"
});
logger.Info("Adding sent");
await db.Images.AddAsync(new ImageDbModel
{
Key = "sent", LastSeen = DateTimeOffset.UtcNow,
Url = "https://i.ibb.co/GHq7hb1/4373-g-N5-HEH2-Hkc.png"
});
logger.Info("Adding helpme");
await db.Images.AddAsync(new ImageDbModel
{
Key = "helpme", LastSeen = DateTimeOffset.UtcNow,
Url = "https://i.postimg.cc/fTw6tGWZ/ineedmoneydumbfuck.png"
});
await db.SaveChangesAsync();
logger.Info("Image migration complete");
}
public static List<BuiltInSettingsModel> BuiltInSettings =
[
@@ -715,6 +799,16 @@ public static class BuiltIn
ValueType = SettingValueType.Boolean
},
new BuiltInSettingsModel
{
Key = Keys.BotImageAcceptableKeys,
Regex = ".+",
Description = "List of valid keys for the image rotation feature",
Default = "[\"gmkasino\", \"gnkasino\", \"winmanjack\", \"prayge\", \"crackpipe\", \"bassmanjack\", \"sent\", \"helpme\"]",
IsSecret = false,
CacheDuration = TimeSpan.FromHours(1),
ValueType = SettingValueType.Array
},
new BuiltInSettingsModel
{
Key = Keys.BotToyStoryImage,
Regex = ".+",
@@ -723,6 +817,19 @@ public static class BuiltIn
IsSecret = false,
CacheDuration = TimeSpan.FromHours(1),
ValueType = SettingValueType.Text
},
new BuiltInSettingsModel
{
Key = Keys.BotImageRandomSliceDivideBy,
Regex = @"\d+",
Description = "What value to divide the image count by for determining how many images to randomly choose from. " +
"e.g. a value of 10 on 50 images means the 5 least seen images are chosen from randomly. " +
"If the count of images is =< this value, it'll just grab the oldest image. " +
"Fractions will be rounded, so a value of 5 with 7 images will round down and take the oldest image.",
Default = "5",
IsSecret = false,
CacheDuration = TimeSpan.FromHours(1),
ValueType = SettingValueType.Text
}
];
@@ -787,6 +894,8 @@ public static class BuiltIn
public static string HowlggEnabled = "Howlgg.Enabled";
public static string ChipsggEnabled = "Chipsgg.Enabled";
public static string RainbetEnabled = "Rainbet.Enabled";
public static string BotImageAcceptableKeys = "Bot.Image.AcceptableKeys";
public static string BotToyStoryImage = "Bot.ToyStoryImage";
public static string BotImageRandomSliceDivideBy = "Bot.Image.RandomSliceDivideBy";
}
}