mirror of
https://github.com/Myxelium/Lunaris2.0.git
synced 2026-07-07 13:35:09 +00:00
Slightly update program.cs
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
78
Program.cs
78
Program.cs
@@ -7,62 +7,72 @@ using Lunaris2.Notification;
|
|||||||
using Lunaris2.SlashCommand;
|
using Lunaris2.SlashCommand;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace Lunaris2;
|
namespace Lunaris2
|
||||||
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
private DiscordSocketClient? _client;
|
public static void Main(string[] args)
|
||||||
private CommandService? _commands;
|
{
|
||||||
private IServiceProvider? _services;
|
CreateHostBuilder(args).Build().Run();
|
||||||
private IConfiguration? _config;
|
}
|
||||||
|
|
||||||
private static void Main(string[] args) => new Program()
|
private static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||||
.RunBotAsync()
|
Host.CreateDefaultBuilder(args)
|
||||||
.GetAwaiter()
|
.ConfigureServices((_, services) =>
|
||||||
.GetResult();
|
|
||||||
|
|
||||||
private async Task RunBotAsync()
|
|
||||||
{
|
{
|
||||||
var config = new DiscordSocketConfig
|
var config = new DiscordSocketConfig
|
||||||
{
|
{
|
||||||
GatewayIntents = GatewayIntents.All
|
GatewayIntents = GatewayIntents.All
|
||||||
};
|
};
|
||||||
|
|
||||||
_client = new DiscordSocketClient(config);
|
var client = new DiscordSocketClient(config);
|
||||||
_client.Ready += Client_Ready;
|
var commands = new CommandService();
|
||||||
_commands = new CommandService();
|
var configuration = new ConfigurationBuilder()
|
||||||
_services = new ServiceCollection()
|
|
||||||
.AddSingleton(_client)
|
|
||||||
.AddSingleton(_commands)
|
|
||||||
.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()))
|
|
||||||
.AddSingleton<DiscordEventListener>()
|
|
||||||
.AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordSocketClient>()))
|
|
||||||
.BuildServiceProvider();
|
|
||||||
|
|
||||||
_client.Log += Log;
|
|
||||||
_config = new ConfigurationBuilder()
|
|
||||||
.SetBasePath(AppContext.BaseDirectory)
|
.SetBasePath(AppContext.BaseDirectory)
|
||||||
.AddJsonFile("appsettings.json")
|
.AddJsonFile("appsettings.json")
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
await _client.LoginAsync(TokenType.Bot, _config["Token"]);
|
services.AddSingleton(client)
|
||||||
await _client.StartAsync();
|
.AddSingleton(commands)
|
||||||
|
.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()))
|
||||||
|
.AddSingleton<DiscordEventListener>()
|
||||||
|
.AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordSocketClient>()));
|
||||||
|
|
||||||
var listener = _services.GetRequiredService<DiscordEventListener>();
|
client.Ready += () => Client_Ready(client);
|
||||||
await listener.StartAsync();
|
client.Log += Log;
|
||||||
|
|
||||||
await Task.Delay(Timeout.Infinite);
|
client
|
||||||
}
|
.LoginAsync(TokenType.Bot, configuration["Token"])
|
||||||
|
.GetAwaiter()
|
||||||
|
.GetResult();
|
||||||
|
|
||||||
private async Task Client_Ready()
|
client
|
||||||
|
.StartAsync()
|
||||||
|
.GetAwaiter()
|
||||||
|
.GetResult();
|
||||||
|
|
||||||
|
var listener = services
|
||||||
|
.BuildServiceProvider()
|
||||||
|
.GetRequiredService<DiscordEventListener>();
|
||||||
|
|
||||||
|
listener
|
||||||
|
.StartAsync()
|
||||||
|
.GetAwaiter()
|
||||||
|
.GetResult();
|
||||||
|
});
|
||||||
|
|
||||||
|
private static Task Client_Ready(DiscordSocketClient client)
|
||||||
{
|
{
|
||||||
_client.RegisterCommands();
|
client.RegisterCommands();
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task Log(LogMessage arg)
|
private static Task Log(LogMessage arg)
|
||||||
{
|
{
|
||||||
Console.WriteLine(arg);
|
Console.WriteLine(arg);
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user