Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jdh8 committed May 4, 2024
1 parent 49755ec commit e8c8f44
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 217 deletions.
19 changes: 9 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "natsuki"
version = "3.0.2"
version = "3.0.3"
authors = [
"Chen-Pang He (https://jdh8.org)",
"u/YourWrstNightmare (https://www.reddit.com/user/YourWrstNightmare)",
Expand All @@ -15,22 +15,21 @@ keywords = ["discord", "ddlc"]

[dependencies]
anyhow = "1.0.75"
base64 = "0.21.3"
base64 = "0.22.1"
bitflags = "2.4.0"
chrono = "0.4.30"
csscolorparser = "0.6.2"
futures = "0.3.28"
image = "0.24.7"
image = "0.25.0"
md5 = "0.7.0"
poise = "0.5.6"
poise = { version = "0.6.1", features = ["collector"] }
rand = "0.8.5"
regex = "1.9.5"
reqwest = "0.11.20"
reqwest = { version = "0.12.4", features = ["json"] }
serde_json = "1.0.105"
shuttle-poise = "0.35.2"
shuttle-runtime = "0.35.2"
shuttle-secrets = "0.35.2"
strum = { version = "0.25", features = ["derive"] }
shuttle-runtime = "0.44.0"
shuttle-serenity = "0.44.0"
strum = { version = "0.26", features = ["derive"] }
tokio = { version = "1.32.0", features = ["full"] }
tree_magic_mini = "3.0.3"
webp = "0.2.5"
webp = "0.3.0"
16 changes: 9 additions & 7 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ pub async fn ping(ctx: Context<'_>) -> anyhow::Result<()> {
let reply = ctx.say("Pong!").await?;
let duration = tick.elapsed();

reply.edit(ctx, |m| m.content(
if duration < std::time::Duration::from_secs(1) {
format!("Pong! {} ms", duration.as_millis())
} else {
format!("Pong! {:.3} s", duration.as_secs_f32())
}
)).await?;
let message = if duration < std::time::Duration::from_secs(1) {
format!("Pong! {} ms", duration.as_millis())
} else {
format!("Pong! {:.3} s", duration.as_secs_f32())
};

reply.edit(ctx, poise::CreateReply {
content: Some(message),
..Default::default()
}).await?;
Ok(())
}

Expand Down
63 changes: 35 additions & 28 deletions src/fun/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@ pub async fn cupcake(ctx: Context<'_>,
let cake = image::open("assets/290px-Hostess-Cupcake-Whole.jpg")?.into_rgba8();
let cake: image::RgbImage = blend_image(cake, &face, 80, 80).convert();
let cake = webp::Encoder::from_rgb(&cake, cake.width(), cake.height());
let cake = cake.encode(90.0).to_vec();

ctx.send(|m| m
.content(format!("{} has been turned into a cupcake. IT LOOKS SO CUUUUTE!", target.mention()))
.attachment(serenity::model::channel::AttachmentType::Bytes {
data: cake.encode(90.0).to_vec().into(),
filename: "cupcake.webp".into(),
})
).await?;
ctx.send(poise::CreateReply {
content: Some(format!("{} has been turned into a cupcake. IT LOOKS SO CUUUUTE!", target.mention())),
attachments: vec![serenity::CreateAttachment::bytes(
cake,
"cupcake.webp",
)],
..Default::default()
}).await?;
Ok(())
}

Expand All @@ -105,24 +107,24 @@ pub async fn cupcake(ctx: Context<'_>,
pub async fn cute(ctx: Context<'_>) -> anyhow::Result<()> {
let mut content = "Don't say this embarrassing thing, dummy!".to_owned();
let reply = ctx.say(&content).await?;
let typing = ctx.serenity_context().http.start_typing(ctx.channel_id().0);
let typing = ctx.serenity_context().http.start_typing(ctx.channel_id());

content.push_str("\nY-You t-too....");
sleep(Duration::from_secs(3)).await;
reply.edit(ctx, |m| m.content(&content)).await?;
reply.edit(ctx, poise::CreateReply { content: Some(content.clone()), ..Default::default() }).await?;

content.push_str("\nI'M NOT CUUUUUUUUUUUTE!");
sleep(Duration::from_secs(2)).await;
reply.edit(ctx, |m| m.content(&content)).await?;
reply.edit(ctx, poise::CreateReply { content: Some(content.clone()), ..Default::default() }).await?;

content.push_str("\nDon't think you can make me say this embarrassing thing just because we're not at school!");
sleep(Duration::from_secs(2)).await;
reply.edit(ctx, |m| m.content(&content)).await?;
reply.edit(ctx, poise::CreateReply { content: Some(content.clone()), ..Default::default() }).await?;

let _ = typing.map(serenity::Typing::stop);
typing.stop();
content.push_str("\nI-I have to go to the bathroom.");
sleep(Duration::from_secs(4)).await;
reply.edit(ctx, |m| m.content(&content)).await?;
reply.edit(ctx, poise::CreateReply { content: Some(content.clone()), ..Default::default() }).await?;
Ok(())
}

Expand Down Expand Up @@ -221,16 +223,19 @@ async fn fuck(ctx: Context<'_>, user: Option<&serenity::User>) -> anyhow::Result
}.convert();

let image = webp::Encoder::from_rgb(&image, image.width(), image.height());
let image = image.encode(90.0).to_vec();

ctx.send(|m| m
.content(format!("{} fucked {}!",
ctx.send(poise::CreateReply {
content: Some(format!("{} fucked {}!",
ctx.author().mention(),
user.map_or_else(|| "Natsuki".to_owned(), |u| u.mention().to_string())))
.attachment(serenity::model::channel::AttachmentType::Bytes {
data: image.encode(90.0).to_vec().into(),
filename: "fuck.webp".into(),
})
).await?;
user.map_or_else(|| "Natsuki".to_owned(), |u| u.mention().to_string()))
),
attachments: vec![serenity::CreateAttachment::bytes(
image,
"fuck.webp",
)],
..Default::default()
}).await?;
Ok(())
}

Expand All @@ -247,14 +252,16 @@ pub async fn smash(ctx: Context<'_>,
if ctx.channel_id().to_channel(&ctx).await?.is_nsfw() {
return fuck(ctx, user.as_ref()).await;
}

let author = ctx.author().mention();
ctx.send(|m| m.embed(|e| e
.description(user.map_or_else(
|| format!("{author} smashed!"),
|u| format!("{author} smashed {}!", u.mention())))
.image("https://raw.githubusercontent.com/jdh8/natsuki/master/assets/smash.png"))
).await?;
ctx.send(poise::CreateReply {
embeds: vec![serenity::CreateEmbed::new()
.description(user.map_or_else(
|| format!("{author} smashed!"),
|u| format!("{author} smashed {}!", u.mention())))
.image("https://raw.githubusercontent.com/jdh8/natsuki/master/assets/smash.png")
],
..Default::default()
}).await?;
Ok(())
}

Expand Down
111 changes: 50 additions & 61 deletions src/fun/poem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,27 +260,33 @@ enum Doki {
Monika,
}

fn make_buttons(
buttons: &[(EmojiId, Doki)],
style: impl Fn(Doki) -> serenity::ButtonStyle,
disabled: bool,
) -> serenity::CreateActionRow {
serenity::CreateActionRow::Buttons(
buttons.iter().copied().map(|(emoji, doki)|
serenity::CreateButton::new(doki.to_string())
.style(style(doki))
.emoji(emoji)
.label(doki.to_string())
.disabled(disabled)
).collect()
)
}

async fn game(ctx: Context<'_>, word: &str, answer: Doki, buttons: &[(EmojiId, Doki)]) -> anyhow::Result<()> {
let mut question = ctx.send(|m| m
.content("Whose word is **".to_owned() + word + "**? Please answer in 15 seconds.")
//XXX Try to deduplicate these
.components(|c| c.create_action_row(|a| {
for (emoji, doki) in buttons {
a.create_button(|b| b
.style(Secondary)
.emoji(*emoji)
.label(doki)
.custom_id(doki)
);
}
a
}))
).await?.into_message().await?;
let content = Some("Whose word is **".to_owned() + word + "**? Please answer in 15 seconds.");
let question = ctx.send(poise::CreateReply {
content,
components: Some(vec![make_buttons(buttons, |_| Secondary, false)]),
..Default::default()
}).await?;

let collected = serenity::CollectComponentInteraction::new(ctx)
let collected = serenity::ComponentInteractionCollector::new(ctx)
.author_id(ctx.author().id)
.message_id(question.id)
.collect_limit(1)
.message_id(question.message().await?.id)
.timeout(Duration::from_secs(15))
.await;

Expand All @@ -289,46 +295,29 @@ async fn game(ctx: Context<'_>, word: &str, answer: Doki, buttons: &[(EmojiId, D
let right = "Congratulations! That's correct.".to_owned();
let wrong = format!("Sorry, it's **{answer}**.");
let selected = interaction.data.custom_id.parse::<Doki>()?;

let response = interaction.create_interaction_response(ctx, |m| m
.kind(serenity::InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|r| r
.content(if selected == answer { right } else { wrong }))
);

let edit = question.edit(ctx, |m| m
//XXX Try to deduplicate these
.components(|c| c.create_action_row(|a| {
for (emoji, doki) in buttons {
a.create_button(|b| b
.emoji(*emoji)
.label(doki)
.custom_id(doki)
.style(if doki == &answer { Success }
else if doki == &selected { Danger }
else { Secondary })
);
}
a
})));


let response = interaction.create_response(ctx, serenity::CreateInteractionResponse::Message(
serenity::CreateInteractionResponseMessage::new()
.content(if selected == answer { right } else { wrong })
));
let style = |doki: Doki| {
if doki == answer { Success }
else if doki == selected { Danger }
else { Secondary }
};
let edit = question.edit(ctx, poise::CreateReply {
//content,
components: Some(vec![make_buttons(buttons, style, false)]),
..Default::default()
});
try_join!(response, edit)?;
},
None => {
question.edit(ctx, |m| m
//XXX Try to deduplicate these
.components(|c| c.create_action_row(|a| {
for (emoji, doki) in buttons {
a.create_button(|b| b
.emoji(*emoji)
.label(doki)
.custom_id(doki)
.disabled(true)
.style(Secondary)
);
}
a
}))).await?;
question.edit(ctx, poise::CreateReply {
//content,
components: Some(vec![make_buttons(buttons, |_| Secondary, true)]),
..Default::default()
}).await?;
}
}
Ok(())
Expand All @@ -341,9 +330,9 @@ async fn poem1(ctx: Context<'_>) -> anyhow::Result<()> {
else { Doki::Natsuki };

game(ctx, word, doki, &[
(EmojiId(424_991_418_386_350_081), Doki::Sayori),
(EmojiId(424_991_419_329_937_428), Doki::Natsuki),
(EmojiId(424_987_242_986_078_218), Doki::Yuri),
(EmojiId::new(424_991_418_386_350_081), Doki::Sayori),
(EmojiId::new(424_991_419_329_937_428), Doki::Natsuki),
(EmojiId::new(424_987_242_986_078_218), Doki::Yuri),
]).await
}

Expand All @@ -352,13 +341,13 @@ async fn poem2(ctx: Context<'_>) -> anyhow::Result<()> {
let doki = if preference.contains(Preference::Yuri) { Doki::Yuri } else { Doki::Natsuki };

game(ctx, word, doki, &[
(EmojiId(424_991_419_329_937_428), Doki::Natsuki),
(EmojiId(424_987_242_986_078_218), Doki::Yuri),
(EmojiId::new(424_991_419_329_937_428), Doki::Natsuki),
(EmojiId::new(424_987_242_986_078_218), Doki::Yuri),
]).await
}

async fn poem3(ctx: Context<'_>) -> anyhow::Result<()> {
game(ctx, "Monika", Doki::Monika, &[(EmojiId(501_274_687_842_680_832), Doki::Monika)]).await
game(ctx, "Monika", Doki::Monika, &[(EmojiId::new(501_274_687_842_680_832), Doki::Monika)]).await
}

/// Play a poem game
Expand Down
27 changes: 14 additions & 13 deletions src/information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ pub async fn snowflake(ctx: Context<'_>,

#[poise::command(context_menu_command = "Snowflake (user)")]
pub async fn snowflake_user(ctx: Context<'_>, user: serenity::User) -> anyhow::Result<()> {
ctx.say(format_snowflake(user.id.0.into())).await?;
ctx.say(format_snowflake(user.id.get().into())).await?;
Ok(())
}

#[poise::command(context_menu_command = "Snowflake (message)")]
pub async fn snowflake_message(ctx: Context<'_>, message: serenity::Message) -> anyhow::Result<()> {
ctx.say(format_snowflake(message.id.0.into())).await?;
ctx.say(format_snowflake(message.id.get().into())).await?;
Ok(())
}

Expand All @@ -107,20 +107,21 @@ pub async fn role(ctx: Context<'_>,
#[description = "Role to inspect"]
role: serenity::Role,
) -> anyhow::Result<()> {
ctx.send(|m| m
.embed(|e| e
ctx.send(poise::CreateReply {
embeds: vec![serenity::CreateEmbed::new()
.title("Role ".to_owned() + &role.name)
.color(role.colour)
.field("Name", role.name, false)
.field("ID", role.id, false)
.field("ID", role.id.to_string(), false)
.field("Color", "#".to_owned() + &role.colour.hex(), false)
.field("Hoisted", role.hoist, false)
.field("Managed", role.managed, false)
.field("Mentionable", role.mentionable, false)
.field("Permissions", role.permissions, false)
.field("Position", role.position, false)
.field("Created at", format_rfc3339(Snowflake(role.id.0).time()), false)
)
).await?;
.field("Hoisted", role.hoist.to_string(), false)
.field("Managed", role.managed.to_string(), false)
.field("Mentionable", role.mentionable.to_string(), false)
.field("Permissions", role.permissions.to_string(), false)
.field("Position", role.position.to_string(), false)
.field("Created at", format_rfc3339(Snowflake(role.id.get()).time()), false)
],
..Default::default()
}).await?;
Ok(())
}
Loading

0 comments on commit e8c8f44

Please sign in to comment.