Update serenity and make prelude compile

This commit is contained in:
Natsu Kagami 2024-02-17 23:33:21 +01:00 committed by Natsu Kagami
parent a6d1259362
commit 3f115eaab0
8 changed files with 363 additions and 32 deletions

View file

@ -19,6 +19,6 @@ flume = "0.10.13"
dashmap = "5.3.4"
[dependencies.serenity]
version = "0.11.2"
version = "0.12"
default-features = true
features = ["collector"]

View file

@ -5,22 +5,40 @@ use futures_util::{
stream::{FuturesUnordered, StreamExt},
};
use serenity::{
client::Cache,
framework::standard::{
macros::{command, group},
Args, CommandResult,
},
http::CacheHttp,
http::{CacheHttp, Http},
model::{
channel::Message,
id::{ChannelId, GuildId, UserId},
},
prelude::*,
utils::MessageBuilder,
CacheAndHttp,
};
use std::{collections::HashMap, sync::Arc};
use youmubot_db::DB;
#[derive(Debug, Clone)]
pub struct CacheAndHttp(Arc<Cache>, Arc<Http>);
impl CacheAndHttp {
pub fn from_client(client: &Client) -> Self {
Self(client.cache.clone(), client.http.clone())
}
}
impl CacheHttp for CacheAndHttp {
fn cache(&self) -> Option<&Arc<Cache>> {
Some(&self.0)
}
fn http(&self) -> &Http {
&*self.1
}
}
/// A list of assigned channels for an announcer.
pub(crate) type AnnouncerChannels = DB<HashMap<String, HashMap<GuildId, ChannelId>>>;
@ -39,7 +57,7 @@ pub trait Announcer: Send {
/// Errors returned from this function gets ignored and logged down.
async fn updates(
&mut self,
c: Arc<CacheAndHttp>,
c: CacheAndHttp,
d: AppData,
channels: MemberToChannels,
) -> Result<()>;
@ -76,7 +94,7 @@ impl MemberToChannels {
///
/// This struct manages the list of all Announcers, firing them in a certain interval.
pub struct AnnouncerHandler {
cache_http: Arc<CacheAndHttp>,
cache_http: CacheAndHttp,
data: AppData,
announcers: HashMap<&'static str, RwLock<Box<dyn Announcer + Send + Sync>>>,
}
@ -91,7 +109,7 @@ impl AnnouncerHandler {
/// Create a new instance of the handler.
pub fn new(client: &serenity::Client) -> Self {
Self {
cache_http: client.cache_and_http.clone(),
cache_http: CacheAndHttp(client.cache.clone(), client.http.clone()),
data: client.data.clone(),
announcers: HashMap::new(),
}
@ -135,7 +153,7 @@ impl AnnouncerHandler {
/// Run the announcing sequence on a certain announcer.
async fn announce(
data: AppData,
cache_http: Arc<CacheAndHttp>,
cache_http: CacheAndHttp,
key: &'static str,
announcer: &'_ RwLock<Box<dyn Announcer + Send + Sync>>,
) -> Result<()> {
@ -243,7 +261,11 @@ pub async fn register_announcer(ctx: &Context, m: &Message, mut args: Args) -> C
.await?;
return Ok(());
}
let guild = m.guild(ctx).expect("Guild-only command");
let guild = m
.guild_id
.expect("Guild-only command")
.to_partial_guild(&ctx)
.await?;
let channel = m.channel_id.to_channel(&ctx).await?;
AnnouncerChannels::open(&data)
.borrow_mut()?
@ -258,7 +280,7 @@ pub async fn register_announcer(ctx: &Context, m: &Message, mut args: Args) -> C
.push(" has been activated for server ")
.push_bold_safe(&guild.name)
.push(" on channel ")
.push_bold_safe(channel)
.push_bold_safe(channel.mention().to_string())
.build(),
)
.await?;
@ -286,7 +308,11 @@ pub async fn remove_announcer(ctx: &Context, m: &Message, mut args: Args) -> Com
.await?;
return Ok(());
}
let guild = m.guild(ctx).expect("Guild-only command");
let guild = m
.guild_id
.expect("Guild-only command")
.to_partial_guild(&ctx)
.await?;
AnnouncerChannels::open(&data)
.borrow_mut()?
.entry(key.clone())

View file

@ -1,7 +1,8 @@
use crate::{Context, Result};
use futures_util::{future::Future, StreamExt};
use futures_util::{future::Future, StreamExt as _};
use serenity::{
collector::ReactionAction,
builder::CreateMessage,
collector,
model::{
channel::{Message, Reaction, ReactionType},
id::ChannelId,
@ -35,7 +36,7 @@ pub trait Paginate: Send + Sized {
page: u8,
ctx: &Context,
message: &mut Message,
reaction: &ReactionAction,
reaction: &Reaction,
) -> Result<Option<u8>> {
handle_pagination_reaction(page, self, ctx, message, reaction)
.await
@ -91,7 +92,10 @@ pub async fn paginate(
timeout: std::time::Duration,
) -> Result<()> {
let message = channel
.send_message(&ctx, |e| e.content("Youmu is loading the first page..."))
.send_message(
&ctx,
CreateMessage::new().content("Youmu is loading the first page..."),
)
.await?;
paginate_with_first_message(pager, ctx, message, timeout).await
}
@ -137,7 +141,18 @@ async fn paginate_with_first_message(
rs
};
// Build a reaction collector
let mut reaction_collector = message.await_reactions(ctx).removed(true).build();
let mut reaction_collector = {
// message.await_reactions(ctx).removed(true).build();
let message_id = message.id;
collector::collect(&ctx.shard, move |event| {
match event {
serenity::all::Event::ReactionAdd(r) => Some(r.reaction.clone()),
serenity::all::Event::ReactionRemove(r) => Some(r.reaction.clone()),
_ => None,
}
.filter(|r| r.message_id == message_id)
})
};
let mut page = 0;
// Loop the handler function.
@ -201,11 +216,8 @@ pub async fn handle_pagination_reaction(
pager: &mut impl Paginate,
ctx: &Context,
message: &mut Message,
reaction: &ReactionAction,
reaction: &Reaction,
) -> Result<u8> {
let reaction = match reaction {
ReactionAction::Added(v) | ReactionAction::Removed(v) => v,
};
let pages = pager.len();
let fast = pages.map(|v| v / 10).unwrap_or(5).max(5) as u8;
match &reaction.emoji {