Skip to content

Commit

Permalink
Merge branch 'serenity-rs:current' into current
Browse files Browse the repository at this point in the history
  • Loading branch information
TitaniumBrain authored Jun 15, 2024
2 parents 3687fd8 + c67dde5 commit bb4665c
Show file tree
Hide file tree
Showing 12 changed files with 1,413 additions and 958 deletions.
1,418 changes: 893 additions & 525 deletions .github/Cargo-msrv.lock

Large diffs are not rendered by default.

793 changes: 431 additions & 362 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
authors = ["kangalio <[email protected]>"]
edition = "2018"
edition = "2021"
name = "poise"
version = "0.6.1"
rust-version = "1.74.0"
Expand All @@ -18,11 +18,12 @@ tracing = { version = "0.1.40", features = ["log"] } # warning about weird state
derivative = "2.2.0"
parking_lot = "0.12.1"
trim-in-place = "0.1.7"
indexmap = "2.2.6"

[dependencies.serenity]
default-features = false
features = ["builder", "client", "gateway", "model", "utils", "collector", "framework"]
version = "0.12.0"
version = "0.12.2"

[dev-dependencies]
# For the examples
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ For each bot, there's a list of notable features for you to take inspiration fro
- [Scripty](https://github.com/scripty-bot/scripty) by [@tazz4843](https://github.com/tazz4843): localization, database, voice
- [Etternabot](https://github.com/kangalio/Etternabot) by [@kangalio](https://github.com/kangalio): response transformation, variadic and lazy arguments
- [Rustbot](https://github.com/rust-community-discord/ferrisbot-for-discord) by [@kangalio](https://github.com/kangalio): database, custom prefixes
- [StatPixel](https://github.com/statpixel-rs/statpixel) by [@matteopolak](https://github.com/matteopolak): modals, buttons, dropdowns, database, localization

You're welcome to add your own bot [via a PR](https://github.com/serenity-rs/poise/compare)!

Expand Down
5 changes: 3 additions & 2 deletions src/builtins/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,11 @@ async fn generate_all_commands<U, E>(
ctx: crate::Context<'_, U, E>,
config: &HelpConfiguration<'_>,
) -> Result<String, serenity::Error> {
let mut categories = crate::util::OrderedMap::<Option<&str>, Vec<&crate::Command<U, E>>>::new();
let mut categories = indexmap::IndexMap::<Option<&str>, Vec<&crate::Command<U, E>>>::new();
for cmd in &ctx.framework().options().commands {
categories
.get_or_insert_with(cmd.category.as_deref(), Vec::new)
.entry(cmd.category.as_deref())
.or_default()
.push(cmd);
}

Expand Down
28 changes: 25 additions & 3 deletions src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod paginate;
#[cfg(any(feature = "chrono", feature = "time"))]
pub use paginate::*;

use crate::{serenity_prelude as serenity, CreateReply};
use crate::{serenity::CreateAllowedMentions, serenity_prelude as serenity, CreateReply};

/// An error handler that logs errors either via the [`tracing`] crate or via a Discord message. Set
/// up a logger (e.g. `env_logger::init()`) or a tracing subscriber
Expand Down Expand Up @@ -50,7 +50,18 @@ pub async fn on_error<U, E: std::fmt::Display + std::fmt::Debug>(
crate::FrameworkError::Command { ctx, error } => {
let error = error.to_string();
eprintln!("An error occured in a command: {}", error);
ctx.say(error).await?;

let mentions = CreateAllowedMentions::new()
.everyone(false)
.all_roles(false)
.all_users(false);

ctx.send(
CreateReply::default()
.content(error)
.allowed_mentions(mentions),
)
.await?;
}
crate::FrameworkError::SubcommandRequired { ctx } => {
let subcommands = ctx
Expand Down Expand Up @@ -91,7 +102,18 @@ pub async fn on_error<U, E: std::fmt::Display + std::fmt::Debug>(
} else {
format!("**{}**\n{}", error, usage)
};
ctx.say(response).await?;

let mentions = CreateAllowedMentions::new()
.everyone(false)
.all_roles(false)
.all_users(false);

ctx.send(
CreateReply::default()
.content(response)
.allowed_mentions(mentions),
)
.await?;
}
crate::FrameworkError::CommandStructureMismatch { ctx, description } => {
tracing::error!(
Expand Down
5 changes: 3 additions & 2 deletions src/builtins/pretty_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ async fn pretty_help_all_commands<U, E>(
ctx: crate::Context<'_, U, E>,
config: PrettyHelpConfiguration<'_>,
) -> Result<(), serenity::Error> {
let mut categories = crate::util::OrderedMap::new();
let commands = ctx.framework().options().commands.iter().filter(|cmd| {
!cmd.hide_in_help
&& (cmd.prefix_action.is_some()
|| cmd.slash_action.is_some()
|| (cmd.context_menu_action.is_some() && config.show_context_menu_commands))
});

let mut categories = indexmap::IndexMap::<Option<&str>, Vec<&crate::Command<U, E>>>::new();
for cmd in commands {
categories
.get_or_insert_with(cmd.category.as_deref(), Vec::new)
.entry(cmd.category.as_deref())
.or_default()
.push(cmd);
}

Expand Down
40 changes: 40 additions & 0 deletions src/cooldown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ pub struct CooldownTracker {
member_invocations: HashMap<(serenity::UserId, serenity::GuildId), Instant>,
}

/// Possible types of command cooldowns.
///
/// Currently used for [CooldownTracker::set_last_invocation]
#[non_exhaustive]
pub enum CooldownType {
/// A global cooldown that applies to all users, channels, and guilds.
Global,
/// A cooldown specific to individual users.
User(serenity::UserId),
/// A cooldown that applies to an entire guild.
Guild(serenity::GuildId),
/// A cooldown specific to individual channels.
Channel(serenity::ChannelId),
/// A cooldown specific to individual members within a guild.
Member((serenity::UserId, serenity::GuildId)),
}

/// **Renamed to [`CooldownTracker`]**
pub use CooldownTracker as Cooldowns;

Expand Down Expand Up @@ -122,6 +139,29 @@ impl CooldownTracker {
self.member_invocations.insert((ctx.user_id, guild_id), now);
}
}

/// Sets the last invocation for the specified cooldown bucket.
///
/// This function is not usually needed for regular usage. It was added to allow for extra
/// flexibility in cases where you might want to shorten or lengthen a cooldown after
/// invocation.
pub fn set_last_invocation(&mut self, cooldown_type: CooldownType, instant: Instant) {
match cooldown_type {
CooldownType::Global => self.global_invocation = Some(instant),
CooldownType::User(user_id) => {
self.user_invocations.insert(user_id, instant);
}
CooldownType::Guild(guild_id) => {
self.guild_invocations.insert(guild_id, instant);
}
CooldownType::Channel(channel_id) => {
self.channel_invocations.insert(channel_id, instant);
}
CooldownType::Member(member) => {
self.member_invocations.insert(member, instant);
}
}
}
}

impl<'a> From<&'a serenity::Message> for CooldownContext {
Expand Down
6 changes: 4 additions & 2 deletions src/dispatch/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ async fn check_permissions_and_cooldown_single<'a, U, E>(
}
};

if !channel.is_nsfw() {
return Err(crate::FrameworkError::NsfwOnly { ctx });
if let serenity::Channel::Guild(guild_channel) = channel {
if !guild_channel.nsfw {
return Err(crate::FrameworkError::NsfwOnly { ctx });
}
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use std::sync::Arc;

pub use builder::*;

use crate::{serenity_prelude as serenity, BoxFuture};
use crate::{
serenity_prelude::{self as serenity, TeamMemberRole},
BoxFuture,
};

mod builder;

Expand Down Expand Up @@ -245,9 +248,12 @@ pub async fn insert_owners_from_http(

if let Some(team) = application_info.team {
for member in team.members {
// This `if` currently always evaluates to true but it becomes important once
// Discord implements more team roles than Admin
if member.permissions.iter().any(|p| p == "*") {
// "Owner" is considered anyone with permission to access the token.
if member.membership_state != serenity::MembershipState::Accepted {
continue;
}

if let TeamMemberRole::Admin | TeamMemberRole::Developer = member.role {
owners.insert(member.user.id);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ pub mod reply;
pub mod slash_argument;
pub mod structs;
pub mod track_edits;
mod util;
pub mod macros {
//! Procedural macros used in poise, like [`command`]
#[doc(inline)]
Expand Down
55 changes: 0 additions & 55 deletions src/util.rs

This file was deleted.

0 comments on commit bb4665c

Please sign in to comment.