Renamed the bot from KickBot -> ChatBot and removed the reference to Kick in the project name

This commit is contained in:
barelyprofessional
2024-07-26 16:50:39 +08:00
parent f4db00246a
commit f9d87220d2
46 changed files with 84 additions and 84 deletions

View File

@@ -0,0 +1,391 @@
using System.Text.Json;
using KfChatDotNetBot.Models;
using KfChatDotNetBot.Models.DbModels;
using NLog;
namespace KfChatDotNetBot.Settings;
public static class BuiltIn
{
// Creates DB options if they don't exist and all fields (except value) if these have changed in code
public static async Task SyncSettingsWithDb()
{
var logger = LogManager.GetCurrentClassLogger();
await using var db = new ApplicationDbContext();
logger.Info($"Syncing {BuiltInSettings.Count} settings with the DB");
foreach (var builtIn in BuiltInSettings)
{
var setting = db.Settings.FirstOrDefault(setting => setting.Key == builtIn.Key);
if (setting == null)
{
logger.Info($"{builtIn.Key} doesn't exist in the DB, creating");
db.Settings.Add(new SettingDbModel
{
Key = builtIn.Key,
Value = builtIn.Default,
Regex = builtIn.Regex,
Description = builtIn.Description,
Default = builtIn.Default,
IsSecret = builtIn.IsSecret
});
continue;
}
logger.Debug($"{builtIn.Key} exists in the DB, now going to ensure its fields are consistent");
setting.Key = builtIn.Key;
setting.Regex = builtIn.Regex;
setting.Description = builtIn.Description;
setting.Default = builtIn.Default;
setting.IsSecret = builtIn.IsSecret;
}
logger.Info("Saving changes to the DB");
await db.SaveChangesAsync();
}
public static async Task MigrateJsonSettingsToDb()
{
var oldConfigPath = "config.json";
var logger = LogManager.GetCurrentClassLogger();
await using var db = new ApplicationDbContext();
logger.Info($"Checking {oldConfigPath} exists");
if (!Path.Exists(oldConfigPath))
{
logger.Info($"{oldConfigPath} does not exist. Migration already performed or was never needed");
return;
}
logger.Info($"Migrating {oldConfigPath}");
#pragma warning disable CS0612 // Type or member is obsolete
var oldConfig = JsonSerializer.Deserialize<ConfigModel>(await File.ReadAllTextAsync(oldConfigPath));
#pragma warning restore CS0612 // Type or member is obsolete
if (oldConfig == null)
{
logger.Error($"Caught a null when deserializing {oldConfigPath}");
return;
}
await Helpers.SetValue(Keys.PusherEndpoint, oldConfig.PusherEndpoint.ToString());
await Helpers.SetValue(Keys.KiwiFarmsWsEndpoint, oldConfig.KfWsEndpoint.ToString());
await Helpers.SetValueAsList(Keys.PusherChannels, oldConfig.PusherChannels);
await Helpers.SetValue(Keys.KiwiFarmsRoomId, oldConfig.KfChatRoomId);
await Helpers.SetValue(Keys.Proxy, oldConfig.Proxy);
await Helpers.SetValue(Keys.KiwiFarmsWsReconnectTimeout, oldConfig.KfReconnectTimeout);
await Helpers.SetValue(Keys.PusherReconnectTimeout, oldConfig.PusherReconnectTimeout);
await Helpers.SetValueAsBoolean(Keys.GambaSeshDetectEnabled, oldConfig.EnableGambaSeshDetect);
await Helpers.SetValue(Keys.GambaSeshUserId, oldConfig.GambaSeshUserId);
await Helpers.SetValue(Keys.KickIcon, oldConfig.KickIcon);
await Helpers.SetValue(Keys.KiwiFarmsDomain, oldConfig.KfDomain);
await Helpers.SetValue(Keys.KiwiFarmsUsername, oldConfig.KfUsername);
await Helpers.SetValue(Keys.KiwiFarmsPassword, oldConfig.KfPassword);
await Helpers.SetValue(Keys.KiwiFarmsChromiumPath, oldConfig.ChromiumPath);
await Helpers.SetValue(Keys.TwitchBossmanJackId, oldConfig.BossmanJackTwitchId);
await Helpers.SetValue(Keys.TwitchBossmanJackUsername, oldConfig.BossmanJackTwitchUsername);
await Helpers.SetValueAsBoolean(Keys.KiwiFarmsSuppressChatMessages, oldConfig.SuppressChatMessages);
await Helpers.SetValue(Keys.DiscordToken, oldConfig.DiscordToken);
await Helpers.SetValue(Keys.DiscordBmjId, oldConfig.DiscordBmjId);
logger.Info($"{oldConfigPath} migration done.");
logger.Info("Renaming files no longer in use");
// Utils.SafelyRenameFile will attempt to rename and swallow any exception (with logging) if it fails
Utils.SafelyRenameFile(oldConfigPath, $"{oldConfigPath}.migrated");
logger.Info("File renamed");
}
public static List<BuiltInSettingsModel> BuiltInSettings =
[
new BuiltInSettingsModel
{
Key = Keys.PusherEndpoint,
Regex = @".+",
Description =
"Pusher WebSocket endpoint URL",
Default = "wss://ws-us2.pusher.com/app/eb1d5f283081a78b932c?protocol=7&client=js&version=7.6.0&flash=false",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.KiwiFarmsWsEndpoint,
Regex = @".+",
Description =
"Kiwi Farms chat WebSocket endpoint",
Default = "wss://kiwifarms.st:9443/chat.ws",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.PusherChannels,
Regex = @".+",
Description =
"List of Pusher channels to subscribe to",
Default = null,
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.KiwiFarmsRoomId,
Regex = @"\d+",
Description =
"Kiwi Farms Keno Kasino room ID",
Default = "15",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.Proxy,
Regex = @".+",
Description =
"Proxy to use for all outgoing requests. Null to disable",
Default = null,
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.KiwiFarmsWsReconnectTimeout,
Regex = @"\d+",
Description =
"Kiwi Farms chat reconnect timeout",
Default = "30",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.PusherReconnectTimeout,
Regex = @"\d+",
Description =
"Pusher reconnect timeout",
Default = "30",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.GambaSeshDetectEnabled,
Regex = @"true|false",
Description =
"Whether to enable detection for the presence of GambaSesh",
Default = "true",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.GambaSeshUserId,
Regex = @"\d+",
Description =
"GambaSesh's uer ID for the purposes of detection",
Default = "168162",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.KickIcon,
Regex = @".+",
Description =
"Kick Icon to use for relaying chat messages",
Default = "https://i.postimg.cc/Qtw4nCPG/kick16.png",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.KiwiFarmsDomain,
Regex = @".+",
Description =
"Domain to use when retrieving a session token",
Default = "kiwifarms.st",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.KiwiFarmsUsername,
Regex = @".+",
Description =
"Username to use when authenticating with Kiwi Farms",
Default = null,
IsSecret = true
},
new BuiltInSettingsModel
{
Key = Keys.KiwiFarmsPassword,
Regex = @".+",
Description =
"Password to use when authenticating with Kiwi Farms",
Default = null,
IsSecret = true
},
new BuiltInSettingsModel
{
Key = Keys.KiwiFarmsChromiumPath,
Regex = @".+",
Description =
"Path to download the Chromium install used for the token grabber",
Default = "chromium_install",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.TwitchBossmanJackId,
Regex = @"\d+",
Description =
"BossmanJack's Twitch channel ID",
Default = "114122847",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.TwitchBossmanJackUsername,
Regex = @".+",
Description =
"BossmanJack's Twitch channel username",
Default = "thebossmanjack",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.KiwiFarmsSuppressChatMessages,
Regex = @"true|false",
Description =
"Enable to prevent messages from actually being sent to chat.",
Default = "false",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.DiscordToken,
Regex = @".+",
Description =
"Token to use when authenticating with Discord. Set to null to disable.",
Default = null,
IsSecret = true
},
new BuiltInSettingsModel
{
Key = Keys.DiscordBmjId,
Regex = @"\d+",
Description =
"BossmanJack's Discord user ID",
Default = "554123642246529046",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.TwitchIcon,
Regex = ".+",
Description = "URL for the 16px Twitch icon",
Default = "https://i.postimg.cc/QMFVV2Xk/twitch16.png",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.DiscordIcon,
Regex = ".+",
Description = "URL for the 16px Discord icon",
Default = "https://i.postimg.cc/cLmQrp89/discord16.png",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.ShuffleBmjUsername,
Regex = ".+",
Description = "Bossman's Shuffle Username",
Default = "TheBossmanJack",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.JuiceCooldown,
Regex = @"\d+",
Description = "Cooldown (in seconds) until you can get juiced again",
Default = "3600",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.JuiceAmount,
Regex = @"\d+",
Description = "Amount of $KKK to juice",
Default = "50",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.KiwiFarmsToken,
Regex = ".+",
Description = "Last successfully retrieved forum token (will be refreshed automatically if expired)",
Default = null,
IsSecret = true
},
new BuiltInSettingsModel
{
Key = Keys.KickEnabled,
Regex = "true|false",
Description = "Whether to enable Kick functionality (Pusher websocket mainly)",
Default = "true",
IsSecret = false
},
new BuiltInSettingsModel
{
Key = Keys.HowlggDivisionAmount,
Regex = @"\d+",
Description = "How much to divide the Howlgg bets/profit by to get the real value",
Default = "1650",
IsSecret = false
},
new BuiltInSettingsModel()
{
Key = Keys.KiwiFarmsGreenColor,
Regex = ".+",
Description = "Green color used for showing positive values in chat",
Default = "#3dd179",
IsSecret = false
},
new BuiltInSettingsModel()
{
Key = Keys.KiwiFarmsRedColor,
Regex = ".+",
Description = "Red color used for showing negative values in chat",
Default = "#f1323e",
IsSecret = false
},
new BuiltInSettingsModel()
{
Key = Keys.JackpotBmjUsername,
Regex = ".+",
Description = "Bossman's username on Jackpot",
Default = "TheBossmanJack",
IsSecret = false
}
];
public static class Keys
{
public static string PusherEndpoint = "Pusher.Endpoint";
public static string KiwiFarmsWsEndpoint = "KiwiFarms.WsEndpoint";
public static string PusherChannels = "Pusher.Channels";
public static string KiwiFarmsRoomId = "KiwiFarms.RoomId";
public static string Proxy = "Proxy";
public static string KiwiFarmsWsReconnectTimeout = "KiwiFarms.WsReconnectTimeout";
public static string PusherReconnectTimeout = "Pusher.ReconnectTimeout";
public static string GambaSeshDetectEnabled = "GambaSesh.DetectEnabled";
public static string GambaSeshUserId = "GambaSesh.UserId";
public static string KickIcon = "Kick.Icon";
public static string KiwiFarmsDomain = "KiwiFarms.Domain";
public static string KiwiFarmsUsername = "KiwiFarms.Username";
public static string KiwiFarmsPassword = "KiwiFarms.Password";
public static string KiwiFarmsChromiumPath = "KiwiFarms.ChromiumPath";
public static string TwitchBossmanJackId = "Twitch.BossmanJackId";
public static string TwitchBossmanJackUsername = "Twitch.BossmanJackUsername";
public static string KiwiFarmsSuppressChatMessages = "KiwiFarms.SuppressChatMessages";
public static string DiscordToken = "Discord.Token";
public static string DiscordBmjId = "Discord.BmjId";
public static string TwitchIcon = "Twitch.Icon";
public static string DiscordIcon = "Discord.Icon";
public static string ShuffleBmjUsername = "Shuffle.BmjUsername";
public static string JuiceCooldown = "Juice.Cooldown";
public static string JuiceAmount = "Juice.Amount";
public static string KiwiFarmsToken = "KiwiFarms.Token";
public static string KickEnabled = "Kick.Enabled";
public static string HowlggDivisionAmount = "Howlgg.DivisionAmount";
public static string KiwiFarmsGreenColor = "KiwiFarms.GreenColor";
public static string KiwiFarmsRedColor = "KiwiFarms.RedColor";
public static string JackpotBmjUsername = "Jackpot.BmjUsername";
}
}

