Changed the discord bot to be case insensitive.

Added IsEmpty and HasOne IEnumerable Extensions.
This commit is contained in:
Brett Hewitson 2021-12-17 15:27:52 +10:00
parent 734332f10c
commit dd431e93b2
20 changed files with 383 additions and 107 deletions

View file

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
namespace ServerManagerTool.Common.Extensions
{
public static class IEnumerableExtensions
{
public static bool IsEmpty<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
if (enumerator.MoveNext())
{
return false;
}
}
return true;
}
public static bool HasOne<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
var count = 0;
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
while (enumerator.MoveNext())
{
if (++count > 1)
return false;
}
}
return count == 1;
}
}
}

View file

@ -1,5 +1,6 @@
using Microsoft.Win32.TaskScheduler;
using NLog;
using ServerManagerTool.Common.Extensions;
using System;
using System.Diagnostics;
using System.Linq;
@ -202,7 +203,7 @@ namespace ServerManagerTool.Common.Utils
// Add/Edit the trigger that will fire every x minutes
var triggers = taskDefinition.Triggers.OfType<TimeTrigger>();
if (triggers.Count() == 0)
if (triggers.IsEmpty())
{
var trigger = new TimeTrigger
{
@ -445,7 +446,7 @@ namespace ServerManagerTool.Common.Utils
if (onBoot)
{
var triggers = taskDefinition.Triggers.OfType<BootTrigger>();
if (triggers.Count() == 0)
if (triggers.IsEmpty())
{
var trigger = new BootTrigger
{
@ -465,7 +466,7 @@ namespace ServerManagerTool.Common.Utils
else
{
var triggers = taskDefinition.Triggers.OfType<LogonTrigger>();
if (triggers.Count() == 0)
if (triggers.IsEmpty())
{
var trigger = new LogonTrigger
{
@ -572,7 +573,7 @@ namespace ServerManagerTool.Common.Utils
// Add/Edit the trigger that will fire every x minutes
var triggers = taskDefinition.Triggers.OfType<TimeTrigger>();
if (triggers.Count() == 0)
if (triggers.IsEmpty())
{
var trigger = new TimeTrigger
{

View file

@ -1,4 +1,5 @@
using Ionic.Zip;
using ServerManagerTool.Common.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
@ -73,7 +74,8 @@ namespace ServerManagerTool.Common.Utils
{
if (string.IsNullOrWhiteSpace(zipFile))
throw new ArgumentNullException(nameof(zipFile));
if (!filesToZip.Any())
if (filesToZip is null || filesToZip.IsEmpty())
return;
if (!File.Exists(zipFile))
@ -165,7 +167,8 @@ namespace ServerManagerTool.Common.Utils
{
if (string.IsNullOrWhiteSpace(zipFile))
throw new ArgumentNullException(nameof(zipFile));
if (!filesToZip.Any())
if (filesToZip is null || filesToZip.IsEmpty())
throw new ArgumentNullException(nameof(filesToZip));
using (var zip = new ZipFile(zipFile))