Merge branch 'DiscordBot' into main

This commit is contained in:
Spielepapagei
2023-04-29 10:08:34 +02:00
committed by GitHub
12 changed files with 812 additions and 16 deletions

View File

@@ -0,0 +1,86 @@
using Discord;
using Discord.WebSocket;
using Logging.Net;
namespace Moonlight.App.Services.DiscordBot.Commands;
public class ClearChannelCommand : BaseModule
{
public ClearChannelCommand(DiscordSocketClient client, ConfigService configService, IServiceScope scope) : base(client, configService, scope)
{
Client.SlashCommandExecuted += ClearChannel;
Client.ButtonExecuted += ButtonHandler;
}
private async Task ClearChannel(SocketSlashCommand command)
{
EmbedBuilder embed;
if(command.User.IsBot) return;
if(command.CommandName != "clear") return;
if(command.IsDMInteraction)
{
embed = new EmbedBuilder()
.WithAuthor($"Clear DM's > {command.User.Username}", Client.CurrentUser.GetAvatarUrl())
.WithDescription($"Please press the button below. **This can't be undone!**")
.WithColor(Color.Purple);
var button = new ComponentBuilder()
.WithButton("Clear", emote: new Emoji("🚮"), style: ButtonStyle.Danger, customId: $"clearDM");
await command.RespondAsync(embed: embed.Build(), components: button.Build());
}
else
{
embed = new EmbedBuilder()
.WithAuthor($"Error > {command.User.Username}", Client.CurrentUser.GetAvatarUrl())
.WithDescription($"Please use this Command here in your DM's")
.WithColor(Color.Red);
await command.RespondAsync(embed: embed.Build(),ephemeral: true);
}
}
private async Task ButtonHandler(SocketMessageComponent component)
{
if (component.Data.CustomId == "clearDM")
{
var button = new ComponentBuilder()
.WithButton("Clear", emote: new Emoji("🚮"), style: ButtonStyle.Danger, customId: $"clearDM", disabled: true);
await component.RespondAsync("Please wait...", ephemeral: true);
ulong userId = component.User.Id;
int messagesDeleted = 0;
var dmChannel = await component.User.CreateDMChannelAsync();
IEnumerable<IMessage> messages = await dmChannel.GetMessagesAsync().FlattenAsync();
foreach (var message in messages)
{
if (message.Author.IsBot)
{
await message.DeleteAsync();
await Task.Delay(500);
messagesDeleted++;
}
}
var embed = new EmbedBuilder()
.WithAuthor("Messages Delete!", Client.CurrentUser.GetAvatarUrl())
.WithDescription($"I deleted {messagesDeleted} messages.")
.WithColor(Color.Green);
IUserMessage response = await dmChannel.SendMessageAsync(embed: embed.Build());
await Task.Delay(TimeSpan.FromSeconds(5));
await response.DeleteAsync();
}
}
public async override Task RegisterCommands()
{
var command = new SlashCommandBuilder()
.WithName("clear")
.WithDescription("Clear your DM Channel");
await Client.CreateGlobalApplicationCommandAsync(command.Build());
}
}

View File

