text: // Disconnect.hpp - Header for the "disconnect" function.
  // Aug 25, 2021
  // Chris M.
  // https://github.com/RealTimeChris
  
  #pragma once
  
  #include "../Headers/Index.hpp"
  #include "HelperFunctions.hpp"
  
  namespace DiscordCoreAPI {
  
  	class Disconnect :public BaseFunction {
  	public:
  		Disconnect() {
  			this->commandName = "disconnect";
  			this->helpDescription = "Disconnect the bot from voice chat.";
  			EmbedData msgEmbed;
  			msgEmbed.setDescription("------\nSimply enter !disconnect or /disconnect!\n------");
  			msgEmbed.setTitle("__**Disconnect Usage:**__");
  			msgEmbed.setTimeStamp(getTimeAndDate());
  			msgEmbed.setColor("FeFeFe");
  			this->helpEmbed = msgEmbed;
  		}
  
  		unique_ptr<BaseFunction> create() {
  			return make_unique<Disconnect>();
  		}
  
  		virtual void execute(BaseFunctionArguments args) {
  			try {
  				Channel channel = Channels::getCachedChannelAsync({ .channelId = args.eventData.getChannelId() }).get();
  
  				bool areWeInADm = areWeInADM(args.eventData, channel);
  
  				if (areWeInADm) {
  					return;
  				}
  
  				InputEvents::deleteInputEventResponseAsync(args.eventData).get();
  
  				Guild guild = Guilds::getCachedGuildAsync({ args.eventData.getGuildId() }).get();
  				DiscordGuild discordGuild(guild);
  
  				bool areWeAllowed = checkIfAllowedPlayingInChannel(args.eventData, discordGuild);
  
  				if (!areWeAllowed) {
  					return;
  				}
  
  				GuildMember guildMember = GuildMembers::getCachedGuildMemberAsync({ .guildMemberId = args.eventData.getAuthorId(),.guildId = args.eventData.getGuildId() }).get();
  
  				bool doWeHaveControl = checkIfWeHaveControl(args.eventData, discordGuild, guildMember);
  
  				if (!doWeHaveControl) {
  					return;
  				}
  
  				VoiceConnection* voiceConnection = guild.connectToVoice(guildMember.voiceData.channelId);
  				loadPlaylist(discordGuild);
  				if (voiceConnection == nullptr) {
  					EmbedData newEmbed;
  					newEmbed.setAuthor(args.eventData.getUserName(), args.eventData.getAvatarUrl());
  					newEmbed.setDescription("------\n__**Sorry, but there is no voice connection that is currently held by me!**__\n------");
  					newEmbed.setTimeStamp(getTimeAndDate());
  					newEmbed.setTitle("__**Connection Issue:**__");
  					newEmbed.setColor(discordGuild.data.borderColor);
  					if (args.eventData.eventType == InputEventType::Regular_Message) {
  						RespondToInputEventData dataPackage(args.eventData);
  						dataPackage.type = InputEventResponseType::Regular_Message;
  						dataPackage.addMessageEmbed(newEmbed);
  						auto newEvent = InputEvents::respondToEvent(dataPackage);
  						InputEvents::deleteInputEventResponseAsync(newEvent, 20000).get();
  					}
  					else {
  						RespondToInputEventData dataPackage(args.eventData);
  						dataPackage.type = InputEventResponseType::Ephemeral_Interaction_Response;
  						dataPackage.addMessageEmbed(newEmbed);
  						auto newEvent = InputEvents::respondToEvent(dataPackage);
  					}
  					return;
  				}
  
  				if (guild.areWeConnected()) {
  					EmbedData newEmbed;
  					newEmbed.setAuthor(args.eventData.getUserName(), args.eventData.getAvatarUrl());
  					newEmbed.setDescription("------\n__**I'm disconnecting from the voice channel!**__\n------");
  					newEmbed.setTimeStamp(getTimeAndDate());
  					newEmbed.setTitle("__**Disconnected:**__");
  					newEmbed.setColor(discordGuild.data.borderColor);
  					if (args.eventData.eventType == InputEventType::Regular_Message) {
  						RespondToInputEventData dataPackage(args.eventData);
  						dataPackage.type = InputEventResponseType::Regular_Message;
  						dataPackage.addMessageEmbed(newEmbed);
  						auto newEvent = InputEvents::respondToEvent(dataPackage);
  						InputEvents::deleteInputEventResponseAsync(newEvent, 20000).get();
  					}
  					else {
  						RespondToInputEventData dataPackage(args.eventData);
  						dataPackage.type = InputEventResponseType::Interaction_Response;
  						dataPackage.addMessageEmbed(newEmbed);
  						auto newEvent = InputEvents::respondToEvent(dataPackage);
  						InputEvents::deleteInputEventResponseAsync(newEvent, 20000).get();
  					}
  					guild.disconnect();
  					savePlaylist(discordGuild);
  				}
  				else {
  					EmbedData newEmbed;
  					newEmbed.setAuthor(args.eventData.getUserName(), args.eventData.getAvatarUrl());
  					newEmbed.setDescription("------\n__**I was already disconnected!**__\n------");
  					newEmbed.setTimeStamp(getTimeAndDate());
  					newEmbed.setTitle("__**Disconnected:**__");
  					newEmbed.setColor(discordGuild.data.borderColor);
  					if (args.eventData.eventType == InputEventType::Regular_Message) {
  						RespondToInputEventData dataPackage(args.eventData);
  						dataPackage.type = InputEventResponseType::Regular_Message;
  						dataPackage.addMessageEmbed(newEmbed);
  						auto newEvent = InputEvents::respondToEvent(dataPackage);
  						InputEvents::deleteInputEventResponseAsync(newEvent, 20000).get();
  					}
  					else {
  						RespondToInputEventData dataPackage(args.eventData);
  						dataPackage.type = InputEventResponseType::Ephemeral_Interaction_Response;
  						dataPackage.addMessageEmbed(newEmbed);
  						auto newEvent = InputEvents::respondToEvent(dataPackage);
  					}
  				}
  
  				return;
  			}
  			catch (...) {
  				reportException("Disconnect::executeAsync Error: ");
  			}
  			
  		}
  		virtual ~Disconnect() {};
  	};
  
  }
  