diff --git a/Moonlight/App/Helpers/DiscordMaintenanceToggle.cs b/Moonlight/App/Helpers/DiscordMaintenanceToggle.cs new file mode 100644 index 00000000..07c57423 --- /dev/null +++ b/Moonlight/App/Helpers/DiscordMaintenanceToggle.cs @@ -0,0 +1,21 @@ +using Discord.WebSocket; +using Moonlight.App.Services.DiscordBot; + +namespace Moonlight.App.Helpers; + +public class DiscordMaintenanceToggle +{ + private Task MaintenanceModeToggle(SocketSlashCommand command) + { + if (DiscordBotService.MaintenanceMode) + { + DiscordBotService.MaintenanceMode = false; + } + else + { + DiscordBotService.MaintenanceMode = true; + } + return Task.CompletedTask; + } + +} \ No newline at end of file diff --git a/Moonlight/App/Services/DiscordBot/BaseModule.cs b/Moonlight/App/Services/DiscordBot/BaseModule.cs index 616435fd..17e2360e 100644 --- a/Moonlight/App/Services/DiscordBot/BaseModule.cs +++ b/Moonlight/App/Services/DiscordBot/BaseModule.cs @@ -2,12 +2,14 @@ namespace Moonlight.App.Services.DiscordBot; -public class BaseModule +public abstract class BaseModule { public DiscordSocketClient Client { get; set; } public ConfigService ConfigService { get; set; } public IServiceScope Scope { get; set; } - + + public abstract Task RegisterCommands(); + public BaseModule( DiscordSocketClient client, ConfigService configService, diff --git a/Moonlight/App/Services/DiscordBot/DiscordBotService.cs b/Moonlight/App/Services/DiscordBot/DiscordBotService.cs index 1547a42f..e79b8d6c 100644 --- a/Moonlight/App/Services/DiscordBot/DiscordBotService.cs +++ b/Moonlight/App/Services/DiscordBot/DiscordBotService.cs @@ -1,20 +1,25 @@ -using Discord; +using System.Diagnostics; +using Discord; using Discord.WebSocket; using Logging.Net; +using Moonlight.App.Services.DiscordBot.Commands; namespace Moonlight.App.Services.DiscordBot; public class DiscordBotService { + public static bool MaintenanceMode = false; + 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 RemoveCommandsModuels RemoveCommandsModuels { get; private set; } public DiscordBotService( IServiceScopeFactory serviceScopeFactory, @@ -39,19 +44,61 @@ public class DiscordBotService return; Client.Log += Log; - - // Init here the modules e.g. - // ExampleModule = new(Client, ConfigService, Scope) + Client.Ready += OnReady; + + //Commands + RemoveCommandsModuels = new(Client, ConfigService, ServiceScope); await Client.LoginAsync(TokenType.Bot, discordConfig.GetValue("Token")); await Client.StartAsync(); await Task.Delay(-1); } + private async Task OnReady() + { + //TODO: MASU Ladenachrichten jede Minute ändern. + await Client.SetGameAsync("Masu ist zu lazy um das zu Implementieren.", null, ActivityType.Listening); + 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($"Login as {Client.CurrentUser.Username}#{Client.CurrentUser.DiscriminatorValue}"); + } private Task Log(LogMessage message) { Logger.Debug(message.Message); return Task.CompletedTask; } + + public async Task CreateCommands() + { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + + var type = this.GetType(); + var properties = type.GetProperties(); + Logger.Debug("Start Initializing Commands (1 Command/s)"); + foreach (var property in properties) + { + if (property.PropertyType.IsSubclassOf(typeof(BaseModule))) + { + try + { + var instance = (BaseModule)property.GetValue(this)!; + await instance.RegisterCommands(); + Logger.Debug("Registered" + instance); + await Task.Delay(TimeSpan.FromMilliseconds(1000)); + } + catch (Exception ex) + { + Logger.Error($"Module Error {ex.Message}"); + Logger.Error(ex.InnerException); + } + } + } + + stopwatch.Stop(); + Logger.Info($"Registered all commands. Done in {stopwatch.ElapsedMilliseconds}ms"); + } + } \ No newline at end of file diff --git a/Moonlight/App/Services/DiscordBot/Modules/RemoveCommandsModuels.cs b/Moonlight/App/Services/DiscordBot/Modules/RemoveCommandsModuels.cs new file mode 100644 index 00000000..90e05bab --- /dev/null +++ b/Moonlight/App/Services/DiscordBot/Modules/RemoveCommandsModuels.cs @@ -0,0 +1,34 @@ +using System.Diagnostics; +using Discord; +using Discord.WebSocket; +using Logging.Net; + +namespace Moonlight.App.Services.DiscordBot.Commands; + +public class RemoveCommandsModuels : BaseModule +{ + public RemoveCommandsModuels(DiscordSocketClient client, ConfigService configService, IServiceScope scope) : base(client, configService, scope) {} + public override Task RegisterCommands() + { return Task.CompletedTask; } + + private async void VoidCommands() + { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + + var commands = await Client.GetGlobalApplicationCommandsAsync(); + if (commands == null) return; + + foreach (var slashCommand in commands) + { + if(slashCommand.Name == "commands") continue; + + await slashCommand.DeleteAsync(); + Logger.Debug($"Deleted {slashCommand.Name}, {slashCommand.Id}"); + await Task.Delay(TimeSpan.FromMilliseconds(1000)); + } + + stopwatch.Stop(); + Logger.Info($"Deleted all commands. Done in {stopwatch.ElapsedMilliseconds}ms"); + } +} \ No newline at end of file diff --git a/Moonlight/Moonlight.csproj b/Moonlight/Moonlight.csproj index c9c7eeda..3d16d5f0 100644 --- a/Moonlight/Moonlight.csproj +++ b/Moonlight/Moonlight.csproj @@ -62,7 +62,7 @@ - +