1
0
mirror of https://gitlab.com/Deukhoofd/DeukBot4.git synced 2026-04-03 11:10:05 +00:00

Handling for per server toggleable jokes

This commit is contained in:
2018-10-09 17:18:19 +02:00
parent 2fe04c2d1e
commit 771a825d9f
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 (" +
"server_id bigint NOT NULL," +
"muted_role bigint NOT NULL," +
"muted_role bigint NOT NULL," +
"enabled_jokes varchar(255)," +
"PRIMARY KEY(server_id)" +
")";
cmd.ExecuteNonQuery();

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using DeukBot4.Utilities;
using Npgsql;
@@ -6,14 +7,16 @@ namespace DeukBot4.Database.ServerSettings
{
public class ServerSetting
{
public ServerSetting(ulong serverId, ulong mutedRoleId = 0)
public ServerSetting(ulong serverId, ulong mutedRoleId = 0, List<string> enabledJokes = null)
{
ServerId = serverId;
MutedRoleId = mutedRoleId;
EnabledJokes = enabledJokes ?? new List<string>();
}
public ulong ServerId { get; }
public ulong MutedRoleId { get; private set; }
public List<string> EnabledJokes { get; private set; }
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 Discord;
using Npgsql;
namespace DeukBot4.Database.ServerSettings
@@ -17,13 +16,19 @@ namespace DeukBot4.Database.ServerSettings
using (var cmd = new NpgsqlCommand())
{
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();
while (reader.Read())
{
var id = reader.GetInt64(0).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())
{
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("muted", setting.MutedRoleId.ToLong());
cmd.Parameters.AddWithValue("jokes", string.Join(",", setting.EnabledJokes));
cmd.ExecuteNonQuery();
}
}