CreadedDiscordBot
This commit is contained in:
21
Moonlight/App/Helpers/DiscordMaintenanceToggle.cs
Normal file
21
Moonlight/App/Helpers/DiscordMaintenanceToggle.cs
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
namespace Moonlight.App.Services.DiscordBot;
|
namespace Moonlight.App.Services.DiscordBot;
|
||||||
|
|
||||||
public class BaseModule
|
public abstract class BaseModule
|
||||||
{
|
{
|
||||||
public DiscordSocketClient Client { get; set; }
|
public DiscordSocketClient Client { get; set; }
|
||||||
public ConfigService ConfigService { get; set; }
|
public ConfigService ConfigService { get; set; }
|
||||||
public IServiceScope Scope { get; set; }
|
public IServiceScope Scope { get; set; }
|
||||||
|
|
||||||
|
public abstract Task RegisterCommands();
|
||||||
|
|
||||||
public BaseModule(
|
public BaseModule(
|
||||||
DiscordSocketClient client,
|
DiscordSocketClient client,
|
||||||
ConfigService configService,
|
ConfigService configService,
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
using Discord;
|
using System.Diagnostics;
|
||||||
|
using Discord;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using Logging.Net;
|
using Logging.Net;
|
||||||
|
using Moonlight.App.Services.DiscordBot.Commands;
|
||||||
|
|
||||||
namespace Moonlight.App.Services.DiscordBot;
|
namespace Moonlight.App.Services.DiscordBot;
|
||||||
|
|
||||||
public class DiscordBotService
|
public class DiscordBotService
|
||||||
{
|
{
|
||||||
|
public static bool MaintenanceMode = false;
|
||||||
|
|
||||||
private readonly IServiceScopeFactory ServiceScopeFactory;
|
private readonly IServiceScopeFactory ServiceScopeFactory;
|
||||||
private readonly ConfigService ConfigService;
|
private readonly ConfigService ConfigService;
|
||||||
|
|
||||||
@@ -15,6 +19,7 @@ public class DiscordBotService
|
|||||||
|
|
||||||
// Add here references so e.g.
|
// Add here references so e.g.
|
||||||
// public ExampleModule ExampleModule { get; private set; }
|
// public ExampleModule ExampleModule { get; private set; }
|
||||||
|
public RemoveCommandsModuels RemoveCommandsModuels { get; private set; }
|
||||||
|
|
||||||
public DiscordBotService(
|
public DiscordBotService(
|
||||||
IServiceScopeFactory serviceScopeFactory,
|
IServiceScopeFactory serviceScopeFactory,
|
||||||
@@ -39,19 +44,61 @@ public class DiscordBotService
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
Client.Log += Log;
|
Client.Log += Log;
|
||||||
|
Client.Ready += OnReady;
|
||||||
|
|
||||||
// Init here the modules e.g.
|
//Commands
|
||||||
// ExampleModule = new(Client, ConfigService, Scope)
|
RemoveCommandsModuels = new(Client, ConfigService, ServiceScope);
|
||||||
|
|
||||||
await Client.LoginAsync(TokenType.Bot, discordConfig.GetValue<string>("Token"));
|
await Client.LoginAsync(TokenType.Bot, discordConfig.GetValue<string>("Token"));
|
||||||
await Client.StartAsync();
|
await Client.StartAsync();
|
||||||
|
|
||||||
await Task.Delay(-1);
|
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)
|
private Task Log(LogMessage message)
|
||||||
{
|
{
|
||||||
Logger.Debug(message.Message);
|
Logger.Debug(message.Message);
|
||||||
return Task.CompletedTask;
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -62,7 +62,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="App\Services\DiscordBot\Commands" />
|
||||||
<Folder Include="resources\lang" />
|
<Folder Include="resources\lang" />
|
||||||
<Folder Include="wwwroot\assets\media" />
|
<Folder Include="wwwroot\assets\media" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user