mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
- 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:
@@ -27,6 +27,7 @@ namespace NadekoBot.Services
|
||||
string LocationIqApiKey { get; }
|
||||
string TimezoneDbApiKey { get; }
|
||||
string CoinmarketcapApiKey { get; }
|
||||
string CoordinatorUrl { get; set; }
|
||||
}
|
||||
|
||||
// todo move somewhere else
|
||||
@@ -38,13 +39,7 @@ namespace NadekoBot.Services
|
||||
|
||||
public class RestartConfig
|
||||
{
|
||||
public RestartConfig(string cmd, string args)
|
||||
{
|
||||
this.Cmd = cmd;
|
||||
this.Args = args;
|
||||
}
|
||||
|
||||
public string Cmd { get; }
|
||||
public string Args { get; }
|
||||
public string Cmd { get; set; }
|
||||
public string Args { get; set; }
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,9 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.IO;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Nadeko.Common;
|
||||
using NadekoBot.Common;
|
||||
using NadekoBot.Common.Yml;
|
||||
using Serilog;
|
||||
|
||||
@@ -10,6 +12,7 @@ namespace NadekoBot.Services
|
||||
// todo check why is memory usage so unstable
|
||||
public class BotCredsProvider
|
||||
{
|
||||
private readonly int? _totalShards;
|
||||
private const string _credsFileName = "creds.yml";
|
||||
private string CredsPath => Path.Combine(Directory.GetCurrentDirectory(), _credsFileName);
|
||||
private const string _credsExampleFileName = "creds_example.yml";
|
||||
@@ -28,12 +31,42 @@ namespace NadekoBot.Services
|
||||
_creds.OwnerIds.Clear();
|
||||
_config.Bind(_creds);
|
||||
|
||||
// todo load defaults for restart command, redis, and some others maybe?
|
||||
if (string.IsNullOrWhiteSpace(_creds.Token))
|
||||
{
|
||||
Log.Error("Token is missing from credentials.json or Environment variables.\n" +
|
||||
"Add it and restart the program.");
|
||||
Helpers.ReadErrorAndExit(5);
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_creds.RestartCommand?.Cmd)
|
||||
|| string.IsNullOrWhiteSpace(_creds.RestartCommand?.Args))
|
||||
{
|
||||
if (Environment.OSVersion.Platform == PlatformID.Unix)
|
||||
{
|
||||
_creds.RestartCommand = new RestartConfig()
|
||||
{
|
||||
Args = "dotnet",
|
||||
Cmd = "NadekoBot.dll -- {0}",
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
_creds.RestartCommand = new RestartConfig()
|
||||
{
|
||||
Args = "NadekoBot.exe",
|
||||
Cmd = "{0}",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
_creds.TotalShards = _totalShards ?? _creds.TotalShards;
|
||||
}
|
||||
}
|
||||
|
||||
public BotCredsProvider()
|
||||
public BotCredsProvider(int? totalShards = null)
|
||||
{
|
||||
_totalShards = totalShards;
|
||||
if (!File.Exists(CredsExamplePath))
|
||||
{
|
||||
File.WriteAllText(CredsExamplePath, Yaml.Serializer.Serialize(_creds));
|
@@ -20,8 +20,11 @@ namespace NadekoBot.Services
|
||||
|
||||
public RemoteGrpcCoordinator(IBotCredentials creds, DiscordSocketClient client)
|
||||
{
|
||||
// todo should use credentials
|
||||
var channel = Grpc.Net.Client.GrpcChannel.ForAddress("https://localhost:3443");
|
||||
var coordUrl = string.IsNullOrWhiteSpace(creds.CoordinatorUrl)
|
||||
? "http://localhost:3442"
|
||||
: creds.CoordinatorUrl;
|
||||
|
||||
var channel = Grpc.Net.Client.GrpcChannel.ForAddress(coordUrl);
|
||||
_coordClient = new(channel);
|
||||
_client = client;
|
||||
}
|
||||
@@ -29,8 +32,8 @@ namespace NadekoBot.Services
|
||||
public bool RestartBot()
|
||||
{
|
||||
_coordClient.RestartAllShards(new RestartAllRequest
|
||||
{
|
||||
|
||||
{
|
||||
|
||||
});
|
||||
|
||||
return true;
|
||||
|
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
@@ -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
|
||||
";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user