Skip to content

Commit

Permalink
Enhancements for the register button command(s) (#118)
Browse files Browse the repository at this point in the history
* Add emoji to some messages and buttons, reword timeout message

* Change message on press to "Processing"

* Add timing

* Refractor

* Format so fmt job will pass
  • Loading branch information
SticksDev authored Oct 21, 2022
1 parent 4980fb0 commit 31318ea
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/builtins/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,27 @@ pub async fn register_application_commands_buttons<U, E>(
b.custom_id("register.guild")
.label("Register in guild")
.style(serenity::ButtonStyle::Primary)
.emoji('📋')
})
.create_button(|b| {
b.custom_id("unregister.guild")
.label("Delete in guild")
.style(serenity::ButtonStyle::Danger)
.emoji('🗑')
})
})
.create_action_row(|r| {
r.create_button(|b| {
b.custom_id("register.global")
.label("Register globally")
.style(serenity::ButtonStyle::Primary)
.emoji('📋')
})
.create_button(|b| {
b.custom_id("unregister.global")
.label("Delete globally")
.style(serenity::ButtonStyle::Danger)
.emoji('🗑')
})
})
})
Expand All @@ -183,11 +187,16 @@ pub async fn register_application_commands_buttons<U, E>(
.author_id(ctx.author().id)
.await;

reply.edit(ctx, |b| b.components(|b| b)).await?; // remove buttons after button press
reply
.edit(ctx, |b| {
b.components(|b| b).content("Processing... Please wait.")
})
.await?; // remove buttons after button press and edit message
let pressed_button_id = match &interaction {
Some(m) => &m.data.custom_id,
None => {
ctx.say("You didn't interact in time").await?;
ctx.say(":warning: You didn't interact in time - please run the command again.")
.await?;
return Ok(());
}
};
Expand All @@ -203,44 +212,59 @@ pub async fn register_application_commands_buttons<U, E>(
}
};

let start_time = std::time::Instant::now();

if global {
if register {
ctx.say(format!("Registering {} global commands...", num_commands))
.await?;
ctx.say(format!(
":gear: Registering {} global commands...",
num_commands
))
.await?;
serenity::Command::set_global_application_commands(ctx.discord(), |b| {
*b = create_commands;
b
})
.await?;
} else {
ctx.say("Unregistering global commands...").await?;
ctx.say(":gear: Unregistering global commands...").await?;
serenity::Command::set_global_application_commands(ctx.discord(), |b| b).await?;
}
} else {
let guild_id = match ctx.guild_id() {
Some(x) => x,
None => {
ctx.say("Must be called in guild").await?;
ctx.say(":x: Must be called in guild").await?;
return Ok(());
}
};
if register {
ctx.say(format!("Registering {} guild commands...", num_commands))
.await?;
ctx.say(format!(
":gear: Registering {} guild commands...",
num_commands
))
.await?;
guild_id
.set_application_commands(ctx.discord(), |b| {
*b = create_commands;
b
})
.await?;
} else {
ctx.say("Unregistering guild commands...").await?;
ctx.say(":gear: Unregistering guild commands...").await?;
guild_id
.set_application_commands(ctx.discord(), |b| b)
.await?;
}
}

ctx.say("Done!").await?;
// Calulate time taken and send message
let time_taken = start_time.elapsed();
ctx.say(format!(
":white_check_mark: Done! Took {}ms",
time_taken.as_millis()
))
.await?;

Ok(())
}

0 comments on commit 31318ea

Please sign in to comment.