Skip to content

Commit

Permalink
add prelim tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacherr committed Jul 28, 2024
1 parent 9e65ca0 commit f70019f
Show file tree
Hide file tree
Showing 9 changed files with 526 additions and 52 deletions.
11 changes: 11 additions & 0 deletions assyst-common/src/util/discord.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use regex::Regex;
use twilight_http::Client;
use twilight_model::id::marker::GuildMarker;
use twilight_model::id::Id;
Expand Down Expand Up @@ -80,3 +81,13 @@ pub fn format_discord_timestamp(input: u64) -> String {
format_time(input)
}
}

pub fn mention_to_id(s: &str) -> Option<u64> {
let mention: Regex = Regex::new(r"(?:<@!?)?(\d{16,20})>?").unwrap();

mention
.captures(s)
.and_then(|capture| capture.get(1))
.map(|id| id.as_str())
.and_then(|id| id.parse::<u64>().ok())
}
58 changes: 55 additions & 3 deletions assyst-core/src/command/arguments.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use std::fmt::Display;

use assyst_common::markdown::parse_codeblock;
use assyst_common::util::discord::{get_avatar_url, id_from_mention};
use assyst_common::util::discord::{get_avatar_url, id_from_mention, mention_to_id};
use assyst_common::util::{parse_to_millis, regex};
use serde::Deserialize;
use twilight_model::application::command::CommandOption;
use twilight_model::application::interaction::application_command::CommandOptionValue;
use twilight_model::channel::message::sticker::{MessageSticker, StickerFormatType};
use twilight_model::channel::message::Embed;
use twilight_model::channel::Attachment;
use twilight_model::id::marker::ChannelMarker;
use twilight_model::id::marker::{ChannelMarker, UserMarker};
use twilight_model::id::Id;
use twilight_util::builder::command::{AttachmentBuilder, IntegerBuilder, NumberBuilder, StringBuilder};
use twilight_model::user::User as TwlUser;
use twilight_util::builder::command::{AttachmentBuilder, IntegerBuilder, NumberBuilder, StringBuilder, UserBuilder};

use super::errors::TagParseError;
use super::{CommandCtxt, InteractionCommandParseCtxt, Label, RawMessageParseCtxt};
Expand Down Expand Up @@ -252,6 +253,57 @@ impl ParseArgument for Codeblock {
}
}

/// A user argument (mention or ID)
#[derive(Debug)]
pub struct User(pub TwlUser);
impl ParseArgument for User {
async fn parse_raw_message(ctxt: &mut RawMessageParseCtxt<'_>, label: Label) -> Result<Self, TagParseError> {
let next = ctxt.next_word(label)?;
let id = mention_to_id(next);

let user = ctxt
.cx
.assyst()
.http_client
.user(Id::<UserMarker>::new(id.unwrap_or(next.parse::<u64>().unwrap_or(1))))
.await
.map_err(|e| TagParseError::TwilightHttp(Box::new(e)))?
.model()
.await
.map_err(|e| TagParseError::TwilightDeserialize(Box::new(e)))?;

Ok(User(user))
}

async fn parse_command_option(ctxt: &mut InteractionCommandParseCtxt<'_>) -> Result<Self, TagParseError> {
let word = ctxt.next_option()?;

if let CommandOptionValue::User(id) = word.value {
let user = ctxt
.cx
.assyst()
.http_client
.user(id)
.await
.map_err(|e| TagParseError::TwilightHttp(Box::new(e)))?
.model()
.await
.map_err(|e| TagParseError::TwilightDeserialize(Box::new(e)))?;

Ok(User(user))
} else {
Err(TagParseError::MismatchedCommandOptionType((
"User".to_owned(),
word.value.clone(),
)))
}
}

fn as_command_option(name: &str) -> CommandOption {
UserBuilder::new(name, "user argument").required(true).build()
}
}

/// The rest of a message as an argument. This should be the last argument if used.
#[derive(Debug)]
pub struct Rest(pub String);
Expand Down
2 changes: 1 addition & 1 deletion assyst-core/src/command/misc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use assyst_database::model::free_tier_2_requests::FreeTier2Requests;
use assyst_database::model::guild_disabled_command::GuildDisabledCommand;
use assyst_proc_macro::command;

use super::arguments::{Image, ImageUrl, Rest, RestNoFlags, Word};
use super::arguments::{Image, ImageUrl, RestNoFlags, Word};
use super::registry::get_or_init_commands;
use super::{Category, CommandCtxt};
use crate::command::Availability;
Expand Down
6 changes: 2 additions & 4 deletions assyst-core/src/command/misc/prefix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use anyhow::{bail, Context};
use anyhow::{bail, ensure, Context};
use assyst_common::markdown::Markdown;
use assyst_database::model::prefix::Prefix;
use assyst_proc_macro::command;
Expand Down Expand Up @@ -44,9 +44,7 @@ pub async fn set(ctxt: CommandCtxt<'_>, new: Word) -> anyhow::Result<()> {
bail!("Prefix getting and setting can only be used in guilds.")
};

if new.0.len() > 14 {
bail!("Prefixes cannot be longer than 14 characters.")
}
ensure!(new.0.len() < 14, "Prefixes cannot be longer than 14 characters.");

let new = Prefix { prefix: new.0 };
new.set(&ctxt.assyst().database_handler, guild_id.get())
Expand Down
Loading

0 comments on commit f70019f

Please sign in to comment.