lph/src/main.c

46 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) {
if(msg->type == DISCORD_MESSAGE_REPLY) {
char buf[1024];
sprintf(buf, "INSERT INTO messages(id, message, reply)"
"VALUES('%" PRIu64 "', '%s', '%s')",
msg->id, msg->referenced_message->content, msg->content);
char* error;
if(sqlite3_exec(db, buf, NULL, NULL, &error)) {
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("
"id TEXT PRIMARY KEY,"
"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
}