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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
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();
}
}
}
#[cfg(feature = "emboard-direct")]
pub mod emboard_direct {
use std::{sync::Arc, time::Duration};
use twilight_model::{
gateway::payload::incoming::InteractionCreate,
http::interaction::{InteractionResponse, InteractionResponseType},
id::{Id, marker::GuildMarker},
};
use twilight_util::builder::InteractionResponseDataBuilder;
use ureq::{Agent, RequestBuilder, config::Config, typestate::WithoutBody};
use crate::brain::Brain;
const DATA_URL: &str =
"https://emboard.evolvedmesh.com/api/backend/dashboard/emboard_guild/leaderboard";
pub async fn import(brain: Arc<Brain>, guild_id: Id<GuildMarker>, create: &InteractionCreate) {
let member = create.member.clone().unwrap();
let permissions = member.permissions.unwrap();
if !permissions.contains(twilight_model::guild::Permissions::MANAGE_GUILD) {
brain
.interaction_respond(
create,
InteractionResponseDataBuilder::new()
.content(
"❌ You must have the Manage Server permission to perform this action",
)
.build(),
)
.await;
return;
}
let url = format!("{DATA_URL}/{guild_id}");
let config = Config::builder()
.user_agent("leaberblord +gen@nyble.dev")
.build();
let data = InteractionResponseDataBuilder::new()
.content(
"⏳ waiting on emboard for the leaderboard. we'll display the leaderboard when it's imported",
)
.build();
brain.interaction_respond(create, data).await;
match config.new_agent().get(url).call() {
Err(_e) => todo!(),
Ok(mut resp) => {
let body: String = resp.body_mut().read_to_string().unwrap();
super::import(&brain.db, body);
let embed = crate::get_leaderboard(brain.clone(), guild_id)
.embeds
.unwrap()[0]
.clone();
brain
.send_message_with_embed(
create.channel.as_ref().unwrap().id,
"✅ imported leaderboard from emboard",
embed,
)
.await;
}
}
}
}
|