Skip to content

Commit

Permalink
Clean up embed dispatching and fix formatters with no style options (#…
Browse files Browse the repository at this point in the history
…199)

* Unify embed sending

* Fix formatting for methods with no alternative styles
  • Loading branch information
Headline authored Mar 5, 2023
1 parent 7a01720 commit 2c877e6
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 129 deletions.
9 changes: 1 addition & 8 deletions src/commands/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ use crate::utls::parser;
#[bucket = "nospam"]
pub async fn asm(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
let emb = handle_request(ctx.clone(), msg.content.clone(), msg.author.clone(), msg).await?;
let emb_msg = embeds::embed_message(emb);
let asm_embed = msg
.channel_id
.send_message(&ctx.http, |e| {
*e = emb_msg;
e
})
.await?;
let asm_embed = embeds::dispatch_embed(&ctx.http, msg.channel_id, emb).await?;

// Success/fail react
let compilation_successful = asm_embed.embeds[0].colour.unwrap().0 == COLOR_OKAY;
Expand Down
9 changes: 1 addition & 8 deletions src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ pub async fn compile(ctx: &Context, msg: &Message, _args: Args) -> CommandResult
let embed = handle_request(ctx.clone(), msg.content.clone(), msg.author.clone(), msg).await?;

// Send our final embed
let message = embeds::embed_message(embed);
let compilation_embed = msg
.channel_id
.send_message(&ctx.http, |e| {
*e = message;
e
})
.await?;
let compilation_embed = embeds::dispatch_embed(&ctx.http, msg.channel_id, embed).await?;

// Success/fail react
let compilation_successful = compilation_embed.embeds[0].colour.unwrap().0 == COLOR_OKAY;
Expand Down
9 changes: 1 addition & 8 deletions src/commands/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,9 @@ use crate::{
#[bucket = "nospam"]
pub async fn cpp(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
let emb = handle_request(ctx.clone(), msg.content.clone(), msg.author.clone(), msg).await?;
let emb_msg = embeds::embed_message(emb);

// Dispatch our request
let compilation_embed = msg
.channel_id
.send_message(&ctx.http, |e| {
*e = emb_msg;
e
})
.await?;
let compilation_embed = embeds::dispatch_embed(&ctx.http, msg.channel_id, emb).await?;

// add delete cache
let data_read = ctx.data.read().await;
Expand Down
8 changes: 1 addition & 7 deletions src/commands/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ pub async fn formats(ctx: &Context, msg: &Message, _args: Args) -> CommandResult
emb.field(&format.format_type, &output, false);
}

let emb_msg = embeds::embed_message(emb);
msg.channel_id
.send_message(&ctx.http, |e| {
*e = emb_msg;
e
})
.await?;
embeds::dispatch_embed(&ctx.http, msg.channel_id, emb).await?;

return Ok(());
}
9 changes: 1 addition & 8 deletions src/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,7 @@ pub async fn help(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
};

emb.description(description);

let emb_msg = embeds::embed_message(emb);
msg.channel_id
.send_message(&ctx.http, |e| {
*e = emb_msg;
e
})
.await?;
embeds::dispatch_embed(&ctx.http, msg.channel_id, emb).await?;

return Ok(());
}
Expand Down
10 changes: 1 addition & 9 deletions src/commands/invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ pub async fn invite(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
let invite = env::var("INVITE_LINK").expect("Expected invite link envvar");

let emb = embeds::build_invite_embed(&invite);

let emb_msg = embeds::embed_message(emb);
msg.channel_id
.send_message(&ctx.http, |e| {
*e = emb_msg;
e
})
.await?;

embeds::dispatch_embed(&ctx.http, msg.channel_id, emb).await?;
Ok(())
}
77 changes: 21 additions & 56 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,12 @@ impl EventHandler for Handler {
info!("Joining {}", guild.name);

if let Some(system_channel) = guild.system_channel_id {
let message = embeds::embed_message(embeds::build_welcome_embed());
let _ = system_channel
.send_message(&ctx.http, |e| {
*e = message;
e
})
.await;
let _ = embeds::dispatch_embed(
&ctx.http,
system_channel,
embeds::build_welcome_embed(),
)
.await;
}
}
}
Expand Down Expand Up @@ -213,15 +212,11 @@ impl EventHandler for Handler {
&new_message.author,
&format!("{}", e),
);
let emb_msg = embeds::embed_message(emb);
if let Ok(sent) = new_message
.channel_id
.send_message(&ctx.http, |e| {
*e = emb_msg;
e
})
.await
{

let sent_fail =
embeds::dispatch_embed(&ctx.http, new_message.channel_id, emb)
.await;
if let Ok(sent) = sent_fail {
let mut message_cache =
data.get::<MessageCache>().unwrap().lock().await;
message_cache.insert(
Expand All @@ -232,15 +227,8 @@ impl EventHandler for Handler {
return;
}
};
let mut emb_msg = embeds::embed_message(emb);
emb_msg.reference_message(&new_message);
let _ = new_message
.channel_id
.send_message(&ctx.http, |e| {
*e = emb_msg;
e
})
.await;
let _ =
embeds::dispatch_embed(&ctx.http, new_message.channel_id, emb).await;
}
}
}
Expand Down Expand Up @@ -366,20 +354,11 @@ pub async fn before(ctx: &Context, msg: &Message, _: &str) -> bool {
If you feel that this has been done in error, request an unban in the support server.",
);

