Skip to content

Commit 23c1aa7

Browse files
committed
CAH setup done
1 parent b78d68c commit 23c1aa7

File tree

20 files changed

+494
-149
lines changed

20 files changed

+494
-149
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.envrc
33
.direnv
44
.env
5+
cards

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
serde = { version = "1.0.126", features = ["derive"] }
10+
serde_json = "1.0.96"
911
tokio = { version = "1.27.0", features = ["full"] }
1012
dotenv_codegen = "0.15.0"
1113
async-trait = "0.1.68"

discord/partial_id/src/lib.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,36 @@ pub fn derive_partial(input: proc_macro::TokenStream) -> proc_macro::TokenStream
4949
}
5050
} else {
5151
quote! {
52-
#ident: Some(src.#ident)
52+
#ident: ::core::option::Option::Some(src.#ident)
53+
}
54+
}
55+
});
56+
57+
let get_field = fields.iter().map(|(vis, ident, ty)| {
58+
if ident.to_string() == "id" {
59+
quote!{}
60+
} else {
61+
let get_ident = Ident::new(&format!("get_{}", ident), Span::call_site());
62+
let get_ident_mut = Ident::new(&format!("get_{}_mut", ident), Span::call_site());
63+
quote! {
64+
#vis async fn #get_ident(&mut self, client: &crate::request::Discord) -> crate::request::Result<&#ty> {
65+
crate::request::Result::Ok(match self.#ident {
66+
::core::option::Option::Some(ref v) => v,
67+
::core::option::Option::None => {
68+
crate::resource::Updatable::update(self, client).await?;
69+
self.#ident.as_ref().unwrap()
70+
}
71+
})
72+
}
73+
#vis async fn #get_ident_mut(&mut self, client: &crate::request::Discord) -> crate::request::Result<&mut #ty> {
74+
crate::request::Result::Ok(match self.#ident {
75+
::core::option::Option::Some(ref mut v) => v,
76+
::core::option::Option::None => {
77+
crate::resource::Updatable::update(self, client).await?;
78+
self.#ident.as_mut().unwrap()
79+
}
80+
})
81+
}
5382
}
5483
}
5584
});
@@ -64,7 +93,7 @@ pub fn derive_partial(input: proc_macro::TokenStream) -> proc_macro::TokenStream
6493
#(#field_var),*
6594
}
6695

67-
impl #impl_generics From<#ty #ty_generics> for #partial_ident #ty_generics
96+
impl #impl_generics ::core::convert::From<#ty #ty_generics> for #partial_ident #ty_generics
6897
#where_clause
6998
{
7099
fn from(src: #ty #ty_generics) -> #partial_ident #ty_generics {
@@ -73,6 +102,12 @@ pub fn derive_partial(input: proc_macro::TokenStream) -> proc_macro::TokenStream
73102
}
74103
}
75104
}
105+
106+
impl #impl_generics #partial_ident #ty_generics
107+
#where_clause
108+
{
109+
#(#get_field)*
110+
}
76111
};
77112
tokens.into()
78113
}

discord/src/channel.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use std::fmt::{Display, Formatter};
2+
use std::write;
3+
14
use async_trait::async_trait;
25
use partial_id::Partial;
36
use serde::Deserialize;
@@ -17,10 +20,16 @@ pub struct Channel {
1720
pub id: Snowflake<Channel>,
1821
}
1922

23+
impl Display for Snowflake<Channel> {
24+
fn fmt(&self, f: &mut Formatter<'_>) -> ::std::fmt::Result {
25+
write!(f, "<#{}>", self.as_int())
26+
}
27+
}
28+
2029
impl Endpoint for Snowflake<Channel> {
2130
type Result = Channel;
2231
fn uri(&self) -> String {
23-
format!("/channels/{}", self)
32+
format!("/channels/{}", self.as_int())
2433
}
2534
}
2635

@@ -33,7 +42,6 @@ pub trait ChannelResource: Resource<Endpoint = Snowflake<Channel>> {
3342
let msg = f(CreateMessage::default());
3443
Request::post(format!("{}/messages", self.endpoint().uri()), &msg)
3544
}
36-
3745
async fn send_message(
3846
&self,
3947
client: &Discord,

discord/src/command.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,11 @@ impl Endpoint for Commands {
159159
if let Some(guild) = self.guild_id {
160160
format!(
161161
"/applications/{}/guilds/{}/commands",
162-
self.application_id, guild
162+
self.application_id.as_int(),
163+
guild.as_int()
163164
)
164165
} else {
165-
format!("/applications/{}/commands", self.application_id)
166+
format!("/applications/{}/commands", self.application_id.as_int())
166167
}
167168
}
168169
}
@@ -171,7 +172,7 @@ impl Endpoint for CommandIdentifier {
171172
type Result = Command;
172173
type Delete = ();
173174
fn uri(&self) -> String {
174-
format!("{}/{}", self.command_pool.uri(), self.command_id)
175+
format!("{}/{}", self.command_pool.uri(), self.command_id.as_int())
175176
}
176177
}
177178

