Skip to content

Commit

Permalink
Split GuildThread from GuildChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Nov 12, 2024
1 parent 47d2e1a commit 4ba0fb5
Show file tree
Hide file tree
Showing 56 changed files with 1,106 additions and 998 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ to-arraystring = "0.2.0"
extract_map = { version = "0.1.0", features = ["serde", "iter_mut"] }
aformat = "0.1.3"
bytes = "1.5.0"
ref-cast = "1.0.23"
# Optional dependencies
fxhash = { version = "0.2.1", optional = true }
chrono = { version = "0.4.31", default-features = false, features = ["clock", "serde"], optional = true }
Expand Down
4 changes: 2 additions & 2 deletions examples/e12_parallel_loops/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serenity::builder::{CreateEmbed, CreateMessage};
use serenity::gateway::ActivityData;
use serenity::model::channel::Message;
use serenity::model::gateway::Ready;
use serenity::model::id::{ChannelId, GuildId};
use serenity::model::id::{ChannelOrThreadId, GuildId};
use serenity::prelude::*;

struct Handler {
Expand Down Expand Up @@ -85,7 +85,7 @@ async fn log_system_load(ctx: &Context) {
false,
);
let builder = CreateMessage::new().embed(embed);
let message = ChannelId::new(381926291785383946).send_message(&ctx.http, builder).await;
let message = ChannelOrThreadId::new(381926291785383946).send_message(&ctx.http, builder).await;
if let Err(why) = message {
eprintln!("Error sending message: {why:?}");
};
Expand Down
13 changes: 8 additions & 5 deletions examples/testing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
model_type_sizes::print_ranking();
} else if msg.content == "auditlog" {
// Test special characters in audit log reason
msg.channel_id
channel_id
.expect_channel()
.edit(
&ctx.http,
EditChannel::new().name("new-channel-name").audit_log_reason("hello\nworld\n🙂"),
Expand Down Expand Up @@ -153,6 +154,7 @@ async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
guild_id.ban(&ctx.http, user_id, 0, None).await?;
} else if msg.content == "createtags" {
channel_id
.expect_channel()
.edit(
&ctx.http,
EditChannel::new().available_tags(vec![
Expand All @@ -163,9 +165,10 @@ async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
.await?;
} else if msg.content == "assigntags" {
let forum_id = msg.guild_channel(&ctx).await?.parent_id.unwrap();
let forum = forum_id.to_guild_channel(&ctx, msg.guild_id).await?;
let forum = forum_id.widen().to_guild_channel(&ctx, msg.guild_id).await?;
channel_id
.edit_thread(
.expect_thread()
.edit(
&ctx.http,
EditThread::new()
.applied_tags(forum.available_tags.iter().map(|t| t.id).collect::<Vec<_>>()),
Expand Down Expand Up @@ -197,7 +200,7 @@ async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
msg.author.id.dm(&ctx.http, builder).await?;
} else if let Some(channel) = msg.content.strip_prefix("movetorootandback") {
let mut channel = {
let channel_id = channel.trim().parse::<ChannelId>().unwrap();
let channel_id = channel.trim().parse::<ChannelOrThreadId>().unwrap();
channel_id.to_guild_channel(&ctx, msg.guild_id).await.unwrap()
};

Expand All @@ -207,7 +210,7 @@ async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
} else if msg.content == "channelperms" {
let guild = guild_id.to_guild_cached(&ctx.cache).unwrap().clone();
let perms = guild.user_permissions_in(
guild.channels.get(&channel_id).unwrap(),
guild.channels.get(&channel_id.expect_channel()).unwrap(),
&*guild.member(&ctx.http, msg.author.id).await?,
);
channel_id.say(&ctx.http, format!("{:?}", perms)).await?;
Expand Down
6 changes: 4 additions & 2 deletions examples/testing/src/model_type_sizes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ pub fn print_ranking() {
("ModalInteraction", std::mem::size_of::<ModalInteraction>()),
("ModalInteractionData", std::mem::size_of::<ModalInteractionData>()),
("Options", std::mem::size_of::<Options>()),
("PartialChannel", std::mem::size_of::<PartialChannel>()),
("InteractionChannel", std::mem::size_of::<InteractionChannel>()),
("InteractionGuildChannel", std::mem::size_of::<InteractionGuildChannel>()),
("InteractionGuildThread", std::mem::size_of::<InteractionGuildThread>()),
("PartialCurrentApplicationInfo", std::mem::size_of::<PartialCurrentApplicationInfo>()),
("PartialGuild", std::mem::size_of::<PartialGuild>()),
("PartialGuildChannel", std::mem::size_of::<PartialGuildChannel>()),
("PartialGuildThread", std::mem::size_of::<PartialGuildThread>()),
("PartialMember", std::mem::size_of::<PartialMember>()),
("PermissionOverwrite", std::mem::size_of::<PermissionOverwrite>()),
("Permissions", std::mem::size_of::<Permissions>()),
Expand Down
2 changes: 1 addition & 1 deletion src/builder/create_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub enum CreateSelectMenuKind<'a> {
},
Channel {
channel_types: Option<Cow<'a, [ChannelType]>>,
default_channels: Option<Cow<'a, [ChannelId]>>,
default_channels: Option<Cow<'a, [ChannelOrThreadId]>>,
},
}

Expand Down
2 changes: 1 addition & 1 deletion src/builder/create_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl<'a> CreateMessage<'a> {
pub async fn execute(
mut self,
http: &Http,
channel_id: ChannelId,
channel_id: ChannelOrThreadId,
guild_id: Option<GuildId>,
) -> Result<Message> {
self.check_length()?;
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 @@ -131,8 +131,8 @@ impl<'a> CreateScheduledEvent<'a> {
///
/// [Create Events]: Permissions::CREATE_EVENTS
#[cfg(feature = "http")]
pub async fn execute(self, http: &Http, channel_id: GuildId) -> Result<ScheduledEvent> {
http.create_scheduled_event(channel_id, &self, self.audit_log_reason).await
pub async fn execute(self, http: &Http, guild_id: GuildId) -> Result<ScheduledEvent> {
http.create_scheduled_event(guild_id, &self, self.audit_log_reason).await
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/builder/edit_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ impl<'a> EditChannel<'a> {
.await?;
}

http.edit_channel(channel_id, &self, self.audit_log_reason).await
http.edit_channel(channel_id.widen(), &self, self.audit_log_reason)
.await?
.guild()
.ok_or(Error::Model(ModelError::InvalidChannelType))
}
}
6 changes: 3 additions & 3 deletions src/builder/edit_guild_welcome_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ impl<'a> EditGuildWelcomeScreen<'a> {
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateGuildWelcomeChannel<'a> {
channel_id: ChannelId,
channel_id: ChannelOrThreadId,
emoji_name: Option<String>,
emoji_id: Option<EmojiId>,
description: Cow<'a, str>,
}

impl<'a> CreateGuildWelcomeChannel<'a> {
pub fn new(channel_id: ChannelId, description: impl Into<Cow<'a, str>>) -> Self {
pub fn new(channel_id: ChannelOrThreadId, description: impl Into<Cow<'a, str>>) -> Self {
Self {
channel_id,
emoji_id: None,
Expand All @@ -99,7 +99,7 @@ impl<'a> CreateGuildWelcomeChannel<'a> {
}

/// The Id of the channel to show.
pub fn id(mut self, id: ChannelId) -> Self {
pub fn id(mut self, id: ChannelOrThreadId) -> Self {
self.channel_id = id;
self
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/edit_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl<'a> EditMessage<'a> {
pub async fn execute(
mut self,
cache_http: impl CacheHttp,
channel_id: ChannelId,
channel_id: ChannelOrThreadId,
message_id: MessageId,
user_id: Option<UserId>,
) -> Result<Message> {
Expand Down
8 changes: 6 additions & 2 deletions src/builder/edit_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ impl<'a> EditThread<'a> {
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
/// Returns [`ModelError::InvalidChannelType`] if the `ThreadId` is not identifying a thread.
#[cfg(feature = "http")]
pub async fn execute(self, http: &Http, channel_id: ChannelId) -> Result<GuildChannel> {
http.edit_thread(channel_id, &self, self.audit_log_reason).await
pub async fn execute(self, http: &Http, thread_id: ThreadId) -> Result<GuildThread> {
http.edit_channel(thread_id.widen(), &self, self.audit_log_reason)
.await?
.thread()
.ok_or(Error::Model(ModelError::InvalidChannelType))
}
}
4 changes: 2 additions & 2 deletions src/builder/edit_webhook_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct EditWebhookMessage<'a> {
pub(crate) attachments: Option<EditAttachments<'a>>,

#[serde(skip)]
thread_id: Option<ChannelId>,
thread_id: Option<ThreadId>,
}

impl<'a> EditWebhookMessage<'a> {
Expand All @@ -55,7 +55,7 @@ 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.
pub fn in_thread(mut self, thread_id: ChannelId) -> Self {
pub fn in_thread(mut self, thread_id: ThreadId) -> 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 @@ -76,7 +76,7 @@ pub struct ExecuteWebhook<'a> {
attachments: EditAttachments<'a>,

#[serde(skip)]
thread_id: Option<ChannelId>,
thread_id: Option<ThreadId>,
}

impl<'a> ExecuteWebhook<'a> {
Expand Down Expand Up @@ -159,19 +159,19 @@ impl<'a> ExecuteWebhook<'a> {
/// ```rust,no_run
/// # use serenity::builder::ExecuteWebhook;
/// # use serenity::http::Http;
/// # use serenity::model::{id::ChannelId, webhook::Webhook};
/// # use serenity::model::{id::ThreadId, 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(ChannelId::new(12345678)).content("test");
/// let builder = ExecuteWebhook::new().in_thread(ThreadId::new(12345678)).content("test");
/// webhook.execute(&http, false, builder).await?;
/// # Ok(())
/// # }
/// ```
pub fn in_thread(mut self, thread_id: ChannelId) -> Self {
pub fn in_thread(mut self, thread_id: ThreadId) -> Self {
self.thread_id = Some(thread_id);
self
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/get_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl GetMessages {
pub async fn execute(
self,
cache_http: impl CacheHttp,
channel_id: ChannelId,
channel_id: ChannelOrThreadId,
) -> Result<Vec<Message>> {
let http = cache_http.http();
let search_filter = self.search_filter.map(Into::into);
Expand Down
Loading

0 comments on commit 4ba0fb5

Please sign in to comment.