mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-18 16:28:55 +00:00
Get rid of mut refs
This commit is contained in:
parent
8c34c7a3ba
commit
9287bdf5b7
7 changed files with 52 additions and 67 deletions
|
@ -33,9 +33,9 @@ pub fn soft_ban(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResu
|
||||||
};
|
};
|
||||||
let guild = msg.guild_id.ok_or(Error::from("Command is guild only"))?;
|
let guild = msg.guild_id.ok_or(Error::from("Command is guild only"))?;
|
||||||
|
|
||||||
let mut data = ctx.data.write();
|
let data = ctx.data.read();
|
||||||
let mut data = data
|
let data = data
|
||||||
.get_mut::<SoftBans>()
|
.get::<SoftBans>()
|
||||||
.ok_or(Error::from("DB initialized"))
|
.ok_or(Error::from("DB initialized"))
|
||||||
.map(|v| DBWriteGuard::from(v))?;
|
.map(|v| DBWriteGuard::from(v))?;
|
||||||
let mut data = data.borrow_mut()?;
|
let mut data = data.borrow_mut()?;
|
||||||
|
@ -98,14 +98,14 @@ pub fn soft_ban_init(ctx: &mut Context, msg: &Message, mut args: Args) -> Comman
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
// Check if we already set up
|
// Check if we already set up
|
||||||
let mut data = ctx.data.write();
|
let data = ctx.data.read();
|
||||||
let mut db: DBWriteGuard<_> = data
|
let db: DBWriteGuard<_> = data
|
||||||
.get_mut::<SoftBans>()
|
.get::<SoftBans>()
|
||||||
.ok_or(Error::from("DB uninitialized"))?
|
.ok_or(Error::from("DB uninitialized"))?
|
||||||
.into();
|
.into();
|
||||||
let mut db = db.borrow_mut()?;
|
let mut db = db.borrow_mut()?;
|
||||||
let server = db
|
let server = db
|
||||||
.get_mut(&guild.id)
|
.get(&guild.id)
|
||||||
.map(|v| match v {
|
.map(|v| match v {
|
||||||
ServerSoftBans::Unimplemented => false,
|
ServerSoftBans::Unimplemented => false,
|
||||||
_ => true,
|
_ => true,
|
||||||
|
@ -135,9 +135,9 @@ pub fn watch_soft_bans(client: &mut serenity::Client) -> impl FnOnce() -> () + '
|
||||||
// Scope so that locks are released
|
// Scope so that locks are released
|
||||||
{
|
{
|
||||||
// Poll the data for any changes.
|
// Poll the data for any changes.
|
||||||
let mut data = data.write();
|
let data = data.read();
|
||||||
let mut db: DBWriteGuard<_> = data
|
let db: DBWriteGuard<_> = data
|
||||||
.get_mut::<SoftBans>()
|
.get::<SoftBans>()
|
||||||
.expect("DB wrongly initialized")
|
.expect("DB wrongly initialized")
|
||||||
.into();
|
.into();
|
||||||
let mut db = db.borrow_mut().expect("cannot unpack DB");
|
let mut db = db.borrow_mut().expect("cannot unpack DB");
|
||||||
|
|
|
@ -14,15 +14,12 @@ pub trait Announcer {
|
||||||
fn announcer_key() -> &'static str;
|
fn announcer_key() -> &'static str;
|
||||||
fn send_messages(
|
fn send_messages(
|
||||||
c: &Http,
|
c: &Http,
|
||||||
d: &mut ShareMap,
|
d: &ShareMap,
|
||||||
channels: impl Fn(UserId) -> Vec<ChannelId> + Sync,
|
channels: impl Fn(UserId) -> Vec<ChannelId> + Sync,
|
||||||
) -> CommandResult;
|
) -> CommandResult;
|
||||||
|
|
||||||
fn set_channel(d: &mut ShareMap, guild: GuildId, channel: ChannelId) -> CommandResult {
|
fn set_channel(d: &ShareMap, guild: GuildId, channel: ChannelId) -> CommandResult {
|
||||||
let mut data: DBWriteGuard<_> = d
|
let data: DBWriteGuard<_> = d.get::<AnnouncerChannels>().expect("DB initialized").into();
|
||||||
.get_mut::<AnnouncerChannels>()
|
|
||||||
.expect("DB initialized")
|
|
||||||
.into();
|
|
||||||
let mut data = data.borrow_mut()?;
|
let mut data = data.borrow_mut()?;
|
||||||
data.entry(Self::announcer_key().to_owned())
|
data.entry(Self::announcer_key().to_owned())
|
||||||
.or_default()
|
.or_default()
|
||||||
|
@ -30,7 +27,7 @@ pub trait Announcer {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_guilds(d: &mut ShareMap) -> Result<Vec<(GuildId, ChannelId)>, Error> {
|
fn get_guilds(d: &ShareMap) -> Result<Vec<(GuildId, ChannelId)>, Error> {
|
||||||
let data = d
|
let data = d
|
||||||
.get::<AnnouncerChannels>()
|
.get::<AnnouncerChannels>()
|
||||||
.expect("DB initialized")
|
.expect("DB initialized")
|
||||||
|
@ -42,7 +39,7 @@ pub trait Announcer {
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn announce(c: &Http, d: &mut ShareMap) -> CommandResult {
|
fn announce(c: &Http, d: &ShareMap) -> CommandResult {
|
||||||
let guilds: Vec<_> = Self::get_guilds(d)?;
|
let guilds: Vec<_> = Self::get_guilds(d)?;
|
||||||
let member_sets = {
|
let member_sets = {
|
||||||
let mut v = Vec::with_capacity(guilds.len());
|
let mut v = Vec::with_capacity(guilds.len());
|
||||||
|
@ -75,7 +72,7 @@ pub trait Announcer {
|
||||||
let c = client.cache_and_http.clone();
|
let c = client.cache_and_http.clone();
|
||||||
let data = client.data.clone();
|
let data = client.data.clone();
|
||||||
spawn(move || loop {
|
spawn(move || loop {
|
||||||
if let Err(e) = Self::announce(c.http(), &mut *data.write()) {
|
if let Err(e) = Self::announce(c.http(), &*data.read()) {
|
||||||
dbg!(e);
|
dbg!(e);
|
||||||
}
|
}
|
||||||
std::thread::sleep(cooldown);
|
std::thread::sleep(cooldown);
|
||||||
|
|
|
@ -2,10 +2,9 @@ use super::{embeds::score_embed, BeatmapWithMode};
|
||||||
use crate::{
|
use crate::{
|
||||||
commands::announcer::Announcer,
|
commands::announcer::Announcer,
|
||||||
db::{OsuSavedUsers, OsuUser},
|
db::{OsuSavedUsers, OsuUser},
|
||||||
http::{Osu, HTTP},
|
http::Osu,
|
||||||
};
|
};
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use reqwest::blocking::Client as HTTPClient;
|
|
||||||
use serenity::{
|
use serenity::{
|
||||||
framework::standard::{CommandError as Error, CommandResult},
|
framework::standard::{CommandError as Error, CommandResult},
|
||||||
http::Http,
|
http::Http,
|
||||||
|
@ -30,7 +29,7 @@ impl Announcer for OsuAnnouncer {
|
||||||
}
|
}
|
||||||
fn send_messages(
|
fn send_messages(
|
||||||
c: &Http,
|
c: &Http,
|
||||||
d: &mut ShareMap,
|
d: &ShareMap,
|
||||||
channels: impl Fn(UserId) -> Vec<ChannelId> + Sync,
|
channels: impl Fn(UserId) -> Vec<ChannelId> + Sync,
|
||||||
) -> CommandResult {
|
) -> CommandResult {
|
||||||
let osu = d.get::<Osu>().expect("osu!client").clone();
|
let osu = d.get::<Osu>().expect("osu!client").clone();
|
||||||
|
@ -87,7 +86,7 @@ impl Announcer for OsuAnnouncer {
|
||||||
osu_user.last_update = chrono::Utc::now();
|
osu_user.last_update = chrono::Utc::now();
|
||||||
}
|
}
|
||||||
// Update users
|
// Update users
|
||||||
let f = d.get_mut::<OsuSavedUsers>().expect("DB initialized");
|
let f = d.get::<OsuSavedUsers>().expect("DB initialized");
|
||||||
f.write(|f| *f = data)?;
|
f.write(|f| *f = data)?;
|
||||||
f.save()?;
|
f.save()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -8,12 +8,12 @@ use serenity::{
|
||||||
|
|
||||||
/// Save the beatmap into the server data storage.
|
/// Save the beatmap into the server data storage.
|
||||||
pub(crate) fn save_beatmap(
|
pub(crate) fn save_beatmap(
|
||||||
data: &mut ShareMap,
|
data: &ShareMap,
|
||||||
channel_id: ChannelId,
|
channel_id: ChannelId,
|
||||||
bm: &BeatmapWithMode,
|
bm: &BeatmapWithMode,
|
||||||
) -> CommandResult {
|
) -> CommandResult {
|
||||||
let mut db: DBWriteGuard<_> = data
|
let db: DBWriteGuard<_> = data
|
||||||
.get_mut::<OsuLastBeatmap>()
|
.get::<OsuLastBeatmap>()
|
||||||
.expect("DB is implemented")
|
.expect("DB is implemented")
|
||||||
.into();
|
.into();
|
||||||
let mut db = db.borrow_mut()?;
|
let mut db = db.borrow_mut()?;
|
||||||
|
|
|
@ -47,7 +47,7 @@ pub fn hook(ctx: &mut Context, msg: &Message) -> () {
|
||||||
}
|
}
|
||||||
// Save the beatmap for query later.
|
// Save the beatmap for query later.
|
||||||
if let Some(t) = last_beatmap {
|
if let Some(t) = last_beatmap {
|
||||||
if let Err(v) = super::cache::save_beatmap(&mut *ctx.data.write(), msg.channel_id, &t) {
|
if let Err(v) = super::cache::save_beatmap(&*ctx.data.read(), msg.channel_id, &t) {
|
||||||
dbg!(v);
|
dbg!(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -121,8 +121,7 @@ fn handle_old_links<'a>(ctx: &mut Context, content: &'a str) -> Result<Vec<ToPri
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_new_links<'a>(ctx: &mut Context, content: &'a str) -> Result<Vec<ToPrint<'a>>, Error> {
|
fn handle_new_links<'a>(ctx: &mut Context, content: &'a str) -> Result<Vec<ToPrint<'a>>, Error> {
|
||||||
let data = ctx.data.write();
|
let osu = ctx.data.read().get::<http::Osu>().unwrap().clone();
|
||||||
let osu = data.get::<http::Osu>().unwrap().clone();
|
|
||||||
let mut to_prints: Vec<ToPrint<'a>> = Vec::new();
|
let mut to_prints: Vec<ToPrint<'a>> = Vec::new();
|
||||||
for capture in NEW_LINK_REGEX.captures_iter(content) {
|
for capture in NEW_LINK_REGEX.captures_iter(content) {
|
||||||
let mode = capture.name("mode").and_then(|v| {
|
let mode = capture.name("mode").and_then(|v| {
|
||||||
|
|
|
@ -97,9 +97,9 @@ pub fn save(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
let user: Option<User> = osu.user(UserID::Auto(user), |f| f)?;
|
let user: Option<User> = osu.user(UserID::Auto(user), |f| f)?;
|
||||||
match user {
|
match user {
|
||||||
Some(u) => {
|
Some(u) => {
|
||||||
let mut db = ctx.data.write();
|
let db = ctx.data.read();
|
||||||
let mut db: DBWriteGuard<_> = db
|
let db: DBWriteGuard<_> = db
|
||||||
.get_mut::<OsuSavedUsers>()
|
.get::<OsuSavedUsers>()
|
||||||
.ok_or(Error::from("DB uninitialized"))?
|
.ok_or(Error::from("DB uninitialized"))?
|
||||||
.into();
|
.into();
|
||||||
let mut db = db.borrow_mut()?;
|
let mut db = db.borrow_mut()?;
|
||||||
|
@ -147,18 +147,14 @@ enum UsernameArg {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UsernameArg {
|
impl UsernameArg {
|
||||||
fn to_user_id_query(
|
fn to_user_id_query(s: Option<Self>, data: &ShareMap, msg: &Message) -> Result<UserID, Error> {
|
||||||
s: Option<Self>,
|
|
||||||
data: &mut ShareMap,
|
|
||||||
msg: &Message,
|
|
||||||
) -> Result<UserID, Error> {
|
|
||||||
let id = match s {
|
let id = match s {
|
||||||
Some(UsernameArg::Raw(s)) => return Ok(UserID::Auto(s)),
|
Some(UsernameArg::Raw(s)) => return Ok(UserID::Auto(s)),
|
||||||
Some(UsernameArg::Tagged(r)) => r,
|
Some(UsernameArg::Tagged(r)) => r,
|
||||||
None => msg.author.id,
|
None => msg.author.id,
|
||||||
};
|
};
|
||||||
let db: DBWriteGuard<_> = data
|
let db: DBWriteGuard<_> = data
|
||||||
.get_mut::<OsuSavedUsers>()
|
.get::<OsuSavedUsers>()
|
||||||
.ok_or(Error::from("DB uninitialized"))?
|
.ok_or(Error::from("DB uninitialized"))?
|
||||||
.into();
|
.into();
|
||||||
let db = db.borrow()?;
|
let db = db.borrow()?;
|
||||||
|
@ -202,11 +198,8 @@ impl FromStr for Nth {
|
||||||
pub fn recent(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
pub fn recent(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
let nth = args.single::<Nth>().unwrap_or(Nth(1)).0.min(50).max(1);
|
let nth = args.single::<Nth>().unwrap_or(Nth(1)).0.min(50).max(1);
|
||||||
let mode = args.single::<ModeArg>().unwrap_or(ModeArg(Mode::Std)).0;
|
let mode = args.single::<ModeArg>().unwrap_or(ModeArg(Mode::Std)).0;
|
||||||
let user = UsernameArg::to_user_id_query(
|
let user =
|
||||||
args.single::<UsernameArg>().ok(),
|
UsernameArg::to_user_id_query(args.single::<UsernameArg>().ok(), &*ctx.data.read(), msg)?;
|
||||||
&mut *ctx.data.write(),
|
|
||||||
msg,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let osu: OsuClient = ctx.data.read().get::<http::Osu>().unwrap().clone();
|
let osu: OsuClient = ctx.data.read().get::<http::Osu>().unwrap().clone();
|
||||||
let user = osu
|
let user = osu
|
||||||
|
@ -235,7 +228,7 @@ pub fn recent(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// Save the beatmap...
|
// Save the beatmap...
|
||||||
cache::save_beatmap(&mut *ctx.data.write(), msg.channel_id, &beatmap)?;
|
cache::save_beatmap(&*ctx.data.read(), msg.channel_id, &beatmap)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -244,9 +237,7 @@ pub fn recent(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult
|
||||||
#[description = "Show information from the last queried beatmap."]
|
#[description = "Show information from the last queried beatmap."]
|
||||||
#[num_args(0)]
|
#[num_args(0)]
|
||||||
pub fn last(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult {
|
pub fn last(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult {
|
||||||
let mut data = ctx.data.write();
|
let b = cache::get_beatmap(&*ctx.data.read(), msg.channel_id)?;
|
||||||
|
|
||||||
let b = cache::get_beatmap(&mut *data, msg.channel_id)?;
|
|
||||||
|
|
||||||
match b {
|
match b {
|
||||||
Some(BeatmapWithMode(b, m)) => {
|
Some(BeatmapWithMode(b, m)) => {
|
||||||
|
@ -271,9 +262,7 @@ pub fn last(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult {
|
||||||
#[description = "Check your own or someone else's best record on the last beatmap."]
|
#[description = "Check your own or someone else's best record on the last beatmap."]
|
||||||
#[max_args(1)]
|
#[max_args(1)]
|
||||||
pub fn check(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
pub fn check(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
let mut data = ctx.data.write();
|
let bm = cache::get_beatmap(&*ctx.data.read(), msg.channel_id)?;
|
||||||
|
|
||||||
let bm = cache::get_beatmap(&mut *data, msg.channel_id)?;
|
|
||||||
|
|
||||||
match bm {
|
match bm {
|
||||||
None => {
|
None => {
|
||||||
|
@ -282,10 +271,13 @@ pub fn check(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult
|
||||||
Some(bm) => {
|
Some(bm) => {
|
||||||
let b = &bm.0;
|
let b = &bm.0;
|
||||||
let m = bm.1;
|
let m = bm.1;
|
||||||
let user =
|
let user = UsernameArg::to_user_id_query(
|
||||||
UsernameArg::to_user_id_query(args.single::<UsernameArg>().ok(), &mut *data, msg)?;
|
args.single::<UsernameArg>().ok(),
|
||||||
|
&*ctx.data.read(),
|
||||||
|
msg,
|
||||||
|
)?;
|
||||||
|
|
||||||
let osu = data.get::<http::Osu>().unwrap().clone();
|
let osu = ctx.data.read().get::<http::Osu>().unwrap().clone();
|
||||||
|
|
||||||
let user = osu
|
let user = osu
|
||||||
.user(user, |f| f)?
|
.user(user, |f| f)?
|
||||||
|
@ -319,10 +311,10 @@ pub fn top(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
.map(|ModeArg(t)| t)
|
.map(|ModeArg(t)| t)
|
||||||
.unwrap_or(Mode::Std);
|
.unwrap_or(Mode::Std);
|
||||||
|
|
||||||
let mut data = ctx.data.write();
|
let user =
|
||||||
let user = UsernameArg::to_user_id_query(args.single::<UsernameArg>().ok(), &mut *data, msg)?;
|
UsernameArg::to_user_id_query(args.single::<UsernameArg>().ok(), &*ctx.data.read(), msg)?;
|
||||||
|
|
||||||
let osu: OsuClient = data.get::<http::Osu>().unwrap().clone();
|
let osu: OsuClient = ctx.data.read().get::<http::Osu>().unwrap().clone();
|
||||||
let user = osu
|
let user = osu
|
||||||
.user(user, |f| f.mode(mode))?
|
.user(user, |f| f.mode(mode))?
|
||||||
.ok_or(Error::from("User not found"))?;
|
.ok_or(Error::from("User not found"))?;
|
||||||
|
@ -352,15 +344,15 @@ pub fn top(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// Save the beatmap...
|
// Save the beatmap...
|
||||||
cache::save_beatmap(&mut *data, msg.channel_id, &beatmap)?;
|
cache::save_beatmap(&*ctx.data.read(), msg.channel_id, &beatmap)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_user(ctx: &mut Context, msg: &Message, mut args: Args, mode: Mode) -> CommandResult {
|
fn get_user(ctx: &mut Context, msg: &Message, mut args: Args, mode: Mode) -> CommandResult {
|
||||||
let mut data = ctx.data.write();
|
let user =
|
||||||
let user = UsernameArg::to_user_id_query(args.single::<UsernameArg>().ok(), &mut *data, msg)?;
|
UsernameArg::to_user_id_query(args.single::<UsernameArg>().ok(), &*ctx.data.read(), msg)?;
|
||||||
let osu = data.get::<http::Osu>().unwrap().clone();
|
let osu = ctx.data.read().get::<http::Osu>().unwrap().clone();
|
||||||
let user = osu.user(user, |f| f.mode(mode))?;
|
let user = osu.user(user, |f| f.mode(mode))?;
|
||||||
match user {
|
match user {
|
||||||
Some(u) => {
|
Some(u) => {
|
||||||
|
|
|
@ -63,15 +63,15 @@ pub fn setup_db(client: &mut Client) -> Result<(), Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DBWriteGuard<'a, T>(&'a mut FileDatabase<T, Ron>)
|
pub struct DBWriteGuard<'a, T>(&'a FileDatabase<T, Ron>)
|
||||||
where
|
where
|
||||||
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned;
|
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned;
|
||||||
|
|
||||||
impl<'a, T> From<&'a mut FileDatabase<T, Ron>> for DBWriteGuard<'a, T>
|
impl<'a, T> From<&'a FileDatabase<T, Ron>> for DBWriteGuard<'a, T>
|
||||||
where
|
where
|
||||||
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
|
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
|
||||||
{
|
{
|
||||||
fn from(v: &'a mut FileDatabase<T, Ron>) -> Self {
|
fn from(v: &'a FileDatabase<T, Ron>) -> Self {
|
||||||
DBWriteGuard(v)
|
DBWriteGuard(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,9 +83,7 @@ where
|
||||||
pub fn borrow(&self) -> Result<std::sync::RwLockReadGuard<T>, rustbreak::RustbreakError> {
|
pub fn borrow(&self) -> Result<std::sync::RwLockReadGuard<T>, rustbreak::RustbreakError> {
|
||||||
(*self).0.borrow_data()
|
(*self).0.borrow_data()
|
||||||
}
|
}
|
||||||
pub fn borrow_mut(
|
pub fn borrow_mut(&self) -> Result<std::sync::RwLockWriteGuard<T>, rustbreak::RustbreakError> {
|
||||||
&mut self,
|
|
||||||
) -> Result<std::sync::RwLockWriteGuard<T>, rustbreak::RustbreakError> {
|
|
||||||
(*self).0.borrow_data_mut()
|
(*self).0.borrow_data_mut()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,7 +110,7 @@ impl ServerSoftBans {
|
||||||
// Create a new, implemented role.
|
// Create a new, implemented role.
|
||||||
pub fn new_implemented(role: RoleId) -> ServerSoftBans {
|
pub fn new_implemented(role: RoleId) -> ServerSoftBans {
|
||||||
ServerSoftBans::Implemented(ImplementedSoftBans {
|
ServerSoftBans::Implemented(ImplementedSoftBans {
|
||||||
role: role,
|
role,
|
||||||
periodical_bans: HashMap::new(),
|
periodical_bans: HashMap::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue