private static async Task OnDeletedConfigFile(string name, string fullPath)
{
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(fullPath))
{
ArchiLogger.LogNullError(nameof(name) + " || " + nameof(fullPath));
return;
}
string botName = Path.GetFileNameWithoutExtension(name);
if (string.IsNullOrEmpty(botName))
{
return;
}
DateTime lastWriteTime = DateTime.UtcNow;
if (LastWriteTimes.TryGetValue(name, out DateTime savedLastWriteTime))
{
if (savedLastWriteTime >= lastWriteTime)
{
return;
}
}
LastWriteTimes[name] = lastWriteTime;
// It's entirely possible that some process is still accessing our file, allow at least a second before trying to read it
await Task.Delay(1000).ConfigureAwait(false);
// It's also possible that we got some other event in the meantime
if (LastWriteTimes.TryGetValue(name, out savedLastWriteTime))
{
if (lastWriteTime != savedLastWriteTime)
{
return;
}
if (LastWriteTimes.TryRemove(name, out savedLastWriteTime))
{
if (lastWriteTime != savedLastWriteTime)
{
return;
}
}
}
if (botName.Equals(SharedInfo.ASF))
{
if (File.Exists(fullPath))
{
return;
}
// Some editors might decide to delete file and re-create it in order to modify it
// If that's the case, we wait for maximum of 5 seconds before shutting down
await Task.Delay(5000).ConfigureAwait(false);
if (File.Exists(fullPath))
{
return;
}
ArchiLogger.LogGenericError(Strings.ErrorGlobalConfigRemoved);
await Program.Exit(1).ConfigureAwait(false);
return;
}
if (!IsValidBotName(botName))
{
return;
}
if (Bot.Bots.TryGetValue(botName, out Bot bot))
{
await bot.OnConfigChanged(true).ConfigureAwait(false);
}
}