This commit is contained in:
Spielepapagei 2023-03-06 20:31:49 +01:00
commit fd1ac4078d
4 changed files with 85 additions and 0 deletions

View file

@ -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;
}
}

View file

@ -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<bool>("Enable"))
return;
Client.Log += Log;
// Init here the modules e.g.
// ExampleModule = new(Client, ConfigService, Scope)
await Client.LoginAsync(TokenType.Bot, discordConfig.GetValue<string>("Token"));
await Client.StartAsync();
await Task.Delay(-1);
}
private Task Log(LogMessage message)
{
Logger.Debug(message.Message);
return Task.CompletedTask;
}
}

View file

@ -61,6 +61,7 @@
<Folder Include="App\Http\Middleware" />
<Folder Include="App\Models\AuditLogData" />
<Folder Include="App\Models\Google\Resources" />
<Folder Include="App\Services\DiscordBot\Modules" />
<Folder Include="resources\lang" />
<Folder Include="wwwroot\assets\media" />
</ItemGroup>

View file

@ -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<WingsConsoleHelper>();
builder.Services.AddSingleton<PaperApiHelper>();
builder.Services.AddSingleton<HostSystemHelper>();
// Background services
builder.Services.AddSingleton<DiscordBotService>();
// Third party services
@ -137,6 +141,9 @@ namespace Moonlight
// Support service
var supportServerService = app.Services.GetRequiredService<SupportServerService>();
// Discord bot service
var discordBotService = app.Services.GetRequiredService<DiscordBotService>();
app.Run();
}