Added ActivityStatus

This commit is contained in:
Spielepapagei 2023-04-05 23:26:09 +02:00
parent 0e6910f46b
commit f4efb0f9a7
5 changed files with 83 additions and 24 deletions

View file

@ -2,26 +2,27 @@
using Discord; using Discord;
using Discord.WebSocket; using Discord.WebSocket;
using Logging.Net; using Logging.Net;
using Moonlight.App.Repositories;
using Moonlight.App.Services.DiscordBot.Commands; using Moonlight.App.Services.DiscordBot.Commands;
using Moonlight.App.Services.DiscordBot.Modules;
namespace Moonlight.App.Services.DiscordBot; namespace Moonlight.App.Services.DiscordBot;
public class DiscordBotService public class DiscordBotService
{ {
public static bool MaintenanceMode = false; public static bool MaintenanceMode = false;
private readonly IServiceScopeFactory ServiceScopeFactory; private readonly IServiceScopeFactory ServiceScopeFactory;
private readonly ConfigService ConfigService; private readonly ConfigService ConfigService;
private IServiceScope ServiceScope; private IServiceScope ServiceScope;
private readonly DiscordSocketClient Client; private readonly DiscordSocketClient Client;
// Add here references so e.g. // References
// public ExampleModule ExampleModule { get; private set; } public ActivityStatusModule ActivityStatusModule { get; private set; }
public RemoveCommandsModuels RemoveCommandsModuels { get; private set; }
public DiscordBotService( public DiscordBotService(
IServiceScopeFactory serviceScopeFactory, IServiceScopeFactory serviceScopeFactory,
ConfigService configService) ConfigService configService)
{ {
@ -39,15 +40,19 @@ public class DiscordBotService
var discordConfig = ConfigService var discordConfig = ConfigService
.GetSection("Moonlight") .GetSection("Moonlight")
.GetSection("DiscordBot"); .GetSection("DiscordBot");
if(!discordConfig.GetValue<bool>("Enable")) if (!discordConfig.GetValue<bool>("Enable"))
return; return;
Client.Log += Log; Client.Log += Log;
Client.Ready += OnReady; Client.Ready += OnReady;
//Commands //Commands
RemoveCommandsModuels = new(Client, ConfigService, ServiceScope);
//Module
ActivityStatusModule = new(Client, ConfigService, ServiceScope);
await ActivityStatusModule.UpdateActivityStatusList();
await Client.LoginAsync(TokenType.Bot, discordConfig.GetValue<string>("Token")); await Client.LoginAsync(TokenType.Bot, discordConfig.GetValue<string>("Token"));
await Client.StartAsync(); await Client.StartAsync();
@ -56,12 +61,13 @@ public class DiscordBotService
} }
private async Task OnReady() private async Task OnReady()
{ {
//TODO: MASU Ladenachrichten jede Minute ändern. //await Client.SetGameAsync(name: "https://endelon-hosting.de", type: ActivityType.Watching);
await Client.SetGameAsync("Masu ist zu lazy um das zu Implementieren.", null, ActivityType.Listening);
await Client.SetStatusAsync(UserStatus.Idle); await Client.SetStatusAsync(UserStatus.Idle);
Logger.Info($"Invite link: https://discord.com/api/oauth2/authorize?client_id={Client.CurrentUser.Id}&permissions=1099511696391&scope=bot%20applications.commands"); Logger.Info($"Invite link: https://discord.com/api/oauth2/authorize?client_id={Client.CurrentUser.Id}&permissions=1099511696391&scope=bot%20applications.commands");
Logger.Info($"Login as {Client.CurrentUser.Username}#{Client.CurrentUser.DiscriminatorValue}"); Logger.Info($"Login as {Client.CurrentUser.Username}#{Client.CurrentUser.DiscriminatorValue}");
Task.Run(ActivityStatusModule.ActivityStatusScheduler);
} }
private Task Log(LogMessage message) private Task Log(LogMessage message)
@ -69,12 +75,12 @@ public class DiscordBotService
Logger.Debug(message.Message); Logger.Debug(message.Message);
return Task.CompletedTask; return Task.CompletedTask;
} }
public async Task CreateCommands() public async Task CreateCommands()
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
var type = this.GetType(); var type = this.GetType();
var properties = type.GetProperties(); var properties = type.GetProperties();
Logger.Debug("Start Initializing Commands"); Logger.Debug("Start Initializing Commands");
@ -96,9 +102,9 @@ public class DiscordBotService
} }
} }
} }
stopwatch.Stop(); stopwatch.Stop();
Logger.Info($"Registered all commands. Done in {stopwatch.ElapsedMilliseconds}ms"); Logger.Info($"Registered all commands. Done in {stopwatch.ElapsedMilliseconds}ms");
} }
} }

View file

