fix: remove manual permission check in /watch and validate via Discord API
Some checks are pending
CI / Test & Lint (push) Waiting to run

This commit is contained in:
katR 2026-10-10 09:58:22 +05:30
parent acd3f948f2
commit f4a4f35d52

View file

@ -478,28 +478,6 @@ async fn watch(
)
.await;
}
let bot_id = ctx.serenity_context().cache.current_user().id;
let member = guild.member(ctx.serenity_context(), bot_id).await?;
#[allow(deprecated)]
let permissions = if let Some(guild_cache) = ctx.guild() {
guild_cache.user_permissions_in(&channel, &member)
} else {
channel
.permissions_for_user(ctx.serenity_context(), bot_id)
.unwrap_or_else(|_| serenity::Permissions::all())
};
if !permissions.contains(
serenity::Permissions::VIEW_CHANNEL
| serenity::Permissions::SEND_MESSAGES
| serenity::Permissions::EMBED_LINKS,
) {
return reply(
ctx,
embed("Missing bot permissions")
.description("Allow View Channel, Send Messages, and Embed Links here first."),
)
.await;
}
let roles = guild.roles(ctx.serenity_context()).await?;
let mut guard = ctx.data().store.lock().await;
let mut sub = match patch_subscription(
@ -517,14 +495,29 @@ async fn watch(
.await;
}
};
if sub.ping_enabled
&& !sub
if sub.ping_enabled {
let role_is_valid = sub
.role_id
.and_then(|id| roles.get(&serenity::RoleId::new(id)))
.is_some_and(|role| valid_role(role, guild))
{
drop(guard);
return reply(ctx, embed("Invalid role").description("The configured role is missing. Set ping_role to a valid role or @everyone, or use ping_enabled:false or clear_role:true.")).await;
.map(|id| {
if id == guild.get() {
true
} else {
roles
.get(&serenity::RoleId::new(id))
.is_some_and(|role| valid_role(role, guild))
}
})
.unwrap_or(false);
if !role_is_valid {
drop(guard);
return reply(
ctx,
embed("Invalid role").description(
"The configured role is missing. Set ping_role to a valid role or @everyone, or use ping_enabled:false or clear_role:true.",
),
)
.await;
}
}
// Post or update the public live dashboard in the channel
@ -543,8 +536,8 @@ async fn watch(
{
updated_msg_id = Some(existing_msg_id);
}
if updated_msg_id.is_none()
&& let Ok(sent) = channel
if updated_msg_id.is_none() {
match channel
.id
.send_message(
ctx.serenity_context(),
@ -553,8 +546,22 @@ async fn watch(
.allowed_mentions(no_mentions()),
)
.await
{
updated_msg_id = Some(sent.id.get());
{
Ok(sent) => {
updated_msg_id = Some(sent.id.get());
}
Err(e) => {
drop(guard);
return reply(
ctx,
embed("Missing channel permissions").description(format!(
"The bot could not post the live dashboard in <#{}>: {e}.\nMake sure the bot has **View Channel**, **Send Messages**, and **Embed Links** in that channel.",
channel.id
)),
)
.await;
}
}
}
sub.message_id = updated_msg_id;
@ -743,12 +750,19 @@ async fn testping(ctx: Context<'_>) -> Result<(), Error> {
.await;
};
let roles = guild.roles(ctx.serenity_context()).await?;
if !sub.ping_enabled
|| !sub
.role_id
.and_then(|id| roles.get(&serenity::RoleId::new(id)))
.is_some_and(|role| valid_role(role, guild))
{
let role_is_valid = sub
.role_id
.map(|id| {
if id == guild.get() {
true
} else {
roles
.get(&serenity::RoleId::new(id))
.is_some_and(|role| valid_role(role, guild))
}
})
.unwrap_or(false);
if !sub.ping_enabled || !role_is_valid {
return reply(
ctx,
embed("Pings unavailable").description(