feat(discord): Added /social commands

- Added /social msg to send messages
- Added /social edit to edit sent messages
This commit is contained in:
Alexey 2025-12-11 15:00:55 +03:00
commit 8453de086a
3 changed files with 159 additions and 0 deletions

View file

@ -5,6 +5,7 @@ use crate::{Context, Error};
pub mod quest;
pub mod init;
pub mod answer;
pub mod social;
pub const ERROR_MSG: &str = "Server error :(";

View file

@ -0,0 +1,157 @@
use poise::serenity_prelude::{Attachment, ChannelId, CreateAttachment, CreateMessage, EditMessage, Mentionable, MessageId, UserId};
use crate::{Context, Error};
#[poise::command(
prefix_command,
slash_command,
required_permissions = "ADMINISTRATOR",
guild_only,
subcommands("msg", "edit"),
)]
pub async fn social( _ctx: Context<'_> ) -> Result<(), Error> {
Ok(())
}
#[poise::command(
prefix_command,
slash_command,
required_permissions = "ADMINISTRATOR",
guild_only,
)]
pub async fn msg (
ctx: Context<'_>,
channel: Option<ChannelId>,
user: Option<UserId>,
content: Option<String>,
file: Option<Attachment>,
) -> Result<(), Error> {
if channel.is_none() && user.is_none() {
let reply_string = "Please specify channel **or** user".to_string();
ctx.reply(reply_string).await?;
return Ok(());
}
if channel.is_some() && user.is_some() {
let reply_string = "Please specify **only** channel or **only** user, not both".to_string();
ctx.reply(reply_string).await?;
return Ok(());
}
if content.is_none() && file.is_none() {
let reply_string = "Please specify content and/or file".to_string();
ctx.reply(reply_string).await?;
return Ok(());
}
let mut builder = CreateMessage::new();
if let Some(content) = content {
builder = builder.content(content);
}
if let Some(file) = file {
ctx.defer().await?;
let attachment = CreateAttachment::url(ctx, &file.url).await;
match attachment {
Ok(attachment) => { builder = builder.add_file(attachment); },
Err(error) => {
eprintln!("{error}");
let reply_string = "Couldn't copy attachment, aborting";
ctx.reply(reply_string).await?;
return Ok(());
},
}
}
let reply_string = if let Some(channel) = channel {
let message = channel.send_message(ctx, builder).await?;
format!("Sent {message} ({message_id}) to {channel}!",
message = message.link(),
message_id = message.id,
channel = channel.mention(),
)
} else if let Some(user) = user {
let message = user.dm(ctx, builder).await?;
format!("Sent {message} ({message_id}) to {user}",
message = message.link(),
message_id = message.id,
user = user.mention(),
)
} else {
unreachable!();
};
ctx.reply(reply_string).await?;
Ok(())
}
#[poise::command(
prefix_command,
slash_command,
required_permissions = "ADMINISTRATOR",
guild_only,
)]
pub async fn edit (
ctx: Context<'_>,
#[rename = "message"]
message_id: MessageId,
channel: Option<ChannelId>,
user: Option<UserId>,
content: Option<String>,
file: Option<Attachment>,
) -> Result<(), Error> {
if channel.is_none() && user.is_none() {
let reply_string = "Please specify channel **or** user".to_string();
ctx.reply(reply_string).await?;
return Ok(());
}
if channel.is_some() && user.is_some() {
let reply_string = "Please specify **only** channel or **only** user, not both".to_string();
ctx.reply(reply_string).await?;
return Ok(());
}
if content.is_none() && file.is_none() {
let reply_string = "Please specify content and/or file".to_string();
ctx.reply(reply_string).await?;
return Ok(());
}
let mut builder = EditMessage::new();
if let Some(content) = content {
builder = builder.content(content);
}
if let Some(file) = file {
ctx.defer().await?;
let attachment = CreateAttachment::url(ctx, &file.url).await;
match attachment {
Ok(attachment) => { builder = builder.new_attachment(attachment); },
Err(error) => {
eprintln!("{error}");
let reply_string = "Couldn't copy attachment, aborting";
ctx.reply(reply_string).await?;
return Ok(());
},
}
}
if let Some(channel) = channel {
let mut message = channel.message(ctx, message_id).await?;
message.edit(ctx, builder).await?;
} else if let Some(user) = user {
let channel = user.create_dm_channel(ctx).await?;
let mut message = channel.message(ctx, message_id).await?;
message.edit(ctx, builder).await?;
}
let reply_string = "Successfully edited message.".to_string();
ctx.reply(reply_string).await?;
Ok(())
}

View file

@ -40,6 +40,7 @@ async fn main() {
commands::register(),
commands::init::init(),
commands::answer::answer(),
commands::social::social(),
],
..Default::default()
})