View File

@@ -0,0 +1,173 @@
using KfChatDotNetBot.Models.DbModels;
using Microsoft.EntityFrameworkCore;
using NLog;
namespace KfChatDotNetBot.Settings;
public static class Helpers
{
public static async Task<SettingValue> GetValue(string key, bool caseInsensitive = false)
{
var logger = LogManager.GetCurrentClassLogger();
await using var db = new ApplicationDbContext();
logger.Trace($"Retrieving value for {key}");
SettingDbModel? setting;
if (caseInsensitive)
{
// String comparison doesn't work on EF core if I recall correctly
#pragma warning disable CA1862
setting = await db.Settings.FirstOrDefaultAsync(s => s.Key.ToLower() == key.ToLower());
#pragma warning restore CA1862
}
else
{
setting = await db.Settings.FirstOrDefaultAsync(s => s.Key == key);
}
if (setting == null)
{
logger.Debug($"{key} does not exist, throwing KeyNotFoundException");
throw new KeyNotFoundException($"{key} does not exist");
}
if (setting.Value == "null")
{
logger.Debug($"{key}'s value is null so returning SettingValue(null)");
return new SettingValue(null, null);
}
logger.Debug($"Returning '{setting.Value}' as {typeof(SettingValue)}");
return new SettingValue(setting.Value, setting);
}
public static async Task<Dictionary<string, SettingValue>> GetMultipleValues(string[] keys, bool caseInsensitive = false)
{
var logger = LogManager.GetCurrentClassLogger();
await using var db = new ApplicationDbContext();
logger.Trace($"Getting values for keys {string.Join(", ", keys)}");
Dictionary<string, SettingValue> values = new Dictionary<string, SettingValue>();
foreach (var key in keys)
{
SettingDbModel? setting;
if (caseInsensitive)
{
// String comparison doesn't work on EF core if I recall correctly
#pragma warning disable CA1862
setting = await db.Settings.FirstOrDefaultAsync(s => s.Key.ToLower() == key.ToLower());
#pragma warning restore CA1862
}
else
{
setting = await db.Settings.FirstOrDefaultAsync(s => s.Key == key);
}
if (setting == null)
{
logger.Debug($"{key} does not exist, throwing KeyNotFoundException()");
throw new KeyNotFoundException();
}
if (setting.Value == "null")
{
logger.Debug($"{key}'s value is null so returning SettingValue(null)");
values.Add(key, new SettingValue(null, null));
continue;
}
values.Add(key, new SettingValue(setting.Value, setting));
}
return values;
}
public static async Task SetValue(string key, object? value)
{
var logger = LogManager.GetCurrentClassLogger();
await using var db = new ApplicationDbContext();
string stringValue;
if (value == null)
{
stringValue = "null";
}
else if (value is string)
{
stringValue = (string)value;
}
else
{
stringValue = (string)Convert.ChangeType(value, TypeCode.String);
}
logger.Debug($"Setting {key} to {stringValue}");
var setting = await db.Settings.FirstOrDefaultAsync(s => s.Key == key);
if (setting == null)
{
logger.Debug($"{key} does not exist, throwing KeyNotFoundException()");
throw new KeyNotFoundException();
}
setting.Value = stringValue;
await db.SaveChangesAsync();
}
public static async Task SetValueAsList<T>(string key, List<T> values, char separator = ',')
{
var logger = LogManager.GetCurrentClassLogger();
await using var db = new ApplicationDbContext();
List<string> stringValues = values.Select(val => (string)Convert.ChangeType(val, TypeCode.String)).ToList();
string joinedValue = string.Join(separator, stringValues);
logger.Debug($"Setting {key} to {joinedValue}");
var setting = await db.Settings.FirstOrDefaultAsync(s => s.Key == key);
if (setting == null)
{
logger.Debug($"{key} does not exist, throwing KeyNotFoundException()");
throw new KeyNotFoundException();
}
setting.Value = joinedValue;
await db.SaveChangesAsync();
}
public static async Task SetValueAsKeyValuePairs<T>(string key, Dictionary<string, T> data, char delimiter = ',',
char separator = '=')
{
var logger = LogManager.GetCurrentClassLogger();
await using var db = new ApplicationDbContext();
logger.Debug($"Building data for {key}");
var value = data.Keys.Aggregate(string.Empty,
(current, dictKey) => current + $"{dictKey}{separator}{data[dictKey]}{delimiter}");
// Remove trailing delimiters that would be leftover as it doesn't account for whether it's the last key
value = value.TrimEnd(delimiter);
logger.Debug($"Setting {key} to {value}");
var setting = await db.Settings.FirstOrDefaultAsync(s => s.Key == key);
if (setting == null)
{
logger.Debug($"{key} does not exist, throwing KeyNotFoundException()");
throw new KeyNotFoundException();
}
setting.Value = value;
await db.SaveChangesAsync();
}
public static async Task SetValueAsBoolean(string key, bool value)
{
var logger = LogManager.GetCurrentClassLogger();
await using var db = new ApplicationDbContext();
logger.Debug($"Setting {key} to {value}");
var setting = await db.Settings.FirstOrDefaultAsync(s => s.Key == key);
if (setting == null)
{
logger.Debug($"{key} does not exist, throwing KeyNotFoundException()");
throw new KeyNotFoundException();
}
setting.Value = value ? "true" : "false";
await db.SaveChangesAsync();
}
}

