Skip to content

Commit

Permalink
readd commandlogchannel
Browse files Browse the repository at this point in the history
  • Loading branch information
SylveonDeko committed Aug 17, 2024
1 parent d01dba9 commit 6e22fab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
38 changes: 23 additions & 15 deletions src/Mewdeko/Modules/Administration/LogCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public partial class Administration
/// Module for logging commands.
/// </summary>
[Group]
public class LogCommands : MewdekoSubmodule<NewLogCommandService>
public class LogCommands(GuildSettingsService gss) : MewdekoSubmodule<NewLogCommandService>
{
/// <summary>
/// Sets the logging category for a specified type of logs.
Expand Down Expand Up @@ -175,19 +175,27 @@ public async Task Log(LogType type, ITextChannel? channel = null)
}


// [Cmd, Aliases, RequireContext(ContextType.Guild), UserPerm(GuildPermission.Administrator)]
// public async Task CommandLogChannel(ITextChannel? channel = null)
// {
// if (channel is null)
// {
// await ConfirmLocalizedAsync("command_logging_disabled");
// await Service.UpdateCommandLogChannel(ctx.Guild, 0);
// }
// else
// {
// await ConfirmLocalizedAsync("command_logging_enabled");
// await Service.UpdateCommandLogChannel(ctx.Guild, channel.Id);
// }
// }
/// <summary>
/// Sets a channel to log commands to
/// </summary>
/// <param name="channel">The channel to log commands to</param>
[Cmd, Aliases, RequireContext(ContextType.Guild), UserPerm(GuildPermission.Administrator)]
public async Task CommandLogChannel(ITextChannel? channel = null)
{
if (channel is null)
{
await ConfirmLocalizedAsync("command_logging_disabled");
var gc = await gss.GetGuildConfig(ctx.Guild.Id);
gc.CommandLogChannel = 0;
await gss.UpdateGuildConfig(ctx.Guild.Id, gc);
}
else
{
await ConfirmLocalizedAsync("command_logging_enabled");
var gc = await gss.GetGuildConfig(ctx.Guild.Id);
gc.CommandLogChannel = channel.Id;
await gss.UpdateGuildConfig(ctx.Guild.Id, gc);
}
}
}
}
36 changes: 22 additions & 14 deletions src/Mewdeko/Services/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ private async Task TryRunCommand(IGuild? guild, IChannel channel, IUserMessage u

private async Task LogCommandExecution(IMessage usrMsg, ITextChannel channel, CommandInfo? commandInfo, bool success, int executionTime, string errorMessage = null)
{
var gc = await gss.GetGuildConfig(channel.GuildId);
var logBuilder = new StringBuilder()
.AppendLine(success ? "Command Executed" : "Command Errored")
.AppendLine($"User: {usrMsg.Author} [{usrMsg.Author.Id}]")
Expand All @@ -682,26 +683,33 @@ private async Task LogCommandExecution(IMessage usrMsg, ITextChannel channel, Co
else
Log.Warning(logBuilder.ToString());

if (bss.Data.CommandLogChannel > 0)
{
var embed = new EmbedBuilder()
.WithColor(success ? Mewdeko.OkColor : Mewdeko.ErrorColor)
.WithTitle(success ? "Command Executed" : "Command Errored")
.AddField("User", $"{usrMsg.Author.Mention} {usrMsg.Author} {usrMsg.Author.Id}")
.AddField("Guild", channel == null ? "PRIVATE" : $"{channel.Guild.Name} `{channel.Guild.Id}`")
.AddField("Channel", channel == null ? "PRIVATE" : $"{channel.Name} `{channel.Id}`")
.AddField("Message", usrMsg.Content.TrimTo(1000))
.AddField("Execution Time", $"{executionTime}ms");

if (!success && !string.IsNullOrEmpty(errorMessage))
embed.AddField("Error", errorMessage);
var embed = new EmbedBuilder()
.WithColor(success ? Mewdeko.OkColor : Mewdeko.ErrorColor)
.WithTitle(success ? "Command Executed" : "Command Errored")
.AddField("User", $"{usrMsg.Author.Mention} {usrMsg.Author} {usrMsg.Author.Id}")
.AddField("Guild", channel == null ? "PRIVATE" : $"{channel.Guild.Name} `{channel.Guild.Id}`")
.AddField("Channel", channel == null ? "PRIVATE" : $"{channel.Name} `{channel.Id}`")
.AddField("Message", usrMsg.Content.TrimTo(1000))
.AddField("Execution Time", $"{executionTime}ms");

if (commandInfo != null)
embed.AddField("Command", $"{commandInfo.Module.Name} | {commandInfo.Name}");
if (!success && !string.IsNullOrEmpty(errorMessage))
embed.AddField("Error", errorMessage);

if (commandInfo != null)
embed.AddField("Command", $"{commandInfo.Module.Name} | {commandInfo.Name}");

if (bss.Data.CommandLogChannel > 0)
{
if (await client.Rest.GetChannelAsync(bss.Data.CommandLogChannel) is ITextChannel logChannel)
await logChannel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
}

if (gc.CommandLogChannel != 0)
{
if (await client.Rest.GetChannelAsync(gc.CommandLogChannel) is ITextChannel commandLogChannel)
await commandLogChannel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
}
}

private int GetPrefixLength(string content, string prefix)
Expand Down

0 comments on commit 6e22fab

Please sign in to comment.