/translate supports multiple targets now

This commit is contained in:
Syping 2024-02-15 05:25:46 +01:00
parent 9f995a129c
commit 9202f7ce00
3 changed files with 90 additions and 9 deletions

View file

@ -265,12 +265,6 @@ uint16_t settings::avatar_size()
const channel* settings::get_channel(const guild *guild, dpp::snowflake channel_id) const const channel* settings::get_channel(const guild *guild, dpp::snowflake channel_id) const
{ {
if (!m_externallyLockedCount) {
#ifndef NDEBUG
std::cerr << "[Debug] settings::get_channel(const guild*, dpp::snowflake) have being called without settings being locked." << std::endl;
#endif
return nullptr;
}
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)
return &*channel; return &*channel;
@ -313,6 +307,40 @@ const guild* settings::get_guild(dpp::snowflake guild_id) const
return nullptr; return nullptr;
} }
const target* settings::get_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target) const
{
if (!m_externallyLockedCount) {
#ifndef NDEBUG
std::cerr << "[Debug] settings::get_target(dpp::snowflake, dpp::snowflake, const std::string&) have being called without settings being locked." << std::endl;
#endif
return nullptr;
}
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id) {
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
if (channel->id == channel_id) {
for (auto _target = channel->targets.begin(); _target != channel->targets.end(); _target++) {
if (_target->target == target)
return &*_target;
}
return nullptr;
}
}
return nullptr;
}
}
return nullptr;
}
const target* settings::get_target(const channel *channel, const std::string &target) const
{
for (auto _target = channel->targets.begin(); _target != channel->targets.end(); _target++) {
if (_target->target == target)
return &*_target;
}
return nullptr;
}
const std::vector<std::string> settings::preferred_languages() const const std::vector<std::string> settings::preferred_languages() const
{ {
const std::lock_guard<std::recursive_mutex> guard(m_mutex); const std::lock_guard<std::recursive_mutex> guard(m_mutex);
@ -434,7 +462,7 @@ bool settings::parse_file(const std::string &filename)
std::string sdata(std::istreambuf_iterator<char>{ifs}, {}); std::string sdata(std::istreambuf_iterator<char>{ifs}, {});
ifs.close(); ifs.close();
return parse(std::move(sdata)); return parse(sdata);
} }
void settings::unlock() void settings::unlock()

View file

@ -37,6 +37,8 @@ namespace bot {
const channel* get_channel(const guild *guild, dpp::snowflake channel_id) const; const channel* get_channel(const guild *guild, dpp::snowflake channel_id) const;
const channel* get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) const; const channel* get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) const;
const guild* get_guild(dpp::snowflake guild_id) const; const guild* get_guild(dpp::snowflake guild_id) const;
const target* get_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target) const;
const target* get_target(const channel *channel, const std::string &target) const;
const std::vector<std::string> preferred_languages() const; const std::vector<std::string> preferred_languages() const;
std::shared_ptr<bot::database::database> get_database() const; std::shared_ptr<bot::database::database> get_database() const;
std::unique_ptr<bot::translator::translator> get_translator() const; std::unique_ptr<bot::translator::translator> get_translator() const;

View file

@ -59,7 +59,8 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
if (source_valid && target_valid) { if (source_valid && target_valid) {
const std::lock_guard<bot::settings::settings> guard(*settings); const std::lock_guard<bot::settings::settings> guard(*settings);
if (!settings->get_channel(event.command.guild_id, event.command.channel_id)) { const bot::settings::channel *channel = settings->get_channel(event.command.guild_id, event.command.channel_id);
if (!channel) {
if (dpp::channel *channel = std::get_if<dpp::channel>(&v_target)) { if (dpp::channel *channel = std::get_if<dpp::channel>(&v_target)) {
dpp::webhook webhook; dpp::webhook webhook;
webhook.channel_id = channel->id; webhook.channel_id = channel->id;
@ -118,8 +119,58 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral)); event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral));
} }
} }
else if (!settings->get_target(channel, target)) {
if (channel->source != source) {
event.reply(dpp::message("The current channel is already being translated from a different source language!").set_flags(dpp::m_ephemeral));
}
else if (dpp::channel *channel = std::get_if<dpp::channel>(&v_target)) {
dpp::webhook webhook;
webhook.channel_id = channel->id;
webhook.guild_id = channel->guild_id;
webhook.name = "Translate Bot Webhook <" + std::to_string(event.command.channel_id) + ":" + source + ":" + target + ">";
bot->create_webhook(webhook, [&bot, &settings, event, source, target](const dpp::confirmation_callback_t &callback) {
if (callback.is_error()) {
event.reply(dpp::message("Failed to generate webhook!").set_flags(dpp::m_ephemeral));
return;
}
const dpp::webhook webhook = callback.get<dpp::webhook>();
bot::settings::target s_target;
s_target.target = target;
s_target.webhook = webhook;
settings->lock();
settings->add_target(s_target, event.command.guild_id, event.command.channel_id);
settings->add_translatebot_webhook(webhook.id);
settings->unlock();
std::shared_ptr<bot::database::database> database = settings->get_database();
database->add_channel_target(event.command.guild_id, event.command.channel_id, s_target);
database->sync();
event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral));
});
}
else if (dpp::webhook *webhook = std::get_if<dpp::webhook>(&v_target)) {
bot::settings::target s_target;
s_target.target = target;
s_target.webhook = *webhook;
settings->lock();
settings->add_target(s_target, event.command.guild_id, event.command.channel_id);
settings->add_translatebot_webhook(webhook->id);
settings->unlock();
std::shared_ptr<bot::database::database> database = settings->get_database();
database->add_channel_target(event.command.guild_id, event.command.channel_id, s_target);
database->sync();
event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral));
}
}
else { else {
event.reply(dpp::message("The current channel is already being translated!").set_flags(dpp::m_ephemeral)); event.reply(dpp::message("The current channel is already being translated to the target language!").set_flags(dpp::m_ephemeral));
} }
} }
else if (!source_valid && !target_valid) { else if (!source_valid && !target_valid) {