35EVA5MHEJMVJWQUXIJWAX43LJZ7YLE5Y4YN5T2SOSTBTQB6HTTQC
use crate::Score;
use std::fmt::Write;
command!(blague(ctx, msg, _args) {
let mut res = "Blagues :\n".to_string();
let mut data = ctx.data.lock();
let scores = data
.get::<Score>()
.expect("Expected Score in ShareMap.");
for (k, v) in scores {
let _ = write!(res, "- {}: {}\n", k, v);
}
if let Err(why) = msg.channel_id.say(&res) {
println!("Error sending message: {:?}", why);
}
});
fn get_user_id(user: String) -> Result<String, String> {
let start;
if !user.starts_with("<@") || !user.ends_with(">") {
return Err("Le nom est mal formΓ© !".to_string());
}
if user.starts_with("<@!") {
start = 3
} else {
start = 2
}
let user_id: u64 = match user[start..user.len() - 1].parse() {
Err(_) => return Err("Le nom est mal formΓ© !".to_string()),
Ok(n) => n,
};
let user = serenity::model::id::UserId(user_id);
return match user.to_user() {
Err(_) => Err("Utilisateur inconnu".to_string()),
Ok(u) => Ok(u.to_string()),
};
}
command!(mdr(ctx, msg, args) {
if args.len() != 1 {
let _ = msg.reply("Donne moi **un** nom !");
let _ = msg.react('β');
return Ok(());
}
let user: String = args.iter().next().unwrap().unwrap();
let user = match get_user_id(user) {
Err(e) => {
let _ = msg.reply(&e);
let _ = msg.react('β');
return Ok(());
},
Ok(u) => u,
};
let mut data = ctx.data.lock();
let score = data
.get_mut::<Score>()
.expect("Expected Score in ShareMap.");
let entry = score.entry(user.to_string()).or_insert(0);
*entry += 1;
let _ = msg.react('π');
});
command!(nul(ctx, msg, args) {
if args.len() != 1 {
let _ = msg.reply("Donne moi **un** nom !");
let _ = msg.react('β');
return Ok(());
}
let user: String = args.iter().next().unwrap().unwrap();
let user = match get_user_id(user) {
Err(e) => {
let _ = msg.reply(&e);
let _ = msg.react('β');
return Ok(());
},
Ok(u) => u,
};
let mut data = ctx.data.lock();
let score = data
.get_mut::<Score>()
.expect("Expected Score in ShareMap.");
let entry = score.entry(user.to_string()).or_insert(0);
*entry -= 1;
let _ = msg.react('π');
});
pub mod blague;
.command("commands", |c| c.cmd(commands))
.command("latency", |c| c.cmd(latency))
.command("nul", |c| c.cmd(nul))
.command("mdr", |c| c.cmd(nul))
.command("blague", |c| c.cmd(blague)),
.command("nul", |c| c.cmd(commands::blague::nul))
.command("mdr", |c| c.cmd(commands::blague::mdr))
.command("blague", |c| c.cmd(commands::blague::blague)),
}
}
command!(blague(ctx, msg, _args) {
let mut res = "Blagues :\n".to_string();
let mut data = ctx.data.lock();
let scores = data
.get::<Score>()
.expect("Expected Score in ShareMap.");
for (k, v) in scores {
let _ = write!(res, "- {}: {}\n", k, v);
}
if let Err(why) = msg.channel_id.say(&res) {
println!("Error sending message: {:?}", why);
}
});
fn get_user_id(user: String) -> Result<String, String> {
println!("user: {}", user);
let start;
if !user.starts_with("<@") || !user.ends_with(">") {
return Err("Le nom est mal formΓ© !".to_string());
if user.starts_with("<@!") {
start = 3
} else {
start = 2
}
let user_id: u64 = match user[start..user.len() - 1].parse() {
Err(_) => return Err("Le nom est mal formΓ© !".to_string()),
Ok(n) => n,
};
let user = serenity::model::id::UserId(user_id);
return match user.to_user() {
Err(_) => Err("Utilisateur inconnu".to_string()),
Ok(u) => Ok(u.to_string()),
};
command!(mdr(ctx, msg, args) {
if args.len() != 1 {
let _ = msg.reply("Donne moi **un** nom !");
let _ = msg.react('β');
return Ok(());
}
let user: String = args.iter().next().unwrap().unwrap();
let user = match get_user_id(user) {
Err(e) => {
let _ = msg.reply(&e);
let _ = msg.react('β');
return Ok(());
},
Ok(u) => u,
};
let mut data = ctx.data.lock();
let score = data
.get_mut::<Score>()
.expect("Expected Score in ShareMap.");
let entry = score.entry(user.to_string()).or_insert(0);
*entry += 1;
let _ = msg.react('π');
});
command!(nul(ctx, msg, args) {
if args.len() != 1 {
let _ = msg.reply("Donne moi **un** nom !");
let _ = msg.react('β');
return Ok(());
}
let user: String = args.iter().next().unwrap().unwrap();
let user = match get_user_id(user) {
Err(e) => {
let _ = msg.reply(&e);
let _ = msg.react('β');
return Ok(());
},
Ok(u) => u,
};
let mut data = ctx.data.lock();
let score = data
.get_mut::<Score>()
.expect("Expected Score in ShareMap.");
let entry = score.entry(user.to_string()).or_insert(0);
*entry -= 1;
let _ = msg.react('π');
});
command!(commands(ctx, msg, _args) {
let mut contents = "Commands used:\n".to_string();
let data = ctx.data.lock();
let counter = data.get::<CommandCounter>().expect("Expected CommandCounter in ShareMap.");
for (k, v) in counter {
let _ = write!(contents, "- {name}: {amount}\n", name=k, amount=v);
}
if let Err(why) = msg.channel_id.say(&contents) {
println!("Error sending message: {:?}", why);
}
});
command!(latency(ctx, msg, _args) {
let data = ctx.data.lock();
let shard_manager = match data.get::<ShardManagerContainer>() {
Some(v) => v,
None => {
let _ = msg.reply("There was a problem getting the shard manager");
return Ok(());
},
};
let manager = shard_manager.lock();
let runners = manager.runners.lock();
let runner = match runners.get(&ShardId(ctx.shard_id)) {
Some(runner) => runner,
None => {
let _ = msg.reply("No shard found");
return Ok(());
},
};
let _ = msg.reply(&format!("The shard latency is {:?}", runner.latency));
});