Skip to content

Commit

Permalink
Added admin logging to log important commands that are issued to the …
Browse files Browse the repository at this point in the history
…database
  • Loading branch information
MythicalCuddles committed Oct 18, 2019
1 parent d177bc5 commit a275742
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 8 deletions.
5 changes: 4 additions & 1 deletion DiscordBot/DiscordBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
<Compile Include="Handlers\GuildHandler.cs" />
<Compile Include="Handlers\ReactionHandler.cs" />
<Compile Include="Handlers\UserHandler.cs" />
<Compile Include="Logging\AdminLog.cs" />
<Compile Include="Modules\Admin\AdminModule.cs" />
<Compile Include="Modules\Admin\ForceModule.cs" />
<Compile Include="Modules\Admin\SendModule.cs" />
Expand All @@ -270,7 +271,6 @@
<Compile Include="Objects\Quote.cs" />
<Compile Include="Objects\RequestQuote.cs" />
<Compile Include="Objects\User.cs" />
<Compile Include="Other\VoteLinkHandler.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
Expand Down Expand Up @@ -308,6 +308,9 @@
<DependentUpon>frmConfigure.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Folder Include="Other" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\NETStandard.Library.2.0.1\build\NETStandard.Library.targets" Condition="Exists('..\packages\NETStandard.Library.2.0.1\build\NETStandard.Library.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
1 change: 0 additions & 1 deletion DiscordBot/Handlers/ReactionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using DiscordBot.Common;
using DiscordBot.Extensions;
using DiscordBot.Objects;
using DiscordBot.Other;

namespace DiscordBot.Handlers
{
Expand Down
31 changes: 31 additions & 0 deletions DiscordBot/Logging/AdminLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using DiscordBot.Database;
using MySql.Data.MySqlClient;

namespace DiscordBot.Logging
{
public class AdminLog
{
internal static bool Log(ulong executedBy, string action, ulong executedIn, ulong? userMention = null)
{
List<(string, string)> queryParams = new List<(string id, string value)>()
{
("@executedBy", executedBy.ToString()),
("@action", action),
("@executedIn", executedIn.ToString())
};

queryParams.Add(userMention != null
? ("@mentionedUser", userMention.ToString())
: ("@mentionedUser", null));

int rowsUpdated = DatabaseActivity.ExecuteNonQueryCommand(
"INSERT IGNORE INTO " +
"admin_log(logId,executedBy,action,executedAt,executedIn,userMentioned) " +
"VALUES (NULL, @executedBy, @action, CURRENT_TIMESTAMP, @executedIn, @mentionedUser);", queryParams);

return rowsUpdated == 1;
}
}
}
15 changes: 15 additions & 0 deletions DiscordBot/Modules/Admin/AdminModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using DiscordBot.Common.Preconditions;
using DiscordBot.Common;
using DiscordBot.Extensions;
using DiscordBot.Logging;
using DiscordBot.Objects;

namespace DiscordBot.Modules.Admin
Expand All @@ -24,13 +25,16 @@ public class AdminModule : InteractiveBase
public async Task Say([Remainder, Summary("The text to echo")] string echo)
{
await ReplyAsync(echo);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);
await Context.Message.DeleteAsync();
}

[Command("welcome"), Summary("Send the welcome messages to the user specified.")]
public async Task SendWelcomeMessage(SocketGuildUser user)
{
await Guild.Load(Context.Guild.Id).WelcomeChannelID.GetTextChannel().SendMessageAsync(Guild.Load(Context.Guild.Id).WelcomeMessage.ModifyStringFlags(user));
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

await Guild.Load(Context.Guild.Id).LogChannelID.GetTextChannel().SendMessageAsync("A welcome message for " + user.Mention + " has been posted. (Forced by: " + Context.User.Mention + ")");
}

Expand All @@ -52,6 +56,7 @@ public async Task AddQuote([Remainder]string quote = null)
}

Quote.AddQuote(quote, Context.User.Id, Context.Guild.Id);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);

