Skip to content
Open

V3 #31

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
<Reference Include="UnityEngine.PhysicsModule"/>
<Reference Include="NorthwoodLib"/>
<Reference Include="mscorlib"/>
<Reference Include="System.Net.Http"/>
<Reference Include="System.Text.Json"/>
<Reference Include="LabApi"/>
<Reference Include="YamlDotNet"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Northwood.LabAPI"/>
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" PrivateAssets="all" ExcludeAssets="runtime" ReferenceOutputAssembly="false" Private="true"/>
</ItemGroup>
</Project>
33 changes: 13 additions & 20 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
<Project>
<PropertyGroup>
<!-- Enable central package management, https://learn.microsoft.com/en-us/nuget/consume-packages/Central-Package-Management -->
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="Northwood.LabAPI" Version="1.1.3"/>
<PackageVersion Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.2"/>
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556"/>

<PackageVersion Include="Lib.Harmony" Version="2.2.2"/>

<PackageVersion Include="Discord.Net.WebSocket" Version="3.17.4"/>
<PackageVersion Include="Discord.Net.Rest" Version="3.17.4"/>
<PackageVersion Include="Discord.Net.Webhook" Version="3.17.4"/>
<PackageVersion Include="Lib.Harmony" Version="2.2.2"/>
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" IncludeAssets="all" PrivateAssets="all"/>
<PackageVersion Include="System.Net.Http" Version="4.3.4"/>
<PackageVersion Include="System.Text.Json" Version="9.0.6"/>
</ItemGroup>
<PropertyGroup>
<!-- Enable central package management, https://learn.microsoft.com/en-us/nuget/consume-packages/Central-Package-Management -->
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Discord.Net" Version="3.20.1" />
<PackageVersion Include="Northwood.LabAPI" Version="1.1.7" />
<PackageVersion Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.2" />
<PackageVersion Include="Lib.Harmony" Version="2.2.2" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" IncludeAssets="all" PrivateAssets="all" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Text.Json" Version="9.0.6" />
</ItemGroup>
</Project>
52 changes: 9 additions & 43 deletions DiscordLab.Administration/Commands/SendCommand.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,26 @@
using CommandSystem;
using Discord;
using Discord.WebSocket;
using DiscordLab.Bot.API.Extensions;
using DiscordLab.Bot.API.Features;
using LabApi.Features.Console;
using DiscordLab.Core.API.Commands;
using DiscordLab.Core.API.TranslationBuilders;
using LabApi.Features.Wrappers;
using RemoteAdmin;

namespace DiscordLab.Administration.Commands;

public class SendCommand : AutocompleteCommand
public class SendCommand : ICommand
{
public static Config Config => Plugin.Instance.Config;

public static Translation Translation => Plugin.Instance.Translation;

public override SlashCommandBuilder Data { get; } = new()
{
Name = Translation.SendCommandName,
Description = Translation.SendCommandDescription,
DefaultMemberPermissions = GuildPermission.ModerateMembers,
Options =
[
new()
{
Name = Translation.SendCommandOptionName,
Description = Translation.SendCommandOptionDescription,
Type = ApplicationCommandOptionType.String,
IsRequired = true,
IsAutocomplete = true
}
]
};

protected override ulong GuildId { get; } = Config.GuildId;

public override async Task Run(SocketSlashCommand command)
public CommandBuilder Data => Translation.SendCommand;

public async Task Execute(CommandInformation command)
{
await command.DeferAsync();
await command.DeferResponse();

string response = Server.RunCommand(command.Data.Options.GetOption<string>(Translation.SendCommandOptionName)!);
string response = Server.RunCommand(command.OptionsDictionary["command"]);

TranslationBuilder builder = new TranslationBuilder()
.AddCustomReplacer("response", response);

await Translation.SendCommandResponse.ModifyInteraction(command, builder);
}

public override async Task Autocomplete(SocketAutocompleteInteraction autocomplete)
{
IEnumerable<string> commands =
[
..CommandProcessor.GetAllCommands().Select(x => "/" + x.Command),
..QueryProcessor.DotCommandHandler.AllCommands.Select(x => "." + x.Command)
];
await autocomplete.RespondAsync(commands.Where(x => x.Contains((string)autocomplete.Data.Current.Value))
.Take(25).Select(x => new AutocompleteResult(x, x)));
await command.Reply(Translation.SendCommandResponse.Build(builder));
}
}
12 changes: 5 additions & 7 deletions DiscordLab.Administration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ namespace DiscordLab.Administration;

