DPP icon indicating copy to clipboard operation
DPP copied to clipboard

multiple logs based off how many servers its in

Open silvia552 opened this issue 9 months ago • 9 comments

my bot is in 3 servers and if i send a message it logs it 3 times:

[2026-02-14 10:37:10] [MESSAGE CREATE] [Guild ID: 1471455535000846349] [Channel ID: 1471836999039189093] [Message ID: 1472255356066599094] [User ID: 1466340128359321750] [Username: my_discord_username] [Content: test] 
[2026-02-14 10:37:10] [MESSAGE CREATE] [Guild ID: 1471455535000846349] [Channel ID: 1471836999039189093] [Message ID: 1472255356066599094] [User ID: 1466340128359321750] [Username: my_discord_username] [Content: test] 
[2026-02-14 10:37:13] [MESSAGE DELETE] [Guild ID: 1471455535000846349] [Channel ID: 1471836999039189093] [Message ID: 1472255356066599094] 
[2026-02-14 10:37:13] [MESSAGE DELETE] [Guild ID: 1471455535000846349] [Channel ID: 1471836999039189093] [Message ID: 1472255356066599094] 

here is the code:

#include <dpp/dpp.h>
#include <ctime>
#include <iostream>
#include <fstream>

const std::string BOT_TOKEN = "redacted";
const std::string msg_log_path = "/path/to/file/msg_log.txt";

int main() {
    dpp::cluster bot(BOT_TOKEN, dpp::i_all_intents);

    bot.on_log(dpp::utility::cout_logger());

    bot.on_slashcommand([](const dpp::slashcommand_t &event){
        if (event.command.get_command_name() == "bot") {
            event.reply("### im always watching...");
        }
    });

    bot.on_ready([&bot](const dpp::ready_t& event) {
            if (dpp::run_once<struct register_bot_commands>()) {
                bot.global_command_create(dpp::slashcommand("bot", "### im always watching...", bot.me.id));
            }
        });

    bot.on_message_create([](const dpp::message_create_t &event){

        time_t timestamp = time(NULL);
        struct tm datetime = *localtime(&timestamp);
        char output[50];
        strftime(output, 50, "[%Y-%m-%d %T] ", &datetime);

        std::ofstream msg_log(msg_log_path, std::ios::app);
        if (!msg_log) {
            std::cerr << "[MESSAGE CREATE ERROR] Could not open " << msg_log_path << std::endl;
        }

        msg_log << output <<
            "[MESSAGE CREATE] " <<
            "[Guild ID: " << event.msg.guild_id << "] " <<
            "[Channel ID: " << event.msg.channel_id << "] " <<
            "[Message ID: " << event.msg.id << "] " <<
            "[User ID: " << event.msg.author.id << "] " <<
            "[Username: " << event.msg.author.username << "] ";

        if (event.msg.content != "") {
            msg_log << "[Content: " << event.msg.content << "] ";
        }

        if (event.msg.attachments.size() > 0) {
            msg_log << "[Attachments: ";
        }

        bool comma = false;
        bool attachments = false;
        for (const auto &attachment : event.msg.attachments) {
            if (comma) {
                msg_log << ", ";
            }
            else {
                comma = true;
            }
            msg_log << attachment.url;
            attachments = true;
        }
        if (attachments) {
            msg_log << "] ";
        }

        msg_log << std::endl;
        msg_log.close();
    });

    bot.on_message_delete([](const dpp::message_delete_t &event){
        time_t timestamp = time(NULL);
        struct tm datetime = *localtime(&timestamp);
        char output[50];
        strftime(output, 50, "[%Y-%m-%d %T] ", &datetime);

        std::ofstream msg_log(msg_log_path, std::ios::app);
        if (!msg_log) {
            std::cerr << "[MESSAGE DELETE ERROR] Could not open " << msg_log_path << std::endl;
        }
        msg_log << output <<
            "[MESSAGE DELETE] " <<
            "[Guild ID: " << event.guild_id << "] " <<
            "[Channel ID: " << event.channel_id << "] " <<
            "[Message ID: " << event.id << "] ";
        msg_log << std::endl;
        msg_log.close();
    });

    bot.on_message_update([](const dpp::message_update_t &event){
        time_t timestamp = time(NULL);
        struct tm datetime = *localtime(&timestamp);
        char output[50];
        strftime(output, 50, "[%Y-%m-%d %T] ", &datetime);

        std::ofstream msg_log(msg_log_path, std::ios::app);
        if (!msg_log) {
            std::cerr << "[MESSAGE UPDATE ERROR] Could not open " << msg_log_path << std::endl;
        }
        msg_log << output <<
            "[MESSAGE UPDATE] " <<
            "[Guild ID: " << event.msg.guild_id << "] " <<
            "[Channel ID: " << event.msg.channel_id << "] " <<
            "[Message ID: " << event.msg.id << "] " <<
            "[User ID: " << event.msg.author.id << "] " <<
            "[Username: " << event.msg.author.username << "] " <<
            "[Content: " << event.msg.content << "] ";
        msg_log << std::endl;
        msg_log.close();
    });

    bot.start(dpp::st_wait);
    return 0;
}

silvia552 avatar Feb 14 '26 15:02 silvia552

check you're not running 2 copies of the bot at the same time. there are 2 of each log line not 3.

braindigitalis avatar Feb 16 '26 08:02 braindigitalis

also take a look at spdlog. it will be far better than anything you are doing here.

braindigitalis avatar Feb 16 '26 08:02 braindigitalis

check you're not running 2 copies of the bot at the same time. there are 2 of each log line not 3.

i run it once from the terminal

also take a look at spdlog. it will be far better than anything you are doing here.

ill check it out, thanks

silvia552 avatar Feb 16 '26 08:02 silvia552

the logs say otherwise, log the pid in your log line to confirm

braindigitalis avatar Feb 16 '26 08:02 braindigitalis

the logs say otherwise, log the pid in your log line to confirm

i cannot seem to recreate the doubled logs... ig it fixed itself? (i made another bot and stop using this one like a day after posting this issue i had)

silvia552 avatar Feb 16 '26 08:02 silvia552

i caught it, it seems that it spawns zombie processes after being ran for days on end

and yes i am more than 100% sure that i am not running it twice...

silvia552 avatar Feb 23 '26 13:02 silvia552

I would double check your deployment of the bot and log your PIDs as Brain suggested, if it's happening over a long period of time or many restart cycles you might not be terminating the process correctly

On a side note, I'd recommend keeping std::cerr to errors, since it'll be more consistent with naming conventions

Ashthetik avatar Feb 24 '26 07:02 Ashthetik

@silvia552 Could you try retest this on a different machine to see if the issue occurs?

Jaskowicz1 avatar Mar 22 '26 10:03 Jaskowicz1

Closing due to stale. We can't repro this and will need more info.

Please feel free to reopen this if you can get us more information.

Jaskowicz1 avatar May 04 '26 12:05 Jaskowicz1