Implements Warning system

This commit is contained in:
Deukhoofd 2020-04-08 21:22:48 +02:00
parent da6c73be42
commit 986e518aab
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 130 additions and 7 deletions

View File

@ -34,6 +34,14 @@ namespace DeukBot4.Database
"PRIMARY KEY(key)" +
")";
cmd.ExecuteNonQuery();
cmd.CommandText = "CREATE TABLE IF NOT EXISTS warnings (" +
"id serial NOT NULL," +
"serverId bigint NOT NULL," +
"userId bigint NOT NULL," +
"message varchar(255)," +
"PRIMARY KEY(id)" +
")";
cmd.ExecuteNonQuery();
}
}
}

View File

@ -0,0 +1,59 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using DeukBot4.Utilities;
using Npgsql;
namespace DeukBot4.Database
{
public static class WarnHandler
{
public static async Task AddWarning(ulong serverId, ulong userId, string message)
{
using var conn = new DatabaseConnection();
await using var cmd = new NpgsqlCommand
{
Connection = conn,
CommandText = "INSERT INTO warnings (serverId, userId, message)" +
"VALUES (@server_id, @user_id, @message);"
};
cmd.Parameters.AddWithValue("server_id", serverId.ToLong());
cmd.Parameters.AddWithValue("user_id", userId.ToLong());
cmd.Parameters.AddWithValue("message", message);
await cmd.ExecuteNonQueryAsync();
}
public class Warning
{
public Warning(int id, string message)
{
Id = id;
Message = message;
}
public int Id { get; }
public string Message { get; }
}
public static async Task<List<Warning>> GetWarnings(ulong serverId, ulong userId)
{
using var conn = new DatabaseConnection();
await using var cmd = new NpgsqlCommand
{
Connection = conn,
CommandText = "SELECT id, message from warnings " +
"where serverId = @server_id and userId = @user_id;"
};
cmd.Parameters.AddWithValue("server_id", serverId.ToLong());
cmd.Parameters.AddWithValue("user_id", userId.ToLong());
var reader = cmd.ExecuteReader();
var ls = new List<Warning>();
while (await reader.ReadAsync())
{
var id = reader.GetInt32(0);
var message = reader.GetString(1);
ls.Add(new Warning(id, message));
}
return ls;
}
}
}

View File

@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DeukBot4.Database;
using DeukBot4.Database.ServerSettings;
using DeukBot4.MessageHandlers.CommandHandler.RequestStructure;
using DeukBot4.MessageHandlers.Permissions;
@ -209,18 +211,72 @@ namespace DeukBot4.MessageHandlers.CommandHandler
}
}
[Command("bedtime", PermissionLevel.Helper)]
[Command("warn", PermissionLevel.Helper)]
[CommandParameters(ParameterMatcher.ParameterType.User, ParameterMatcher.ParameterType.Remainder)]
[BlockUsageInPm, RequireParameterMatch]
public async Task WarnUser(CommandRequest request)
{
// get the server channel object out of message. Return if it's somehow not a server channel
if (!(request.OriginalMessage.Channel is IGuildChannel channel))
return;
// get the id of the user, this parses the string to an id
var user = await request.Parameters[0].AsDiscordGuildUser(channel.Guild);
if (user == null)
{
await request.SendSimpleEmbed("Warn", "I can't find that user on the server");
return;
}
var message = request.Parameters[1].AsString();
if (string.IsNullOrWhiteSpace(message))
{
await request.SendSimpleEmbed("Warn", "A warning can't be empty!");
return;
}
await WarnHandler.AddWarning(channel.GuildId, user.Id, message);
var warnEmbed = EmbedFactory.GetStandardEmbedBuilder();
warnEmbed.Title = $"Warning from the {channel.Guild.Name} server!";
warnEmbed.Description = message;
await user.SendMessageAsync(embed: warnEmbed.Build());
request.SendSimpleEmbed("Warn", "User was warned!");
}
[Command("warns", PermissionLevel.Helper)]
[CommandParameters(ParameterMatcher.ParameterType.User)]
[BlockUsageInPm, RequireParameterMatch]
public async Task Bedtime(CommandRequest request)
public async Task GetUserWarns(CommandRequest request)
{
var guildChannel = (IGuildChannel)request.OriginalMessage.Channel;
var user = await request.Parameters[0].AsDiscordGuildUser(guildChannel.Guild);
var msg = await SilenceUser(user, request.OriginalMessage.Author, TimeSpan.FromHours(6));
if (msg != null)
// get the server channel object out of message. Return if it's somehow not a server channel
if (!(request.OriginalMessage.Channel is IGuildChannel channel))
return;
// get the id of the user, this parses the string to an id
var user = await request.Parameters[0].AsDiscordGuildUser(channel.Guild);
if (user == null)
{
request.SendSimpleEmbed("Silence", msg);
await request.SendSimpleEmbed("Warn", "I can't find that user on the server");
return;
}
var warnings = await WarnHandler.GetWarnings(channel.GuildId, user.Id);
var eb = EmbedFactory.GetStandardEmbedBuilder();
eb.Title = $"Warnings for User";
eb.Author = new EmbedAuthorBuilder()
{
Name = user.Username,
IconUrl = user.GetAvatarUrl()
};
var desc = new StringBuilder();
foreach (var warn in warnings)
{
desc.Append($"**{warn.Id} -** {warn.Message}\r\n");
}
eb.Description = desc.ToString();
request.OriginalMessage.Channel.SendMessageAsync("", embed: eb.Build());
}
private static async Task<string> SilenceUser(IGuildUser user, IUser author, TimeSpan span)