| 2497 | 2497 | const prevChId = this_chid; |
| 2498 | 2498 | |
| 2499 | 2499 | // Find the character |
| 2500 | 2500 | const chId = characters.findIndex((e) => e.name === name || e.avatar === name); |
| 2501 | 2501 | if (!characters[chId] || chId === -1) { |
| 2502 | 2502 | toastr.error('Character not found.'); |
| 2503 | 2503 | return ''; |
| 2504 | 2504 | } |
| 2505 | 2505 | |
| 2506 | + // Sending a message implicitly saves the chat, so this needs to be done before changing the character |
| 2507 | + // Otherwise, a corruption will occur |
| 2508 | + await sendMessageAsUser(mesText, ''); |
| 2509 | + |
| 2506 | 2510 | // Override character and send a user message |
| 2507 | 2511 | setCharacterId(String(chId)); |
| 2508 | 2512 | |
| 2509 | | - // TODO: Maybe look up by filename instead of name |
| 2510 | 2513 | const character = characters[chId]; |
| 2511 | 2514 | let force_avatar, original_avatar; |
| 2512 | 2515 | |