discord/src/guild.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ use super::resource::{Resource, Snowflake};
99
#[derive(Debug, Deserialize)]
1010
pub struct Guild {
1111
pub id: Snowflake<Guild>,
12+
pub name: String,
1213
}
1314

1415
impl Endpoint for Snowflake<Guild> {
1516
type Result = Guild;
1617
fn uri(&self) -> String {
17-
format!("/guilds/{}", self)
18+
format!("/guilds/{}", self.as_int())
1819
}
1920
}
2021

discord/src/interaction.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl Client for Webhook {
127127
.header("Content-Type", "application/json")
128128
.body(body.clone())
129129
.unwrap();
130-
println!("{}", request.body());
130+
// println!("{}", request.body());
131131
isahc::send_async(request)
132132
} else {
133133
let request = http.body(()).unwrap();
@@ -148,7 +148,7 @@ impl Client for Webhook {
148148
}
149149

150150
let string = response.text().await.unwrap();
151-
println!("{}", string);
151+
// println!("{}", string);
152152

153153
if response.status().is_client_error() {
154154
return Err(RequestError::ClientError(response.status()));
@@ -175,7 +175,7 @@ impl<T: DropToken> InteractionToken<T> {
175175
let id = self.id;
176176
let token = mem::replace(&mut self.token, String::new());
177177
mem::forget(self); // do not run the destructor
178-
format!("/interactions/{}/{}/callback", id, token)
178+
format!("/interactions/{}/{}/callback", id.as_int(), token)
179179
}
180180
}
181181

@@ -309,7 +309,7 @@ impl InteractionResponseIdentifier {
309309
let application_id = self.application_id;
310310
let token = self.token.clone();
311311
Request::post(
312-
format!("/webhooks/{}/{}", self.application_id, self.token),
312+
format!("/webhooks/{}/{}", self.application_id.as_int(), self.token),
313313
&reply,
314314
)
315315
.map(move |m: Message| {
@@ -342,12 +342,14 @@ impl Endpoint for InteractionResponseIdentifier {
342342
let id = self
343343
.message
344344
.as_ref()
345-
.map(ToString::to_string)
345+
.map(|id| id.as_int().to_string())
346346
.unwrap_or_else(|| "@original".to_owned());
347347

348348
format!(
349349
"/webhooks/{}/{}/messages/{}",
350-
self.application_id, self.token, id
350+
self.application_id.as_int(),
351+
self.token,
352+
id
351353
)
352354
}
353355
}

discord/src/message.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ pub trait MessageResource: Resource<Endpoint = MessageIdentifier> {
212212
Request::post(
213213
format!(
214214
"/channels/{}/messages/{}/threads",
215-
id.channel_id, id.message_id
215+
id.channel_id.as_int(),
216+
id.message_id.as_int()
216217
),
217218
&CreateThread { name },
218219
)
@@ -230,7 +231,11 @@ impl Endpoint for MessageIdentifier {
230231
type Delete = ();
231232

232233
fn uri(&self) -> String {
233-
format!("/channels/{}/messages/{}", self.channel_id, self.message_id)
234+
format!(
235+
"/channels/{}/messages/{}",
236+
self.channel_id.as_int(),
237+
self.message_id.as_int()
238+
)
234239
}
235240
}
236241

discord/src/request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl Client for Discord {
280280
.header("Content-Type", "application/json")
281281
.body(body.clone())
282282
.unwrap();
283-
println!("{}", request.body());
283+
// println!("{}", request.body());
284284
isahc::send_async(request)
285285
} else {
286286
let request = http.body(()).unwrap();
@@ -338,7 +338,7 @@ impl Client for Discord {
338338
}
339339

340340
let string = response.text().await.unwrap();
341-
println!("{}", string);
341+
// println!("{}", string);
342342

343343
if response.status().is_client_error() {
344344
return Err(RequestError::ClientError(response.status()));

0 commit comments

Comments
 (0)