Skip to content

Commit

Permalink
Version 5.2
Browse files Browse the repository at this point in the history
- Unhandled exception handler added
- Improvements to HTML generator
- Moved Extensions.cs to Backend
- Moved interaction handler assignment to MapHandlers()
  • Loading branch information
Red-K0 committed May 6, 2024
1 parent e32b1fe commit c410f08
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 458 deletions.
2 changes: 1 addition & 1 deletion docs/index.html

Large diffs are not rendered by default.

34 changes: 14 additions & 20 deletions src/Bot Client/Backend/Client.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace PBot;

using Microsoft.Extensions.Configuration;
using NetCord.Services.ApplicationCommands;
using PBot.Commands.Helpers;
using PBot.Messages;
using static PBot.Messages.Logging;

/// <summary>
/// Contains methods for initializing and preparing the bot's client.
Expand All @@ -16,7 +15,10 @@ public static class Client
public static readonly GatewayClient client = new
(
new BotToken(new ConfigurationBuilder().AddUserSecrets<Program>().Build().GetSection("Discord")["Token"]!),
new GatewayClientConfiguration() { Intents = GatewayIntents.All }
new GatewayClientConfiguration()
{
Intents = GatewayIntents.All
}
);

/// <summary>
Expand All @@ -34,31 +36,23 @@ public static class Client
/// </summary>
public static async void Start()
{
Events.MapClientHandlers();
await Events.MapClientHandlers();
await client.StartAsync();
await client.ReadyAsync;
Caches.Members.Load();

ProbabilityStateMachine.InitXShift128();
}

/// <summary>
/// Starts the bot's interaction handler and assigns it to the client.
/// Stops the client, releases its resources, and restarts the bot client.
/// </summary>
public static async void StartInteractionHandler()
public static async Task Restart()
{
ApplicationCommandService<SlashCommandContext> cmdSrv = new();
cmdSrv.AddModules(System.Reflection.Assembly.GetEntryAssembly()!);
await cmdSrv.CreateCommandsAsync(client.Rest, client.Id);
await client.CloseAsync();
client.Dispose();

client.InteractionCreate += async interaction =>
{
try
{
if (interaction is SlashCommandInteraction sCmd && await cmdSrv.ExecuteAsync(new SlashCommandContext(sCmd, client)) is IFailResult fRes)
{
WriteAsID(fRes.Message, SpecialId.Network);
}
}
catch { }
};
Process.Start(Environment.ProcessPath!, Environment.GetCommandLineArgs());
Process.GetCurrentProcess().Kill();
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Bot Client/Bot Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<WarningLevel>9999</WarningLevel>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<DebugType>full</DebugType>
<DebugType>none</DebugType>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<Deterministic>False</Deterministic>
<Optimize>True</Optimize>
Expand Down
23 changes: 20 additions & 3 deletions src/Bot Client/Messages/Message Events.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PBot.Commands;
using NetCord.Services.ApplicationCommands;
using PBot.Commands;
using static PBot.Messages.Functions;
using static PBot.Messages.Logging;
namespace PBot.Messages;
Expand All @@ -16,13 +17,29 @@ internal static class Events
/// <summary>
/// Maps the <see cref="client"/>'s events to their appropriate response method.
/// </summary>
public static void MapClientHandlers()
public static async Task MapClientHandlers()
{
client.Log += LogNetworkMessage;
client.MessageCreate += MessageCreated;
client.MessageDelete += MessageDeleted;
client.MessageUpdate += MessageUpdated;
client.MessageReactionAdd += ReactionAdded;

ApplicationCommandService<SlashCommandContext> cmdSrv = new();
cmdSrv.AddModules(System.Reflection.Assembly.GetEntryAssembly()!);
await cmdSrv.CreateCommandsAsync(client.Rest, client.Id);

client.InteractionCreate += async interaction =>
{
try
{
if (interaction is SlashCommandInteraction sCmd && await cmdSrv.ExecuteAsync(new SlashCommandContext(sCmd, client)) is IFailResult fRes)
{
WriteAsID(fRes.Message, SpecialId.Network);
}
}
catch { }
};
}

/// <summary>
Expand All @@ -35,7 +52,7 @@ public static async ValueTask MessageCreated(Message message)
if (message.Author.IsBot) { Caches.Messages.Add(message); return; }

// If a command runs, do nothing else.
if (TextCommands.State != 0 && message.Content[0] == '.')
if (TextCommands.State != 0 && message.Content.Length > 3 && message.Content[0] == '.')
{
TextCommands.Parse(message);
return;
Expand Down
16 changes: 11 additions & 5 deletions src/Bot Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using PBot.Commands.Helpers;
using static PBot.Messages.Logging;

if (!PHelper.EnableVirtualAndHideCursor()) throw new OperationCanceledException("VT_Sequences (phlpr.dll) failed to initlialize the console.");

AppDomain current = AppDomain.CurrentDomain; // Load handler after phlpr.dll to prevent infinite loop.
current.UnhandledException += async (object sender, UnhandledExceptionEventArgs e) =>
{
Console.Clear();
WriteAsID($"An unhandled '{e.ExceptionObject.GetType()}' occured at {DateTime.Now.ToLongTimeString()} on {DateTime.Now.ToLongDateString()}", SpecialId.Network);
await Restart();
};

if (!PHelper.EnableVirtualAndHideCursor()) throw new OperationCanceledException("VT_Sequences (VTs.dll) failed to initlialize the console.");
Console.OutputEncoding = System.Text.Encoding.Unicode;

Start();
StartInteractionHandler();

ProbabilityStateMachine.InitXShift128();

await Task.Delay(Timeout.Infinite);
Binary file modified src/Bot Client/phlpr.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
</PropertyGroup>

<ItemGroup>
<None Remove="ignored_hash.txt" />
<None Remove="Source.xml" />
</ItemGroup>

<ItemGroup>
<Content Include="ignored_hash.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Source.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
Loading

0 comments on commit c410f08

Please sign in to comment.