; -------------------------------Discord-------------------------------
PostToDiscord:
CurrentSite := "Discord"

; SaveOrPostProgress(Message:="Posting through API",PostType:="Tooltip,ErrorLoggingTextFile")
Message = Posting Through API
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

if(DiscordParlerWebhookURL = ""){
	Message = Webhook Link not found. Please set "DiscordParlerWebhookURL" in %SettingsIniFilepath%.
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}


if(BoldTitleWAPI){
	if(NewLineBetweenTitle)
	Message = **%PostTitle%**`n`n%PostBody%
	else, 
	Message = **%PostTitle%**`n%PostBody%
}
else, { ; NOT bold title
	if(NewLineBetweenTitle)
	Message = %PostTitle%`n`n%PostBody%
	else, 
	Message = %PostTitle%`n%PostBody%
}



if(ImageAttachmentFilepath != ""){
	SingleQuotationMark = "
	; remove any single quotes from filepath
	ImageAttachmentFilepath := StrReplace(ImageAttachmentFilepath, SingleQuotationMark, "")

	if(StrLen(Message) > 1999){ ; split message into two parts if longer than this
		loop % StrLen(Message) {

			if(A_index < 1900)
			Continue

			SplitCharNumber := A_index
			; IndexMinusOne := A_index - 1

			SplitLocationText := SubStr(Message, SplitCharNumber, 1)
			if(SplitLocationText = " "){
				Break
				; Msgbox % "SplitLocation: " SplitLocation
			}
		}



		DiscordMessagePartOne := SubStr(Message, 1, SplitCharNumber)
		SplitCharNumber := SplitCharNumber + 1
		DiscordMessagePartTwo := SubStr(Message, SplitCharNumber)
		; Msgbox % "DiscordMessagePartOne: " DiscordMessagePartOne
		; Msgbox % "DiscordMessagePartTwo: " DiscordMessagePartTwo

		Status := SaveOrPostProgress(Message:=DiscordMessagePartOne,PostType:="ErrorLoggingTextFile,DiscordParler")
		Status := SaveOrPostProgress(Message:=DiscordMessagePartTwo,PostType:="ErrorLoggingTextFile,DiscordParler")
		Status := UploadImageToDiscord(DiscordParlerWebhookURL, "", ImageAttachmentFilepath)

		; Msgbox % "StatusOne: " StatusOne
		; Msgbox % "StatusTwo: " StatusTwo

/*		if(!InStr(StatusOne, "id")){ ; if error for each of the messages, post its
			parsed := JSON.Load(StatusOne)
			StatusOneFailure := parsed.content[1]

			parsed := JSON.Load(StatusTwo)
			StatusTwoFailure := parsed.content[1]

			Message = Partial Failure for the following reason: %StatusOneFailure% OR %StatusTwoFailure%
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		}
		*/
		} ; end of strlen > 1999
		else, {
			Status := UploadImageToDiscord(DiscordParlerWebhookURL, Message, ImageAttachmentFilepath)
			SaveOrPostProgress(Message:="Status",PostType:="ErrorLoggingTextFile")

			if(!InStr(Status, "id")){
				parsed := JSON.Load(Status)
				Error := parsed.content[1]

				Message = Discord Post Failed due to API Issue. Please See ErrorLog for details
				SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
				
				Message = Discord API Returned Data:`n %Error%
				SaveOrPostProgress(Message,PostType:=",ErrorLoggingTextFile")
				Return
			}
		}
	}
	else {
		if(StrLen(Message) > 1999){ ; split message into two parts if longer than this
			loop % StrLen(Message) {

				if(A_index < 1900)
				Continue

				SplitCharNumber := A_index

				SplitLocationText := SubStr(Message, SplitCharNumber, 1)
				if(SplitLocationText = " "){
					Break
				}
			}

			DiscordMessagePartOne := SubStr(Message, 1, SplitCharNumber)
			SplitCharNumber := SplitCharNumber + 1
			DiscordMessagePartTwo := SubStr(Message, SplitCharNumber)

			Status := SaveOrPostProgress(Message:=DiscordMessagePartOne,PostType:="ErrorLoggingTextFile,DiscordParler")
			Status := SaveOrPostProgress(Message:=DiscordMessagePartTwo,PostType:="ErrorLoggingTextFile,DiscordParler")
		}
		else,
		SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordParler")
	}

	IniWrite, Successful, %StatusFileFilePath%, Status, Discord
	AddToTotalPostsPostedCount()

	Message = Post Publish Successful
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


	Return
	; -------------------------------/Discord-------------------------------
