-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotClient.cpp
More file actions
84 lines (67 loc) · 2.43 KB
/
Copy pathBotClient.cpp
File metadata and controls
84 lines (67 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "BotClient.h"
#include <iostream>
#include <cstdlib>
BotClient::BotClient() : dpp::cluster(
std::getenv("VISORBOT_TOKEN") ? std::getenv("VISORBOT_TOKEN") : "",
dpp::i_default_intents | dpp::i_message_content | dpp::i_guild_members
)
{
if (sqlite3_open("VisorBot.db", &db) != SQLITE_OK)
{
std::cerr << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
}
else
{
std::cout << "Successfully opened database" << std::endl;
InitDatabase();
}
}
BotClient::~BotClient()
{
if (db)
{
sqlite3_close(db);
std::cout << "Successfully closed database" << std::endl;
}
}
void BotClient::InitDatabase()
{
std::string sql = "CREATE TABLE IF NOT EXISTS ServerConfig ("
"GuildID TEXT PRIMARY KEY, "
"ModChannelID TEXT);"
"CREATE TABLE IF NOT EXISTS KnowledgeSources("
"SourceID INTEGER PRIMARY KEY AUTOINCREMENT,"
"GuildID TEXT NOT NULL,"
"SourceName TEXT NOT NULL,"
"Url TEXT NOT NULL);";
char* errMsg = nullptr;
if (sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg) != SQLITE_OK) {
std::cerr << "SQL error: " << errMsg << std::endl;
sqlite3_free(errMsg);
}
addColumnIfNotExists("ServerConfig", "HelpChannelID", "TEXT");
addColumnIfNotExists("ServerConfig", "IssueChannelID", "TEXT");
addColumnIfNotExists("ServerConfig", "FaqChannelID", "TEXT");
addColumnIfNotExists("ServerConfig", "IssueResponseChannelID", "TEXT");
}
void BotClient::addColumnIfNotExists(const std::string& table, const std::string& column, const std::string& type)
{
std::string sql = "ALTER TABLE " + table + " ADD COLUMN " + column + " " + type + ";";
char* errMsg = nullptr;
if (sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg) != SQLITE_OK)
{
std::string err_str = errMsg ? errMsg : "";
if (err_str.find("duplicate column name") == std::string::npos)
{
std::cerr << "SQL error (ALTER TABLE " << table << "." << column << "): " << errMsg << std::endl;
}
sqlite3_free(errMsg);
}
}
bool BotClient::userHasAdminPermission(dpp::snowflake user_id)
{
std::string user_str = std::to_string(user_id);
std::string query = "SELECT HasPerms FROM UserHasAdmin WHERE UserID = ?";
bool has_perms = false;
return has_perms;
}