· 8 years ago · Jul 04, 2018, 03:10 AM
1namespace TamamoSharp.Database
2{
3 public class TamamoDb
4 {
5 private readonly string _connectionString;
6
7 public TamamoDb(string connectionString)
8 {
9 _connectionString = connectionString;
10 }
11
12 public NpgsqlConnection GetConnection()
13 {
14 return new NpgsqlConnection(_connectionString);
15 }
16
17 public async Task EnsureDbCreated()
18 {
19 using (NpgsqlConnection conn = GetConnection())
20 {
21 await conn.OpenAsync();
22 string sql = @"
23CREATE TABLE IF NOT EXISTS guild_configs (
24 ""guild_id"" BIGINT NOT NULL PRIMARY KEY,
25 ""is_ignored"" BOOLEAN NOT NULL DEFAULT FALSE,
26 ""delete_invoking_message"" BOOLEAN NOT NULL DEFAULT FALSE,
27 ""delete_command_replies"" BOOLEAN NOT NULL DEFAULT FALSE,
28 ""delete_error_messages"" BOOLEAN NOT NULL DEFAULT FALSE,
29 ""leave_message_enabled"" BOOLEAN NOT NULL DEFAULT FALSE,
30 ""leave_message"" TEXT NOT NULL DEFAULT('%user% has left the guild. :('),
31 ""leave_message_channel_id"" BIGINT NOT NULL DEFAULT(0),
32 ""join_message_enabled"" BOOLEAN NOT NULL DEFAULT FALSE,
33 ""join_message"" TEXT NOT NULL DEFAULT('Welcome, %user%!'),
34 ""join_message_channel_id"" BIGINT NOT NULL DEFAULT(0),
35 ""starboard_enabled"" BOOLEAN NOT NULL DEFAULT FALSE,
36 ""starboard_channel_id"" BIGINT NOT NULL DEFAULT(0),
37 ""starboard_max_age"" INT NOT NULL DEFAULT(7),
38 ""starboard_threshold"" INT NOT NULL DEFAULT(1),
39 ""starboard_locked"" BOOLEAN NOT NULL DEFAULT FALSE,
40 ""starboard_auto_clear"" BOOLEAN NOT NULL DEFAULT FALSE,
41 ""max_tag_count_per_user"" INT NOT NULL DEFAULT(10),
42 ""max_tag_count"" INT NOT NULL DEFAULT(0),
43 ""auto_assign_role"" BOOLEAN NOT NULL DEFAULT FALSE,
44 ""auto_assign_role_id"" BIGINT NOT NULL DEFAULT(0),
45 ""mute_role_id"" BIGINT NOT NULL DEFAULT(0)
46);
47
48CREATE TABLE IF NOT EXISTS channel_configs (
49 ""channel_id"" BIGINT NOT NULL PRIMARY KEY,
50 ""is_ignored"" BOOLEAN NOT NULL DEFAULT FALSE,
51 ""guild_id"" BIGINT NOT NULL REFERENCES guild_configs(guild_id) ON DELETE CASCADE
52);
53
54CREATE TABLE IF NOT EXISTS user_configs (
55 ""id"" UUID NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
56 ""user_id"" BIGINT NOT NULL,
57 ""is_ignored"" BOOLEAN NOT NULL DEFAULT FALSE,
58 ""created_tag_count"" INT NOT NULL DEFAULT(0),
59 ""guild_id"" BIGINT NOT NULL REFERENCES guild_configs(guild_id) ON DELETE CASCADE
60);
61
62CREATE TABLE IF NOT EXISTS starboard_entries (
63 ""id"" UUID NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
64 ""message_id"" BIGINT NOT NULL,
65 ""bot_message_id"" BIGINT NOT NULL,
66 ""channel_id"" BIGINT NOT NULL,
67 ""author_id"" BIGINT NOT NULL,
68 ""star_count"" INT NOT NULL,
69 ""guild_id"" BIGINT NOT NULL REFERENCES guild_configs(guild_id) ON DELETE CASCADE
70);";
71
72 NpgsqlCommand query = new NpgsqlCommand(sql, conn);
73 //await query.PrepareAsync();
74 await query.ExecuteScalarAsync();
75 }
76 }
77 }
78}