add edit command

This commit is contained in:
Syping 2024-02-18 22:26:02 +01:00
parent ed1b93037c
commit a6feb1273c
8 changed files with 154 additions and 17 deletions

View file

@ -58,6 +58,7 @@ void database::delete_guild(dpp::snowflake guild_id)
#endif
}
/*
std::variant<std::monostate,bot::settings::target> database::find_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target)
{
#ifndef NDEBUG
@ -65,6 +66,7 @@ std::variant<std::monostate,bot::settings::target> database::find_channel_target
#endif
return {};
}
*/
bot::settings::channel database::get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id)
{

View file

@ -36,7 +36,9 @@ namespace bot {
virtual void delete_channel(dpp::snowflake guild_id, dpp::snowflake channel_id);
virtual void delete_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target);
virtual void delete_guild(dpp::snowflake guild_id);
/* unused atm.
virtual std::variant<std::monostate,bot::settings::target> find_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target);
*/
virtual bot::settings::channel get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id);
virtual std::vector<dpp::snowflake> get_channels(dpp::snowflake guild_id);
/* unused atm.

View file

@ -221,6 +221,7 @@ void file::delete_guild(dpp::snowflake guild_id)
std::filesystem::remove(guild_file);
}
/*
std::variant<std::monostate,bot::settings::target> file::find_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target)
{
const std::lock_guard<std::mutex> guard(m_mutex);
@ -248,6 +249,7 @@ std::variant<std::monostate,bot::settings::target> file::find_channel_target(dpp
}
return {};
}
*/
bot::settings::channel file::get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id)
{

View file

@ -38,7 +38,9 @@ namespace bot {
void delete_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) override;
void delete_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target) override;
void delete_guild(dpp::snowflake guild_id) override;
/* unused atm.
std::variant<std::monostate,bot::settings::target> find_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target) override;
*/
bot::settings::channel get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) override;
std::vector<dpp::snowflake> get_channels(dpp::snowflake guild_id) override;
/* unused atm.

View file

@ -260,7 +260,7 @@ uint16_t settings::avatar_size()
return m_avatarSize;
}
const channel* settings::get_channel(const guild *guild, dpp::snowflake channel_id) const
channel* settings::get_channel(guild *guild, dpp::snowflake channel_id)
{
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
if (channel->id == channel_id)
@ -269,7 +269,7 @@ const channel* settings::get_channel(const guild *guild, dpp::snowflake channel_
return nullptr;
}
const channel* settings::get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) const
channel* settings::get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id)
{
if (!m_externallyLockedCount) {
#ifndef NDEBUG
@ -289,7 +289,7 @@ const channel* settings::get_channel(dpp::snowflake guild_id, dpp::snowflake cha
return nullptr;
}
const guild* settings::get_guild(dpp::snowflake guild_id) const
guild* settings::get_guild(dpp::snowflake guild_id)
{
if (!m_externallyLockedCount) {
#ifndef NDEBUG
@ -304,7 +304,7 @@ const guild* settings::get_guild(dpp::snowflake guild_id) const
return nullptr;
}
const target* settings::get_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target) const
target* settings::get_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target)
{
if (!m_externallyLockedCount) {
#ifndef NDEBUG
@ -329,7 +329,16 @@ const target* settings::get_target(dpp::snowflake guild_id, dpp::snowflake chann
return nullptr;
}
const target* settings::get_target(const channel *channel, const std::string &target) const
target* settings::get_target(channel *channel, const std::string &target)
{
for (auto _target = channel->targets.begin(); _target != channel->targets.end(); _target++) {
if (_target->target == target)
return &*_target;
}
return nullptr;
}
const target* settings::get_target(const channel *channel, const std::string &target)
{
for (auto _target = channel->targets.begin(); _target != channel->targets.end(); _target++) {
if (_target->target == target)

View file

@ -34,11 +34,12 @@ namespace bot {
/* get functions */
uint16_t avatar_size();
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 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;
static channel* get_channel(guild *guild, dpp::snowflake channel_id);
channel* get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id);
guild* get_guild(dpp::snowflake guild_id);
target* get_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target);
static target* get_target(channel *channel, const std::string &target);
static const target* get_target(const channel *channel, const std::string &target);
const std::vector<std::string> preferred_languages() const;
std::shared_ptr<bot::database::database> get_database() const;
std::unique_ptr<bot::translator::translator> get_translator() const;