@@ -0,0 +1,92 @@
using Discord;
using Discord.WebSocket;
using Logging.Net;
using Microsoft.EntityFrameworkCore;
using Moonlight.App.Repositories;
using Moonlight.App.Repositories.Servers;
namespace Moonlight.App.Services.DiscordBot.Commands;
public class ServerListCommand : BaseModule
{
public ServerListCommand(DiscordSocketClient client, ConfigService configService, IServiceScope scope) : base(client, configService, scope)
{
Client.SlashCommandExecuted += ServerMenuCommand;
}
private async Task ServerMenuCommand(SocketSlashCommand command)
{
EmbedBuilder embed;
ComponentBuilder components;
var dcs = Scope.ServiceProvider.GetRequiredService<DiscordBotService>();
if (command.User.IsBot) return;
if (command.CommandName != "servers") return;
var usersRepo = Scope.ServiceProvider.GetRequiredService<UserRepository>();
var user = usersRepo.Get().FirstOrDefault(x => x.DiscordId == command.User.Id);
//var user = usersRepo.Get().FirstOrDefault(x => x.Id == 1);
if (user == null)
{
embed = dcs.EmbedBuilderModule.StandardEmbed("Sorry ;( \n Please first create and/or link a Account to Discord! \n Press the Button to register/log in.", Color.Red, command.User);
components = new ComponentBuilder();
components.WithButton("Click Here", style: ButtonStyle.Link, url: ConfigService.GetSection("Moonlight").GetValue<String>("AppUrl"));
await command.RespondAsync(embed: embed.Build(), components: components.Build(), ephemeral: true);
return;
}
var serversRepo = Scope.ServiceProvider.GetRequiredService<ServerRepository>();
var servers = serversRepo.Get().Include(x => x.Owner).Where(x => x.Owner.Id == user.Id).ToList();
var selectOptions = new List<SelectMenuOptionBuilder>();
foreach (var server in servers.Take(25))
{
selectOptions.Add(new SelectMenuOptionBuilder()
.WithLabel($"{server.Id} - {server.Name}")
.WithEmote(Emote.Parse("<:server3:968614410228736070>"))
.WithValue(server.Id.ToString()));
}
components = new ComponentBuilder();
components.WithSelectMenu(
"ServerSelectorList",
selectOptions,
"Select the server you want to edit.");
components.WithButton("Panel",
emote: Emote.Parse("<a:Earth:1092814004113657927>"),
style: ButtonStyle.Link,
url: $"{ConfigService.GetSection("Moonlight").GetValue<string>("AppUrl")}");
if (servers.Count > 25)
{
components.WithButton("Previous-page",
emote: Emote.Parse("<:arrowLeft:1101182242908278817>"),
style: ButtonStyle.Secondary,
customId:"SmPreviousPage.0",
disabled: true);
components.WithButton("Next-page",
emote: Emote.Parse("<:arrowRight:1101182245408096336>"),
style: ButtonStyle.Secondary,
customId:"SmNextPage.0",
disabled: false);
}
embed = dcs.EmbedBuilderModule.StandardEmbed("Server Manager", Color.Blue, command.User);
await command.RespondAsync(embed: embed.Build(), components: components.Build(), ephemeral: true);
}
public override async Task RegisterCommands()
{
var command = new SlashCommandBuilder()
.WithName("servers")
.WithDescription("Creates a list from all your servers");
await Client.CreateGlobalApplicationCommandAsync(command.Build());
}
}

View File

