Skip to content

Commit

Permalink
Remove Into<Id> and AsRef<str>
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Jan 10, 2024
1 parent f4d84d8 commit b165f32
Show file tree
Hide file tree
Showing 40 changed files with 301 additions and 496 deletions.
2 changes: 1 addition & 1 deletion examples/e05_command_framework/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ async fn say(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
// We do not want to clean channel mentions as they do not ping users.
.clean_channel(false);

x = content_safe(&guild, x, settings, &msg.mentions);
x = content_safe(&guild, &x, settings, &msg.mentions);
}

msg.channel_id.say(&ctx.http, x).await?;
Expand Down
2 changes: 1 addition & 1 deletion examples/e11_gateway_intents/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn main() {
let intents =
GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT;
// Build our client.
let mut client = Client::builder(token, intents)
let mut client = Client::builder(&token, intents)
.event_handler(Handler)
.await
.expect("Error creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/e14_slash_commands/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async fn main() {
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");

// Build our client.
let mut client = Client::builder(token, GatewayIntents::empty())
let mut client = Client::builder(&token, GatewayIntents::empty())
.event_handler(Handler)
.await
.expect("Error creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/e15_simple_dashboard/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(token, intents)
let mut client = Client::builder(&token, intents)
.event_handler(Handler)
.framework(framework)
.type_map_insert::<RillRateComponents>(components)
Expand Down
2 changes: 1 addition & 1 deletion examples/e17_message_components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async fn main() {
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(token, intents)
let mut client = Client::builder(&token, intents)
.event_handler(Handler)
.await
.expect("Error creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/testing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,5 +400,5 @@ async fn main() -> Result<(), serenity::Error> {
env_logger::init();
let token = std::env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
Client::builder(token, intents).event_handler(Handler).await?.start().await
Client::builder(&token, intents).event_handler(Handler).await?.start().await
}
8 changes: 4 additions & 4 deletions src/builder/bot_auth_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ impl<'a> CreateBotAuthParameters<'a> {
}

/// Specify the client Id of your application.
pub fn client_id(mut self, client_id: impl Into<ApplicationId>) -> Self {
self.client_id = Some(client_id.into());
pub fn client_id(mut self, client_id: ApplicationId) -> Self {
self.client_id = Some(client_id);
self
}

Expand Down Expand Up @@ -107,8 +107,8 @@ impl<'a> CreateBotAuthParameters<'a> {
}

/// Specify the Id of the guild to prefill the dropdown picker for the user.
pub fn guild_id(mut self, guild_id: impl Into<GuildId>) -> Self {
self.guild_id = Some(guild_id.into());
pub fn guild_id(mut self, guild_id: GuildId) -> Self {
self.guild_id = Some(guild_id);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/create_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ impl<'a> CreateChannel<'a> {
/// Only for [`ChannelType::Text`], [`ChannelType::Voice`], [`ChannelType::News`],
/// [`ChannelType::Stage`], [`ChannelType::Forum`]
#[doc(alias = "parent_id")]
pub fn category(mut self, id: impl Into<ChannelId>) -> Self {
self.parent_id = Some(id.into());
pub fn category(mut self, id: ChannelId) -> Self {
self.parent_id = Some(id);
self
}

Expand Down
8 changes: 4 additions & 4 deletions src/builder/create_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ impl<'a> CreateMessage<'a> {
///
/// **Note**: This will replace all existing stickers. Use [`Self::add_sticker_id()`] to keep
/// existing stickers.
pub fn sticker_id(self, sticker_id: impl Into<StickerId>) -> Self {
self.sticker_ids(vec![sticker_id.into()])
pub fn sticker_id(self, sticker_id: StickerId) -> Self {
self.sticker_ids(vec![sticker_id])
}

/// Sets a list of sticker IDs to include in the message.
Expand All @@ -246,8 +246,8 @@ impl<'a> CreateMessage<'a> {
///
/// **Note**: This will keep all existing stickers. Use [`Self::sticker_id()`] to replace
/// existing sticker.
pub fn add_sticker_id(mut self, sticker_id: impl Into<StickerId>) -> Self {
self.sticker_ids.to_mut().push(sticker_id.into());
pub fn add_sticker_id(mut self, sticker_id: StickerId) -> Self {
self.sticker_ids.to_mut().push(sticker_id);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/create_scheduled_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ impl<'a> CreateScheduledEvent<'a> {

/// Sets the channel id of the scheduled event. Required if [`Self::kind`] is
/// [`ScheduledEventType::StageInstance`] or [`ScheduledEventType::Voice`].
pub fn channel_id<C: Into<ChannelId>>(mut self, channel_id: C) -> Self {
self.channel_id = Some(channel_id.into());
pub fn channel_id(mut self, channel_id: ChannelId) -> Self {
self.channel_id = Some(channel_id);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ impl<'a> EditGuild<'a> {
///
/// **Note**: The current user must be the owner of the guild.
#[inline]
pub fn owner(mut self, user_id: impl Into<UserId>) -> Self {
self.owner_id = Some(user_id.into());
pub fn owner(mut self, user_id: UserId) -> Self {
self.owner_id = Some(user_id);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_guild_welcome_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ impl<'a> CreateGuildWelcomeChannel<'a> {
}

/// The Id of the channel to show.
pub fn id(mut self, id: impl Into<ChannelId>) -> Self {
self.channel_id = id.into();
pub fn id(mut self, id: ChannelId) -> Self {
self.channel_id = id;
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_guild_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ impl<'a> EditGuildWidget<'a> {
}

/// The server description shown in the welcome screen.
pub fn channel_id(mut self, id: impl Into<ChannelId>) -> Self {
self.channel_id = Some(id.into());
pub fn channel_id(mut self, id: ChannelId) -> Self {
self.channel_id = Some(id);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ impl<'a> EditMember<'a> {
///
/// [Move Members]: Permissions::MOVE_MEMBERS
#[inline]
pub fn voice_channel(mut self, channel_id: impl Into<ChannelId>) -> Self {
self.channel_id = Some(Some(channel_id.into()));
pub fn voice_channel(mut self, channel_id: ChannelId) -> Self {
self.channel_id = Some(Some(channel_id));
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_scheduled_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl<'a> EditScheduledEvent<'a> {
/// [`kind`]: EditScheduledEvent::kind
/// [`Voice`]: ScheduledEventType::Voice
/// [`External`]: ScheduledEventType::External
pub fn channel_id(mut self, channel_id: impl Into<ChannelId>) -> Self {
self.channel_id = Some(Some(channel_id.into()));
pub fn channel_id(mut self, channel_id: ChannelId) -> Self {
self.channel_id = Some(Some(channel_id));
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl<'a> EditWebhook<'a> {
}

/// Set the channel to move the webhook to.
pub fn channel_id(mut self, channel_id: impl Into<ChannelId>) -> Self {
self.channel_id = Some(channel_id.into());
pub fn channel_id(mut self, channel_id: ChannelId) -> Self {
self.channel_id = Some(channel_id);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_webhook_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ impl<'a> EditWebhookMessage<'a> {
/// Edits a message within a given thread. If the provided thread Id doesn't belong to the
/// current webhook, the API will return an error.
#[inline]
pub fn in_thread(mut self, thread_id: impl Into<ChannelId>) -> Self {
self.thread_id = Some(thread_id.into());
pub fn in_thread(mut self, thread_id: ChannelId) -> Self {
self.thread_id = Some(thread_id);
self
}

Expand Down
8 changes: 4 additions & 4 deletions src/builder/execute_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,20 @@ impl<'a> ExecuteWebhook<'a> {
/// ```rust,no_run
/// # use serenity::builder::ExecuteWebhook;
/// # use serenity::http::Http;
/// # use serenity::model::webhook::Webhook;
/// # use serenity::model::{id::ChannelId, webhook::Webhook};
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http: Http = unimplemented!();
/// let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
/// let mut webhook = Webhook::from_url(&http, url).await?;
///
/// let builder = ExecuteWebhook::new().in_thread(12345678).content("test");
/// let builder = ExecuteWebhook::new().in_thread(ChannelId::new(12345678)).content("test");
/// webhook.execute(&http, false, builder).await?;
/// # Ok(())
/// # }
/// ```
pub fn in_thread(mut self, thread_id: impl Into<ChannelId>) -> Self {
self.thread_id = Some(thread_id.into());
pub fn in_thread(mut self, thread_id: ChannelId) -> Self {
self.thread_id = Some(thread_id);
self
}

Expand Down
12 changes: 6 additions & 6 deletions src/builder/get_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ impl GetMessages {
}

/// Indicates to retrieve the messages after a specific message, given its Id.
pub fn after(mut self, message_id: impl Into<MessageId>) -> Self {
self.search_filter = Some(SearchFilter::After(message_id.into()));
pub fn after(mut self, message_id: MessageId) -> Self {
self.search_filter = Some(SearchFilter::After(message_id));
self
}

/// Indicates to retrieve the messages _around_ a specific message, in other words in either
/// direction from the message in time.
pub fn around(mut self, message_id: impl Into<MessageId>) -> Self {
self.search_filter = Some(SearchFilter::Around(message_id.into()));
pub fn around(mut self, message_id: MessageId) -> Self {
self.search_filter = Some(SearchFilter::Around(message_id));
self
}

/// Indicates to retrieve the messages before a specific message, given its Id.
pub fn before(mut self, message_id: impl Into<MessageId>) -> Self {
self.search_filter = Some(SearchFilter::Before(message_id.into()));
pub fn before(mut self, message_id: MessageId) -> Self {
self.search_filter = Some(SearchFilter::Before(message_id));
self
}

Expand Down
65 changes: 19 additions & 46 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,7 @@ impl Cache {

/// Retrieves a [`GuildChannel`] from the cache based on the given Id.
#[inline]
pub fn channel<C: Into<ChannelId>>(&self, id: C) -> Option<GuildChannelRef<'_>> {
self._channel(id.into())
}

fn _channel(&self, id: ChannelId) -> Option<GuildChannelRef<'_>> {
pub fn channel(&self, id: ChannelId) -> Option<GuildChannelRef<'_>> {
let guild_id = *self.channels.get(&id)?;
let guild_ref = self.guilds.get(&guild_id)?;
let channel = guild_ref.try_map(|g| g.channels.get(&id)).ok();
Expand Down Expand Up @@ -389,17 +385,16 @@ impl Cache {
/// Find all messages by user ID 8 in channel ID 7:
///
/// ```rust,no_run
/// # use serenity::model::id::ChannelId;
/// #
/// # let cache: serenity::cache::Cache = todo!();
/// let messages_in_channel = cache.channel_messages(7);
/// let messages_in_channel = cache.channel_messages(ChannelId::new(7));
/// let messages_by_user = messages_in_channel
/// .as_ref()
/// .map(|msgs| msgs.values().filter(|m| m.author.id == 8).collect::<Vec<_>>());
/// ```
pub fn channel_messages(
&self,
channel_id: impl Into<ChannelId>,
) -> Option<ChannelMessagesRef<'_>> {
self.messages.get(&channel_id.into()).map(CacheRef::from_ref)
pub fn channel_messages(&self, channel_id: ChannelId) -> Option<ChannelMessagesRef<'_>> {
self.messages.get(&channel_id).map(CacheRef::from_ref)
}

/// Gets a reference to a guild from the cache based on the given `id`.
Expand All @@ -410,19 +405,16 @@ impl Cache {
///
/// ```rust,no_run
/// # use serenity::cache::Cache;
/// # use serenity::model::id::GuildId;
/// #
/// # let cache = Cache::default();
/// // assuming the cache is in scope, e.g. via `Context`
/// if let Some(guild) = cache.guild(7) {
/// if let Some(guild) = cache.guild(GuildId::new(7)) {
/// println!("Guild name: {}", guild.name);
/// };
/// ```
#[inline]
pub fn guild<G: Into<GuildId>>(&self, id: G) -> Option<GuildRef<'_>> {
self._guild(id.into())
}

fn _guild(&self, id: GuildId) -> Option<GuildRef<'_>> {
pub fn guild(&self, id: GuildId) -> Option<GuildRef<'_>> {
self.guilds.get(&id).map(CacheRef::from_ref)
}

Expand Down Expand Up @@ -475,12 +467,8 @@ impl Cache {
/// [`EventHandler::message`]: crate::client::EventHandler::message
/// [`members`]: crate::model::guild::Guild::members
#[inline]
pub fn member(
&self,
guild_id: impl Into<GuildId>,
user_id: impl Into<UserId>,
) -> Option<MemberRef<'_>> {
self._member(guild_id.into(), user_id.into())
pub fn member(&self, guild_id: GuildId, user_id: UserId) -> Option<MemberRef<'_>> {
self._member(guild_id, user_id)
}

fn _member(&self, guild_id: GuildId, user_id: UserId) -> Option<MemberRef<'_>> {
Expand All @@ -489,8 +477,8 @@ impl Cache {
}

#[inline]
pub fn guild_roles(&self, guild_id: impl Into<GuildId>) -> Option<GuildRolesRef<'_>> {
self._guild_roles(guild_id.into())
pub fn guild_roles(&self, guild_id: GuildId) -> Option<GuildRolesRef<'_>> {
self._guild_roles(guild_id)
}

fn _guild_roles(&self, guild_id: GuildId) -> Option<GuildRolesRef<'_>> {
Expand All @@ -506,8 +494,8 @@ impl Cache {

/// This method returns all channels from a guild of with the given `guild_id`.
#[inline]
pub fn guild_channels(&self, guild_id: impl Into<GuildId>) -> Option<GuildChannelsRef<'_>> {
self._guild_channels(guild_id.into())
pub fn guild_channels(&self, guild_id: GuildId) -> Option<GuildChannelsRef<'_>> {
self._guild_channels(guild_id)
}

fn _guild_channels(&self, guild_id: GuildId) -> Option<GuildChannelsRef<'_>> {
Expand Down Expand Up @@ -550,15 +538,7 @@ impl Cache {
///
/// [`EventHandler::message`]: crate::client::EventHandler::message
#[inline]
pub fn message<C, M>(&self, channel_id: C, message_id: M) -> Option<MessageRef<'_>>
where
C: Into<ChannelId>,
M: Into<MessageId>,
{
self._message(channel_id.into(), message_id.into())
}

fn _message(&self, channel_id: ChannelId, message_id: MessageId) -> Option<MessageRef<'_>> {
pub fn message(&self, channel_id: ChannelId, message_id: MessageId) -> Option<MessageRef<'_>> {
#[cfg(feature = "temp_cache")]
if let Some(message) = self.temp_messages.get(&message_id) {
return Some(CacheRef::from_arc(message));
Expand All @@ -579,27 +559,20 @@ impl Cache {
/// Retrieve a role from the cache and print its name:
///
/// ```rust,no_run
/// # use serenity::model::id::{GuildId, RoleId};
/// # use serenity::cache::Cache;
/// #
/// # let cache = Cache::default();
/// // assuming the cache is in scope, e.g. via `Context`
/// if let Some(role) = cache.role(7, 77) {
/// if let Some(role) = cache.role(GuildId::new(7), RoleId::new(77)) {
/// println!("Role with Id 77 is called {}", role.name);
/// };
/// ```
///
/// [`Guild`]: crate::model::guild::Guild
/// [`roles`]: crate::model::guild::Guild::roles
#[inline]
pub fn role<G, R>(&self, guild_id: G, role_id: R) -> Option<GuildRoleRef<'_>>
where
G: Into<GuildId>,
R: Into<RoleId>,
{
self._role(guild_id.into(), role_id.into())
}

fn _role(&self, guild_id: GuildId, role_id: RoleId) -> Option<GuildRoleRef<'_>> {
pub fn role(&self, guild_id: GuildId, role_id: RoleId) -> Option<GuildRoleRef<'_>> {
let role = self.guilds.get(&guild_id)?.try_map(|g| g.roles.get(&role_id)).ok()?;
Some(CacheRef::from_mapped_ref(role))
}
Expand Down
Loading

0 comments on commit b165f32

Please sign in to comment.