LOS ANGELES — Elon Musk testified Tuesday that he was being insulting, not literally calling a British cave diver a pedophile when he lashed out on Twitter and ended up in a defamation lawsuit from the man who helped rescue a dozen boys and their soccer coach from a flooded Thailand cave last year.

The Tesla CEO told jurors that he called Vernon Unsworth "pedo guy" because he was upset the spelunker had belittled his efforts to build a mini-submarine to transport the boys to safety. The sub was never used and Unsworth called it nothing more than a "PR stunt" and said Musk could stick the craft "where it hurts."

"It was wrong and insulting, so I insulted him back," the billionaire said in federal court in Los Angeles. "It was an unprovoked attack on what was a good-natured attempt to help the kids."

Unsworth’s lawyers argue that Musk’s tweet overshadowed what should have been one of the diver's proudest moments and left him no choice but to sue.

At the time, Musk said he didn't know Unsworth had helped with the rescue. He said his tweet wasn’t asserted as a fact.

"Just as I felt he didn't mean to sodomize me with a submarine, I likewise didn't think ("pedo guy") required any clarification," Musk said. "I will say it again, I apologize to Mr. Unsworth."

Unsworth is seeking unspecified damages for pain, suffering and emotional distress from the tech entrepreneur whose net worth exceeds $20 billion.

Musk’s lawyer, Alex Spiro, said earlier that Unsworth deserves nothing for what he called "joking, taunting tweets in a fight between men."

The shame and mortification Unsworth said he experienced is undercut by the attention he received after the rescue, Spiro said, including honors from the Thai king and British prime minister and offers from agents and film crews.

Musk deleted the July 15, 2018, tweets and apologized on Twitter, though subsequent statements he made have called into question what he meant.

In an email to a Buzzfeed reporter seeking comment on the threat of a lawsuit by Unsworth's lawyer, Musk wrote the journalist "should stop defending child rapists," followed by expletives.

Musk maintained his composure throughout an afternoon of questioning from Unsworth’s lawyer, who called him as his first witness.

Dressed in a charcoal suit and white shirt, Musk calmly answered the questions and slipped in an occasional droll remark that drew snickers from jurors and the gallery.

When asked about the final line of his email to the Buzzfeed reporter that said he hoped Unsworth would sue him, Musk quipped: "I guess be careful what you wish for."

Throughout the testimony, he didn’t back down from his assertion that he didn’t mean pedophile in his tweet, saying it was only meant as "creepy old man" where he grew up in South Africa, though he said the phrase was widely used across the English language.

Unsworth's lawyers laughed off that explanation, saying Musk responded to a question on Twitter about whether he had accused Unsworth of being a pedophile with another tweet that said, "Bet ya a signed dollar it’s true."

Musk also made a crack about that tweet, joking that it was a "very high stakes situation."

"If I really thought that it was true, I would have said more than a dollar," he testified.

Musk admitted one of his staffers tried to enlist a man to dig up dirt on Unsworth, but the investigator turned out to be a con man who got $52,000 for his efforts and produced nothing damning about Unsworth.

The lawsuit is not the first time Musk’s tweets have landed him in hot water.

Musk and Tesla reached a $40 million settlement with the Securities and Exchange Commission last year over allegations he misled investors with a tweet declaring he had secured financing to buy out the electric car maker. He agreed in the settlement to have future tweets about the company screened.

He went back to court on accusations he violated that agreement by tweeting a misleading figure about how many cars Tesla would manufacture this year. The SEC sought to hold him in contempt of court, which led to a new agreement imposing tighter controls on Musk's tweets about the company.