Skip to content

Commit 1c5ba1a

Browse files
committed
various code fixes
- Fixes missing . in regex for repos - fix list-repos sending 2 messages - fix missing url (and url parsing) in embed command - fix embed command sending 2 responses in certain errors. - lazily match everywhere due to autocomplete limitations - remove redundant 25+ snippet warning
1 parent eca81b3 commit 1c5ba1a

2 files changed

Lines changed: 27 additions & 36 deletions

File tree

src/commands/snippets.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub async fn edit_snippet(
112112
#[description = "The snippet's title"] title: Option<String>,
113113
#[description = "The snippet's content"] content: Option<String>,
114114
) -> Result<(), Error> {
115-
match get_snippet(&ctx, &id).await {
115+
match get_snippet_lazy(&ctx, &id).await {
116116
Some(mut snippet) => {
117117
if let Some(title) = title {
118118
snippet.title = title;
@@ -143,16 +143,14 @@ pub async fn edit_snippet(
143143
}
144144

145145
/// Removes a snippet
146-
///
147-
/// Must use the full formatted snippet name (id: title)
148146
#[poise::command(rename = "remove-snippet", slash_command, guild_only)]
149147
pub async fn remove_snippet(
150148
ctx: Context<'_>,
151149
#[autocomplete = "autocomplete_snippet"]
152150
#[description = "The snippet's id"]
153151
id: String,
154152
) -> Result<(), Error> {
155-
match get_snippet(&ctx, &id).await {
153+
match get_snippet_lazy(&ctx, &id).await {
156154
Some(snippet) => {
157155
rm_snippet(&ctx, &snippet).await;
158156
let title = &"Snippet successfully removed";
@@ -247,7 +245,7 @@ impl Embeddable for Snippet {
247245
}
248246

249247
// Exact matches the snippet id and name.
250-
async fn get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
248+
async fn _get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
251249
let data = ctx.data();
252250
let rwlock_guard = data.state.read().unwrap();
253251

src/commands/utils.rs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,41 @@ pub async fn embed(
5050
|| thumbnail.is_some()
5151
|| footer.is_some();
5252

53-
let url_invalid = url.is_some() && title.is_none();
54-
5553
if !at_least_one_property_set {
5654
respond_err(
5755
&ctx,
5856
"Failed to respond with embed",
5957
"Please provide at least one title, description, image, footer or thumbnail",
6058
)
6159
.await;
62-
ctx.say("You must provide at least one title, description, image or thumbnail.")
63-
.await?;
6460
return Ok(());
6561
}
62+
let mut embed = CreateEmbed::default();
6663

67-
if url_invalid {
68-
respond_err(
69-
&ctx,
70-
"Failed to respond with embed",
71-
"To set a url, you must set a title",
72-
)
73-
.await;
74-
return Ok(());
64+
if let Some(url) = url {
65+
match url.parse::<reqwest::Url>() {
66+
Ok(_) => {
67+
if title.is_some() {
68+
embed = embed.url(url)
69+
} else {
70+
respond_err(
71+
&ctx,
72+
"Failed to respond with embed",
73+
"To set a url, you must set a title.",
74+
)
75+
.await;
76+
return Ok(());
77+
}
78+
}
79+
Err(e) => {
80+
let title = "Invalid url provided";
81+
let content = &format!("The url '{}' is not a valid url: {}", url, e);
82+
respond_err(&ctx, title, content).await;
83+
return Ok(());
84+
}
85+
}
7586
}
7687

77-
let mut embed = CreateEmbed::default();
78-
7988
if let Some(title) = title {
8089
embed = embed.title(title);
8190
}
@@ -264,7 +273,7 @@ pub async fn add_repo(
264273
#[description = "The respository name."] repository: String,
265274
) -> Result<(), Error> {
266275
let key_regex = Regex::new(r"[a-z+]+$").unwrap();
267-
let repo_details_regex = Regex::new(r"^([a-zA-Z0-9-_]+)*$").unwrap();
276+
let repo_details_regex = Regex::new(r"^([a-zA-Z0-9-_.]+)*$").unwrap();
268277
if !key_regex.is_match(&key) {
269278
respond_err(
270279
&ctx,
@@ -381,22 +390,6 @@ pub async fn list_repos(ctx: Context<'_>) -> Result<(), Error> {
381390

382391
super::paginate_lists(ctx, &pages, "Repositories").await?;
383392

384-
let mut embed = CreateEmbed::default()
385-
.title("Issue tokens")
386-
.color(Colour::TEAL);
387-
388-
// fields are limited to 25 max, we can't display more than 25 snippets in the snippets command
389-
// due to a discord limitation.
390-
for token in tokens {
391-
embed = embed.field(
392-
format!("**{}**", token.0),
393-
format!("{}/{}", token.1.owner, token.1.name),
394-
false,
395-
);
396-
}
397-
398-
ctx.send(poise::CreateReply::default().embed(embed)).await?;
399-
400393
Ok(())
401394
}
402395

0 commit comments

Comments
 (0)