Skip to content

Community creation env var#810

Closed
Matobi98 wants to merge 4 commits into
lnp2pBot:mainfrom
Matobi98:community-creation-env-var
Closed

Community creation env var#810
Matobi98 wants to merge 4 commits into
lnp2pBot:mainfrom
Matobi98:community-creation-env-var

Conversation

@Matobi98
Copy link
Copy Markdown
Contributor

@Matobi98 Matobi98 commented May 21, 2026

Summary

  • Introduces a COMMUNITY_CREATION_ENABLED environment variable (default: false) that controls whether the
    /community command is registered at bot startup
  • Replaces the commented-out /community command block in bot/modules/community/index.ts with a conditional
    registration guarded by the env var
  • Adds COMMUNITY_CREATION_ENABLED=false to .env-sample with an explanatory comment

Behavior

  • COMMUNITY_CREATION_ENABLED=false (default) — /community is not registered; community creation is disabled
  • COMMUNITY_CREATION_ENABLED=true/community is registered and users can create communities through the existing
    wizard flow

The wizard scene itself (COMMUNITY_WIZARD_SCENE_ID) is unchanged — it was already fully implemented.

Test plan

  • With COMMUNITY_CREATION_ENABLED unset or false, confirm /community returns no response / unknown command
  • With COMMUNITY_CREATION_ENABLED=true, confirm /community enters the community creation wizard correctly
    EOF

Summary by CodeRabbit

Release Notes

  • New Features
    • Added configurable environment setting to control community feature availability
    • Added new commands to enable or disable communities with automatic creator notifications

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 21, 2026

Walkthrough

This PR adds environment-controlled community management commands. It introduces a COMMUNITY_CREATION_ENABLED feature flag, conditionally registers the /community command, and implements disableCommunity and enableCommunity handlers that toggle community state with validation, persistence, and creator notification.

Changes

Community Management Feature Gate and Control Commands

Layer / File(s) Summary
Feature gate configuration
.env-sample, bot/modules/community/index.ts
Introduces COMMUNITY_CREATION_ENABLED environment variable (default false) and conditionally registers /community command when enabled, replacing prior commented TODO.
Community disable/enable command handlers
bot/modules/community/commands.ts
Imports User model and adds findCommunityByInput helper (resolves by @groupUsername with case-insensitive regex or by ObjectId) and buildCommunityInfoText helper for i18n replies. Exports disableCommunity and enableCommunity handlers that validate input, look up community, check not-found and state conditions, toggle enabled flag, save to database, optionally notify creator via Telegram (with error logging), and reply with i18n confirmation or generic error.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

  • #808 — Shares the COMMUNITY_CREATION_ENABLED environment variable and the conditional /community command registration logic added in bot/modules/community/index.ts.

Poem

🐰 Communities now bow to the switch,
Toggle their fate from enabled to glitch,
With whispers sent forth to creators so true,
The bot tends the garden—what grand work to do!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Community creation env var' accurately summarizes the main change: introducing an environment variable to control community creation feature enabling/disabling.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
bot/modules/community/commands.ts (1)

278-380: ⚡ Quick win

Consolidate enable/disable logic into one toggle helper.

disableCommunity and enableCommunity duplicate the same control flow, DB operations, notification path, and reply composition. Extracting a shared internal function will reduce drift risk and make future fixes safer.