public class Config
{
public ulong GuildId { get; set; } = 0;

[Description("The channel to send server start logs")]
public ulong ServerStartChannelId { get; set; } = 0;
public string ServerStartChannel { get; set; } = "default";

[Description("Where server shutdown logs should be sent")]
public ulong ServerShutdownChannelId { get; set; } = 0;
public string ServerShutdownChannel { get; set; } = "default";

[Description("The channel to send error logs")]
public ulong ErrorLogChannelId { get; set; } = 0;
public string ErrorLogChannel { get; set; } = "default";

[Description("The channel to send remote admin logs")]
public ulong RemoteAdminChannelId { get; set; } = 0;
public string RemoteAdminChannel { get; set; } = "default";

[Description("The channel to send normal command logs")]
public ulong CommandLogChannelId { get; set; } = 0;
public string CommandLogChannel { get; set; } = "default";

[Description("Should a secondary translation be used for remote admin commands whose response is a failure?")]
public bool UseSecondaryTranslationRemoteAdmin { get; set; } = false;
Expand Down
4 changes: 2 additions & 2 deletions DiscordLab.Administration/DiscordLab.Administration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<LangVersion>14</LangVersion>
<PlatformTarget>x64</PlatformTarget>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>2.6.1</Version>
<Version>3.0.0</Version>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DiscordLab.Bot\DiscordLab.Bot.csproj"/>
<ProjectReference Include="..\DiscordLab.Core\DiscordLab.Core.csproj"/>
</ItemGroup>

<ItemGroup>
Expand Down
64 changes: 9 additions & 55 deletions DiscordLab.Administration/Events.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using Discord.WebSocket;
using DiscordLab.Bot;
using DiscordLab.Bot.API.Attributes;
using DiscordLab.Bot.API.Extensions;
using DiscordLab.Bot.API.Features;
using DiscordLab.Bot.API.Utilities;
using LabApi.Events;
using DiscordLab.Core;
using DiscordLab.Core.API.Attributes;
using DiscordLab.Core.API.Extensions;
using DiscordLab.Core.API.TranslationBuilders;
using LabApi.Events.Arguments.ServerEvents;
using LabApi.Events.CustomHandlers;
using LabApi.Events.Handlers;
using LabApi.Features.Console;
using LabApi.Features.Enums;
using LabApi.Features.Wrappers;

Expand Down Expand Up @@ -42,34 +38,13 @@ public static void Unload()

public static void OnServerQuit()
{
if (Config.ServerShutdownChannelId == 0)
return;

if (!Client.TryGetOrAddChannel(Config.ServerShutdownChannelId, out SocketTextChannel channel))
{
Logger.Error(LoggingUtils.GenerateMissingChannelMessage("server quit logs", Config.ServerShutdownChannelId, Config.GuildId));
return;
}

Translation.ServerShutdown.SendToChannel(channel, new());
Translation.ServerShutdown.Send(Config.ServerShutdownChannel, new());
}

public static void OnServerStart()
{
ServerEvents.WaitingForPlayers -= OnServerStart;
IsSubscribed = false;

if (Config.ServerStartChannelId == 0)
return;

if (!Client.TryGetOrAddChannel(Config.ServerStartChannelId, out SocketTextChannel channel))
{
Logger.Error(LoggingUtils.GenerateMissingChannelMessage("server start logs", Config.ServerStartChannelId,
Config.GuildId));
return;
}

Translation.ServerStart.SendToChannel(channel, new());
Translation.ServerStart.Send(Config.ServerStartChannel, new());
}

public override void OnServerCommandExecuted(CommandExecutedEventArgs ev)
Expand All @@ -80,8 +55,7 @@ public override void OnServerCommandExecuted(CommandExecutedEventArgs ev)
if (string.IsNullOrEmpty(ev.CommandName))
return;

SocketTextChannel channel;
TranslationBuilder builder = new TranslationBuilder("player", player)
TranslationBuilder builder = new PlayerTranslationBuilder("player", player)
.AddCustomReplacer("type", ev.CommandType.ToString())
.AddCustomReplacer("arguments", () => !ev.Arguments.Any() ? " " : string.Join(" ", ev.Arguments))
.AddCustomReplacer("command", ev.CommandName)
Expand All @@ -91,32 +65,12 @@ public override void OnServerCommandExecuted(CommandExecutedEventArgs ev)
MessageContent translation;
if (ev.CommandType == CommandType.RemoteAdmin)
{
if (Config.RemoteAdminChannelId == 0)
return;

if (!Client.TryGetOrAddChannel(Config.RemoteAdminChannelId, out channel))
{
Logger.Error(LoggingUtils.GenerateMissingChannelMessage("remote admin logs",
Config.RemoteAdminChannelId, Config.GuildId));
return;
}

translation = Config.UseSecondaryTranslationRemoteAdmin && !ev.ExecutedSuccessfully ? Translation.RemoteAdminCommandFailResponse : Translation.RemoteAdmin;
translation.SendToChannel(channel, builder);
translation.Send(Config.RemoteAdminChannel, builder);
return;
}

if (Config.CommandLogChannelId == 0)
return;

if (!Client.TryGetOrAddChannel(Config.CommandLogChannelId, out channel))
{
Logger.Error(LoggingUtils.GenerateMissingChannelMessage("command logs", Config.CommandLogChannelId,
Config.GuildId));
return;
}

translation = Config.UseSecondaryTranslationCommand && !ev.ExecutedSuccessfully ? Translation.CommandLogFailResponse : Translation.CommandLog;
translation.SendToChannel(channel, builder);
translation.Send(Config.CommandLogChannel, builder);
}
}
32 changes: 9 additions & 23 deletions DiscordLab.Administration/Patches/ErrorLog.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using Discord;
using Discord.WebSocket;
using DiscordLab.Bot;
using DiscordLab.Bot.API.Extensions;
using DiscordLab.Bot.API.Features;
using DiscordLab.Bot.API.Utilities;
using DiscordLab.Core;
using DiscordLab.Core.API.TranslationBuilders;
using HarmonyLib;
using LabApi.Features.Console;
using Attachment = DiscordLab.Core.API.Features.Attachment;

