Added base discord bot
This commit is contained in:
20
Moonlight/App/Services/DiscordBot/BaseModule.cs
Normal file
20
Moonlight/App/Services/DiscordBot/BaseModule.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
57
Moonlight/App/Services/DiscordBot/DiscordBotService.cs
Normal file
57
Moonlight/App/Services/DiscordBot/DiscordBotService.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -61,6 +61,7 @@
|
|||||||
<Folder Include="App\Http\Middleware" />
|
<Folder Include="App\Http\Middleware" />
|
||||||
<Folder Include="App\Models\AuditLogData" />
|
<Folder Include="App\Models\AuditLogData" />
|
||||||
<Folder Include="App\Models\Google\Resources" />
|
<Folder Include="App\Models\Google\Resources" />
|
||||||
|
<Folder Include="App\Services\DiscordBot\Modules" />
|
||||||
<Folder Include="resources\lang" />
|
<Folder Include="resources\lang" />
|
||||||
<Folder Include="wwwroot\assets\media" />
|
<Folder Include="wwwroot\assets\media" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ using Moonlight.App.Repositories.LogEntries;
|
|||||||
using Moonlight.App.Repositories.Servers;
|
using Moonlight.App.Repositories.Servers;
|
||||||
using Moonlight.App.Repositories.Subscriptions;
|
using Moonlight.App.Repositories.Subscriptions;
|
||||||
using Moonlight.App.Services;
|
using Moonlight.App.Services;
|
||||||
|
using Moonlight.App.Services.DiscordBot;
|
||||||
using Moonlight.App.Services.Interop;
|
using Moonlight.App.Services.Interop;
|
||||||
using Moonlight.App.Services.LogServices;
|
using Moonlight.App.Services.LogServices;
|
||||||
using Moonlight.App.Services.Notifications;
|
using Moonlight.App.Services.Notifications;
|
||||||
@@ -110,6 +111,9 @@ namespace Moonlight
|
|||||||
builder.Services.AddScoped<WingsConsoleHelper>();
|
builder.Services.AddScoped<WingsConsoleHelper>();
|
||||||
builder.Services.AddSingleton<PaperApiHelper>();
|
builder.Services.AddSingleton<PaperApiHelper>();
|
||||||
builder.Services.AddSingleton<HostSystemHelper>();
|
builder.Services.AddSingleton<HostSystemHelper>();
|
||||||
|
|
||||||
|
// Background services
|
||||||
|
builder.Services.AddSingleton<DiscordBotService>();
|
||||||
|
|
||||||
// Third party services
|
// Third party services
|
||||||
|
|
||||||
@@ -137,6 +141,9 @@ namespace Moonlight
|
|||||||
|
|
||||||
// Support service
|
// Support service
|
||||||
var supportServerService = app.Services.GetRequiredService<SupportServerService>();
|
var supportServerService = app.Services.GetRequiredService<SupportServerService>();
|
||||||
|
|
||||||
|
// Discord bot service
|
||||||
|
var discordBotService = app.Services.GetRequiredService<DiscordBotService>();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user