♻️ Refactor sketch
+type CommunityToggleConfig = {
+  targetEnabled: boolean;
+  alreadyStateKey: string;
+  notifyKey: string;
+  infoKey: string;
+};
+
+async function toggleCommunity(ctx: MainContext, cfg: CommunityToggleConfig) {
+  const [input] = (await validateParams(
+    ctx,
+    2,
+    '\\<_community id \\| `@groupUsername_`\\>',
+  ))!;
+  if (!input) return;
+
+  const community = await findCommunityByInput(ctx, input);
+  if (community === undefined) return;
+  if (community === null) return ctx.reply(ctx.i18n.t('community_not_found'));
+
+  if (community.enabled === cfg.targetEnabled) {
+    return ctx.reply(ctx.i18n.t(cfg.alreadyStateKey));
+  }
+
+  community.enabled = cfg.targetEnabled;
+  await community.save();
+
+  const creator = await User.findById(community.creator_id);
+  const creatorUsername = creator?.username || 'unknown';
+
+  if (creator) {
+    try {
+      await ctx.telegram.sendMessage(
+        creator.tg_id,
+        ctx.i18n.t(cfg.notifyKey, { communityName: community.name }),
+      );
+    } catch (notifyError) {
+      logger.error(notifyError);
+    }
+  }
+
+  return ctx.reply(
+    buildCommunityInfoText(ctx, community, creatorUsername, cfg.infoKey),
+  );
+}
+
 export const disableCommunity = async (ctx: MainContext) => {
   try {
-    // existing duplicated body
+    return await toggleCommunity(ctx, {
+      targetEnabled: false,
+      alreadyStateKey: 'community_already_disabled',
+      notifyKey: 'community_disabled_by_admin',
+      infoKey: 'community_disabled_info',
+    });
   } catch (error) {
     logger.error(error);
     await ctx.reply(ctx.i18n.t('generic_error'));
   }
 };
 
 export const enableCommunity = async (ctx: MainContext) => {
   try {
-    // existing duplicated body
+    return await toggleCommunity(ctx, {
+      targetEnabled: true,
+      alreadyStateKey: 'community_already_enabled',
+      notifyKey: 'community_enabled_by_admin',
+      infoKey: 'community_enabled_info',
+    });
   } catch (error) {
     logger.error(error);
     await ctx.reply(ctx.i18n.t('generic_error'));
   }
 };
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@bot/modules/community/commands.ts` around lines 278 - 380, Extract the shared
logic from disableCommunity and enableCommunity into a single helper function
(e.g., toggleCommunity(ctx: MainContext, input: string, enable: boolean)) that:
validates params or accepts already-validated input, calls findCommunityByInput,
checks for null/undefined, compares/sets community.enabled based on the enable
flag, saves via community.save(), resolves creator via User.findById, sends the
appropriate notification text keys ('community_disabled_by_admin' /
'community_enabled_by_admin') and returns the same buildCommunityInfoText reply
with the correct info key ('community_disabled_info' /
'community_enabled_info'); then rewrite disableCommunity and enableCommunity to
call toggleCommunity(ctx, input, false) and toggleCommunity(ctx, input, true)
respectively, preserving existing error handling and logging (logger.error) and
keeping validateParams usage in the top-level functions if you prefer.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@bot/modules/community/commands.ts`:
- Around line 278-380: Extract the shared logic from disableCommunity and
enableCommunity into a single helper function (e.g., toggleCommunity(ctx:
MainContext, input: string, enable: boolean)) that: validates params or accepts
already-validated input, calls findCommunityByInput, checks for null/undefined,
compares/sets community.enabled based on the enable flag, saves via
community.save(), resolves creator via User.findById, sends the appropriate
notification text keys ('community_disabled_by_admin' /
'community_enabled_by_admin') and returns the same buildCommunityInfoText reply
with the correct info key ('community_disabled_info' /
'community_enabled_info'); then rewrite disableCommunity and enableCommunity to
call toggleCommunity(ctx, input, false) and toggleCommunity(ctx, input, true)
respectively, preserving existing error handling and logging (logger.error) and
keeping validateParams usage in the top-level functions if you prefer.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: fa40f34b-a6e6-4037-9874-ba4a6ed3075d

📥 Commits

Reviewing files that changed from the base of the PR and between d2c5b57 and 2165f9f.

📒 Files selected for processing (3)
  • .env-sample
  • bot/modules/community/commands.ts
  • bot/modules/community/index.ts

@Luquitasjeffrey
Copy link
Copy Markdown
Collaborator

This pr has changes not related to the title and description

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants