update poise

This commit is contained in:
jude 2023-06-18 10:47:31 +01:00
parent c8c1a171d4
commit a66db37b33
9 changed files with 63 additions and 59 deletions

8
Cargo.lock generated
View File

@ -1967,9 +1967,9 @@ checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
[[package]] [[package]]
name = "poise" name = "poise"
version = "0.4.1" version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aee439543df35482730552e7c9ed0c45a5f1d521548e6c0249967c4ba8828f60" checksum = "6d591af1c934c29adda172665f69b837e642d4fee85598baffb95dd98110467d"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"derivative", "derivative",
@ -1986,9 +1986,9 @@ dependencies = [
[[package]] [[package]]
name = "poise_macros" name = "poise_macros"
version = "0.4.0" version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d21213ff7aeef5ab69729a5cddfb351a84a9bf3dadf9f470032440d43746c2" checksum = "40270099e1527efae99fdc0609d397e76310b529d4980ad38ab14d81803ca0fa"
dependencies = [ dependencies = [
"darling", "darling",
"proc-macro2", "proc-macro2",

View File

@ -1,13 +1,13 @@
[package] [package]
name = "reminder-rs" name = "reminder-rs"
version = "1.6.13" version = "1.6.14"
authors = ["Jude Southworth <judesouthworth@pm.me>"] authors = ["Jude Southworth <judesouthworth@pm.me>"]
edition = "2021" edition = "2021"
license = "AGPL-3.0 only" license = "AGPL-3.0 only"
description = "Reminder Bot for Discord, now in Rust" description = "Reminder Bot for Discord, now in Rust"
[dependencies] [dependencies]
poise = "0.4" poise = "0.5.5"
dotenv = "0.15" dotenv = "0.15"
tokio = { version = "1", features = ["process", "full"] } tokio = { version = "1", features = ["process", "full"] }
reqwest = "0.11" reqwest = "0.11"

View File

@ -6,8 +6,8 @@ use crate::{models::CtxData, Context, Error, THEME_COLOR};
fn footer( fn footer(
ctx: Context<'_>, ctx: Context<'_>,
) -> impl FnOnce(&mut serenity::CreateEmbedFooter) -> &mut serenity::CreateEmbedFooter { ) -> impl FnOnce(&mut serenity::CreateEmbedFooter) -> &mut serenity::CreateEmbedFooter {
let shard_count = ctx.discord().cache.shard_count(); let shard_count = ctx.serenity_context().cache.shard_count();
let shard = ctx.discord().shard_id; let shard = ctx.serenity_context().shard_id;
move |f| { move |f| {
f.text(format!( f.text(format!(

View File

@ -2,6 +2,7 @@ use std::{collections::HashSet, string::ToString};
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{DateTime, NaiveDateTime, Utc};
use chrono_tz::Tz; use chrono_tz::Tz;
use log::warn;
use num_integer::Integer; use num_integer::Integer;
use poise::{ use poise::{
serenity_prelude::{ serenity_prelude::{
@ -215,7 +216,7 @@ pub async fn look(
}), }),
}; };
let channel_opt = ctx.channel_id().to_channel_cached(&ctx.discord()); let channel_opt = ctx.channel_id().to_channel_cached(&ctx);
let channel_id = if let Some(Channel::Guild(channel)) = channel_opt { let channel_id = if let Some(Channel::Guild(channel)) = channel_opt {
if Some(channel.guild_id) == ctx.guild_id() { if Some(channel.guild_id) == ctx.guild_id() {
@ -227,8 +228,7 @@ pub async fn look(
ctx.channel_id() ctx.channel_id()
}; };
let channel_name = let channel_name = if let Some(Channel::Guild(channel)) = channel_id.to_channel_cached(&ctx) {
if let Some(Channel::Guild(channel)) = channel_id.to_channel_cached(&ctx.discord()) {
Some(channel.name) Some(channel.name)
} else { } else {
None None
@ -294,8 +294,7 @@ pub async fn delete(ctx: Context<'_>) -> Result<(), Error> {
let timezone = ctx.timezone().await; let timezone = ctx.timezone().await;
let reminders = let reminders =
Reminder::from_guild(&ctx.discord(), &ctx.data().database, ctx.guild_id(), ctx.author().id) Reminder::from_guild(&ctx, &ctx.data().database, ctx.guild_id(), ctx.author().id).await;
.await;
let resp = show_delete_page(&reminders, 0, timezone); let resp = show_delete_page(&reminders, 0, timezone);
@ -585,8 +584,10 @@ pub async fn multiline(
timezone: Option<String>, timezone: Option<String>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let tz = timezone.map(|t| t.parse::<Tz>().ok()).flatten(); let tz = timezone.map(|t| t.parse::<Tz>().ok()).flatten();
let data = ContentModal::execute(ctx).await?; let data_opt = ContentModal::execute(ctx).await?;
match data_opt {
Some(data) => {
create_reminder( create_reminder(
Context::Application(ctx), Context::Application(ctx),
time, time,
@ -598,6 +599,16 @@ pub async fn multiline(
tz, tz,
) )
.await .await
}
None => {
warn!("Unexpected None encountered in /multiline");
Ok(Context::Application(ctx)
.send(|m| m.content("Unexpected error.").ephemeral(true))
.await
.map(|_| ())?)
}
}
} }
/// Create a reminder. Press "+4 more" for other options. Use "/multiline" for multiline content. /// Create a reminder. Press "+4 more" for other options. Use "/multiline" for multiline content.
@ -681,9 +692,9 @@ async fn create_reminder(
}; };
let (processed_interval, processed_expires) = if let Some(repeat) = &interval { let (processed_interval, processed_expires) = if let Some(repeat) = &interval {
if check_subscription(&ctx.discord(), ctx.author().id).await if check_subscription(&ctx, ctx.author().id).await
|| (ctx.guild_id().is_some() || (ctx.guild_id().is_some()
&& check_guild_subscription(&ctx.discord(), ctx.guild_id().unwrap()).await) && check_guild_subscription(&ctx, ctx.guild_id().unwrap()).await)
{ {
( (
parse_duration(repeat) parse_duration(repeat)

View File

@ -47,21 +47,19 @@ async fn macro_check(ctx: Context<'_>) -> bool {
async fn check_self_permissions(ctx: Context<'_>) -> bool { async fn check_self_permissions(ctx: Context<'_>) -> bool {
if let Some(guild) = ctx.guild() { if let Some(guild) = ctx.guild() {
let user_id = ctx.discord().cache.current_user_id(); let user_id = ctx.serenity_context().cache.current_user_id();
let manage_webhooks = guild let manage_webhooks =
.member_permissions(&ctx.discord(), user_id) guild.member_permissions(&ctx, user_id).await.map_or(false, |p| p.manage_webhooks());
.await
.map_or(false, |p| p.manage_webhooks());
let (view_channel, send_messages, embed_links) = ctx let (view_channel, send_messages, embed_links) = ctx
.channel_id() .channel_id()
.to_channel(&ctx.discord()) .to_channel(&ctx)
.await .await
.ok() .ok()
.and_then(|c| { .and_then(|c| {
if let Channel::Guild(channel) = c { if let Channel::Guild(channel) = c {
let perms = channel.permissions_for_user(&ctx.discord(), user_id).ok()?; let perms = channel.permissions_for_user(&ctx, user_id).ok()?;
Some((perms.view_channel(), perms.send_messages(), perms.embed_links())) Some((perms.view_channel(), perms.send_messages(), perms.embed_links()))
} else { } else {

View File

@ -175,7 +175,7 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
], ],
allowed_mentions: None, allowed_mentions: None,
command_check: Some(|ctx| Box::pin(all_checks(ctx))), command_check: Some(|ctx| Box::pin(all_checks(ctx))),
listener: |ctx, event, _framework, data| Box::pin(listener(ctx, event, data)), event_handler: |ctx, event, _framework, data| Box::pin(listener(ctx, event, data)),
..Default::default() ..Default::default()
}; };
@ -201,7 +201,7 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
poise::Framework::builder() poise::Framework::builder()
.token(discord_token) .token(discord_token)
.user_data_setup(move |ctx, _bot, framework| { .setup(move |ctx, _bot, framework| {
Box::pin(async move { Box::pin(async move {
register_application_commands(ctx, framework, None).await.unwrap(); register_application_commands(ctx, framework, None).await.unwrap();

View File

@ -31,11 +31,12 @@ pub trait CtxData {
#[async_trait] #[async_trait]
impl CtxData for Context<'_> { impl CtxData for Context<'_> {
async fn user_data<U: Into<UserId> + Send>(&self, user_id: U) -> Result<UserData, Error> { async fn user_data<U: Into<UserId> + Send>(&self, user_id: U) -> Result<UserData, Error> {
UserData::from_user(user_id, &self.discord(), &self.data().database).await UserData::from_user(user_id, &self.serenity_context(), &self.data().database).await
} }
async fn author_data(&self) -> Result<UserData, Error> { async fn author_data(&self) -> Result<UserData, Error> {
UserData::from_user(&self.author().id, &self.discord(), &self.data().database).await UserData::from_user(&self.author().id, &self.serenity_context(), &self.data().database)
.await
} }
async fn guild_data(&self) -> Option<Result<GuildData, Error>> { async fn guild_data(&self) -> Option<Result<GuildData, Error>> {
@ -52,18 +53,18 @@ impl CtxData for Context<'_> {
async fn channel_data(&self) -> Result<ChannelData, Box<dyn std::error::Error + Sync + Send>> { async fn channel_data(&self) -> Result<ChannelData, Box<dyn std::error::Error + Sync + Send>> {
// If we're in a thread, get the parent channel. // If we're in a thread, get the parent channel.
let recv_channel = self.channel_id().to_channel(&self.discord()).await?; let recv_channel = self.channel_id().to_channel(&self).await?;
let channel = match recv_channel.guild() { let channel = match recv_channel.guild() {
Some(guild_channel) => { Some(guild_channel) => {
if guild_channel.kind == ChannelType::PublicThread { if guild_channel.kind == ChannelType::PublicThread {
guild_channel.parent_id.unwrap().to_channel_cached(&self.discord()).unwrap() guild_channel.parent_id.unwrap().to_channel_cached(&self).unwrap()
} else { } else {
self.channel_id().to_channel_cached(&self.discord()).unwrap() self.channel_id().to_channel_cached(&self).unwrap()
} }
} }
None => self.channel_id().to_channel_cached(&self.discord()).unwrap(), None => self.channel_id().to_channel_cached(&self).unwrap(),
}; };
ChannelData::from_channel(&channel, &self.data().database).await ChannelData::from_channel(&channel, &self.data().database).await

View File

@ -230,17 +230,17 @@ impl<'a> MultiReminderBuilder<'a> {
let thread_id = None; let thread_id = None;
let db_channel_id = match scope { let db_channel_id = match scope {
ReminderScope::User(user_id) => { ReminderScope::User(user_id) => {
if let Ok(user) = UserId(user_id).to_user(&self.ctx.discord()).await { if let Ok(user) = UserId(user_id).to_user(&self.ctx).await {
let user_data = UserData::from_user( let user_data = UserData::from_user(
&user, &user,
&self.ctx.discord(), &self.ctx.serenity_context(),
&self.ctx.data().database, &self.ctx.data().database,
) )
.await .await
.unwrap(); .unwrap();
if let Some(guild_id) = self.guild_id { if let Some(guild_id) = self.guild_id {
if guild_id.member(&self.ctx.discord(), user).await.is_err() { if guild_id.member(&self.ctx, user).await.is_err() {
Err(ReminderError::InvalidTag) Err(ReminderError::InvalidTag)
} else if self.set_by.map_or(true, |i| i != user_data.id) } else if self.set_by.map_or(true, |i| i != user_data.id)
&& !user_data.allowed_dm && !user_data.allowed_dm
@ -257,8 +257,7 @@ impl<'a> MultiReminderBuilder<'a> {
} }
} }
ReminderScope::Channel(channel_id) => { ReminderScope::Channel(channel_id) => {
let channel = let channel = ChannelId(channel_id).to_channel(&self.ctx).await.unwrap();
ChannelId(channel_id).to_channel(&self.ctx.discord()).await.unwrap();
if let Some(mut guild_channel) = channel.clone().guild() { if let Some(mut guild_channel) = channel.clone().guild() {
if Some(guild_channel.guild_id) != self.guild_id { if Some(guild_channel.guild_id) != self.guild_id {
@ -271,7 +270,7 @@ impl<'a> MultiReminderBuilder<'a> {
let parent = guild_channel let parent = guild_channel
.parent_id .parent_id
.unwrap() .unwrap()
.to_channel(&self.ctx.discord()) .to_channel(&self.ctx)
.await .await
.unwrap(); .unwrap();
guild_channel = parent.clone().guild().unwrap(); guild_channel = parent.clone().guild().unwrap();
@ -287,12 +286,7 @@ impl<'a> MultiReminderBuilder<'a> {
if channel_data.webhook_id.is_none() if channel_data.webhook_id.is_none()
|| channel_data.webhook_token.is_none() || channel_data.webhook_token.is_none()
{ {
match create_webhook( match create_webhook(&self.ctx, guild_channel, "Reminder").await
&self.ctx.discord(),
guild_channel,
"Reminder",
)
.await
{ {
Ok(webhook) => { Ok(webhook) => {
channel_data.webhook_id = channel_data.webhook_id =

View File

@ -83,7 +83,7 @@ pub fn send_as_initial_response(
components, components,
ephemeral, ephemeral,
allowed_mentions, allowed_mentions,
reference_message: _, // can't reply to a message in interactions reply: _,
} = data; } = data;
if let Some(content) = content { if let Some(content) = content {