mirror of
https://github.com/revanced/revanced-discord-bot.git
synced 2025-05-09 10:14:26 +02:00
feat: clone message to create a poll
This commit is contained in:
parent
5574779c70
commit
14a1052cd8
@ -1,7 +1,7 @@
|
|||||||
use chrono::Utc;
|
use poise::serenity_prelude::{self as serenity, MessageId, ParseValue, ReactionType};
|
||||||
use poise::serenity_prelude::{self as serenity, MessageId, ReactionType};
|
|
||||||
use poise::ReplyHandle;
|
use poise::ReplyHandle;
|
||||||
|
|
||||||
|
use crate::utils::message::clone_message;
|
||||||
use crate::{Context, Error};
|
use crate::{Context, Error};
|
||||||
|
|
||||||
/// Make the Discord bot sentient.
|
/// Make the Discord bot sentient.
|
||||||
@ -50,43 +50,47 @@ pub async fn reply(
|
|||||||
#[poise::command(slash_command)]
|
#[poise::command(slash_command)]
|
||||||
pub async fn poll(
|
pub async fn poll(
|
||||||
ctx: Context<'_>,
|
ctx: Context<'_>,
|
||||||
#[description = "The id of the poll"] id: u64,
|
#[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 poll message"] message: String,
|
#[description = "The link to a message to clone"] message_link: String,
|
||||||
#[description = "The poll title"] title: String,
|
|
||||||
#[description = "The minumum server age in days to allow members to poll"] age: u16,
|
#[description = "The minumum server age in days to allow members to poll"] age: u16,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let data = ctx.data().read().await;
|
let get_id =
|
||||||
let configuration = &data.configuration;
|
|segments: &mut std::str::Split<char>| segments.next_back().unwrap().parse::<u64>();
|
||||||
let embed_color = configuration.general.embed_color;
|
|
||||||
|
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| {
|
ctx.send(|m| {
|
||||||
m.embed(|e| {
|
clone_message(&message, m)
|
||||||
let guild = &ctx.guild().unwrap();
|
.components(|c| {
|
||||||
if let Some(url) = guild.icon_url() {
|
c.create_action_row(|r| {
|
||||||
e.thumbnail(url.clone()).footer(|f| {
|
r.create_button(|b| {
|
||||||
f.icon_url(url).text(format!(
|
b.label("Vote")
|
||||||
"{} • {}",
|
.emoji(ReactionType::Unicode("🗳️".to_string()))
|
||||||
guild.name,
|
.custom_id(format!("poll:{id}:{age}"))
|
||||||
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}"))
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
.allowed_mentions(|am| {
|
||||||
|
am.parse(ParseValue::Users)
|
||||||
|
.parse(ParseValue::Roles)
|
||||||
|
.parse(ParseValue::Everyone)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
use poise::serenity_prelude::CreateEmbed;
|
|
||||||
|
|
||||||
trait PoiseEmbed {
|
|
||||||
fn create_embed(self, embed: &mut CreateEmbed) -> &mut CreateEmbed;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PoiseEmbed for crate::model::application::Embed {
|
|
||||||
fn create_embed(self, embed: &mut CreateEmbed) -> &mut CreateEmbed {
|
|
||||||
embed
|
|
||||||
.title(self.title)
|
|
||||||
.description(self.description)
|
|
||||||
.color(self.color)
|
|
||||||
.fields(
|
|
||||||
self.fields
|
|
||||||
.iter()
|
|
||||||
.map(|field| (field.name.clone(), field.value.clone(), field.inline)),
|
|
||||||
)
|
|
||||||
.footer(|f| {
|
|
||||||
f.text(self.footer.text);
|
|
||||||
f.icon_url(self.footer.icon_url)
|
|
||||||
})
|
|
||||||
.thumbnail(self.thumbnail.url)
|
|
||||||
.image(self.image.url)
|
|
||||||
.author(|a| a.name(self.author.name).icon_url(self.author.icon_url))
|
|
||||||
}
|
|
||||||
}
|
|
55
src/utils/message.rs
Normal file
55
src/utils/message.rs
Normal file
@ -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
|
||||||
|
}
|
@ -4,7 +4,7 @@ pub mod autorespond;
|
|||||||
pub mod bot;
|
pub mod bot;
|
||||||
pub mod code_embed;
|
pub mod code_embed;
|
||||||
pub mod decancer;
|
pub mod decancer;
|
||||||
pub mod embed;
|
pub mod message;
|
||||||
pub mod macros;
|
pub mod macros;
|
||||||
pub mod media_channel;
|
pub mod media_channel;
|
||||||
pub mod moderation;
|
pub mod moderation;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user