An online conspiracy theory dubbed “pizzagate” ended Thursday with real-world consequences when a North Carolina man was sentenced to prison for arming himself with an assault rifle, traveling to the nation’s capital and firing his weapon inside a neighborhood pizza restaurant.

Edgar Maddison Welch’s “ill-conceived plot” last year did “actual damage to the lives of real people”, a judge said before sentencing him to four years in prison.

Judge Ketanji B Jackson said she had never seen a case like Welch’s, and she gave him a punishment on the upper end of guidelines, in part to send a message to others.

If Welch believed an internet conspiracy theory that children were being harmed at the restaurant, he should have notified law enforcement, not attempted to take the law into his own hands, the judge said during Thursday’s hearing in US district court in Washington.

Jackson said it was “sheer luck” that no one was physically injured when Welch entered Washington’s Comet Ping Pong restaurant on 4 December armed with an AR-15 assault rifle and a revolver. He was there just about a month after the election of Donald Trump to investigate unfounded internet rumors about prominent Democrats harboring child sex slaves at the restaurant.

As diners and staff fled, leaving half-eaten pizza and cups of soda, Welch went through the restaurant. At one point, he fired his AR-15 at a locked closet, but he discovered there were no children being held in the restaurant and surrendered peacefully.

Welch’s sentence was just below the 4 ½ years prosecutors sought and above the 1 ½ years Welch’s attorney asked for.

During the hearing, the 29-year-old Welch spoke briefly to apologize, saying he realized that his words “cannot undo or change what already happened”. In a letter filed with the court, he wrote that he was “truly sorry for endangering the safety of any and all bystanders who were present that day”, but he didn’t talk about the conspiracy theory that motivated him to act, saying just that he came to Washington “with the intent of helping people I believed were in dire need of assistance”.

On Thursday, he sat quietly in an orange jail jumpsuit throughout most of the hearing as his mother, father, sister and fiancée sat in the front of the courtroom.

Welch’s attorney, Dani Jahn, said that Welch’s actions were “reckless” and “misguided”, but she said Welch, a father and former emergency medical technician, had acted with the intent of defending children.

Welch, who is from Salisbury, North Carolina, pleaded guilty in March to interstate transportation of a firearm and ammunition and assault with a dangerous weapon. Though the rumors he went to investigate were unfounded, they have upended the lives of those who worked in the restaurant.

The restaurant’s owner, James Alefantis, said in court that the “vicious web of lies” about his business had been traumatic for him and his staff. He still needs security there, he said, and has suffered both emotionally and financially. In letters to the judge and in court, employees described the terror of Welch’s actions, with some saying they have depression and nightmares and need trauma counseling.

But Alefantis also said he was hopeful.

“I am hopeful that those who provoke fear, traffic in lies and perpetuate conspiracy will awake to the tangible harms that result from their actions,” he said in court. “I am hopeful that one day reason will prevail before a shot rings out again in a place of warmth and love and communal gathering.”