eb = new EmbedBuilder()
.WithDescription(Context.User.Mention + " Quote Added")
Expand All @@ -77,6 +82,7 @@ public async Task ListQuotes()
return;
}

AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);
//todo: print quotes.
}

Expand Down Expand Up @@ -113,6 +119,7 @@ public async Task EditQuote(int quoteId = 0, [Remainder]string quote = null)
string oldQuote = Quote.Quotes.Find(q => q.QId == quoteId).QuoteText;

Quote.UpdateQuote(quoteId, quote);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);

eb = new EmbedBuilder
{
Expand Down Expand Up @@ -160,6 +167,8 @@ public async Task RemoveQuote(int quoteId = 0)
}

Quote.DeleteQuote(quoteId);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);

eb = new EmbedBuilder
{
Title = "Quote #" + quoteId + " deleted",
Expand Down Expand Up @@ -189,6 +198,8 @@ public async Task ListRequestQuotes()
return;
}

AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);

//todo: print quotes.
}

Expand Down Expand Up @@ -222,7 +233,9 @@ public async Task AcceptQuote(int quoteId = 0)
await ReplyAsync("", false, eb.Build());
return;
}

RequestQuote.ApproveRequestQuote(quoteId, Context.User.Id, Context.Guild.Id);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);

eb = new EmbedBuilder
{
Expand Down Expand Up @@ -275,7 +288,9 @@ public async Task DenyQuote(int quoteId)
await ReplyAsync("", false, eb.Build());
return;
}

RequestQuote.RemoveRequestQuote(quoteId);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);

eb = new EmbedBuilder
{
Expand Down
26 changes: 26 additions & 0 deletions DiscordBot/Modules/Admin/ForceModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using DiscordBot.Common;
using DiscordBot.Database;
using DiscordBot.Extensions;
using DiscordBot.Logging;
using DiscordBot.Objects;

namespace DiscordBot.Modules.Admin
Expand Down Expand Up @@ -38,6 +39,7 @@ await ReplyAsync("**Syntax:** " +
"[11] force websitename [mention/id] [value]\n" +
"[12] force websiteurl [mention/id] [value]\n" +
"```");
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);
}