@ -0,0 +1,39 @@
using Discord;
using Discord.WebSocket;
using Moonlight.App.Database.Entities;
using Moonlight.App.Repositories;
namespace Moonlight.App.Services.DiscordBot.Modules;
public class ActivityStatusModule : BaseModule
{
private List<LoadingMessage> LoadingMessages;
private readonly PeriodicTimer Timer = new(TimeSpan.FromMinutes(1));
public ActivityStatusModule(DiscordSocketClient client, ConfigService configService, IServiceScope scope) : base(client, configService, scope)
{ }
public override Task RegisterCommands()
{ return Task.CompletedTask; }
public Task UpdateActivityStatusList()
{
var loadingMessageRepo = Scope.ServiceProvider.GetRequiredService<LoadingMessageRepository>();
LoadingMessages = loadingMessageRepo.Get().ToList();
return Task.CompletedTask;
}
public async void ActivityStatusScheduler()
{
while (await Timer.WaitForNextTickAsync())
{
Random rand = new Random();
LoadingMessage random = LoadingMessages[rand.Next(LoadingMessages.Count)];
await Client.SetGameAsync(random.Message, "https://www.endelon.team", ActivityType.Streaming);
}
}
}

View file

@ -66,7 +66,6 @@
<Folder Include="App\Http\Middleware" /> <Folder Include="App\Http\Middleware" />
<Folder Include="App\Models\Daemon\Requests" /> <Folder Include="App\Models\Daemon\Requests" />
<Folder Include="App\Models\Google\Resources" /> <Folder Include="App\Models\Google\Resources" />
<Folder Include="App\Services\DiscordBot\Commands" />
<Folder Include="resources\lang" /> <Folder Include="resources\lang" />
</ItemGroup> </ItemGroup>

View file

@ -60,7 +60,8 @@ namespace Moonlight
builder.Services.AddScoped<NotificationRepository>(); builder.Services.AddScoped<NotificationRepository>();
builder.Services.AddScoped<DdosAttackRepository>(); builder.Services.AddScoped<DdosAttackRepository>();
builder.Services.AddScoped<SubscriptionRepository>(); builder.Services.AddScoped<SubscriptionRepository>();
builder.Services.AddScoped<LoadingMessageRepository>();
builder.Services.AddScoped<AuditLogEntryRepository>(); builder.Services.AddScoped<AuditLogEntryRepository>();
builder.Services.AddScoped<ErrorLogEntryRepository>(); builder.Services.AddScoped<ErrorLogEntryRepository>();
builder.Services.AddScoped<SecurityLogEntryRepository>(); builder.Services.AddScoped<SecurityLogEntryRepository>();
@ -125,7 +126,6 @@ namespace Moonlight
builder.Services.AddSingleton<DiscordBotService>(); builder.Services.AddSingleton<DiscordBotService>();
// Third party services // Third party services
builder.Services.AddBlazorTable(); builder.Services.AddBlazorTable();
builder.Services.AddSweetAlert2(options => { options.Theme = SweetAlertTheme.Dark; }); builder.Services.AddSweetAlert2(options => { options.Theme = SweetAlertTheme.Dark; });
builder.Services.AddBlazorContextMenu(); builder.Services.AddBlazorContextMenu();
@ -153,8 +153,9 @@ namespace Moonlight
// Support service // Support service
var supportServerService = app.Services.GetRequiredService<SupportServerService>(); var supportServerService = app.Services.GetRequiredService<SupportServerService>();
// cleanup service // AutoStart services
_ = app.Services.GetRequiredService<CleanupService>(); _ = app.Services.GetRequiredService<CleanupService>();
_ = app.Services.GetRequiredService<DiscordBotService>();
// Discord bot service // Discord bot service
//var discordBotService = app.Services.GetRequiredService<DiscordBotService>(); //var discordBotService = app.Services.GetRequiredService<DiscordBotService>();

View file

@ -6,20 +6,34 @@
<OnlyAdmin> <OnlyAdmin>
<AdminSystemNavigation Index="6"/> <AdminSystemNavigation Index="6"/>
<div class="mt-3 card card-body"> <div class="mt-3 card card-body">
<WButton Text="Register commands" <WButton Text="Register commands"
WorkingText="Working" WorkingText="Working"
CssClasses="btn-primary" CssClasses="btn-primary"
OnClick="RegisterCommands"> OnClick="RegisterCommands">
</WButton> </WButton>
<WButton Text="Void commands"
WorkingText="Working"
CssClasses="mt-3 btn-danger"
OnClick="VoidCommands">
</WButton>
</div> </div>
</OnlyAdmin> </OnlyAdmin>
@code @code
{ {
//ToDo: Ole muss ins Bett gehen
//ToDo: Bot Info Card with Name, Bot Avatar, (RichPresence) Game Status, Activity Status
private async Task RegisterCommands() private async Task RegisterCommands()
{ {
await DiscordBotService.CreateCommands();
}
private Task VoidCommands()
{
return Task.CompletedTask;
} }
} }