# palopil/counter-bot-js architecture

> Architecture diagram of the GitHub repository palopil/counter-bot-js, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/palopil/counter-bot-js
- Repository: https://github.com/palopil/counter-bot-js

## Overview

counter-bot is a Discord bot for configuring per-server counting channels and enforcing sequential counts. The main workflow is: a server administrator runs /setup, the bot saves the rules and posts the starting number, then message events load those rules, evaluate submitted expressions through the external Matheval service, and update the count or apply a timeout role. Edit and delete events also protect the current count. The sampled source shows these paths; other runtime wiring is limited to the event and command loader in index.js.

Main entry points: [`commands/setup.js`](https://github.com/palopil/counter-bot-js/blob/main/commands/setup.js) (Setup command), [`lib/utils.js`](https://github.com/palopil/counter-bot-js/blob/main/lib/utils.js) (Count utilities), [`events/messageCreate.js`](https://github.com/palopil/counter-bot-js/blob/main/events/messageCreate.js) (New messages), [`events/messageUpdate.js`](https://github.com/palopil/counter-bot-js/blob/main/events/messageUpdate.js) (Edited messages)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_setup["Server setup"]
  node_setup_command["Setup command<br/>[setup.js]"]
  node_guild_config[("Guild configuration<br/>[setup.js]")]
  node_starting_post["Starting number<br/>[setup.js]"]
end

subgraph group_counting["Counting workflow"]
  node_message_create["New messages<br/>[messageCreate.js]"]
  node_message_update["Edited messages<br/>[messageUpdate.js]"]
  node_message_delete["Deleted messages<br/>[messageDelete.js]"]
  node_count_utils["Count utilities<br/>[utils.js]"]
  node_matheval["Matheval calculator"]
  node_timeout_role["Timeout role"]
end

subgraph group_support["Bot runtime"]
  node_interaction_router["Interaction router"]
  node_event_loader["Event and command loader<br/>[index.js]"]
  node_ready_presence["Ready presence<br/>[ready.js]"]
end

node_admin(("Server admin"))
node_discord(("Discord"))

node_admin -->|"runs setup"| node_setup_command
node_discord -.->|"delivers interaction"| node_setup_command
node_setup_command -->|"writes settings"| node_guild_config
node_setup_command -->|"posts start"| node_starting_post
node_setup_command -->|"replies with embed"| node_discord
node_discord -->|"delivers interactions"| node_interaction_router
node_interaction_router -->|"dispatches command"| node_setup_command
node_discord -->|"delivers messages"| node_message_create
node_discord -->|"delivers edits"| node_message_update
node_discord -->|"delivers deletions"| node_message_delete
node_message_create -->|"loads and updates count"| node_count_utils
node_message_update -->|"loads and parses count"| node_count_utils
node_message_delete -->|"loads and parses count"| node_count_utils
node_count_utils -->|"reads and writes state"| node_guild_config
node_count_utils -->|"posts expression"| node_matheval
node_message_create -->|"reacts or deletes"| node_discord
node_message_create -->|"applies timeout"| node_timeout_role
node_message_update -->|"applies timeout"| node_timeout_role
node_message_delete -->|"applies timeout"| node_timeout_role
node_message_update -->|"restores original"| node_discord
node_message_delete -->|"reposts deleted count"| node_discord
node_discord -->|"signals ready"| node_ready_presence
node_event_loader -->|"registers handlers"| node_discord

click node_setup_command "https://github.com/palopil/counter-bot-js/blob/main/commands/setup.js"
click node_guild_config "https://github.com/palopil/counter-bot-js/blob/main/commands/setup.js"
click node_starting_post "https://github.com/palopil/counter-bot-js/blob/main/commands/setup.js"
click node_message_create "https://github.com/palopil/counter-bot-js/blob/main/events/messageCreate.js"
click node_message_update "https://github.com/palopil/counter-bot-js/blob/main/events/messageUpdate.js"
click node_message_delete "https://github.com/palopil/counter-bot-js/blob/main/events/messageDelete.js"
click node_count_utils "https://github.com/palopil/counter-bot-js/blob/main/lib/utils.js"
click node_interaction_router "https://github.com/palopil/counter-bot-js/blob/main/events/interactionCreate.js"
click node_event_loader "https://github.com/palopil/counter-bot-js/blob/main/index.js"
click node_ready_presence "https://github.com/palopil/counter-bot-js/blob/main/events/ready.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_setup_command,node_guild_config,node_starting_post toneBlue
class node_message_create,node_message_update,node_message_delete,node_count_utils,node_matheval,node_timeout_role toneAmber
class node_interaction_router,node_event_loader,node_ready_presence,node_admin toneMint
class node_discord toneIndigo
```

## Components

### Server setup

- [Setup command](https://github.com/palopil/counter-bot-js/blob/main/commands/setup.js): `commands/setup.js`
- [Guild configuration](https://github.com/palopil/counter-bot-js/blob/main/commands/setup.js): `commands/setup.js`
- [Starting number](https://github.com/palopil/counter-bot-js/blob/main/commands/setup.js): `commands/setup.js`

### Counting workflow

- [New messages](https://github.com/palopil/counter-bot-js/blob/main/events/messageCreate.js): `events/messageCreate.js`
- [Edited messages](https://github.com/palopil/counter-bot-js/blob/main/events/messageUpdate.js): `events/messageUpdate.js`
- [Deleted messages](https://github.com/palopil/counter-bot-js/blob/main/events/messageDelete.js): `events/messageDelete.js`
- [Count utilities](https://github.com/palopil/counter-bot-js/blob/main/lib/utils.js): `lib/utils.js`
- **Matheval calculator**
- **Timeout role**

### Bot runtime

- [Interaction router](https://github.com/palopil/counter-bot-js/blob/main/events/interactionCreate.js): `events/interactionCreate.js`
- [Event and command loader](https://github.com/palopil/counter-bot-js/blob/main/index.js): `index.js`
- [Ready presence](https://github.com/palopil/counter-bot-js/blob/main/events/ready.js): `events/ready.js`

### Other components

- **Server admin**
- **Discord**

## Connections

- Server admin → Setup command: runs setup (evidence: `commands/setup.js`)
- Discord → Setup command: delivers interaction
- Setup command → Guild configuration: writes settings (evidence: `commands/setup.js`)
- Setup command → Starting number: posts start (evidence: `commands/setup.js`)
- Setup command → Discord: replies with embed (evidence: `commands/setup.js`)
- Discord → Interaction router: delivers interactions
- Interaction router → Setup command: dispatches command (evidence: `events/interactionCreate.js`)
- Discord → New messages: delivers messages
- Discord → Edited messages: delivers edits
- Discord → Deleted messages: delivers deletions
- New messages → Count utilities: loads and updates count (evidence: `events/messageCreate.js`)
- Edited messages → Count utilities: loads and parses count (evidence: `events/messageUpdate.js`)
- Deleted messages → Count utilities: loads and parses count (evidence: `events/messageDelete.js`)
- Count utilities → Guild configuration: reads and writes state (evidence: `lib/utils.js`)
- Count utilities → Matheval calculator: posts expression (evidence: `lib/utils.js`)
- New messages → Discord: reacts or deletes (evidence: `events/messageCreate.js`)
- New messages → Timeout role: applies timeout (evidence: `events/messageCreate.js`)
- Edited messages → Timeout role: applies timeout (evidence: `events/messageUpdate.js`)
- Deleted messages → Timeout role: applies timeout (evidence: `events/messageDelete.js`)
- Edited messages → Discord: restores original (evidence: `events/messageUpdate.js`)
- Deleted messages → Discord: reposts deleted count (evidence: `events/messageDelete.js`)
- Discord → Ready presence: signals ready
- Event and command loader → Discord: registers handlers (evidence: `index.js`)