[Command("about"), Summary("Force set the about message for the specified user.")]
Expand All @@ -49,7 +51,9 @@ public async Task ForceAbout(IUser user, [Remainder]string about)
{
("@about", about)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET about=@about WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s about text successfully.")
Expand All @@ -72,7 +76,9 @@ public async Task ForceName(IUser user, [Remainder]string name)
{
("@name", name)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET name=@name WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s name text successfully.")
Expand All @@ -95,7 +101,9 @@ public async Task ForceGender(IUser user, [Remainder]string gender)
{
("@gender", gender)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET gender=@gender WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s gender text successfully.")
Expand All @@ -118,7 +126,9 @@ public async Task ForcePronouns(IUser user, [Remainder]string pronouns)
{
("@pronouns", pronouns)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET pronouns=@pronouns WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s pronoun text successfully.")
Expand All @@ -141,7 +151,9 @@ public async Task ForceMinecraftUsername(IUser user, [Remainder]string username)
{
("@minecraftUsername", username)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET minecraftUsername=@minecraftUsername WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s Minecraft username text successfully.")
Expand All @@ -164,7 +176,9 @@ public async Task ForceInstagramUsername(IUser user, [Remainder]string username)
{
("@instagramUsername", username)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET instagramUsername=@instagramUsername WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s Instagram username text successfully.")
Expand All @@ -187,7 +201,9 @@ public async Task ForcePokemonGoFriendCode(IUser user, [Remainder]string code)
{
("@pokemonGoFriendCode", code)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET pokemonGoFriendCode=@pokemonGoFriendCode WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s Pokemon Go Friend Code successfully.")
Expand All @@ -210,7 +226,9 @@ public async Task ForceSnapchatUsername(IUser user, [Remainder]string username)
{
("@snapchatUsername", username)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET snapchatUsername=@snapchatUsername WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s Snapchat username text successfully.")
Expand All @@ -233,7 +251,9 @@ public async Task ForceGithubUsername(IUser user, [Remainder]string username)
{
("@githubUsername", username)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET githubUsername=@githubUsername WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s GitHub username text successfully.")
Expand All @@ -256,7 +276,9 @@ public async Task ForcePrefix(IUser user, [Remainder]string prefix)
{
("@customPrefix", prefix)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET customPrefix=@customPrefix WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s custom prefix successfully.")
Expand All @@ -279,7 +301,9 @@ public async Task ForceWebsiteUrl(IUser user, [Remainder]string url)
{
("@websiteUrl", url)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET websiteURL=@websiteUrl WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s website URL successfully.")
Expand All @@ -302,7 +326,9 @@ public async Task ForceWebsiteName(IUser user, [Remainder]string name)
{
("@websiteName", name)
};

DatabaseActivity.ExecuteNonQueryCommand("UPDATE users SET websiteName=@websiteName WHERE id='" + user.Id + "';", queryParams);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id, user.Id);

var eb = new EmbedBuilder()
.WithDescription(Context.User.Username + " changed " + user.Mention + "'s website name successfully.")
Expand Down
4 changes: 4 additions & 0 deletions DiscordBot/Modules/Admin/SendModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using DiscordBot.Common.Preconditions;
using DiscordBot.Common;
using DiscordBot.Logging;
using DiscordBot.Objects;

namespace DiscordBot.Modules.Admin
Expand Down Expand Up @@ -34,6 +35,7 @@ await ReplyAsync("**Syntax:** " +
"[2c] send directmessage [mention/id] [message]\n" +
"[2d] send dm [mention/id] [message]\n" +
"```");
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);
}

[Command("channelmessage"), Summary("Sends a message to the channel specified.")]
Expand All @@ -47,6 +49,7 @@ public async Task SendChannelMessage([Summary("The channel id to send the messag
}

await channel.SendMessageAsync(message);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);

EmbedAuthorBuilder eab = new EmbedAuthorBuilder()
.WithName("Log Message");
Expand Down Expand Up @@ -75,6 +78,7 @@ public async Task SendPrivateMessage([Summary("The user to send the message to."
}

await user.GetOrCreateDMChannelAsync().Result.SendMessageAsync(message);
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);

EmbedAuthorBuilder eab = new EmbedAuthorBuilder()
.WithName("to: @" + user.Username);
Expand Down
2 changes: 2 additions & 0 deletions DiscordBot/Modules/Mod/EmbedModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using DiscordBot.Common;
using DiscordBot.Extensions;
using DiscordBot.Handlers;
using DiscordBot.Logging;
using DiscordBot.Objects;

namespace DiscordBot.Modules.Mod
Expand All @@ -36,6 +37,7 @@ await ReplyAsync("**Syntax:** " +
"[5] embed withcolor [\"R Value\"] [\"G Value\"] [\"B Value\"]\n" +
"[6] embed send [\"#channel = #" + Context.Channel.Name + "\"]\n" +
"```");
AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);
}

[Command("new")]
Expand Down
2 changes: 2 additions & 0 deletions DiscordBot/Modules/Mod/ModeratorModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using DiscordBot.Common.Preconditions;
using DiscordBot.Common;
using DiscordBot.Extensions;
using DiscordBot.Logging;

namespace DiscordBot.Modules.Mod
{
Expand Down Expand Up @@ -88,6 +89,7 @@ public async Task ShowStatistics()
.WithThumbnailUrl(DiscordBot.Bot.CurrentUser.GetAvatarUrl())
.WithColor(new Color(255, 116, 140));

AdminLog.Log(Context.User.Id, Context.Message.Content, Context.Guild.Id);
await ReplyAsync("", false, eb.Build());
}

Expand Down
Loading

0 comments on commit a275742

Please sign in to comment.