Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat: clone message to create a poll
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Mar 26, 2023
1 parent 5574779 commit 14a1052
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 59 deletions.
68 changes: 36 additions & 32 deletions src/commands/misc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::Utc;
use poise::serenity_prelude::{self as serenity, MessageId, ReactionType};
use poise::serenity_prelude::{self as serenity, MessageId, ParseValue, ReactionType};
use poise::ReplyHandle;

use crate::utils::message::clone_message;
use crate::{Context, Error};

/// Make the Discord bot sentient.
Expand Down Expand Up @@ -50,43 +50,47 @@ pub async fn reply(
#[poise::command(slash_command)]
pub async fn poll(
ctx: Context<'_>,
#[description = "The id of the poll"] id: u64,
#[description = "The poll message"] message: String,
#[description = "The poll title"] title: String,
#[description = "The id of the poll"] id: u64, /* This is currently unused in the API, leaving as a placeholder in case it is required. */
#[description = "The link to a message to clone"] message_link: String,
#[description = "The minumum server age in days to allow members to poll"] age: u16,
) -> Result<(), Error> {
let data = ctx.data().read().await;
let configuration = &data.configuration;
let embed_color = configuration.general.embed_color;
let get_id =
|segments: &mut std::str::Split<char>| segments.next_back().unwrap().parse::<u64>();

let url = reqwest::Url::parse(&message_link)?;
let mut segments = url.path_segments().ok_or("Invalid Discord message link")?;

if segments.clone().count() != 4 {
return Err("Invalid Discord message link".into());
}

let message_id = get_id(&mut segments)?;
let channel_id = get_id(&mut segments)?;

let message = ctx
.discord()
.http
.get_message(channel_id, message_id)
.await?;

ctx.send(|m| {
m.embed(|e| {
let guild = &ctx.guild().unwrap();
if let Some(url) = guild.icon_url() {
e.thumbnail(url.clone()).footer(|f| {
f.icon_url(url).text(format!(
"{} • {}",
guild.name,
Utc::today().format("%Y/%m/%d")
))
})
} else {
e
}
.title(title)
.description(message)
.color(embed_color)
})
.components(|c| {
c.create_action_row(|r| {
r.create_button(|b| {
b.label("Vote")
.emoji(ReactionType::Unicode("🗳️".to_string()))
.custom_id(format!("poll:{id}:{age}"))
clone_message(&message, m)
.components(|c| {
c.create_action_row(|r| {
r.create_button(|b| {
b.label("Vote")
.emoji(ReactionType::Unicode("🗳️".to_string()))
.custom_id(format!("poll:{id}:{age}"))
})
})
})
})
.allowed_mentions(|am| {
am.parse(ParseValue::Users)
.parse(ParseValue::Roles)
.parse(ParseValue::Everyone)
})
})
.await?;

Ok(())
}
26 changes: 0 additions & 26 deletions src/utils/embed.rs

This file was deleted.

55 changes: 55 additions & 0 deletions src/utils/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use chrono::Utc;
use poise::serenity_prelude::Message;
use poise::CreateReply;

pub fn clone_message<'a, 'b>(
message: &'a Message,
to_reply: &'b mut CreateReply<'a>,
) -> &'b mut CreateReply<'a> {
let mut reply = to_reply.content(message.content.as_str());

if let Some(embed) = message.embeds.get(0) {
reply = reply.embed(|e| {
let mut new_embed = e;

if let Some(color) = embed.colour {
new_embed = new_embed.color(color);
}

new_embed = new_embed.timestamp(Utc::now().to_rfc3339());

if let Some(title) = &embed.title {
new_embed = new_embed.title(title);
}

if let Some(description) = &embed.description {
new_embed = new_embed.description(description);
}

if let Some(footer) = &embed.footer {
new_embed = new_embed.footer(|f| f.text(&footer.text));
}

if let Some(author) = &embed.author {
new_embed = new_embed.author(|a| a.name(&author.name));
}

if let Some(image) = &embed.image {
new_embed = new_embed.image(image.url.as_str());
}

if let Some(thumbnail) = &embed.thumbnail {
new_embed = new_embed.thumbnail(thumbnail.url.as_str());
}

for field in &embed.fields {
new_embed =
new_embed.field(field.name.as_str(), field.value.as_str(), field.inline);
}

new_embed
})
}

reply
}
2 changes: 1 addition & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod autorespond;
pub mod bot;
pub mod code_embed;
pub mod decancer;
pub mod embed;
pub mod message;
pub mod macros;
pub mod media_channel;
pub mod moderation;
Expand Down

0 comments on commit 14a1052

Please sign in to comment.