RegexBot/Services/CommonFunctions/CF_ModLogs.Hooks.cs
Noi 4f896e8311 Implement moderation logging to database
Further commits will implement a system to propagate these logs,
allowing modules and services to act on them regardless of their origin.
Additionally, further commits shall implement these changes within
built-in modules to allow for their immediate use.
2022-08-16 12:37:06 -07:00

80 lines
No EOL
3.5 KiB
C#

#pragma warning disable CA1822 // "Mark members as static" - will not make static to encourage better structure
using Discord.Net;
using RegexBot.Common;
using RegexBot.Data;
namespace RegexBot;
partial class RegexbotClient {
/// <summary>
/// Appends a note to the moderation log regarding the given user, containing the given message.
/// </summary>
/// <remarks>
/// Unlike warnings, notes are private and intended for moderators only. Users are never notified and may
/// never be aware of notes associated with them. Otherwise, they function as any other entry in the log.
/// </remarks>
/// <param name="guild">The guild which the target user is associated.</param>
/// <param name="targetUser">The snowflake ID of the target user.</param>
/// <param name="source">
/// The the entity which issued this log item.
/// If it was a user, this value preferably is in the <seealso cref="EntityName"/> format.
/// </param>
/// <param name="message">The message to add to this entry.</param>
/// <returns>
/// The resulting <see cref="ModLogEntry"/> from the creation of this note.
/// </returns>
public ModLogEntry AddUserNote(SocketGuild guild, ulong targetUser, string source, string? message) {
var entry = new ModLogEntry() {
GuildId = (long)guild.Id,
UserId = (long)targetUser,
LogType = ModLogType.Note,
IssuedBy = source,
Message = message
};
using (var db = new BotDatabaseContext()) {
db.Add(entry);
db.SaveChanges();
}
// TODO notify
return entry;
}
/// <summary>
/// Warns a user, adding an entry to the moderation log and also attempting to notify the user.
/// </summary>
/// <param name="guild">The guild which the target user is associated.</param>
/// <param name="targetUser">The snowflake ID of the target user.</param>
/// <param name="source">
/// The the entity which issued this log item.
/// If it was a user, this value preferably is in the <seealso cref="EntityName"/> format.
/// </param>
/// <param name="message">The message to add to this entry.</param>
/// <returns>
/// A tuple containing the resulting <see cref="ModLogEntry"/> and <see cref="LogAppendResult"/>.
/// </returns>
public async Task<(ModLogEntry, LogAppendResult)> AddUserWarnAsync(SocketGuild guild, ulong targetUser, string source, string? message) {
var entry = new ModLogEntry() {
GuildId = (long)guild.Id,
UserId = (long)targetUser,
LogType = ModLogType.Warn,
IssuedBy = source,
Message = message
};
using (var db = new BotDatabaseContext()) {
db.Add(entry);
await db.SaveChangesAsync();
}
// TODO notify
// Attempt warning message
var userSearch = _svcEntityCache.QueryUserCache(targetUser.ToString());
var userDisp = userSearch != null
? $" user **{userSearch.Username}#{userSearch.Discriminator}**"
: $" user with ID **{targetUser}**";
var targetGuildUser = guild.GetUser(targetUser);
if (targetGuildUser == null) return (entry, new LogAppendResult(
new HttpException(System.Net.HttpStatusCode.NotFound, null), entry.LogId, userDisp));
var sendStatus = await _svcCommonFunctions.SendUserWarningAsync(targetGuildUser, message);
return (entry, new LogAppendResult(sendStatus, entry.LogId, userDisp));
}
}