diff --git a/Moonlight/App/Services/DiscordBot/BaseModule.cs b/Moonlight/App/Services/DiscordBot/BaseModule.cs new file mode 100644 index 0000000..616435f --- /dev/null +++ b/Moonlight/App/Services/DiscordBot/BaseModule.cs @@ -0,0 +1,20 @@ +using Discord.WebSocket; + +namespace Moonlight.App.Services.DiscordBot; + +public class BaseModule +{ + public DiscordSocketClient Client { get; set; } + public ConfigService ConfigService { get; set; } + public IServiceScope Scope { get; set; } + + public BaseModule( + DiscordSocketClient client, + ConfigService configService, + IServiceScope scope) + { + Client = client; + ConfigService = configService; + Scope = scope; + } +} \ No newline at end of file diff --git a/Moonlight/App/Services/DiscordBot/DiscordBotService.cs b/Moonlight/App/Services/DiscordBot/DiscordBotService.cs new file mode 100644 index 0000000..1547a42 --- /dev/null +++ b/Moonlight/App/Services/DiscordBot/DiscordBotService.cs @@ -0,0 +1,57 @@ +using Discord; +using Discord.WebSocket; +using Logging.Net; + +namespace Moonlight.App.Services.DiscordBot; + +public class DiscordBotService +{ + private readonly IServiceScopeFactory ServiceScopeFactory; + private readonly ConfigService ConfigService; + + private IServiceScope ServiceScope; + + private readonly DiscordSocketClient Client; + + // Add here references so e.g. + // public ExampleModule ExampleModule { get; private set; } + + public DiscordBotService( + IServiceScopeFactory serviceScopeFactory, + ConfigService configService) + { + ServiceScopeFactory = serviceScopeFactory; + ConfigService = configService; + Client = new(); + + Task.Run(MainAsync); + } + + private async Task MainAsync() + { + ServiceScope = ServiceScopeFactory.CreateScope(); + + var discordConfig = ConfigService + .GetSection("Moonlight") + .GetSection("DiscordBot"); + + if(!discordConfig.GetValue("Enable")) + return; + + Client.Log += Log; + + // Init here the modules e.g. + // ExampleModule = new(Client, ConfigService, Scope) + + await Client.LoginAsync(TokenType.Bot, discordConfig.GetValue("Token")); + await Client.StartAsync(); + + await Task.Delay(-1); + } + + private Task Log(LogMessage message) + { + Logger.Debug(message.Message); + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/Moonlight/Moonlight.csproj b/Moonlight/Moonlight.csproj index 3e686cd..55d6a86 100644 --- a/Moonlight/Moonlight.csproj +++ b/Moonlight/Moonlight.csproj @@ -61,6 +61,7 @@ + diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index 4b2a599..83a37f7 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -10,6 +10,7 @@ using Moonlight.App.Repositories.LogEntries; using Moonlight.App.Repositories.Servers; using Moonlight.App.Repositories.Subscriptions; using Moonlight.App.Services; +using Moonlight.App.Services.DiscordBot; using Moonlight.App.Services.Interop; using Moonlight.App.Services.LogServices; using Moonlight.App.Services.Notifications; @@ -110,6 +111,9 @@ namespace Moonlight builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); + + // Background services + builder.Services.AddSingleton(); // Third party services @@ -137,6 +141,9 @@ namespace Moonlight // Support service var supportServerService = app.Services.GetRequiredService(); + + // Discord bot service + var discordBotService = app.Services.GetRequiredService(); app.Run(); }