Skip to content

Commit 47d688e

Browse files
committed
.NET 8 and new C# syntax features
1 parent 1e6ce4f commit 47d688e

8 files changed

+22
-41
lines changed

CommandHandlers/CoreCommands.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@
99
namespace DiscordBotBase.CommandHandlers
1010
{
1111
/// <summary>Commands that most bots need.</summary>
12-
public class CoreCommands : UserCommands
12+
public class CoreCommands(Func<IUser, bool> isUserAdmin) : UserCommands
1313
{
14-
/// <summary>Constructs the core commands helper.</summary>
15-
public CoreCommands(Func<IUser, bool> isUserAdmin)
16-
{
17-
UserAdminCheckMethod = isUserAdmin;
18-
}
19-
2014
/// <summary>Method to check if the user is an admin.</summary>
21-
public Func<IUser, bool> UserAdminCheckMethod;
15+
public Func<IUser, bool> UserAdminCheckMethod = isUserAdmin;
2216

2317
/// <summary>Bot restart admin command.</summary>
2418
public void CMD_Restart(CommandData command)

ConnectionMonitor.cs

+4-10
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,11 @@
1616

1717
namespace DiscordBotBase
1818
{
19-
/// <summary>Helper to monitor a Discord bot's connectivity.</summary>
20-
public class ConnectionMonitor
19+
/// <summary>Helper to monitor a Discord bot's connectivity. Call <see cref="StartMonitorLoop"/> to start the monitor loop.</summary>
20+
public class ConnectionMonitor(DiscordBot bot)
2121
{
2222
/// <summary>The bot to monitor.</summary>
23-
public DiscordBot Bot;
24-
25-
/// <summary>Initializes the connection monitor. Call <see cref="StartMonitorLoop"/> to start the monitor loop.</summary>
26-
public ConnectionMonitor(DiscordBot bot)
27-
{
28-
Bot = bot;
29-
}
23+
public DiscordBot Bot = bot;
3024

3125
/// <summary>Whether the bot has ever connected to Discord (since this instance of the class was started).</summary>
3226
public bool ConnectedOnce = false;
@@ -69,7 +63,7 @@ public void ForceRestartBot()
6963
{
7064
Bot.Shutdown();
7165
});
72-
DiscordBotBaseHelper.LaunchBotThread(Array.Empty<string>(), Bot.ClientConfig);
66+
DiscordBotBaseHelper.LaunchBotThread([], Bot.ClientConfig);
7367
}
7468

7569
/// <summary>Lock object for monitor variables.</summary>

DiscordBot.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public void Respond(IUserMessage message, bool outputUnknowns, bool wasMentioned
8585
}
8686
string[] messageDataSplit = messageText.Split(' ');
8787
StringBuilder resultBuilder = new(messageText.Length);
88-
List<string> argsCleaned = new();
89-
List<string> argsRaw = new();
88+
List<string> argsCleaned = [];
89+
List<string> argsRaw = [];
9090
foreach (string originalArg in messageDataSplit)
9191
{
9292
if (originalArg.Contains("<@") && originalArg.Contains('>'))
@@ -114,7 +114,7 @@ public void Respond(IUserMessage message, bool outputUnknowns, bool wasMentioned
114114
string commandNameLowered = argsCleaned[0].ToLowerFast();
115115
argsCleaned.RemoveAt(0);
116116
argsRaw.RemoveAt(0);
117-
CommandData commandData = new() { Message = message, CleanedArguments = argsCleaned.ToArray(), RawArguments = argsRaw.ToArray(), WasBotMention = wasMentioned, Bot = this };
117+
CommandData commandData = new() { Message = message, CleanedArguments = [.. argsCleaned], RawArguments = [.. argsRaw], WasBotMention = wasMentioned, Bot = this };
118118
if (ChatCommands.TryGetValue(commandNameLowered, out Action<CommandData> commandHandlerMethod))
119119
{
120120
commandHandlerMethod.Invoke(commandData);

DiscordBotBaseHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static void BotThread(Object obj)
4141
{
4242
Console.WriteLine("Discord crash: " + ex.ToString());
4343
Thread.Sleep(10 * 1000);
44-
LaunchBotThread(Array.Empty<string>(), CurrentBot.ClientConfig);
44+
LaunchBotThread([], CurrentBot.ClientConfig);
4545
}
4646
}
4747
}

DiscordMessageCache.cs

+4-11
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,13 @@
1010
namespace DiscordBotBase
1111
{
1212
/// <summary>Helper for caching Discord messages.</summary>
13-
public class DiscordMessageCache
13+
public class DiscordMessageCache(DiscordBot _bot, int size)
1414
{
1515
/// <summary>How many messages to store per text channel.</summary>
16-
public int MessagesPerChannel;
16+
public int MessagesPerChannel = size;
1717

1818
/// <summary>The backing Discord bot.</summary>
19-
public DiscordBot Bot;
20-
21-
/// <summary>Construct the cache instance.</summary>
22-
public DiscordMessageCache(DiscordBot _bot, int size)
23-
{
24-
Bot = _bot;
25-
MessagesPerChannel = size;
26-
}
19+
public DiscordBot Bot = _bot;
2720

2821
/// <summary>Represents a single cached message.</summary>
2922
public struct CachedMessage
@@ -148,7 +141,7 @@ public void PrefillInternal(int amountToFill)
148141
try
149142
{
150143
SingleChannelCache cache = GetCacheForChannel(channel.Id);
151-
List<IMessage> messages = new();
144+
List<IMessage> messages = [];
152145
channel.GetMessagesAsync(amountToFill).ForEachAwaitAsync(async col =>
153146
{
154147
messages.AddRange(col);

MicroWebHelper.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public class MicroWebHelper
2727
public CancellationTokenSource CancelToken = new();
2828

2929
/// <summary>Token indicates that the whole system is done.</summary>
30-
public CancellationTokenSource IsEnded = new ();
30+
public CancellationTokenSource IsEnded = new();
3131

3232
/// <summary>Pre-scanned list of valid paths for the raw file root.</summary>
33-
public HashSet<string> RawRootStartPaths = new();
33+
public HashSet<string> RawRootStartPaths = [];
3434

3535
/// <summary>Function to get dynamic pages as-needed.</summary>
3636
public Func<string, HttpListenerContext, WebResult> PageGetter;
@@ -130,7 +130,7 @@ public void Apply(HttpListenerResponse response)
130130
}
131131

132132
/// <summary>Cache of raw root files.</summary>
133-
public Dictionary<string, WebResult> RawFileCache = new();
133+
public Dictionary<string, WebResult> RawFileCache = [];
134134

135135
/// <summary>Creates and immediately starts the web helper.</summary>
136136
public MicroWebHelper(string bind, Func<string, HttpListenerContext, WebResult> _pageGetter, string _rawFileRoot = "wwwroot/")

Reactables/ReactableMessage.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ReactableMessage
2525
/// <summary>Removes the actual reactions from the message.</summary>
2626
public void RemoveReactions()
2727
{
28-
Message.RemoveReactionsAsync(DiscordBotBaseHelper.CurrentBot.Client.CurrentUser, new IEmote[] { new Emoji(Constants.ACCEPT_EMOJI), new Emoji(Constants.DENY_EMOJI) }).Wait();
28+
Message.RemoveReactionsAsync(DiscordBotBaseHelper.CurrentBot.Client.CurrentUser, [new Emoji(Constants.ACCEPT_EMOJI), new Emoji(Constants.DENY_EMOJI)]).Wait();
2929
}
3030
}
3131
}

mcmonkeyDiscordBotBase.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<OutputType>Library</OutputType>
55
<RootNamespace>DiscordBotBase</RootNamespace>
66
<AssemblyName>mcmonkeyDiscordBotBase</AssemblyName>
7-
<TargetFramework>net6.0</TargetFramework>
7+
<TargetFramework>net8.0</TargetFramework>
88
<LangVersion>Latest</LangVersion>
99
<Product>mcmonkeyDiscordBotBase</Product>
1010
<PackageId>mcmonkeyDiscordBotBase</PackageId>
@@ -14,8 +14,8 @@
1414
<EmbeddedResource Include="**\*.resx" />
1515
</ItemGroup>
1616
<ItemGroup>
17-
<PackageReference Include="FreneticLLC.FreneticUtilities" Version="1.0.14" />
18-
<PackageReference Include="Discord.Net" Version="3.12.0" />
19-
<PackageReference Include="Discord.Net.WebSocket" Version="3.12.0" />
17+
<PackageReference Include="FreneticLLC.FreneticUtilities" Version="1.0.24" />
18+
<PackageReference Include="Discord.Net" Version="3.13.0" />
19+
<PackageReference Include="Discord.Net.WebSocket" Version="3.13.0" />
2020
</ItemGroup>
2121
</Project>

0 commit comments

Comments
 (0)