View file

@ -22,12 +22,127 @@ using namespace std::string_literals;
void bot::slashcommands::process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
{
if (event.command.get_command_name() == "list")
if (event.command.get_command_name() == "edit")
bot::slashcommands::process_edit_command(bot, settings, event);
else if (event.command.get_command_name() == "list")
bot::slashcommands::process_list_command(bot, settings, event);
else if (event.command.get_command_name() == "translate" || event.command.get_command_name() == "translate_pref")
bot::slashcommands::process_translate_command(bot, settings, event);
}
void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
{
try {
dpp::command_interaction interaction = event.command.get_command_interaction();
if (interaction.options[0].name == "delete") {
const std::lock_guard<bot::settings::settings> guard(*settings);
if (bot::settings::channel *channel = settings->get_channel(event.command.guild_id, event.command.channel_id)) {
const std::string target = std::get<std::string>(event.get_parameter("target"));
std::shared_ptr<bot::database::database> database = settings->get_database();
const bot::settings::channel db_channel = database->get_channel(event.command.guild_id, event.command.channel_id);
if (db_channel.targets.empty()) {
event.reply(dpp::message("The current channel has no deleteable targets!").set_flags(dpp::m_ephemeral));
}
else if (target == "**") {
std::vector<std::string> targets;
for (auto db_target = db_channel.targets.begin(); db_target != db_channel.targets.end(); db_target++) {
targets.push_back(db_target->target);
}
for (auto target = channel->targets.begin(); target != channel->targets.end();) {
if (std::find(targets.begin(), targets.end(), target->target) != targets.end()) {
bot->delete_webhook(target->webhook.id);
target = channel->targets.erase(target);
}
else {
target++;
}
}
database->delete_channel(event.command.guild_id, event.command.channel_id);
database->sync();
event.reply(dpp::message("Deleteable targets have being deleted!").set_flags(dpp::m_ephemeral));
}
else {
bool target_found = false;
for (auto db_target = db_channel.targets.begin(); db_target != db_channel.targets.end(); db_target++) {
if (db_target->target == target) {
target_found = true;
break;
}
}
if (target_found) {
for (auto _target = channel->targets.begin(); _target != channel->targets.end(); _target++) {
if (_target->target == target) {
bot->delete_webhook(_target->webhook.id);
channel->targets.erase(_target);
break;
}
}
if (db_channel.targets.size() == 1)
database->delete_channel(event.command.guild_id, event.command.channel_id);
else
database->delete_channel_target(event.command.guild_id, event.command.channel_id, target);
database->sync();
event.reply(dpp::message("Target have being deleted!").set_flags(dpp::m_ephemeral));
}
else {
event.reply(dpp::message("Target language is not being found or deleteable!").set_flags(dpp::m_ephemeral));
}
}
}
else {
event.reply(dpp::message("The current channel is not being translated!").set_flags(dpp::m_ephemeral));
}
}
else if (interaction.options[0].name == "source") {
const std::lock_guard<bot::settings::settings> guard(*settings);
if (bot::settings::channel *channel = settings->get_channel(event.command.guild_id, event.command.channel_id)) {
const std::string source = std::get<std::string>(event.get_parameter("source"));
const std::vector<bot::translator::language> languages = settings->get_translator()->get_languages();
std::ostringstream language_codes;
bool source_valid = false;
for (const bot::translator::language &language : languages) {
if (language.code == source) {
source_valid = true;
break;
}
language_codes << " " << language.code;
}
if (source_valid) {
channel->source = source;
std::shared_ptr<bot::database::database> database = settings->get_database();
database->set_channel_source(event.command.guild_id, event.command.channel_id, source);
database->sync();
event.reply(dpp::message("Source language have being updated!").set_flags(dpp::m_ephemeral));
}
else {
event.reply(dpp::message("Source language is not valid!\nAvailable languages are:" + language_codes.str()).set_flags(dpp::m_ephemeral));
}
}
else {
event.reply(dpp::message("The current channel is not being translated!").set_flags(dpp::m_ephemeral));
}
}
else {
throw std::invalid_argument("Option " + interaction.options[0].name + " is not known");
}
}
catch (const std::exception& exception) {
std::cerr << "[Exception] " << exception.what() << std::endl;
event.reply(dpp::message("Exception while processing command:\n"s + exception.what()).set_flags(dpp::m_ephemeral));
}
}
void bot::slashcommands::process_list_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
{
try {
@ -254,13 +369,20 @@ void bot::slashcommands::register_commands(dpp::cluster *bot, bot::settings::set
std::vector<dpp::slashcommand> commands;
/*
dpp::command_option source_option(dpp::co_string, "source", "Source language (ISO 639-1)", true);
source_option.set_max_length(static_cast<int64_t>(2)).set_min_length(static_cast<int64_t>(2));
dpp::command_option target_option(dpp::co_string, "target", "Target language (ISO 639-1)", true);
target_option.set_max_length(static_cast<int64_t>(2)).set_min_length(static_cast<int64_t>(2));
dpp::slashcommand command_edit("edit", "Edit current channel settings", bot->me.id);
command_edit.set_default_permissions(dpp::p_manage_webhooks);
dpp::command_option delete_edit_subcommand(dpp::co_sub_command, "delete", "Delete current channel target language");
dpp::command_option source_edit_subcommand(dpp::co_sub_command, "source", "Edit current channel source language");
delete_edit_subcommand.add_option(target_option);
source_edit_subcommand.add_option(source_option);
command_edit.add_option(delete_edit_subcommand);
command_edit.add_option(source_edit_subcommand);
commands.push_back(command_edit);
*/
dpp::slashcommand command_list("list", "List translation settings", bot->me.id);
dpp::command_option channel_list_subcommand(dpp::co_sub_command, "channel", "List current channel translation settings");
@ -273,10 +395,6 @@ void bot::slashcommands::register_commands(dpp::cluster *bot, bot::settings::set
command_translate.set_default_permissions(dpp::p_manage_webhooks);
dpp::command_option channel_translate_subcommand(dpp::co_sub_command, "channel", "Translate current channel to a channel");
dpp::command_option webhook_translate_subcommand(dpp::co_sub_command, "webhook", "Translate current channel to a webhook");
dpp::command_option source_option(dpp::co_string, "source", "Source language (ISO 639-1)", true);
source_option.set_max_length(static_cast<int64_t>(2)).set_min_length(static_cast<int64_t>(2));
dpp::command_option target_option(dpp::co_string, "target", "Target language (ISO 639-1)", true);
target_option.set_max_length(static_cast<int64_t>(2)).set_min_length(static_cast<int64_t>(2));
dpp::command_option channel_option(dpp::co_channel, "channel", "Target channel", true);
channel_option.add_channel_type(dpp::CHANNEL_TEXT);
dpp::command_option webhook_option(dpp::co_string, "webhook", "Target webhook", true);

View file

@ -25,6 +25,7 @@
namespace bot {
namespace slashcommands {
extern void process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
extern void process_edit_command(dpp::cluster* bot, bot::settings::settings* settings, const dpp::slashcommand_t &event);
extern void process_list_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
extern void process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
extern void process_translate_webhook_add_target(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event, const std::string &target, const dpp::confirmation_callback_t &callback);