- Bot now takes shard id (optional) and total shards (optional) command line arguments (changed from shard id and parent process id)

- Sharding with coordinator now works properly
- Documented creds.yml RestartCommand - it has no effect when coordinator is starting the bot
- Regenerated creds_example.yml
- Removed all db migrators as the v3 requires the user to have updated 2.x all the way
- TotalShards in creds.yml gets overriden by coord.yml's TotalShards if the bot is ran through coordinator (more precisely, by the command line argument to the bot)
- Coordinator now runs on http://localhost:3442 by default, you can change this in appsettings.json
    - This is done because of macos https issues
    - Primarily because https for regular users is a massive hassle. Coordinator shouldn't be exposed anyway
- Minor cleanup
This commit is contained in:
Kwoth
2021-06-25 19:14:34 +02:00
parent a1ef862382
commit 670b0aca96
14 changed files with 119 additions and 398 deletions

View File

@@ -1,122 +0,0 @@
using System.Collections.Generic;
using System.Data.Common;
using System.Globalization;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Common.Configs;
using Serilog;
using SixLabors.ImageSharp.PixelFormats;
namespace NadekoBot.Services
{
public sealed class BotConfigMigrator : IConfigMigrator
{
private readonly DbService _db;
private readonly BotConfigService _bss;
public BotConfigMigrator(DbService dbService, BotConfigService bss)
{
_db = dbService;
_bss = bss;
}
public void EnsureMigrated()
{
using var uow = _db.GetDbContext();
using var conn = uow.Database.GetDbConnection();
// check if bot config exists
using (var checkTableCommand = conn.CreateCommand())
{
// make sure table still exists
checkTableCommand.CommandText =
"SELECT name FROM sqlite_master WHERE type='table' AND name='BotConfig';";
var checkReader = checkTableCommand.ExecuteReader();
if (!checkReader.HasRows)
return;
}
MigrateBotConfig(conn);
using var dropBlockedTable = conn.CreateCommand();
dropBlockedTable.CommandText = "DROP TABLE IF EXISTS BlockedCmdOrMdl;";
dropBlockedTable.ExecuteNonQuery();
}
private void MigrateBotConfig(DbConnection conn)
{
using (var checkMigratedCommand = conn.CreateCommand())
{
checkMigratedCommand.CommandText =
"UPDATE BotConfig SET HasMigratedBotSettings = 1 WHERE HasMigratedBotSettings = 0;";
var changedRows = checkMigratedCommand.ExecuteNonQuery();
if (changedRows == 0)
return;
}
Log.Information("Migrating bot settings...");
var blockedCommands = new HashSet<string>();
using (var cmdCom = conn.CreateCommand())
{
cmdCom.CommandText = $"SELECT Name from BlockedCmdOrMdl WHERE BotConfigId is not NULL";
var cmdReader = cmdCom.ExecuteReader();
while (cmdReader.Read())
blockedCommands.Add(cmdReader.GetString(0));
}
var blockedModules = new HashSet<string>();
using (var mdlCom = conn.CreateCommand())
{
mdlCom.CommandText = $"SELECT Name from BlockedCmdOrMdl WHERE BotConfigId is NULL";
var mdlReader = mdlCom.ExecuteReader();
while (mdlReader.Read())
blockedModules.Add(mdlReader.GetString(0));
}
using var com = conn.CreateCommand();
com.CommandText = $@"SELECT DefaultPrefix, ForwardMessages, ForwardToAllOwners,
OkColor, ErrorColor, ConsoleOutputType, DMHelpString, HelpString, RotatingStatuses, Locale, GroupGreets
FROM BotConfig";
using var reader = com.ExecuteReader();
if (!reader.Read())
return;
_bss.ModifyConfig((x) =>
{
x.Prefix = reader.GetString(0);
x.ForwardMessages = reader.GetBoolean(1);
x.ForwardToAllOwners = reader.GetBoolean(2);
x.Color = new ColorConfig()
{
Ok = Rgba32.TryParseHex(reader.GetString(3), out var okColor)
? okColor
: Rgba32.ParseHex("00e584"),
Error = Rgba32.TryParseHex(reader.GetString(4), out var errorColor)
? errorColor
: Rgba32.ParseHex("ee281f"),
};
x.ConsoleOutputType = (ConsoleOutputType) reader.GetInt32(5);
x.DmHelpText = reader.IsDBNull(6) ? string.Empty : reader.GetString(6);
x.HelpText = reader.IsDBNull(7) ? string.Empty : reader.GetString(7);
x.RotateStatuses = reader.GetBoolean(8);
try
{
x.DefaultLocale = new CultureInfo(reader.GetString(9));
}
catch
{
x.DefaultLocale = new CultureInfo("en-US");
}
x.GroupGreets = reader.GetBoolean(10);
x.Blocked.Commands = blockedCommands;
x.Blocked.Modules = blockedModules;
});
Log.Information("Data written to data/bot.yml");
}
}
}

View File

