/* Discord Integration Settings * * From: https://github.com/PokemonAutomation/ * */ #include "Common/Cpp/Strings/Unicode.h" #include "Common/Qt/StringToolsQt.h" #include "Integrations/DppIntegration/DppClient.h" #include "DiscordIntegrationSettings.h" //#include //using std::cout; //using std::endl; namespace PokemonAutomation{ namespace Integration{ DiscordIntegrationSettingsOption::~DiscordIntegrationSettingsOption(){ m_start_button.remove_listener(static_cast(*this)); m_stop_button.remove_listener(static_cast(*this)); this->remove_listener(*this); } DiscordIntegrationSettingsOption::DiscordIntegrationSettingsOption() : GroupOption( "Discord Integration Settings", LockMode::LOCK_WHILE_RUNNING, GroupOption::EnableMode::DEFAULT_DISABLED, false, false ) , m_start_button("Bot Control:", "Start Bot") , m_stop_button("", "Stop Bot") , run_on_start( "Run Discord Integration on Launch:
Automatically connect to Discord as soon as the program is launched.", LockMode::LOCK_WHILE_RUNNING, false ) , register_slash_button( "Register Slash Commands on Start:
Only needs to be run once, or after additional commands are added.", "Register Slash Commands" ) , command_type( "Discord Integration Command Type:
Restart the program for this to take effect.", { {CommandType::SlashCommands, "slash", "Slash commands"}, {CommandType::MessageCommands, "message", "Message (prefix) commands"}, }, LockMode::LOCK_WHILE_RUNNING, CommandType::SlashCommands ) , token( true, "Discord Token:
Enter your Discord bot's token. Keep it safe and don't share it with anyone.", LockMode::LOCK_WHILE_RUNNING, "", "0123456789abcdefghijklmnopqrstuvwxyzABCDEGFHIJKLMNOPQRSTUVWXYZ" ) , command_prefix( false, "Discord Command Prefix:
Enter a command prefix for your bot.", LockMode::LOCK_WHILE_RUNNING, "^", "^" ) , game_status( false, "Discord Game Status:
Enter a status message your bot would display.", LockMode::LOCK_WHILE_RUNNING, "", "Controlling your Switch. :)" ) , hello_message( false, "Discord Hello Message:
Enter a message you'd like the bot to respond with to the \"$hi\" command.", LockMode::LOCK_WHILE_RUNNING, "", "Automation at your service!" ) , allow_buttons_from_users( "Allow Buttons from Users:
Allow other users to issue button presses.", LockMode::LOCK_WHILE_RUNNING, false ) { PA_ADD_OPTION(m_start_button); PA_ADD_OPTION(m_stop_button); PA_ADD_OPTION(run_on_start); PA_ADD_OPTION(register_slash_button); PA_ADD_OPTION(command_type); PA_ADD_OPTION(token); PA_ADD_OPTION(command_prefix); PA_ADD_OPTION(game_status); PA_ADD_OPTION(hello_message); PA_ADD_OPTION(allow_buttons_from_users); PA_ADD_OPTION(channels); DiscordIntegrationSettingsOption::on_config_value_changed(this); this->add_listener(*this); m_start_button.add_listener(static_cast(*this)); m_stop_button.add_listener(static_cast(*this)); } void DiscordIntegrationSettingsOption::on_config_value_changed([[maybe_unused]] void* object){ // cout << this->enabled() << endl; #ifdef PA_DPP bool options_enabled = this->enabled(); options_enabled &= !DppClient::Client::instance().is_initialized(); ConfigOptionState state = options_enabled ? ConfigOptionState::ENABLED : ConfigOptionState::DISABLED; command_type.set_visibility(state); token.set_visibility(state); game_status.set_visibility(state); hello_message.set_visibility(state); allow_buttons_from_users.set_visibility(state); command_prefix.set_visibility(state); #endif } void DiscordIntegrationSettingsOption::on_press(ButtonCell& button){ #ifdef PA_DPP if (&button == &m_start_button){ DppClient::Client::instance().connect(); on_config_value_changed(this); return; } if (&button == &m_stop_button){ DppClient::Client::instance().disconnect(); on_config_value_changed(this); return; } #else (void)button; #endif } std::string convert(const std::string& message){ std::u32string ret; std::u32string block; for (char32_t ch : utf8_to_utf32(message)){ switch (ch){ case 38: if (block.empty() || block.back() != 64){ break; } continue; case 64: block += ch; continue; } ret += ch; block.clear(); } return utf32_to_str(ret); } MessageBuilder::MessageBuilder(const std::vector& message_tags){ for (const std::string& tag : message_tags){ m_message_tags.insert(to_lower(tag)); } } bool MessageBuilder::should_send(const std::vector& channel_tags) const{ for (const std::string& tag : channel_tags){ auto iter = m_message_tags.find(to_lower(tag)); if (iter != m_message_tags.end()){ return true; } } return false; } std::string MessageBuilder::build_message( std::chrono::seconds delay, bool ping, const std::string& user_id, const std::string& message ) const{ if (std::atoll(user_id.c_str()) == 0){ ping = false; } std::string str; if (ping){ str += ""; str[1]++; } if (!message.empty()){ if (!str.empty()){ str += " "; } str += convert(message); } auto delay_seconds = delay.count(); if (delay_seconds != 0){ if (!str.empty()){ str += "\n"; } str += "*("; str += "Message "; str += "delay"; str += "ed "; str += "by " + std::to_string(delay_seconds); str += delay_seconds == 1 ? " second" : " seconds"; str += ".)*"; } return str; } } }