lph/src/main.c

45 lines
1.5 KiB
C
Raw Normal View History

2024-02-26 21:59:25 +13:00
#include <string.h>
#include <concord/discord.h>
#include <concord/log.h>
2024-02-29 22:02:46 +13:00
#include <sqlite3.h>
#include <stdlib.h>
2024-02-26 21:59:25 +13:00
2024-02-29 22:02:46 +13:00
sqlite3* db;
void on_ready(struct discord *client, const struct discord_ready *msg) {
log_info("Logged in as %s!", msg->user->username);
2024-02-26 21:59:25 +13:00
}
2024-02-29 22:02:46 +13:00
void on_message(struct discord *client, const struct discord_message *msg) {
2024-02-29 13:23:49 +00:00
if(msg->type == DISCORD_MESSAGE_REPLY && msg->content != NULL) {
2024-02-29 22:02:46 +13:00
char* error;
2024-02-29 22:45:11 +00:00
if(sqlite3_exec(db, sqlite3_mprintf(
"INSERT INTO messages(message, reply)"
"VALUES('%q', '%q')",
msg->referenced_message->content, msg->content),
NULL, NULL, &error)) {
2024-02-29 22:02:46 +13:00
log_error(error);
}
}
2024-02-26 21:59:25 +13:00
}
int main(void) {
2024-02-29 22:02:46 +13:00
sqlite3_open("messages.db", &db);
char* error;
if(sqlite3_exec(db,
"CREATE TABLE IF NOT EXISTS messages("
2024-02-29 13:18:52 +00:00
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
2024-02-29 22:02:46 +13:00
"message TEXT NOT NULL,"
"reply TEXT NOT NULL"
");", NULL, NULL, &error)) {
log_error(error);
}
struct discord *client = discord_config_init("config.json");
discord_add_intents(client, DISCORD_GATEWAY_MESSAGE_CONTENT);
discord_set_on_ready(client, &on_ready);
discord_set_on_message_create(client, &on_message);
discord_run(client);
2024-02-26 21:59:25 +13:00
}