about summary refs log tree commit diff
path: root/src/import.rs
blob: f7d98734f6269f768e5f87ef2cd27bbefc0fc22b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use serde::Deserialize;

use crate::database::{BoardRow, Database, Error};

#[derive(Debug, Deserialize)]
pub struct Emboard {
	guildName: String,
	leaderboard: Vec<EmboardRow>,
}

#[derive(Debug, Deserialize)]
pub struct EmboardRow {
	guildId: String,
	discordId: String,
	points: String,
	username: String,
}

pub fn import(db: &Database, json: String) {
	let embaord: Emboard = match serde_json::from_str(&json) {
		Ok(e) => e,
		Err(e) => {
			panic!("{e}");
		}
	};

	let Some(first) = embaord.leaderboard.first() else {
		return;
	};

	let guild_id = u64::from_str_radix(&first.guildId, 10).unwrap();
	if db.get_leaderboard(guild_id).is_err() {
		db.create_leaderboard(guild_id).unwrap();
	}

	for user in embaord.leaderboard {
		let user_id = u64::from_str_radix(&user.discordId, 10).unwrap();
		let points = i64::from_str_radix(&user.points, 10).unwrap();

		let res = db.give_user_points(guild_id, user_id, points);
		if let Err(Error::UserNotExist) = res {
			db.add_user_to_leaderboard(
				guild_id,
				BoardRow {
					user_id,
					user_handle: user.username,
					user_nickname: None,
					points,
				},
			)
			.unwrap();
		}
	}
}