Initial work on system for server settings
This commit is contained in:
parent
349382301e
commit
b3c878db04
|
@ -18,6 +18,12 @@ namespace DeukBot4.Database
|
|||
"PRIMARY KEY(server_id, role_id)" +
|
||||
")";
|
||||
cmd.ExecuteNonQuery();
|
||||
cmd.CommandText = "CREATE TABLE IF NOT EXISTS server_settings (" +
|
||||
"server_id bigint NOT NULL," +
|
||||
"muted_role bigint NOT NULL," +
|
||||
"PRIMARY KEY(server_id)" +
|
||||
")";
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
using DeukBot4.Utilities;
|
||||
using Npgsql;
|
||||
|
||||
namespace DeukBot4.Database.ServerSettings
|
||||
{
|
||||
public class ServerSetting
|
||||
{
|
||||
public ServerSetting(ulong serverId, ulong mutedRoleId = 0)
|
||||
{
|
||||
ServerId = serverId;
|
||||
MutedRoleId = mutedRoleId;
|
||||
}
|
||||
|
||||
public ulong ServerId { get; }
|
||||
public ulong MutedRoleId { get; }
|
||||
|
||||
public void SetMutedRoleId(ulong id)
|
||||
{
|
||||
using (var conn = new DatabaseConnection())
|
||||
{
|
||||
using (var cmd = new NpgsqlCommand())
|
||||
{
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = "UPDATE server_settings SET muted_role = @val WHERE server_id = @key";
|
||||
cmd.Parameters.AddWithValue("val", id.ToLong());
|
||||
cmd.Parameters.AddWithValue("key", ServerId.ToLong());
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
using System.Collections.Generic;
|
||||
using DeukBot4.Utilities;
|
||||
using Discord;
|
||||
using Npgsql;
|
||||
|
||||
namespace DeukBot4.Database.ServerSettings
|
||||
{
|
||||
public static class ServerSettingHandler
|
||||
{
|
||||
private static readonly Dictionary<ulong, ServerSetting> Settings = new Dictionary<ulong, ServerSetting>();
|
||||
|
||||
public static void OnBotStartUp()
|
||||
{
|
||||
using (var conn = new DatabaseConnection())
|
||||
{
|
||||
using (var cmd = new NpgsqlCommand())
|
||||
{
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = "SELECT server_id muted_role 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddSettingsToDatabase(ServerSetting setting)
|
||||
{
|
||||
using (var conn = new DatabaseConnection())
|
||||
{
|
||||
using (var cmd = new NpgsqlCommand())
|
||||
{
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = "INSERT INTO server_settings (server_id, muted_role) VALUES (@id, @muted)";
|
||||
cmd.Parameters.AddWithValue("id", setting.ServerId.ToLong());
|
||||
cmd.Parameters.AddWithValue("muted", setting.MutedRoleId.ToLong());
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ServerSetting GetSettings(ulong id)
|
||||
{
|
||||
if (Settings.TryGetValue(id, out var setting))
|
||||
{
|
||||
return setting;
|
||||
}
|
||||
setting = new ServerSetting(id);
|
||||
Settings.Add(id, setting);
|
||||
AddSettingsToDatabase(setting);
|
||||
return setting;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue