fix(app): the wizard resumes until finished, late answers stay off other screens, logs follow the chosen folder

- app_status reports firstRun until wizard_finish; wizard_step saves the
  step reached in config/setup.json, and the wizard resumes there (a stored
  token lets the bot step go on).
- show() and refresh() drop answers that arrive after the screen changed.
- folder_choose points the log hub at the folder's logs/ at once.
- Choosing another server clears the voice channel picker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 14:25:59 +09:00
parent 20300c017e
commit b68eef3ec1
4 changed files with 126 additions and 27 deletions

View File

@@ -60,6 +60,19 @@ impl AdminApi {
.ok_or_else(|| "봇 토큰을 먼저 저장해 주세요".into())
}
/// The wizard's progress, `config/setup.json`: `{step, done}`.
fn setup(&self, dir: &DataDir) -> Value {
std::fs::read_to_string(dir.config().join("setup.json")).ok()
.and_then(|t| serde_json::from_str::<Value>(&t).ok())
.filter(Value::is_object)
.unwrap_or_else(|| json!({"step": 1, "done": false}))
}
fn save_setup(&self, dir: &DataDir, v: &Value) -> Result<(), String> {
crate::datadir::write_atomic(&dir.config().join("setup.json"), v.to_string().as_bytes())
.map_err(|e| format!("설정 진행 상황을 저장하지 못했습니다: {e}"))
}
fn controller(&self) -> Result<Arc<Controller>, String> {
let dir = self.dir()?;
let mut c = self.controller.lock().unwrap();
@@ -72,8 +85,31 @@ impl AdminApi {
"app_status" => {
let dir = self.dir.lock().unwrap().clone();
let token = dir.as_ref().and_then(|d| Secrets::new(&d.secrets_file()).get(DISCORD_TOKEN).ok().flatten());
// The wizard is done only when its last step was reached, not when a token exists.
let setup = dir.as_ref().map(|d| self.setup(d));
let done = setup.as_ref().is_some_and(|s| s["done"] == json!(true));
Ok(json!({"version": APP_VERSION, "dataDir": dir.as_ref().map(|d| d.path().to_string_lossy()),
"tokenSet": token.is_some(), "firstRun": dir.is_none() || token.is_none()}))
"tokenSet": token.is_some(), "firstRun": !done,
"wizardStep": setup.as_ref().and_then(|s| s["step"].as_u64()).unwrap_or(0)}))
}
"wizard_step" => {
let dir = self.dir()?;
let step = arg(&args, "step")?.as_u64().ok_or("step은 숫자여야 합니다")?;
let mut s = self.setup(&dir);
s["step"] = json!(step);
self.save_setup(&dir, &s)?;
Ok(s)
}
"wizard_finish" => {
let dir = self.dir()?;
if self.secrets()?.get(DISCORD_TOKEN).map_err(|e| e.to_string())?.is_none() {
return Err("봇 토큰을 먼저 저장해 주세요".into());
}
let mut s = self.setup(&dir);
s["done"] = json!(true);
self.save_setup(&dir, &s)?;
crate::info_log!("admin", "first-run setup finished");
Ok(s)
}
"folder_check" => {
let path = PathBuf::from(arg_str(&args, "path")?);
@@ -87,6 +123,8 @@ impl AdminApi {
"folder_choose" => {
let path = PathBuf::from(arg_str(&args, "path")?);
let dir = DataDir::choose(&path, &self.location_file).map_err(|e| e.to_string())?;
// Log files go to the chosen folder from now on, not only after a restart.
self.hub.set_dir(&dir.logs());
*self.dir.lock().unwrap() = Some(dir);
*self.controller.lock().unwrap() = None;
crate::info_log!("admin", "data folder chosen");
@@ -182,13 +220,27 @@ mod tests {
let data = base.join("data");
let check = a.call("folder_check", json!({"path": data.to_string_lossy()})).await.unwrap();
assert!(check["freeBytes"].as_u64().unwrap() > 0 && check["exists"] == json!(false));
a.hub.push(Level::Info, "app", "before the folder");
a.call("folder_choose", json!({"path": data.to_string_lossy()})).await.unwrap();
assert!(data.join("config").is_dir());
// From the choice on, lines go to the folder's log files and the zip export works.
a.hub.push(Level::Info, "app", "after the folder");
let today = chrono::Local::now().date_naive().to_string();
let logs = std::fs::read_to_string(data.join("logs").join(format!("app-{today}.log"))).unwrap();
assert!(logs.contains("after the folder"));
let zip = a.call("logs_export", json!({"from": today, "to": today})).await.unwrap();
assert!(std::path::Path::new(zip["path"].as_str().unwrap()).exists());
assert_eq!(a.call("app_status", json!({})).await.unwrap()["firstRun"], json!(true), "no token yet");
assert!(a.call("wizard_finish", json!({})).await.is_err(), "not without a token");
let saved = a.call("token_save", json!({"token": "abc.def.ghijkl"})).await.unwrap();
assert_eq!(saved["masked"], json!("••••ijkl"));
a.call("wizard_step", json!({"step": 4})).await.unwrap();
// A stored token does not finish the wizard: it resumes at the step it reached.
let s = a.call("app_status", json!({})).await.unwrap();
assert_eq!((s["firstRun"].clone(), s["tokenSet"].clone()), (json!(false), json!(true)));
assert_eq!((s["firstRun"].clone(), s["tokenSet"].clone(), s["wizardStep"].clone()),
(json!(true), json!(true), json!(4)));
a.call("wizard_finish", json!({})).await.unwrap();
assert_eq!(a.call("app_status", json!({})).await.unwrap()["firstRun"], json!(false));
// The token never comes back whole.
assert_eq!(a.call("token_status", json!({})).await.unwrap(), json!({"set": true, "masked": "••••ijkl"}));
}