erase empty guilds from memory

This commit is contained in:
Syping 2024-03-08 16:56:38 +01:00
parent 0dbf987ff2
commit 92d9aba8d0
3 changed files with 28 additions and 8 deletions

View file

@ -301,11 +301,22 @@ void settings::add_translatebot_webhook(dpp::snowflake webhook_id)
m_webhookIds.push_back(webhook_id); m_webhookIds.push_back(webhook_id);
} }
void settings::erase_channel(guild *guild, dpp::snowflake channel_id) void settings::erase_channel(guild &guild, dpp::snowflake channel_id)
{ {
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) { for (auto channel = guild.channel.begin(); channel != guild.channel.end(); channel++) {
if (channel->id == channel_id) { if (channel->id == channel_id) {
guild->channel.erase(channel); guild.channel.erase(channel);
return;
}
}
}
void settings::erase_guild(dpp::snowflake guild_id)
{
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id) {
m_guilds.erase(guild);
return; return;
} }
} }

View file

@ -33,7 +33,8 @@ namespace bot {
void add_translatebot_webhook(dpp::snowflake webhook_id); void add_translatebot_webhook(dpp::snowflake webhook_id);
/* erase functions */ /* erase functions */
static void erase_channel(guild *guild, dpp::snowflake channel_id); static void erase_channel(guild &guild, dpp::snowflake channel_id);
void erase_guild(dpp::snowflake guild_id);
void erase_translatebot_webhook(dpp::snowflake webhook_id); void erase_translatebot_webhook(dpp::snowflake webhook_id);
/* get functions */ /* get functions */

View file

@ -68,8 +68,12 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings::
database->delete_channel(event.command.guild_id, event.command.channel_id); database->delete_channel(event.command.guild_id, event.command.channel_id);
database->sync(); database->sync();
if (channel->targets.empty()) if (channel->targets.empty()) {
settings->erase_channel(guild, event.command.channel_id); settings->erase_channel(*guild, event.command.channel_id);
if (guild->channel.empty()) {
settings->erase_guild(event.command.guild_id);
}
}
event.reply(dpp::message("Deleteable targets have being deleted!").set_flags(dpp::m_ephemeral)); event.reply(dpp::message("Deleteable targets have being deleted!").set_flags(dpp::m_ephemeral));
} }
@ -97,8 +101,12 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings::
database->delete_channel_target(event.command.guild_id, event.command.channel_id, target); database->delete_channel_target(event.command.guild_id, event.command.channel_id, target);
database->sync(); database->sync();
if (channel->targets.empty()) if (channel->targets.empty()) {
settings->erase_channel(guild, event.command.channel_id); settings->erase_channel(*guild, event.command.channel_id);
if (guild->channel.empty()) {
settings->erase_guild(event.command.guild_id);
}
}
event.reply(dpp::message("Target have being deleted!").set_flags(dpp::m_ephemeral)); event.reply(dpp::message("Target have being deleted!").set_flags(dpp::m_ephemeral));
} }