let emb_msg = embeds::embed_message(emb);
let msg_response = msg
.channel_id
.send_message(&ctx.http, |e| {
*e = emb_msg;
e
})
.await;
if msg_response.is_ok() {
if author_blocklisted {
warn!("Blocked user {} [{}]", msg.author.tag(), msg.author.id.0);
} else {
warn!("Blocked guild {}", guild_id);
}
let _ = embeds::dispatch_embed(&ctx.http, msg.channel_id, emb).await;
if author_blocklisted {
warn!("Blocked user {} [{}]", msg.author.tag(), msg.author.id.0);
} else {
warn!("Blocked guild {}", guild_id);
}
return false;
}
Expand All @@ -399,15 +378,8 @@ pub async fn after(

if let Err(e) = command_result {
let emb = embeds::build_fail_embed(&msg.author, &format!("{}", e));
let emb_msg = embeds::embed_message(emb);
if let Ok(sent) = msg
.channel_id
.send_message(&ctx.http, |e| {
*e = emb_msg;
e
})
.await
{
let sent_fail = embeds::dispatch_embed(&ctx.http, msg.channel_id, emb).await;
if let Ok(sent) = sent_fail {
let mut message_cache = data.get::<MessageCache>().unwrap().lock().await;
message_cache.insert(msg.id.0, MessageCacheEntry::new(sent, msg.clone()));
}
Expand All @@ -424,13 +396,6 @@ pub async fn after(
pub async fn dispatch_error(ctx: &Context, msg: &Message, error: DispatchError, _: &str) {
if let DispatchError::Ratelimited(_) = error {
let emb = embeds::build_fail_embed(&msg.author, "You are sending requests too fast!");
let emb_msg = embeds::embed_message(emb);
let _ = msg
.channel_id
.send_message(&ctx.http, |e| {
*e = emb_msg;
e
})
.await;
let _ = embeds::dispatch_embed(&ctx.http, msg.channel_id, emb).await;
}
}
53 changes: 28 additions & 25 deletions src/slashcmds/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub async fn format(ctx: &Context, command: &ApplicationCommandInteraction) -> C
create_formats_interaction(response, &comp_mgr.gbolt.as_ref().unwrap().formats)
})
.await?;

// Handle response from select menu / button interactions
let resp = command.get_interaction_response(&ctx.http).await?;
let mut cib = resp
Expand Down Expand Up @@ -80,32 +79,36 @@ pub async fn format(ctx: &Context, command: &ApplicationCommandInteraction) -> C
.find(|p| p.format_type == formatter)
.unwrap()
.styles;
command
.edit_original_interaction_response(&ctx.http, |resp| {
create_styles_interaction(resp, styles)
})
.await?;

let resp = command.get_interaction_response(&ctx.http).await?;
cib = resp
.await_component_interactions(&ctx.shard)
.timeout(Duration::from_secs(30));
cic = cib.build();
selected = false;
// WebKit is our default value for clang-fmt
let mut style = String::from("WebKit");
while let Some(interaction) = &cic.next().await {
match interaction.data.custom_id.as_str() {
"style" => {
style = interaction.data.values[0].clone();
interaction.defer(&ctx.http).await?;
}
"select" => {
selected = true;
cic.stop();
break;
}
_ => {
unreachable!("Cannot get here..");
if !styles.is_empty() {
command
.edit_original_interaction_response(&ctx.http, |resp| {
create_styles_interaction(resp, styles)
})
.await?;

let resp = command.get_interaction_response(&ctx.http).await?;
cib = resp
.await_component_interactions(&ctx.shard)
.timeout(Duration::from_secs(30));
cic = cib.build();
selected = false;
while let Some(interaction) = &cic.next().await {
match interaction.data.custom_id.as_str() {
"style" => {
style = interaction.data.values[0].clone();
interaction.defer(&ctx.http).await?;
}
"select" => {
selected = true;
cic.stop();
break;
}
_ => {
unreachable!("Cannot get here..");
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/utls/discordhelpers/embeds.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Write as _;
use std::{env, str};

use serenity::http::Http;
use serenity::{
builder::{CreateEmbed, CreateMessage},
client::Context,
Expand Down Expand Up @@ -256,6 +257,20 @@ pub fn embed_message(emb: CreateEmbed) -> CreateMessage<'static> {
msg
}

pub async fn dispatch_embed(
http: impl AsRef<Http>,
channel: ChannelId,
emb: CreateEmbed,
) -> serenity::Result<Message> {
let emb_msg = embed_message(emb);
channel
.send_message(http, |e| {
*e = emb_msg;
e
})
.await
}

pub fn build_dblvote_embed(tag: String) -> CreateEmbed {
let mut embed = CreateEmbed::default();
embed.color(COLOR_OKAY);
Expand Down

0 comments on commit 2c877e6

Please sign in to comment.