@@ -1,155 +0,0 @@
using System;
using System.Data.Common;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Modules.Gambling.Common;
using NadekoBot.Modules.Gambling.Services;
using Serilog;
namespace NadekoBot.Services
{
public sealed class GamblingConfigMigrator : IConfigMigrator
{
private readonly DbService _db;
private readonly GamblingConfigService _gss;
public GamblingConfigMigrator(DbService dbService, GamblingConfigService gss)
{
_db = dbService;
_gss = gss;
}
public void EnsureMigrated()
{
using var uow = _db.GetDbContext();
using var conn = uow.Database.GetDbConnection();
Migrate(conn);
}
private void Migrate(DbConnection conn)
{
using (var checkTableCommand = conn.CreateCommand())
{
// make sure table still exists
checkTableCommand.CommandText =
"SELECT name FROM sqlite_master WHERE type='table' AND name='BotConfig';";
var checkReader = checkTableCommand.ExecuteReader();
if (!checkReader.HasRows)
return;
}
using (var checkMigratedCommand = conn.CreateCommand())
{
checkMigratedCommand.CommandText =
"UPDATE BotConfig SET HasMigratedGamblingSettings = 1 WHERE HasMigratedGamblingSettings = 0;";
var changedRows = checkMigratedCommand.ExecuteNonQuery();
if (changedRows == 0)
return;
}
Log.Information("Migrating gambling settings...");
using var com = conn.CreateCommand();
com.CommandText = $@"SELECT CurrencyGenerationChance, CurrencyGenerationCooldown,
CurrencySign, CurrencyName, CurrencyGenerationPassword, MinBet, MaxBet, BetflipMultiplier,
TimelyCurrency, TimelyCurrencyPeriod, CurrencyDropAmount, CurrencyDropAmountMax, DailyCurrencyDecay,
DivorcePriceMultiplier, PatreonCurrencyPerCent, MinWaifuPrice, WaifuGiftMultiplier
FROM BotConfig";
using var reader = com.ExecuteReader();
if (!reader.Read())
return;
using (var itemsCommand = conn.CreateCommand())
{
itemsCommand.CommandText = WaifuItemUpdateQuery;
itemsCommand.ExecuteNonQuery();
}
_gss.ModifyConfig(ModifyAction(reader));
Log.Information("Data written to data/gambling.yml");
}
private static Action<GamblingConfig> ModifyAction(DbDataReader reader)
=> realConfig =>
{
realConfig.Currency.Sign = (string) reader["CurrencySign"];
realConfig.Currency.Name = (string) reader["CurrencyName"];
realConfig.MinBet = (int) (long) reader["MinBet"];
realConfig.MaxBet = (int) (long) reader["MaxBet"];
realConfig.BetFlip = new GamblingConfig.BetFlipConfig()
{
Multiplier = (decimal) (double) reader["BetflipMultiplier"],
};
realConfig.Generation = new GamblingConfig.GenerationConfig()
{
MaxAmount = (int) (reader["CurrencyDropAmountMax"] as long? ?? (long) reader["CurrencyDropAmount"]),
MinAmount = (int) (long) reader["CurrencyDropAmount"],
Chance = (decimal) (double) reader["CurrencyGenerationChance"],
GenCooldown = (int) (long) reader["CurrencyGenerationCooldown"],
HasPassword = reader.GetBoolean(4),
};
realConfig.Timely = new GamblingConfig.TimelyConfig()
{
Amount = (int) (long) reader["TimelyCurrency"],
Cooldown = (int) (long) reader["TimelyCurrencyPeriod"],
};
realConfig.Decay = new GamblingConfig.DecayConfig()
{Percent = (decimal) (double) reader["DailyCurrencyDecay"],};
realConfig.Waifu = new GamblingConfig.WaifuConfig()
{
MinPrice = (int) (long) reader["MinWaifuPrice"],
Multipliers = new GamblingConfig.WaifuConfig.MultipliersData()
{
AllGiftPrices = (decimal) (long) reader["WaifuGiftMultiplier"],
WaifuReset = (int) (long) reader["DivorcePriceMultiplier"]
}
};
realConfig.PatreonCurrencyPerCent = (decimal) (double) reader["PatreonCurrencyPerCent"];
};
private const string WaifuItemUpdateQuery = @"UPDATE WaifuItem
SET Name = CASE ItemEmoji
WHEN '🥔' THEN 'potato'
WHEN '🍪' THEN 'cookie'
WHEN '🥖' THEN 'bread'
WHEN '🍭' THEN 'lollipop'
WHEN '🌹' THEN 'rose'
WHEN '🍺' THEN 'beer'
WHEN '🌮' THEN 'taco'
WHEN '💌' THEN 'loveletter'
WHEN '🥛' THEN 'milk'
WHEN '🍕' THEN 'pizza'
WHEN '🍫' THEN 'chocolate'
WHEN '🍦' THEN 'icecream'
WHEN '🍣' THEN 'sushi'
WHEN '🍚' THEN 'rice'
WHEN '🍉' THEN 'watermelon'
WHEN '🍱' THEN 'bento'
WHEN '🎟' THEN 'movieticket'
WHEN '🍰' THEN 'cake'
WHEN '📔' THEN 'book'
WHEN '🐱' THEN 'cat'
WHEN '🐶' THEN 'dog'
WHEN '🐼' THEN 'panda'
WHEN '💄' THEN 'lipstick'
WHEN '👛' THEN 'purse'
WHEN '📱' THEN 'iphone'
WHEN '👗' THEN 'dress'
WHEN '💻' THEN 'laptop'
WHEN '🎻' THEN 'violin'
WHEN '🎹' THEN 'piano'
WHEN '🚗' THEN 'car'
WHEN '💍' THEN 'ring'
WHEN '🛳' THEN 'ship'
WHEN '🏠' THEN 'house'
WHEN '🚁' THEN 'helicopter'
WHEN '🚀' THEN 'spaceship'
WHEN '🌕' THEN 'moon'
ELSE 'unknown'
END
";
}
}