Skip to content

multiple logs based off how many servers its in #1551

@silvia552

Description

@silvia552

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions