Implements Warning system
This commit is contained in:
parent
da6c73be42
commit
986e518aab
|
@ -34,6 +34,14 @@ namespace DeukBot4.Database
|
||||||
"PRIMARY KEY(key)" +
|
"PRIMARY KEY(key)" +
|
||||||
")";
|
")";
|
||||||
cmd.ExecuteNonQuery();
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using DeukBot4.Database;
|
||||||
using DeukBot4.Database.ServerSettings;
|
using DeukBot4.Database.ServerSettings;
|
||||||
using DeukBot4.MessageHandlers.CommandHandler.RequestStructure;
|
using DeukBot4.MessageHandlers.CommandHandler.RequestStructure;
|
||||||
using DeukBot4.MessageHandlers.Permissions;
|
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)]
|
[CommandParameters(ParameterMatcher.ParameterType.User)]
|
||||||
[BlockUsageInPm, RequireParameterMatch]
|
[BlockUsageInPm, RequireParameterMatch]
|
||||||
public async Task Bedtime(CommandRequest request)
|
public async Task GetUserWarns(CommandRequest request)
|
||||||
{
|
{
|
||||||
var guildChannel = (IGuildChannel)request.OriginalMessage.Channel;
|
// get the server channel object out of message. Return if it's somehow not a server channel
|
||||||
var user = await request.Parameters[0].AsDiscordGuildUser(guildChannel.Guild);
|
if (!(request.OriginalMessage.Channel is IGuildChannel channel))
|
||||||
var msg = await SilenceUser(user, request.OriginalMessage.Author, TimeSpan.FromHours(6));
|
return;
|
||||||
if (msg != null)
|
|
||||||
|
// 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)
|
private static async Task<string> SilenceUser(IGuildUser user, IUser author, TimeSpan span)
|
||||||
|
|
Loading…
Reference in New Issue