Handling for per server toggleable jokes

This commit is contained in:
Deukhoofd 2018-10-09 17:18:19 +02:00
parent 2fe04c2d1e
commit 771a825d9f
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
7 changed files with 136 additions and 18 deletions

View File

@ -21,7 +21,8 @@ namespace DeukBot4.Database
cmd.CommandText = "CREATE TABLE IF NOT EXISTS server_settings (" + cmd.CommandText = "CREATE TABLE IF NOT EXISTS server_settings (" +
"server_id bigint NOT NULL," + "server_id bigint NOT NULL," +
"muted_role bigint NOT NULL," + "muted_role bigint NOT NULL," +
"enabled_jokes varchar(255)," +
"PRIMARY KEY(server_id)" + "PRIMARY KEY(server_id)" +
")"; ")";
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();

View File

@ -1,4 +1,5 @@
using System.Threading.Tasks; using System.Collections.Generic;
using System.Threading.Tasks;
using DeukBot4.Utilities; using DeukBot4.Utilities;
using Npgsql; using Npgsql;
@ -6,14 +7,16 @@ namespace DeukBot4.Database.ServerSettings
{ {
public class ServerSetting public class ServerSetting
{ {
public ServerSetting(ulong serverId, ulong mutedRoleId = 0) public ServerSetting(ulong serverId, ulong mutedRoleId = 0, List<string> enabledJokes = null)
{ {
ServerId = serverId; ServerId = serverId;
MutedRoleId = mutedRoleId; MutedRoleId = mutedRoleId;
EnabledJokes = enabledJokes ?? new List<string>();
} }
public ulong ServerId { get; } public ulong ServerId { get; }
public ulong MutedRoleId { get; private set; } public ulong MutedRoleId { get; private set; }
public List<string> EnabledJokes { get; private set; }
public async Task SetMutedRoleId(ulong id) public async Task SetMutedRoleId(ulong id)
{ {
@ -30,5 +33,21 @@ namespace DeukBot4.Database.ServerSettings
} }
} }
} }
public async Task SetEnabledJokes(List<string> enabledJokes)
{
EnabledJokes = enabledJokes;
using (var conn = new DatabaseConnection())
{
using (var cmd = new NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "UPDATE server_settings SET enabled_jokes = @val WHERE server_id = @key";
cmd.Parameters.AddWithValue("val", string.Join(",", enabledJokes));
cmd.Parameters.AddWithValue("key", ServerId.ToLong());
await cmd.ExecuteNonQueryAsync();
}
}
}
} }
} }

View File

@ -1,7 +1,6 @@
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Linq;
using DeukBot4.Utilities; using DeukBot4.Utilities;
using Discord;
using Npgsql; using Npgsql;
namespace DeukBot4.Database.ServerSettings namespace DeukBot4.Database.ServerSettings
@ -17,13 +16,19 @@ namespace DeukBot4.Database.ServerSettings
using (var cmd = new NpgsqlCommand()) using (var cmd = new NpgsqlCommand())
{ {
cmd.Connection = conn; cmd.Connection = conn;
cmd.CommandText = "SELECT server_id, muted_role FROM server_settings"; cmd.CommandText = "SELECT server_id, muted_role, enabled_jokes FROM server_settings";
var reader = cmd.ExecuteReader(); var reader = cmd.ExecuteReader();
while (reader.Read()) while (reader.Read())
{ {
var id = reader.GetInt64(0).ToUlong(); var id = reader.GetInt64(0).ToUlong();
var mutedRole = reader.GetInt64(1).ToUlong(); var mutedRole = reader.GetInt64(1).ToUlong();
Settings.Add(id, new ServerSetting(id, mutedRole)); string[] enabledJokes = null;
if (!reader.IsDBNull(2))
{
enabledJokes = reader.GetString(2).Split(',');
}
Settings.Add(id, new ServerSetting(id, mutedRole, enabledJokes?.ToList()));
} }
} }
} }
@ -36,9 +41,11 @@ namespace DeukBot4.Database.ServerSettings
using (var cmd = new NpgsqlCommand()) using (var cmd = new NpgsqlCommand())
{ {
cmd.Connection = conn; cmd.Connection = conn;
cmd.CommandText = "INSERT INTO server_settings (server_id, muted_role) VALUES (@id, @muted)"; cmd.CommandText =
"INSERT INTO server_settings (server_id, muted_role, enabled_jokes) VALUES (@id, @muted, @jokes)";
cmd.Parameters.AddWithValue("id", setting.ServerId.ToLong()); cmd.Parameters.AddWithValue("id", setting.ServerId.ToLong());
cmd.Parameters.AddWithValue("muted", setting.MutedRoleId.ToLong()); cmd.Parameters.AddWithValue("muted", setting.MutedRoleId.ToLong());
cmd.Parameters.AddWithValue("jokes", string.Join(",", setting.EnabledJokes));
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
} }

View File

