Skip to content

Commit c0d05f3

Browse files
committed
chore(devforum-rank-verification): Add a roblox_verified role to make an early check and avoid making unnecessary API calls
1 parent 84871a7 commit c0d05f3

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ The following is an example of the file structure:
1212
roles:
1313
devforum_member: "ROLE_ID"
1414
devforum_regular: "ROLE_ID"
15+
# Optional, allows the bot to avoid making unnecessary API calls
16+
roblox_verified: "ROLE_ID"
1517
```
1618
1719
[ci badge]:https://img.shields.io/github/actions/workflow/status/archasion/discord-bot-rs/ci.yml?branch=main&event=push&label=CI

bot/src/components/verify_devforum_rank.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,37 @@ impl ComponentHandler for VerifyDevForumRank<'_> {
3636
async fn exec(&self, ctx: crate::Context) -> anyhow::Result<()> {
3737
let guild_id = self.cmd.guild_id.context("get guild id")?;
3838
let author_id = self.cmd.author_id().context("get interaction author id")?;
39+
let member_roles = ctx
40+
.http
41+
.guild_member(guild_id, author_id)
42+
.await
43+
.context("get guild member")?
44+
.model()
45+
.await?
46+
.roles;
47+
48+
// Respond early if the user doesn't have the verified role
49+
// this is a quick check to avoid unnecessary API calls
50+
if let Some(r_id) = ctx.cfg.roles.roblox_verified {
51+
if !member_roles.contains(&r_id) {
52+
ctx.http
53+
.interaction(self.cmd.application_id)
54+
.create_response(self.cmd.id, &self.cmd.token, &InteractionResponse {
55+
kind: InteractionResponseType::ChannelMessageWithSource,
56+
data: Some(
57+
InteractionResponseDataBuilder::new()
58+
.flags(MessageFlags::EPHEMERAL)
59+
.content("You must be Roblox verified to use this command.")
60+
.build(),
61+
),
62+
})
63+
.await
64+
.context("respond to interaction")?;
65+
return Ok(());
66+
}
67+
}
3968

40-
// Defer the interaction response
69+
// Defer the interaction response since the API calls may take some time
4170
ctx.http
4271
.interaction(self.cmd.application_id)
4372
.create_response(self.cmd.id, &self.cmd.token, &InteractionResponse {

bot/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub(crate) struct Config {
1212
pub(crate) struct ConfigRoles {
1313
pub(crate) devforum_member: Id<RoleMarker>,
1414
pub(crate) devforum_regular: Id<RoleMarker>,
15+
pub(crate) roblox_verified: Option<Id<RoleMarker>>,
1516
}
1617

1718
#[tracing::instrument(ret)]

0 commit comments

Comments
 (0)