View File

@@ -0,0 +1,9 @@
using KfChatDotNetBot.Models.DbModels;
namespace KfChatDotNetBot.Settings;
public class SettingValue(string? value, SettingDbModel? dbEntry)
{
public string? Value { get; set; } = value;
public SettingDbModel? DbEntry { get; set; } = dbEntry;
}

View File

@@ -0,0 +1,59 @@
using NLog;
namespace KfChatDotNetBot.Settings;
public static class Utils
{
public static List<string> ToList(this SettingValue settingValue, char separator = ',')
{
if (settingValue.Value == null) return new List<string>();
return settingValue.Value.Split(separator).ToList();
}
public static Dictionary<string, T> ToKeyValuePairs<T>(this SettingValue settingValue, char delimiter = ',',
char separator = '=')
{
if (settingValue.Value == null)
{
return new Dictionary<string, T>();
}
return settingValue.Value.Split(delimiter).ToDictionary(kv => kv.Split(separator)[0],
kv => ValueToType<T>(kv.Split(separator)[1]));
}
public static bool ToBoolean(this SettingValue settingValue)
{
var logger = LogManager.GetCurrentClassLogger();
if (settingValue.Value is null or "null")
{
return default;
}
return settingValue.Value.Equals("true", StringComparison.CurrentCultureIgnoreCase);
}
public static T ValueToType<T>(string value)
{
return (T)Convert.ChangeType(value, typeof(T));
}
public static T ToType<T>(this SettingValue settingValue)
{
return (T)Convert.ChangeType(settingValue.Value, typeof(T));
}
public static void SafelyRenameFile(string oldName, string newName)
{
var logger = LogManager.GetCurrentClassLogger();
logger.Debug($"Renaming {oldName} to {newName}");
try
{
File.Move(oldName, newName);
}
catch (Exception e)
{
logger.Error($"Failed to rename {oldName} to {newName}");
logger.Error(e);
}
}
}