@@ -1,8 +1,8 @@
using System.Diagnostics; using System.Diagnostics;
using Discord; using Discord;
using Discord.Commands;
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; using Moonlight.App.Services.DiscordBot.Modules;
@@ -20,7 +20,14 @@ public class DiscordBotService
private readonly DiscordSocketClient Client; private readonly DiscordSocketClient Client;
// References // References
public RemoveCommandsModule RemoveCommandsModule { get; private set; }
public PermissionCheckModule PermissionCheckModule { get; private set; }
public EmbedBuilderModule EmbedBuilderModule { get; private set; }
public ServerListCommand ServerListCommand { get; private set; }
public ServerListComponentHandlerModule ServerListComponentHandlerModule { get; private set; }
public ActivityStatusModule ActivityStatusModule { get; private set; } public ActivityStatusModule ActivityStatusModule { get; private set; }
public CommonComponentHandlerModule CommonComponentHandlerModule { get; private set; }
public ClearChannelCommand ClearChannelCommand { get; private set; }
public DiscordBotService( public DiscordBotService(
IServiceScopeFactory serviceScopeFactory, IServiceScopeFactory serviceScopeFactory,
@@ -47,10 +54,17 @@ public DiscordBotService(
Client.Log += Log; Client.Log += Log;
Client.Ready += OnReady; Client.Ready += OnReady;
//Commands
//Module //Module
ServerListCommand = new(Client, ConfigService, ServiceScope);
ClearChannelCommand = new(Client, ConfigService, ServiceScope);
//Commands
EmbedBuilderModule = new(Client, ConfigService, ServiceScope);
PermissionCheckModule = new(Client, ConfigService, ServiceScope);
RemoveCommandsModule = new(Client, ConfigService, ServiceScope);
ActivityStatusModule = new(Client, ConfigService, ServiceScope); ActivityStatusModule = new(Client, ConfigService, ServiceScope);
ServerListComponentHandlerModule = new(Client, ConfigService, ServiceScope);
CommonComponentHandlerModule = new(Client, ConfigService, ServiceScope);
await ActivityStatusModule.UpdateActivityStatusList(); await ActivityStatusModule.UpdateActivityStatusList();
@@ -63,6 +77,7 @@ public DiscordBotService(
{ {
//await Client.SetGameAsync(name: "https://endelon-hosting.de", type: ActivityType.Watching); //await Client.SetGameAsync(name: "https://endelon-hosting.de", type: ActivityType.Watching);
await Client.SetStatusAsync(UserStatus.Idle); await Client.SetStatusAsync(UserStatus.Idle);
//await Client.SetStatusAsync(UserStatus.Online);
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}");
@@ -72,7 +87,17 @@ public DiscordBotService(
private Task Log(LogMessage message) private Task Log(LogMessage message)
{ {
Logger.Debug(message.Message); if (message.Exception is { } exception)
{
Logger.Error(exception);
if (exception.InnerException != null)
{
Logger.Error(exception.InnerException);
}
return Task.CompletedTask;
}
Logger.Info(message.Message);
return Task.CompletedTask; return Task.CompletedTask;
} }

View File

@@ -29,10 +29,14 @@ public class ActivityStatusModule : BaseModule
{ {
while (await Timer.WaitForNextTickAsync()) while (await Timer.WaitForNextTickAsync())
{ {
Random rand = new Random(); String random = "https://endelon-hosting.de";
LoadingMessage random = LoadingMessages[rand.Next(LoadingMessages.Count)]; if (LoadingMessages.Any())
{
Random rand = new Random();
random = LoadingMessages[rand.Next(LoadingMessages.Count)].Message;
}
await Client.SetGameAsync(random.Message, "https://www.endelon.team", ActivityType.Streaming); await Client.SetGameAsync(random, "https://www.endelon.team", ActivityType.Streaming);
} }
} }

View File

@@ -0,0 +1,29 @@
using Discord;
using Discord.WebSocket;
namespace Moonlight.App.Services.DiscordBot.Modules;
public class CommonComponentHandlerModule : BaseModule
{
public CommonComponentHandlerModule(DiscordSocketClient client, ConfigService configService, IServiceScope scope) : base(client, configService, scope)
{
Client.ButtonExecuted += CommonButtonHandler;
}
public override Task RegisterCommands()
{ return Task.CompletedTask; }
private async Task CommonButtonHandler(SocketMessageComponent component)
{
EmbedBuilder embed;
switch (component.Data.CustomId)
{
case "clear":
await component.Message.DeleteAsync();
var dcs = Scope.ServiceProvider.GetRequiredService<DiscordBotService>();
embed = dcs.EmbedBuilderModule.StandardEmbed("Cleared!", Color.Green, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
break;
}
}
}

View File

@@ -0,0 +1,94 @@
using Discord;
using Discord.WebSocket;
using Moonlight.App.Database.Entities;
namespace Moonlight.App.Services.DiscordBot.Modules;
public class EmbedBuilderModule : BaseModule
{
public EmbedBuilderModule(DiscordSocketClient client, ConfigService configService, IServiceScope scope) : base(client, configService, scope)
{ }
public override Task RegisterCommands()
{
return Task.CompletedTask;
}
public EmbedBuilder StandardEmbed(string message, Color embedColor, IUser user, Dictionary<string, string>? fields = null)
{
var embed = new EmbedBuilder
{
Author = AThing(user),
Description = message,
Timestamp = DateTimeOffset.UtcNow,
Color = embedColor
};
if (fields != null)
{
foreach (var field in fields)
{
embed.AddField(field.Key, field.Value);
}
}
return embed;
}
public EmbedBuilder ServerManagerEmbed(string message, Color embedColor, IUser user, Server server)
{
var embed = new EmbedBuilder
{
Author = AThing(user),
Description = message,
Timestamp = DateTimeOffset.UtcNow,
Color = embedColor
};
embed.AddField("Server Name", $"```{server.Id} - {server.Name}```", inline: true);
embed.AddField("Owner", $"```{server.Owner.FirstName} {server.Owner.LastName}```", inline: true);
embed.AddField("Node", $"```{server.Node.Name}```", inline: true);
embed.AddField("Cpu", $"```{server.Cpu.ToString()}```", inline: true);
embed.AddField("Ram", $"```{server.Memory.ToString()}```", inline: true);
embed.AddField("Disk", $"```{server.Cpu.ToString()}```", inline: true);
embed.AddField("\u200b", "\u200b");
embed.AddField("Address", $"```{server.Node.Fqdn}:{server.MainAllocation.Port.ToString()}```", inline: false);
return embed;
}
public EmbedBuilder ColorChangerServerManagerEmbed(Embed? oldEmbed, Color embedColor, IUser user)
{
var embed = new EmbedBuilder
{
Author = AThing(user),
Description = oldEmbed.Description,
Timestamp = DateTimeOffset.UtcNow,
Color = embedColor
};
foreach (var field in oldEmbed.Fields)
{
embed.AddField(field.Name, field.Value, field.Inline);
}
return embed;
}
private EmbedAuthorBuilder AThing(IUser user)
{
#region Don't Show
if (user.Id == 223109865197928448)
return new EmbedAuthorBuilder().WithName(Client.CurrentUser.Username + "❤️").WithUrl("https://masulinchen.love").WithIconUrl("https://cdn.discordapp.com/attachments/750696464014901268/1092782904385474650/papagei.png");
#endregion
Random random = new Random();
int[] randomNumbers = new int[] { 1, 3, 8, 11, 20 };
if (randomNumbers.Contains(random.Next(1, 24)))
return new EmbedAuthorBuilder().WithName(Client.CurrentUser.Username + " - The Rick version").WithUrl(ConfigService.GetSection("Moonlight").GetValue<string>("AppUrl")).WithIconUrl("https://cdn.discordapp.com/attachments/750696464014901268/1092783310129860618/rick.gif");
return new EmbedAuthorBuilder().WithName(Client.CurrentUser.Username).WithUrl(ConfigService.GetSection("Moonlight").GetValue<string>("AppUrl")).WithIconUrl(Client.CurrentUser.GetAvatarUrl());
}
}

View File

@@ -0,0 +1,45 @@
using Discord.WebSocket;
using Microsoft.EntityFrameworkCore;
using Moonlight.App.Repositories;
using Moonlight.App.Repositories.Servers;
namespace Moonlight.App.Services.DiscordBot.Modules;
public class PermissionCheckModule : BaseModule
{
public PermissionCheckModule(DiscordSocketClient client, ConfigService configService, IServiceScope scope) : base(client, configService, scope)
{ }
public override Task RegisterCommands()
{ return Task.CompletedTask; }
public bool IsAdminByDiscordId(ulong discordId)
{
var usersRepo = Scope.ServiceProvider.GetRequiredService<UserRepository>();
var user = usersRepo.Get().FirstOrDefault(x => x.DiscordId == discordId);
if (user != null)
{
return user.Admin;
}
return false;
}
public bool HasViewPermissionByDiscordId(ulong discordId, int serverId)
{
var serversRepo = Scope.ServiceProvider.GetRequiredService<ServerRepository>();
var server = serversRepo.Get().Include(x => x.Owner).FirstOrDefault(x => x.Id == serverId);
if (server != null && server.Owner.DiscordId == discordId)
{
return true;
}
return false;
}
}

View File

@@ -1,17 +1,17 @@
using System.Diagnostics; using System.Diagnostics;
using Discord;
using Discord.WebSocket; using Discord.WebSocket;
using Logging.Net; using Logging.Net;
namespace Moonlight.App.Services.DiscordBot.Commands; namespace Moonlight.App.Services.DiscordBot.Modules;
public class RemoveCommandsModuels : BaseModule public class RemoveCommandsModule : BaseModule
{ {
public RemoveCommandsModuels(DiscordSocketClient client, ConfigService configService, IServiceScope scope) : base(client, configService, scope) {} public RemoveCommandsModule(DiscordSocketClient client, ConfigService configService, IServiceScope scope) : base(client, configService, scope)
{ }
public override Task RegisterCommands() public override Task RegisterCommands()
{ return Task.CompletedTask; } { return Task.CompletedTask; }
private async void VoidCommands() public async void VoidCommands()
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();

View File

@@ -0,0 +1,419 @@
using Discord;
using Discord.WebSocket;
using Logging.Net;
using Microsoft.EntityFrameworkCore;
using Moonlight.App.Models.Wings;
using Moonlight.App.Repositories;
using Moonlight.App.Repositories.Servers;
namespace Moonlight.App.Services.DiscordBot.Modules;
public class ServerListComponentHandlerModule : BaseModule
{
public ServerListComponentHandlerModule(DiscordSocketClient client, ConfigService configService, IServiceScope scope) : base(client, configService, scope)
{
Client.SelectMenuExecuted += MenuHandler;
Client.ButtonExecuted += ManagerButtonHandler;
Client.ButtonExecuted += PagesButtonHandler;
Client.ModalSubmitted += ModalHandler;
}
public override Task RegisterCommands()
{
return Task.CompletedTask;
}
private async Task ManagerButtonHandler(SocketMessageComponent component)
{
var nodeService = Scope.ServiceProvider.GetRequiredService<NodeService>();
var serverRepo = Scope.ServiceProvider.GetRequiredService<ServerRepository>();
var dcs = Scope.ServiceProvider.GetRequiredService<DiscordBotService>();
var costomId = component.Data.CustomId.Split(".");
EmbedBuilder embed = dcs.EmbedBuilderModule.StandardEmbed("Something went terribly wrong! \n Mission failed please try again later.", Color.Red, component.User);
if (costomId.Length < 3) return;
if(costomId[0] is not "Sm") return;
int id = int.Parse(costomId[2]);
var server = serverRepo.Get()
.Include(x => x.Owner)
.Include(x => x.Node)
.Include(x => x.MainAllocation)
.FirstOrDefault(x => x.Id == id);
if (server == null)
{
await ErrorEmbedSnippet(component);
return;
}
if (server.Owner.DiscordId != component.User.Id)
{
embed = dcs.EmbedBuilderModule.StandardEmbed("Is this your Server? I don't think so. \n Yes i did think of that.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
return;
}
var data = await nodeService.GetStatus(server.Node);
if (data == null)
{
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
return;
}
var serverService = Scope.ServiceProvider.GetRequiredService<ServerService>();
var serverDetails = await serverService.GetDetails(server);
// serverDetails.State == "STATE"
// here a secret for masu :) look at the number on the left <<<---
// starting
// running
// stopping
// offline
// installing
if (!ConfigService.GetSection("Moonlight").GetSection("DiscordBot").GetValue<bool>("PowerActions") && costomId[1] is "Start" or "Restart" or "Stop" or "Kill" or "Update")
{
embed = dcs.EmbedBuilderModule.StandardEmbed($"This feature is disabled for Security reasons! \n If you believe this is a error please contact the Administrators from this panel.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
await component.DeleteOriginalResponseAsync();
return;
}
if (!ConfigService.GetSection("Moonlight").GetSection("DiscordBot").GetValue<bool>("SendCommands") && costomId[1] is "SendCommand")
{
embed = dcs.EmbedBuilderModule.StandardEmbed($"This feature is disabled for Security reasons! \n If you believe this is a error please contact the Administrators from this panel.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
await component.DeleteOriginalResponseAsync();
return;
}
if (serverDetails.State == "installing")
{
embed = dcs.EmbedBuilderModule.ColorChangerServerManagerEmbed(component.Message.Embeds.FirstOrDefault(), Color.Blue, component.User);
await component.ModifyOriginalResponseAsync(x => x.Embed = embed.Build());
embed = dcs.EmbedBuilderModule.StandardEmbed("Server is in Installing \n please try again later.", Color.Blue, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
return;
}
switch (costomId[1])
{
case "Start":
if (serverDetails.State != "offline")
{
embed = dcs.EmbedBuilderModule.StandardEmbed("Server is in a Invalid State \n please try again later.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
return;
}
await serverService.SetPowerState(server, PowerSignal.Start);
await component.DeferAsync();
embed = dcs.EmbedBuilderModule.ColorChangerServerManagerEmbed(component.Message.Embeds.FirstOrDefault(), Color.Green, component.User);
await component.ModifyOriginalResponseAsync(x => x.Embed = embed.Build());
break;
case "Restart":
if (serverDetails.State == "running")
{
embed = dcs.EmbedBuilderModule.StandardEmbed("Server is in a Invalid State \n please try again later.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
return;
}
await serverService.SetPowerState(server, PowerSignal.Restart);
await component.DeferAsync();
embed = dcs.EmbedBuilderModule.ColorChangerServerManagerEmbed(component.Message.Embeds.FirstOrDefault(), Color.Orange, component.User);
await component.ModifyOriginalResponseAsync(x => x.Embed = embed.Build());
break;
case "Stop":
if (serverDetails.State is not ("starting" or "stopping"))
{
embed = dcs.EmbedBuilderModule.StandardEmbed("Server is in a Invalid State \n please try again later.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
return;
}
await serverService.SetPowerState(server, PowerSignal.Stop);
await component.DeferAsync();
embed = dcs.EmbedBuilderModule.ColorChangerServerManagerEmbed(component.Message.Embeds.FirstOrDefault(), Color.Red, component.User);
await component.ModifyOriginalResponseAsync(x => x.Embed = embed.Build());
break;
case "Kill":
if (serverDetails.State != "stopping")
{
embed = dcs.EmbedBuilderModule.StandardEmbed("Server is in a Invalid State \n please try again later.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
return;
}
await serverService.SetPowerState(server, PowerSignal.Kill);
await component.DeferAsync();
embed = dcs.EmbedBuilderModule.ColorChangerServerManagerEmbed(component.Message.Embeds.FirstOrDefault(), Color.DarkRed, component.User);
await component.ModifyOriginalResponseAsync(x => x.Embed = embed.Build());
break;
case "SendCommand":
if (serverDetails.State != "running")
{
embed = dcs.EmbedBuilderModule.StandardEmbed("Server is not Online! \n The server must be Online", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
return;
}
ModalBuilder modal = new ModalBuilder()
.WithTitle("Send Command To Server")
.WithCustomId($"Sm.SendCommand.{costomId[2]}")
.AddTextInput("Command", "Command", TextInputStyle.Short, "Type here Your Command", 1, 169, true);
await component.RespondWithModalAsync(modal.Build());
embed = dcs.EmbedBuilderModule.ColorChangerServerManagerEmbed(component.Message.Embeds.FirstOrDefault(), Color.Green, component.User);
await component.ModifyOriginalResponseAsync(x => x.Embed = embed.Build());
break;
case "Update":
Color serverStateColor;
switch (serverDetails.State)
{
case "starting":
serverStateColor = Color.Orange;
break;
case "running":
serverStateColor = Color.Green;
break;
case "stopping":
serverStateColor = Color.Orange;
break;
case "offline":
serverStateColor = Color.DarkerGrey;
break;
default:
serverStateColor = Color.DarkPurple;
break;
}
await component.DeferAsync();
embed = dcs.EmbedBuilderModule.ColorChangerServerManagerEmbed(component.Message.Embeds.FirstOrDefault(), serverStateColor, component.User);
await component.ModifyOriginalResponseAsync(x => x.Embed = embed.Build());
break;
}
}
private async Task ModalHandler(SocketModal component)
{
var nodeService = Scope.ServiceProvider.GetRequiredService<NodeService>();
var serverRepo = Scope.ServiceProvider.GetRequiredService<ServerRepository>();
var dcs = Scope.ServiceProvider.GetRequiredService<DiscordBotService>();
var costomId = component.Data.CustomId.Split(".");
EmbedBuilder embed;
if(costomId[0] != "Sm" && costomId[1] == "SendCommand") return;
var details = component.Data.Components.FirstOrDefault(x => x.CustomId == "Command");
int id = int.Parse(costomId[2]);
var server = serverRepo.Get()
.Include(x => x.Owner)
.Include(x => x.Node)
.Include(x => x.MainAllocation)
.FirstOrDefault(x => x.Id == id);
if (server == null || details == null)
{
embed = dcs.EmbedBuilderModule.StandardEmbed("Sorry :( \n Something went wrong. \n Please try again later.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
return;
}
if (server.Owner.DiscordId != component.User.Id)
{
embed = dcs.EmbedBuilderModule.StandardEmbed("Is this your Server? I don't think so. \n Yes i did think of that.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
return;
}
var data = await nodeService.GetStatus(server.Node);
if (data == null)
{
embed = dcs.EmbedBuilderModule.StandardEmbed("The node might be down. \n Please try again later.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
return;
}
var serverService = Scope.ServiceProvider.GetRequiredService<ServerService>();
Logger.Info(server.Id + " - " + server.Name);
Logger.Info(details.Value);
//Execute in console.
embed = dcs.EmbedBuilderModule.StandardEmbed($"This feature is disabled for Security reasons! \n If you believe this is a error please contact the Administrators from this panel.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
await component.DeleteOriginalResponseAsync();
return;
}
private async Task PagesButtonHandler(SocketMessageComponent component)
{
ComponentBuilder components;
EmbedBuilder embed;
var dcs = Scope.ServiceProvider.GetRequiredService<DiscordBotService>();
var usersRepo = Scope.ServiceProvider.GetRequiredService<UserRepository>();
var serverRepo = Scope.ServiceProvider.GetRequiredService<ServerRepository>();
var user = usersRepo.Get().FirstOrDefault(x => x.DiscordId == component.User.Id);
var costomId = component.Data.CustomId.Split(".");
if (costomId.Length < 2) return;
int nextPage;
switch (costomId[0])
{
case "SmPreviousPage":
nextPage = int.Parse(costomId[1]) -1;
break;
case "SmNextPage":
nextPage = int.Parse(costomId[1]) +1;
break;
default:
return;
}
if (user == null)
{
embed = dcs.EmbedBuilderModule.StandardEmbed("Sorry ;( \n Please first create and/or link a Account to Discord! \n Press the Button to register/log in.", Color.Red, component.User);
components = new ComponentBuilder();
components.WithButton("Click Here", style: ButtonStyle.Link, url: ConfigService.GetSection("Moonlight").GetValue<String>("AppUrl"));
await component.RespondAsync(embed: embed.Build(), components: components.Build(), ephemeral: true);
return;
}
var servers = serverRepo.Get().Include(x => x.Owner).Where(x => x.Owner.Id == user.Id).ToList();
var selectOptions = new List<SelectMenuOptionBuilder>();
foreach (var server in servers.Skip(nextPage * 25).Take(25).ToList())
{
selectOptions.Add(new SelectMenuOptionBuilder()
.WithLabel($"{server.Id} - {server.Name}")
.WithEmote(Emote.Parse("<:server3:968614410228736070>"))
.WithValue(server.Id.ToString()));
}
int totalPages = (int)Math.Ceiling((double)servers.Count / 25 -1);
bool lastPage = nextPage == totalPages;
bool firstPage = nextPage == 0;
components = new ComponentBuilder();
components.WithSelectMenu(
"ServerSelectorList",
selectOptions,
"Select the server you want to edit.");
components.WithButton("Panel",
emote: Emote.Parse("<a:Earth:1092814004113657927>"),
style: ButtonStyle.Link,
url: $"{ConfigService.GetSection("Moonlight").GetValue<string>("AppUrl")}");
components.WithButton("Previous-page",
emote: Emote.Parse("<:ArrowLeft:1101547474180649030>"),
style: ButtonStyle.Secondary,
customId:$"SmPreviousPage.{nextPage}",
disabled: firstPage);
components.WithButton("Next-page",
emote: Emote.Parse("<:ArrowRight:1101547475380228257>"),
style: ButtonStyle.Secondary,
customId:$"SmNextPage.{nextPage}",
disabled: lastPage);
await component.DeferAsync();
await component.ModifyOriginalResponseAsync(x => x.Components = components.Build());
}
private async Task MenuHandler(SocketMessageComponent component)
{
if (component.Data.CustomId != "ServerSelectorList") return;
var dcs = Scope.ServiceProvider.GetRequiredService<DiscordBotService>();
if (!int.TryParse(component.Data.Values.FirstOrDefault(), out int serverId))
{
await ErrorEmbedSnippet(component);
return;
}
var serverRepo = Scope.ServiceProvider.GetRequiredService<ServerRepository>();
var server = serverRepo.Get()
.Include(x => x.Owner)
.Include(x => x.Node)
.Include(x => x.MainAllocation)
.FirstOrDefault(x => x.Id == serverId);
if (server == null || server.Owner.DiscordId != component.User.Id)
{
await ErrorEmbedSnippet(component);
return;
}
var embed = dcs.EmbedBuilderModule.ServerManagerEmbed("You're server", Color.Blue, component.User, server);
var components = new ComponentBuilder();
if (ConfigService.GetSection("Moonlight").GetSection("DiscordBot").GetValue<bool>("PowerActions"))
{
components.WithButton("Start", style: ButtonStyle.Success, customId: $"Sm.Start.{server.Id}", disabled: false);
components.WithButton("Restart", style: ButtonStyle.Primary, customId: $"Sm.Restart.{server.Id}", disabled: false);
components.WithButton("Stop", style: ButtonStyle.Danger, customId: $"Sm.Stop.{server.Id}", disabled: false);
components.WithButton("Kill", style: ButtonStyle.Danger, customId: $"Sm.Kill.{server.Id}", disabled: false);
}
components.WithButton("Way2Server",
emote: Emote.Parse("<a:Earth:1092814004113657927>"),
style: ButtonStyle.Link,
url: $"{ConfigService.GetSection("Moonlight").GetValue<string>("AppUrl")}/server/{server.Uuid}");
components.WithButton("Update",
emote: Emote.Parse("<:refresh:1101547898803605605>"),
style: ButtonStyle.Secondary,
customId: $"Sm.Update.{server.Id}");
if (ConfigService.GetSection("Moonlight").GetSection("DiscordBot").GetValue<bool>("SendCommands"))
{
components.WithButton("SendCommand",
emote: Emote.Parse("<:Console:1101547358157819944>"),
style: ButtonStyle.Secondary,
customId: $"Sm.SendCommand.{server.Id}");
}
await component.DeferAsync();
await component.ModifyOriginalResponseAsync(x => x.Embed = embed.Build());
await component.ModifyOriginalResponseAsync(x => x.Components = components.Build());
}
private async Task ErrorEmbedSnippet(SocketMessageComponent component)
{
var dcs = Scope.ServiceProvider.GetRequiredService<DiscordBotService>();
var embed = dcs.EmbedBuilderModule.StandardEmbed("Sorry :( \n Something went wrong. \n Please try again later.", Color.Red, component.User);
await component.RespondAsync(embed: embed.Build(), ephemeral: true);
}
}

View File

@@ -10,7 +10,7 @@
"profiles": { "profiles": {
"Moonlight": { "Moonlight": {
"commandName": "Project", "commandName": "Project",
"launchBrowser": true, "launchBrowser": false,
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
}, },
@@ -29,7 +29,7 @@
}, },
"Docker": { "Docker": {
"commandName": "Docker", "commandName": "Docker",
"launchBrowser": true, "launchBrowser": false,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"publishAllPorts": true, "publishAllPorts": true,
"useSSL": true "useSSL": true

View File

@@ -34,6 +34,7 @@
private Task VoidCommands() private Task VoidCommands()
{ {
DiscordBotService.RemoveCommandsModule.VoidCommands();
return Task.CompletedTask; return Task.CompletedTask;
} }
} }

View File

@@ -9,8 +9,9 @@
"Username": "user_name" "Username": "user_name"
}, },
"DiscordBot": { "DiscordBot": {
"Enable": "True", "Enable": false,
"Token": "Discord.Token.Here" "Token": "Discord.Token.Here",
"PowerActions": false
}, },
"Domains": { "Domains": {
"_comment": "Cloudflare Api Credentials", "_comment": "Cloudflare Api Credentials",