namespace DiscordLab.Administration.Patches;

Expand All @@ -14,35 +11,24 @@ public static class ErrorLog
{
public static void Postfix(object message)
{
if (Plugin.Instance.Config.ErrorLogChannelId == 0)
return;
TranslationBuilder builder = new();

if (!Client.TryGetOrAddChannel(Plugin.Instance.Config.ErrorLogChannelId, out SocketTextChannel channel))
{
Logger.Raw(
$"[ERROR] [{Plugin.Instance.Name}] {LoggingUtils.GenerateMissingChannelMessage("error logs", Plugin.Instance.Config.ErrorLogChannelId, Plugin.Instance.Config.GuildId)}",
ConsoleColor.Red);
return;
}

TranslationBuilder builder = new TranslationBuilder();

(Embed embed, string content) = Plugin.Instance.Translation.ErrorLog.Build(builder);
MessageInformation info = Plugin.Instance.Translation.ErrorLog.Build(builder);

MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
MemoryStream stream = new();
StreamWriter writer = new(stream);
writer.Write(message.ToString());
writer.Flush();
stream.Position = 0;

FileAttachment attachment = new(stream,
Attachment attachment = new(stream,
$"Error {DateTime.UtcNow.ToShortDateString()} {DateTime.UtcNow.ToLongTimeString()}.txt");

Task.Run(async () =>
{
try
{
await channel.SendFileAsync(attachment, content, embed: embed);
await MessageHandler.SendMessages(Plugin.Instance.Config.ErrorLogChannel, info with { Attachment = attachment });
}
catch (Exception ex)
{
Expand Down
7 changes: 4 additions & 3 deletions DiscordLab.Administration/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DiscordLab.Bot.API.Attributes;
using DiscordLab.Bot.API.Features;
using DiscordLab.Core.API.Attributes;
using DiscordLab.Core.API.Commands;
using DiscordLab.Core.API.Features;
using HarmonyLib;
using LabApi.Events.CustomHandlers;
using LabApi.Features;
Expand Down Expand Up @@ -30,7 +31,7 @@ public override void Enable()
CallOnLoadAttribute.Load();

if (Config.AddCommands)
SlashCommand.FindAll();
ICommand.FindAll();

CustomHandlersManager.RegisterEventsHandler(Events);
}
Expand Down
26 changes: 18 additions & 8 deletions DiscordLab.Administration/Translation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DiscordLab.Bot.API.Features;
using DiscordLab.Core;
using DiscordLab.Core.API.Commands;

namespace DiscordLab.Administration;

Expand All @@ -20,13 +21,22 @@ public class Translation

public MessageContent CommandLogFailResponse { get; set; } = "Player {player} has attempted to run a command which failed: `{command}`";

public string SendCommandName { get; set; } = "send";

public string SendCommandDescription { get; set; } = "Sends a command to the server";

public string SendCommandOptionName { get; set; } = "command";

public string SendCommandOptionDescription { get; set; } = "The command to send";
public CommandBuilder SendCommand = new()
{
Name = "send",
Description = "Sends a command to the server",
DefaultPermission = DefaultCommandPermissions.Admins,
Options =
[
new()
{
Name = "command",
Description = "The command to send",
IsRequired = true,
Type = CommandOptionType.String
}
]
};

public MessageContent SendCommandResponse { get; set; } = "The command has been sent, it returned: {response}";
}
Loading
Loading