Refactor string checks and fix exception when exporting multiple channels

Fix #164
This commit is contained in:
Alexey Golub
2019-04-11 22:56:29 +03:00
parent 7951703cf2
commit 30cba7959f
20 changed files with 33 additions and 26 deletions

View File

@@ -41,14 +41,14 @@ namespace DiscordChatExporter.Core.Services
var guildId = json["guild_id"]?.Value<string>();
// If the guild ID is blank, it's direct messages
if (guildId == null)
if (guildId.IsNullOrWhiteSpace())
guildId = Guild.DirectMessages.Id;
// Try to extract name
var name = json["name"]?.Value<string>();
// If the name is blank, it's direct messages
if (name == null)
if (name.IsNullOrWhiteSpace())
name = json["recipients"].Select(ParseUser).Select(u => u.Name).JoinToString(", ");
return new Channel(id, parentId, guildId, name, topic, type);

View File

@@ -48,7 +48,7 @@ namespace DiscordChatExporter.Core.Services
var value = parameter.SubstringAfter("=");
// Skip empty values
if (value.IsEmpty())
if (value.IsNullOrWhiteSpace())
continue;
request.RequestUri = request.RequestUri.SetQueryParameter(key, value);

View File

@@ -8,7 +8,7 @@
<PackageReference Include="Failsafe" Version="1.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Onova" Version="2.4.2" />
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.0" />
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.1" />
<PackageReference Include="Tyrrrz.Settings" Version="1.3.4" />
</ItemGroup>

View File

@@ -38,7 +38,7 @@ namespace DiscordChatExporter.Core.Services
{
// Create output directory
var dirPath = Path.GetDirectoryName(filePath);
if (!dirPath.EmptyIfNull().IsWhiteSpace())
if (!dirPath.IsNullOrWhiteSpace())
Directory.CreateDirectory(dirPath);
// Render chat log to output file
@@ -74,7 +74,7 @@ namespace DiscordChatExporter.Core.Services
var partitionFilePath = $"{fileNameWithoutExt} [{partitionNumber} of {partitions.Length}]{fileExt}";
// Compose full file path
if (!dirPath.EmptyIfNull().IsWhiteSpace())
if (!dirPath.IsNullOrWhiteSpace())
partitionFilePath = Path.Combine(dirPath, partitionFilePath);
// Export

View File

@@ -3,6 +3,7 @@ using System.IO;
using System.Linq;
using System.Text;
using DiscordChatExporter.Core.Models;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Services.Helpers
{
@@ -11,7 +12,7 @@ namespace DiscordChatExporter.Core.Services.Helpers
public static bool IsDirectoryPath(string path) =>
path.Last() == Path.DirectorySeparatorChar ||
path.Last() == Path.AltDirectorySeparatorChar ||
Path.GetExtension(path) == null;
(Path.GetExtension(path).IsNullOrWhiteSpace() && !File.Exists(path));
public static string GetDefaultExportFileName(ExportFormat format, Guild guild, Channel channel,
DateTimeOffset? after = null, DateTimeOffset? before = null)