send sim messages to the proper rooms
This commit is contained in:
		
							parent
							
								
									7526d63690
								
							
						
					
					
						commit
						da6603d258
					
				
					 10 changed files with 38 additions and 10 deletions
				
			
		| 
						 | 
				
			
			@ -24,10 +24,18 @@ async function createSim(user) {
 | 
			
		|||
	const mxid = "@" + localpart + ":cadence.moe"
 | 
			
		||||
 | 
			
		||||
	// Save chosen name in the database forever
 | 
			
		||||
	// Making this database change right away so that in a concurrent registration, the 2nd registration will already have generated a different localpart because it can see this row when it generates
 | 
			
		||||
	db.prepare("INSERT INTO sim (discord_id, sim_name, localpart, mxid) VALUES (?, ?, ?, ?)").run(user.id, simName, localpart, mxid)
 | 
			
		||||
 | 
			
		||||
	// Register matrix user with that name
 | 
			
		||||
	await api.register(localpart)
 | 
			
		||||
	try {
 | 
			
		||||
		await api.register(localpart)
 | 
			
		||||
	} catch (e) {
 | 
			
		||||
		// If user creation fails, manually undo the database change. Still isn't perfect, but should help.
 | 
			
		||||
		// (A transaction would be preferable, but I don't think it's safe to leave transaction open across event loop ticks.)
 | 
			
		||||
		db.prepare("DELETE FROM sim WHERE discord_id = ?").run(user.id)
 | 
			
		||||
		throw e
 | 
			
		||||
	}
 | 
			
		||||
	return mxid
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,4 +28,4 @@ async function sendMessage(message) {
 | 
			
		|||
	return eventID
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports.sendMessage = sendMessage
 | 
			
		||||
module.exports.sendMessage = sendMessage
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue