mirror of
https://github.com/Myxelium/Lunaris2.0.git
synced 2026-07-07 13:35:09 +00:00
Refactor
This commit is contained in:
@@ -1,16 +1,6 @@
|
||||
using System.Reflection;
|
||||
using Discord;
|
||||
using Discord.Interactions;
|
||||
using Discord.WebSocket;
|
||||
using Lunaris2.Handler.ChatCommand;
|
||||
using Lavalink4NET.Extensions;
|
||||
using Lavalink4NET.Integrations.SponsorBlock.Extensions;
|
||||
using Lunaris2.Handler.MusicPlayer;
|
||||
using Lunaris2.Notification;
|
||||
using Lunaris2.Service;
|
||||
using Lunaris2.SlashCommand;
|
||||
using Lavalink4NET.Integrations.SponsorBlock.Extensions;
|
||||
using Lunaris2.Registration;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Lunaris2;
|
||||
@@ -33,71 +23,11 @@ public class Program
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureServices((_, services) =>
|
||||
{
|
||||
var config = new DiscordSocketConfig
|
||||
{
|
||||
GatewayIntents = GatewayIntents.All
|
||||
};
|
||||
|
||||
var client = new DiscordSocketClient(config);
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(AppContext.BaseDirectory)
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build();
|
||||
|
||||
services
|
||||
.AddMediatR(mediatRServiceConfiguration => mediatRServiceConfiguration.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()))
|
||||
.AddLavalink()
|
||||
.ConfigureLavalink(options =>
|
||||
{
|
||||
options.BaseAddress = new Uri(
|
||||
$"http://{configuration["LavaLinkHostname"]}:{configuration["LavaLinkPort"]}"
|
||||
);
|
||||
options.WebSocketUri = new Uri($"ws://{configuration["LavaLinkHostname"]}:{configuration["LavaLinkPort"]}/v4/websocket");
|
||||
options.Passphrase = configuration["LavaLinkPassword"] ?? "youshallnotpass";
|
||||
options.Label = "Node";
|
||||
})
|
||||
.AddSingleton<MusicEmbed>()
|
||||
.AddSingleton<ChatSettings>()
|
||||
.AddSingleton(client)
|
||||
.AddSingleton<DiscordEventListener>()
|
||||
.AddSingleton<VoiceChannelMonitorService>()
|
||||
.AddSingleton(service => new InteractionService(service.GetRequiredService<DiscordSocketClient>()))
|
||||
.Configure<ChatSettings>(configuration.GetSection("LLM"));
|
||||
|
||||
client.Ready += () => Client_Ready(client);
|
||||
client.Log += Log;
|
||||
|
||||
client
|
||||
.LoginAsync(TokenType.Bot, configuration["Token"])
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
client
|
||||
.StartAsync()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
var listener = services
|
||||
.BuildServiceProvider()
|
||||
.GetRequiredService<DiscordEventListener>();
|
||||
|
||||
listener
|
||||
.StartAsync()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
services.AddDiscordBot(configuration);
|
||||
});
|
||||
|
||||
private static Task Client_Ready(DiscordSocketClient client)
|
||||
{
|
||||
client.RegisterCommands();
|
||||
|
||||
new VoiceChannelMonitorService(client).StartMonitoring();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static Task Log(LogMessage arg)
|
||||
{
|
||||
Console.WriteLine(arg);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
16
Bot/Registration/ChatRegistration.cs
Normal file
16
Bot/Registration/ChatRegistration.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Lunaris2.Handler.ChatCommand;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Lunaris2.Registration;
|
||||
|
||||
public static class ChatRegistration
|
||||
{
|
||||
public static IServiceCollection AddChat(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddSingleton<ChatSettings>();
|
||||
services.Configure<ChatSettings>(configuration.GetSection("LLM"));
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
72
Bot/Registration/DiscordBotRegistration.cs
Normal file
72
Bot/Registration/DiscordBotRegistration.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System.Reflection;
|
||||
using Discord;
|
||||
using Discord.Interactions;
|
||||
using Discord.WebSocket;
|
||||
using Lunaris2.Notification;
|
||||
using Lunaris2.Service;
|
||||
using Lunaris2.SlashCommand;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Lunaris2.Registration
|
||||
{
|
||||
public static class DiscordBotRegistration
|
||||
{
|
||||
public static IServiceCollection AddDiscordBot(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var config = new DiscordSocketConfig
|
||||
{
|
||||
GatewayIntents = GatewayIntents.All
|
||||
};
|
||||
|
||||
var client = new DiscordSocketClient(config);
|
||||
|
||||
services
|
||||
.AddMediatR(mediatRServiceConfiguration =>
|
||||
mediatRServiceConfiguration.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()))
|
||||
.AddMusicPlayer(configuration)
|
||||
.AddSingleton(client)
|
||||
.AddSingleton<DiscordEventListener>()
|
||||
.AddSingleton(service => new InteractionService(service.GetRequiredService<DiscordSocketClient>()))
|
||||
.AddChat(configuration);
|
||||
|
||||
client.Ready += () => Client_Ready(client);
|
||||
client.Log += Log;
|
||||
|
||||
client
|
||||
.LoginAsync(TokenType.Bot, configuration["Token"])
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
client
|
||||
.StartAsync()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
var listener = services
|
||||
.BuildServiceProvider()
|
||||
.GetRequiredService<DiscordEventListener>();
|
||||
|
||||
listener
|
||||
.StartAsync()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static Task Client_Ready(DiscordSocketClient client)
|
||||
{
|
||||
client.RegisterCommands();
|
||||
|
||||
new VoiceChannelMonitorService(client).StartMonitoring();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static Task Log(LogMessage arg)
|
||||
{
|
||||
Console.WriteLine(arg);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
29
Bot/Registration/MusicPlayerRegistration.cs
Normal file
29
Bot/Registration/MusicPlayerRegistration.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Lavalink4NET.Extensions;
|
||||
using Lunaris2.Handler.MusicPlayer;
|
||||
using Lunaris2.Service;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Lunaris2.Registration;
|
||||
|
||||
public static class MusicPlayerRegistration
|
||||
{
|
||||
public static IServiceCollection AddMusicPlayer(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services
|
||||
.AddLavalink()
|
||||
.ConfigureLavalink(options =>
|
||||
{
|
||||
options.BaseAddress = new Uri(
|
||||
$"http://{configuration["LavaLinkHostname"]}:{configuration["LavaLinkPort"]}"
|
||||
);
|
||||
options.WebSocketUri = new Uri($"ws://{configuration["LavaLinkHostname"]}:{configuration["LavaLinkPort"]}/v4/websocket");
|
||||
options.Passphrase = configuration["LavaLinkPassword"] ?? "youshallnotpass";
|
||||
options.Label = "Node";
|
||||
})
|
||||
.AddSingleton<MusicEmbed>()
|
||||
.AddSingleton<VoiceChannelMonitorService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user