Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MessageQueue support #15

Merged
merged 14 commits into from
Jun 14, 2024
35 changes: 35 additions & 0 deletions Kook.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kook.Net.Webhook.HttpListen
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kook.Net.Samples.Webhook.HttpListener", "samples\Kook.Net.Samples.Webhook.HttpListener\Kook.Net.Samples.Webhook.HttpListener.csproj", "{4625F550-9A14-46E2-AD17-8D49485F46D0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kook.Net.MessageQueue.MassTransit", "src\Kook.Net.MessageQueue.MassTransit\Kook.Net.MessageQueue.MassTransit.csproj", "{2E73C29A-BF51-46C6-B1AB-B3F007B620D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kook.Net.Samples.MessageQueue", "samples\Kook.Net.Samples.MessageQueue\Kook.Net.Samples.MessageQueue.csproj", "{211C1521-9A8E-414A-B703-0B5BE0D347FA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kook.Net.DependencyInjection.Microsoft", "src\Kook.Net.DependencyInjection.Microsoft\Kook.Net.DependencyInjection.Microsoft.csproj", "{7D3A6FFF-1055-44AD-AD7B-81D44706A984}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kook.Net.Hosting", "src\Kook.Net.Hosting\Kook.Net.Hosting.csproj", "{876A01D6-06EC-42A0-B6B6-2D336C8E1043}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kook.Net.MessageQueue.InMemory", "src\Kook.Net.MessageQueue.InMemory\Kook.Net.MessageQueue.InMemory.csproj", "{4F5162ED-5FBA-42D4-9510-9E40637473DE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kook.Net.Samples.OAuth", "samples\Kook.Net.Samples.OAuth\Kook.Net.Samples.OAuth.csproj", "{6D07EB30-874A-4BCD-870E-0C8CE77B9990}"
EndProject
Global
Expand Down Expand Up @@ -170,6 +180,26 @@ Global
{4625F550-9A14-46E2-AD17-8D49485F46D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4625F550-9A14-46E2-AD17-8D49485F46D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4625F550-9A14-46E2-AD17-8D49485F46D0}.Release|Any CPU.Build.0 = Release|Any CPU
{2E73C29A-BF51-46C6-B1AB-B3F007B620D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E73C29A-BF51-46C6-B1AB-B3F007B620D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E73C29A-BF51-46C6-B1AB-B3F007B620D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E73C29A-BF51-46C6-B1AB-B3F007B620D2}.Release|Any CPU.Build.0 = Release|Any CPU
{211C1521-9A8E-414A-B703-0B5BE0D347FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{211C1521-9A8E-414A-B703-0B5BE0D347FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{211C1521-9A8E-414A-B703-0B5BE0D347FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{211C1521-9A8E-414A-B703-0B5BE0D347FA}.Release|Any CPU.Build.0 = Release|Any CPU
{7D3A6FFF-1055-44AD-AD7B-81D44706A984}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D3A6FFF-1055-44AD-AD7B-81D44706A984}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D3A6FFF-1055-44AD-AD7B-81D44706A984}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D3A6FFF-1055-44AD-AD7B-81D44706A984}.Release|Any CPU.Build.0 = Release|Any CPU
{876A01D6-06EC-42A0-B6B6-2D336C8E1043}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{876A01D6-06EC-42A0-B6B6-2D336C8E1043}.Debug|Any CPU.Build.0 = Debug|Any CPU
{876A01D6-06EC-42A0-B6B6-2D336C8E1043}.Release|Any CPU.ActiveCfg = Release|Any CPU
{876A01D6-06EC-42A0-B6B6-2D336C8E1043}.Release|Any CPU.Build.0 = Release|Any CPU
{4F5162ED-5FBA-42D4-9510-9E40637473DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F5162ED-5FBA-42D4-9510-9E40637473DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F5162ED-5FBA-42D4-9510-9E40637473DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F5162ED-5FBA-42D4-9510-9E40637473DE}.Release|Any CPU.Build.0 = Release|Any CPU
{6D07EB30-874A-4BCD-870E-0C8CE77B9990}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6D07EB30-874A-4BCD-870E-0C8CE77B9990}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6D07EB30-874A-4BCD-870E-0C8CE77B9990}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -197,6 +227,11 @@ Global
{62E149CC-9C22-41A0-B9C5-DE930FC26EC5} = {B51E68C1-B6FC-49A3-B04D-57BB0C19B192}
{5E447382-3D75-4BD0-8657-FF24900F879A} = {B51E68C1-B6FC-49A3-B04D-57BB0C19B192}
{4625F550-9A14-46E2-AD17-8D49485F46D0} = {8DE556F9-829D-48D3-A41C-FA57207CAE72}
{2E73C29A-BF51-46C6-B1AB-B3F007B620D2} = {B51E68C1-B6FC-49A3-B04D-57BB0C19B192}
{211C1521-9A8E-414A-B703-0B5BE0D347FA} = {8DE556F9-829D-48D3-A41C-FA57207CAE72}
{7D3A6FFF-1055-44AD-AD7B-81D44706A984} = {B51E68C1-B6FC-49A3-B04D-57BB0C19B192}
{876A01D6-06EC-42A0-B6B6-2D336C8E1043} = {B51E68C1-B6FC-49A3-B04D-57BB0C19B192}
{4F5162ED-5FBA-42D4-9510-9E40637473DE} = {B51E68C1-B6FC-49A3-B04D-57BB0C19B192}
{6D07EB30-874A-4BCD-870E-0C8CE77B9990} = {8DE556F9-829D-48D3-A41C-FA57207CAE72}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Kook.Net.CardMarkup\Kook.Net.CardMarkup.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>Kook.Net.Samples.MessageQueue.MassTransit</RootNamespace>
</PropertyGroup>

<Import Project="../../Kook.Net.Sample.targets" />

<ItemGroup>
<ProjectReference Include="..\..\src\Kook.Net.Hosting\Kook.Net.Hosting.csproj" />
<ProjectReference Include="..\..\src\Kook.Net.MessageQueue.InMemory\Kook.Net.MessageQueue.InMemory.csproj" />
<ProjectReference Include="..\..\src\Kook.Net.MessageQueue.MassTransit\Kook.Net.MessageQueue.MassTransit.csproj" />
<ProjectReference Include="..\..\src\Kook.Net.Webhook.AspNet\Kook.Net.Webhook.AspNet.csproj" />
<ProjectReference Include="..\..\src\Kook.Net.Webhook.HttpListener\Kook.Net.Webhook.HttpListener.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Kook.WebSocket;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Kook.Net.Samples.MessageQueue.MassTransit;

public class KookClientSubscriptionService : IHostedService
{
private readonly ILogger<KookClientSubscriptionService> _logger;
private readonly KookSocketClient _kookClient;

public KookClientSubscriptionService(ILogger<KookClientSubscriptionService> logger, KookSocketClient kookClient)
{
_logger = logger;
_kookClient = kookClient;
SubscribeToEvents();
}

private void SubscribeToEvents()
{
_kookClient.Log += message =>
{
_logger.Log(message.Severity switch
{
LogSeverity.Critical => LogLevel.Critical,
LogSeverity.Error => LogLevel.Error,
LogSeverity.Warning => LogLevel.Warning,
LogSeverity.Info => LogLevel.Information,
LogSeverity.Verbose => LogLevel.Debug,
LogSeverity.Debug => LogLevel.Trace,
_ => throw new ArgumentOutOfRangeException(nameof(message.Severity), message.Severity, null)
}, message.Exception, "Kook: {Message}", message.Message);
return Task.CompletedTask;
};
_kookClient.MessageReceived += (message, author, channel) =>
{
_logger.LogInformation("Message received: {Message}", message);
return Task.CompletedTask;
};
_kookClient.Ready += () =>
{
_logger.LogInformation("Ready!");
return Task.CompletedTask;
};
}

/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;

/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
71 changes: 71 additions & 0 deletions samples/Kook.Net.Samples.MessageQueue/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Kook;
using Kook.Net.DependencyInjection.Microsoft;
using Kook.Net.Hosting;
using Kook.Net.Queue.MassTransit;
using Kook.Net.Samples.MessageQueue.MassTransit;
using Kook.Webhook.AspNet;
using MassTransit;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

string? mode = Environment.GetEnvironmentVariable("KOOK_CLIENT_MODE");
if (mode is "Webhook")
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddKook(c =>
{
c.UseAspNetWebhookClient(config =>
{
config.LogLevel = LogSeverity.Debug;
config.VerifyToken = string.Empty;
config.EncryptKey = string.Empty;
config.LogLevel = LogSeverity.Debug;
config.TokenType = TokenType.Bot;
config.Token = string.Empty;
})
.UseMassTransitMessageQueue(x =>
{
x.UsingInMemory((context, configuration) =>
{
configuration.ConfigureEndpoints(context);
// Internal gateway message processing needs to be serialized,
// so that the event can be fired with correct cache state.
configuration.UseConcurrencyLimit(1);
});
x.AddLogging(b => b.AddConsole());
});
// To use in-memory message queue, invoke:
// .UseMessageQueue(SynchronousImmediateMessageQueueProvider.Instance);
});
builder.Services.AddHostedService<KookClientSubscriptionService>();
WebApplication app = builder.Build();
app.UseKookEndpoint();
await app.RunAsync();
}
else if (mode is "WebSocket")
{
HostApplicationBuilder builder = Host.CreateEmptyApplicationBuilder(new HostApplicationBuilderSettings());
builder.Services.AddKook(c =>
c.UseSocketClient(config => config.LogLevel = LogSeverity.Debug)
.UseHostedClient(TokenType.Bot, string.Empty)
.UseMassTransitMessageQueue(x =>
{
x.UsingInMemory((context, configuration) =>
{
configuration.ConfigureEndpoints(context);
// Internal gateway message processing needs to be serialized,
// so that the event can be fired with correct cache state.
configuration.UseConcurrencyLimit(1);
});
x.AddLogging(b => b.AddConsole());
}));
builder.Services.AddHostedService<KookClientSubscriptionService>();
IHost app = builder.Build();
await app.RunAsync();
}
else
{
throw new InvalidOperationException("Invalid KOOK_CLIENT_MODE");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"webhook": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5043",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"KOOK_CLIENT_MODE": "Webhook"
}
},
"websocket": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"KOOK_CLIENT_MODE": "WebSocket"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Kook.Commands;
using Kook.Net.Samples.TextCommands.Services;
using Kook.Rest;
using Kook.WebSocket;

namespace Kook.Net.Samples.TextCommands.Modules;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

<Import Project="../../Kook.Net.Sample.targets" />

<ItemGroup>
<ProjectReference Include="..\..\src\Kook.Net.Webhook.AspNet\Kook.Net.Webhook.AspNet.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Kook.Webhook;

namespace Kook.Net.Samples.Webhook.AspNet;

public class KookClientSubscriptionService : IHostedService
{
private readonly ILogger<KookClientSubscriptionService> _logger;
private readonly KookWebhookClient _kookWebhookClient;

public KookClientSubscriptionService(ILogger<KookClientSubscriptionService> logger, KookWebhookClient kookWebhookClient)
{
_logger = logger;
_kookWebhookClient = kookWebhookClient;

SubscribeToEvents();
}

private void SubscribeToEvents()
{
_kookWebhookClient.Log += message =>
{
_logger.Log(message.Severity switch
{
LogSeverity.Critical => LogLevel.Critical,
LogSeverity.Error => LogLevel.Error,
LogSeverity.Warning => LogLevel.Warning,
LogSeverity.Info => LogLevel.Information,
LogSeverity.Verbose => LogLevel.Debug,
LogSeverity.Debug => LogLevel.Trace,
_ => throw new ArgumentOutOfRangeException(nameof(message.Severity), message.Severity, null)
}, message.Exception, "Kook.Webhook: {Message}", message.Message);
return Task.CompletedTask;
};
_kookWebhookClient.MessageReceived += (message, author, channel) =>
{
_logger.LogInformation("Message received: {Message}", message);
return Task.CompletedTask;
};
_kookWebhookClient.Ready += () =>
{
_logger.LogInformation("Ready!");
return Task.CompletedTask;
};
}

/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;

/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
28 changes: 3 additions & 25 deletions samples/Kook.Net.Samples.Webhook.AspNet/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Kook;
using Kook.Webhook;
using Kook.Net.Samples.Webhook.AspNet;
using Kook.Webhook.AspNet;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
Expand All @@ -9,32 +9,10 @@
config.Token = default;
config.VerifyToken = default;
config.EncryptKey = default;
config.RouteEndpoint = "kook";
config.RoutePattern = "kook";
config.LogLevel = LogSeverity.Debug;
config.ConfigureKookClient = (serviceProvider, client) =>
{
client.Log += message =>
{
ILogger<KookWebhookClient> logger = serviceProvider.GetRequiredService<ILogger<KookWebhookClient>>();
logger.Log(message.Severity switch
{
LogSeverity.Critical => LogLevel.Critical,
LogSeverity.Error => LogLevel.Error,
LogSeverity.Warning => LogLevel.Warning,
LogSeverity.Info => LogLevel.Information,
LogSeverity.Verbose => LogLevel.Debug,
LogSeverity.Debug => LogLevel.Trace,
_ => throw new ArgumentOutOfRangeException(nameof(message.Severity), message.Severity, null)
}, message.Exception, "Kook.Webhook: {Message}", message.Message);
return Task.CompletedTask;
};
client.Ready += () =>
{
Console.WriteLine("Ready!");
return Task.CompletedTask;
};
};
});
builder.Services.AddHostedService<KookClientSubscriptionService>();

WebApplication app = builder.Build();
app.UseKookEndpoint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5043",
"environmentVariables": {
Expand All @@ -22,7 +22,7 @@
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7151;http://localhost:5043",
"environmentVariables": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

<Import Project="../../Kook.Net.Sample.targets" />

<ItemGroup>
<ProjectReference Include="..\..\src\Kook.Net.Webhook.HttpListener\Kook.Net.Webhook.HttpListener.csproj" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion src/Kook.Net.Commands/Extensions/MessageExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Text;

namespace Kook.Commands;

Expand Down
1 change: 0 additions & 1 deletion src/Kook.Net.Commands/Info/ModuleInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Kook.Commands.Builders;
using System.Collections.Immutable;

namespace Kook.Commands;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;

namespace Kook;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;

namespace Kook;
Expand Down
Loading