@ -0,0 +1,83 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using DeukBot4.Database.ServerSettings;
using DeukBot4.MessageHandlers.CommandHandler.RequestStructure;
using DeukBot4.MessageHandlers.JokeHandling;
using DeukBot4.MessageHandlers.Permissions;
using Discord;
namespace DeukBot4.MessageHandlers.CommandHandler
{
public class JokeControlCommands : CommandContainerBase
{
public override string Name => "Joke Control";
[Command("togglejoke", PermissionLevel.Admin)]
[CommandParameters(ParameterMatcher.ParameterType.Remainder)]
[CommandHelp("Toggle a joke on or off", "Toggle a joke, defined by id, on or off")]
[BlockUsageInPm, RequireParameterMatch]
public async Task ToggleJoke(CommandRequest request)
{
var jokeName = request.Parameters[0].AsString();
if (!JokeHandler.Jokes.ContainsKey(jokeName))
{
request.SendSimpleEmbed("Joke Toggle", $"Not a valid joke id: {jokeName}");
return;
};
if (request.OriginalMessage.Channel is IGuildChannel guildChannel)
{
var serverSettings = ServerSettingHandler.GetSettings(guildChannel.GuildId);
if (serverSettings.EnabledJokes.Contains(jokeName))
{
serverSettings.EnabledJokes.Remove(jokeName);
serverSettings.SetEnabledJokes(serverSettings.EnabledJokes);
request.SendSimpleEmbed("Joke Toggle", $"Disabled Joke with id {jokeName}");
}
else
{
serverSettings.EnabledJokes.Add(jokeName);
serverSettings.SetEnabledJokes(serverSettings.EnabledJokes);
request.SendSimpleEmbed("Joke Toggle", $"Enabled Joke with id {jokeName}");
}
}
}
[Command("jokes", PermissionLevel.Admin)]
[CommandHelp("Lists jokes per server", "Lists jokes per server")]
[BlockUsageInPm]
public async Task ListJokes(CommandRequest request)
{
if (request.OriginalMessage.Channel is IGuildChannel guildChannel)
{
var eb = EmbedFactory.GetStandardEmbedBuilder();
var serverSettings = ServerSettingHandler.GetSettings(guildChannel.GuildId);
foreach (var jokesKey in JokeHandler.Jokes.Keys)
{
if (serverSettings.EnabledJokes.Contains(jokesKey))
{
eb.AddField(jokesKey, "Enabled", true);
}
else
{
eb.AddField(jokesKey, "Disabled", true);
}
}
request.SendMessageAsync("", embed: eb.Build());
}
}
[Command("nofunallowed", PermissionLevel.Admin)]
[CommandHelp("Disables all jokes", "Disables all jokes")]
[BlockUsageInPm]
public async Task DisableJokes(CommandRequest request)
{
if (request.OriginalMessage.Channel is IGuildChannel guildChannel)
{
var serverSettings = ServerSettingHandler.GetSettings(guildChannel.GuildId);
serverSettings.SetEnabledJokes(new List<string>());
request.SendSimpleEmbed("Joke Toggle", $"Disabled all jokes");
}
}
}
}

View File

@ -10,7 +10,7 @@ namespace DeukBot4.MessageHandlers.JokeHandling
public string Id => "dad"; public string Id => "dad";
public string Name => "Hi I'm dad"; public string Name => "Hi I'm dad";
private Regex _regex = new Regex(@".*i['| a]*m ([\w\s]{3,15})$", private readonly Regex _regex = new Regex(@".*i['| a]*m ([\w\s]{3,15})$",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
public async Task Run(SocketMessage message) public async Task Run(SocketMessage message)

View File

@ -2,6 +2,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using DeukBot4.Database.ServerSettings;
using Discord;
using Discord.WebSocket; using Discord.WebSocket;
namespace DeukBot4.MessageHandlers.JokeHandling namespace DeukBot4.MessageHandlers.JokeHandling
@ -17,15 +19,21 @@ namespace DeukBot4.MessageHandlers.JokeHandling
foreach (var joke in jokes) foreach (var joke in jokes)
{ {
var c = (IJokeController)Activator.CreateInstance(joke); var c = (IJokeController)Activator.CreateInstance(joke);
Jokes.Add(c.Id, c); Jokes.Add(c.Id.ToLowerInvariant(), c);
} }
} }
public static async Task RunJokes(SocketMessage message) public static async Task RunJokes(SocketMessage message)
{ {
foreach (var jokeController in Jokes) if (message.Channel is IGuildChannel guildChannel)
{ {
jokeController.Value.Run(message); var guildSettings = ServerSettingHandler.GetSettings(guildChannel.GuildId);
var jokes = guildSettings.EnabledJokes;
foreach (var enabledJoke in jokes)
{
var jokeController = Jokes[enabledJoke];
jokeController.Run(message);
}
} }
} }
} }

View File

@ -9,8 +9,8 @@ namespace DeukBot4.MessageHandlers.JokeHandling
public string Id => "thanks"; public string Id => "thanks";
public string Name => "Claim credit when people say thanks"; public string Name => "Claim credit when people say thanks";
private readonly Regex ThanksMatcher = new Regex(@"(?i)((\b)thanks*(\W|$)+)"); private readonly Regex _thanksMatcher = new Regex(@"(?i)((\b)thanks*(\W|$)+)");
private readonly string[] ThanksResponses = { private readonly string[] _thanksResponses = {
"You're welcome!", "No problem", "You're welcome!", "No problem",
"It was the least I could do, I always do the least I can do!", "It was the least I could do, I always do the least I can do!",
"Yes, well, we'd be absolutely nowhere without me.", "Yes, well, we'd be absolutely nowhere without me.",
@ -22,10 +22,10 @@ namespace DeukBot4.MessageHandlers.JokeHandling
public async Task Run(SocketMessage message) public async Task Run(SocketMessage message)
{ {
var content = message.Content; var content = message.Content;
if (ThanksMatcher.IsMatch(content)) if (_thanksMatcher.IsMatch(content))
{ {
var response = ThanksResponses[_pos]; var response = _thanksResponses[_pos];
_pos = (_pos + 1) % ThanksResponses.Length; _pos = (_pos + 1) % _thanksResponses.Length;
message.Channel.SendMessageAsync(response); message.Channel.SendMessageAsync(response);
} }
} }