Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly shutdown the bot (and fix memory leaks alongside) #12

Merged
merged 2 commits into from
Mar 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,42 @@
/// Config file for the bot
#define CONFIG_FILE "config.json"

/// Discord client instance
static struct discord *discord_client = NULL;

/**
* Handle SIGINT signal and shut down the bot
*
* \param signum Signal number
*/
static void handle_sigint(int signum) {
log_info("[TAS8999] Received SIGINT, shutting down bot...");
if (discord_client) discord_shutdown(discord_client);
ccord_shutdown_async();
}

/**
* Initialize discord client
*
* \return 0 on success, 1 on failure
* \return Discord client on success, NULL on failure
*/
static int initialize_discord() {
static struct discord* initialize_discord() {
// initialize concord
CCORDcode code = ccord_global_init();
if (code) {
log_trace("[TAS8999] ccord_global_init() failed: %d", code);

return 1;
return NULL;
}
log_trace("[TAS8999] ccord_global_init() success");

// create discord client
discord_client = discord_config_init(CONFIG_FILE);
if (!discord_client) {
struct discord* client = discord_config_init(CONFIG_FILE);
if (!client) {
log_trace("[TAS8999] discord_create() failed");

ccord_global_cleanup();
return 1;
return NULL;
}
log_trace("[TAS8999] discord_create() success");

return 0;
return client;
}

/**
Expand Down Expand Up @@ -129,7 +126,8 @@ static void bot_main(struct discord *client, const struct discord_ready *event)
int main() {
// initialize discord bot
log_info("[TAS8999] Initializing tas8999 discord bot...");
if (initialize_discord()) {
struct discord* client = initialize_discord();
if (!client) {
log_fatal("[TAS8999] Failed to initialize discord bot");

return EXIT_FAILURE;
Expand All @@ -138,16 +136,16 @@ int main() {
// run discord bot
log_info("[TAS8999] Launching tas8999 discord bot...");
signal(SIGINT, handle_sigint);
discord_add_intents(discord_client, DISCORD_GATEWAY_MESSAGE_CONTENT);
discord_set_on_ready(discord_client, bot_main);
discord_set_on_message_create(discord_client, spamprotection_on_message);
CCORDcode code = discord_run(discord_client);
discord_add_intents(client, DISCORD_GATEWAY_MESSAGE_CONTENT);
discord_set_on_ready(client, bot_main);
discord_set_on_message_create(client, spamprotection_on_message);
CCORDcode code = discord_run(client);

// cleanup discord bot
log_info("[TAS8999] Discord bot exited (%d), cleaning up...", code);
customcommands_deinitialize();
submissions_deinitialize();
discord_cleanup(discord_client);
discord_cleanup(client);
ccord_global_cleanup();
return EXIT_SUCCESS;
}