Skip to content

Commit

Permalink
fix(Moderation): requiring moderationlogs
Browse files Browse the repository at this point in the history
  • Loading branch information
JayXTQ committed Dec 9, 2024
1 parent 05710b2 commit cc920ea
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
10 changes: 6 additions & 4 deletions DiscordLab.Moderation/Commands/Ban.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Discord;
using Discord.WebSocket;
using DiscordLab.Bot.API.Interfaces;
using DiscordLab.Moderation.Handlers;
using Exiled.API.Features;
using RemoteAdmin;

namespace DiscordLab.Moderation.Commands
{
Expand Down Expand Up @@ -61,10 +61,12 @@ await command.RespondAsync(Translation.FailedExecuteCommand.Replace("{reason}",
else
{
await command.RespondAsync(Translation.BanCommandSuccess.Replace("{player}", user), ephemeral: true);
if (Plugin.Instance.CheckModerationLogsEnabled())
if (ModerationLogsHandler.Instance.IsEnabled)
{
ModerationLogs.Handlers.DiscordBot.Instance.SendBanMessage(null, user, reason,
$"<@{command.User.Id}>", null, duration);
ModerationLogsHandler.Instance.SendBanLogMethod.Invoke(
ModerationLogsHandler.Instance.HandlerInstance,
new object[] { null, user, reason, $"<@{command.User.Id}>", null, duration }
);
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions DiscordLab.Moderation/Commands/Unban.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Discord;
using Discord.WebSocket;
using DiscordLab.Bot.API.Interfaces;
using DiscordLab.Moderation.Handlers;
using Exiled.API.Features;
using RemoteAdmin;

namespace DiscordLab.Moderation.Commands
{
Expand Down Expand Up @@ -43,9 +43,12 @@ await command.RespondAsync(Translation.FailedExecuteCommand.Replace("{reason}",
else
{
await command.RespondAsync(Translation.UnbanCommandSuccess.Replace("{player}", user), ephemeral: true);
if (Plugin.Instance.CheckModerationLogsEnabled())
if (ModerationLogsHandler.Instance.IsEnabled)
{
ModerationLogs.Handlers.DiscordBot.Instance.SendUnbanMessage(user);
ModerationLogsHandler.Instance.SendUnbanLogMethod.Invoke(
ModerationLogsHandler.Instance.HandlerInstance,
new object[] { user }
);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion DiscordLab.Moderation/DiscordLab.Moderation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DiscordLab.Bot\DiscordLab.Bot.csproj" />
<ProjectReference Include="..\DiscordLab.ModerationLogs\DiscordLab.ModerationLogs.csproj" />
</ItemGroup>
</Project>
48 changes: 48 additions & 0 deletions DiscordLab.Moderation/Handlers/ModerationLogsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Reflection;
using DiscordLab.Bot.API.Interfaces;
using Exiled.API.Features;
using Exiled.API.Interfaces;
using Exiled.Loader;

namespace DiscordLab.Moderation.Handlers
{
public class ModerationLogsHandler : IRegisterable
{
public static ModerationLogsHandler Instance { get; private set; }

private Type HandlerType { get; set; }
public bool IsEnabled;
public object HandlerInstance { get; private set; }
public MethodInfo SendBanLogMethod { get; private set; }
public MethodInfo SendUnbanLogMethod { get; private set; }

public void Init()
{
Instance = this;
IPlugin<IConfig> moderationPlugin = Loader.Plugins.FirstOrDefault(p => p.Name == "DiscordLab.ModerationLogs");
if (moderationPlugin == null)
{
IsEnabled = false;
return;
}

IsEnabled = moderationPlugin.Config.IsEnabled;

Assembly assembly = moderationPlugin.Assembly;

HandlerType = assembly.GetType("DiscordLab.ModerationLogs.Handlers.DiscordBot");
HandlerInstance = HandlerType.GetProperty("Instance")!.GetValue(null);
Log.Info(HandlerInstance);
SendBanLogMethod = HandlerType.GetMethod("SendBanMessage")!;
SendUnbanLogMethod = HandlerType.GetMethod("SendUnbanMessage")!;
}

public void Unregister()
{
HandlerType = null;
HandlerInstance = null;
SendBanLogMethod = null;
SendUnbanLogMethod = null;
}
}
}
14 changes: 3 additions & 11 deletions DiscordLab.Moderation/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ public class Plugin : Plugin<Config, Translation>
public override string Name => "DiscordLab.Moderation";
public override string Author => "JayXTQ";
public override string Prefix => "DL.Moderation";
public override Version Version => new (1, 4, 0);
public override Version Version => new (1, 4, 1);
public override Version RequiredExiledVersion => new (8, 11, 0);
public override PluginPriority Priority => PluginPriority.Default;
public override PluginPriority Priority => PluginPriority.Low;

public static Plugin Instance { get; private set; }

private bool ModerationLogsEnabled { get; set; }
private object ModerationLogsHandler { get; set; }

private HandlerLoader _handlerLoader;

Expand All @@ -37,14 +38,5 @@ public override void OnDisabled()

base.OnDisabled();
}

public bool CheckModerationLogsEnabled()
{
if (!ModerationLogsEnabled)
{
ModerationLogsEnabled = Loader.Plugins.FirstOrDefault(p => p.Name == "DiscordLab.ModerationLogs" && p.Config.IsEnabled) != null;
}
return ModerationLogsEnabled;
}
}
}

0 comments on commit cc920ea

Please sign in to comment.