cbot is a discord bot written in pure C99 designed with an emphasis on deterministic performance and architectural simplicity. The codebase strongly adheres to -Wpedantic standards and utilises a modular command dispatch system that ensures maintainability without external dependencies beyond the core API wrapper. Key technical highlights include a manual memory management strategy that maintains a maximum resident set size between 11.6MB to 11.8MB, a native BF interpreter that is immune to infinite loops and memory exhaustion, and a simple banking system with persistence.
The bot implements a flat-file banking architecture for transparent state persistence. This aids in avoiding the overhead and black box nature of traditional database engines. Security is handled with rigorous runtime validation against integer overflows and underflows whilst keeping race conditions related to banking persistence structurally eliminated. This elimination is achieved as a result of the fact that bank state is stored only when the bot is shutdown via command, and the command is restricted to the master of the bot. Effectively, the master is the mutex.
-
[DETERMINISTIC MEMORY FOOTPRINT] — The application operates with an RSS profile ranging from 11.6MB to 11.8MB. Heap fragmentation is a non-issue here as I’ve opted for static struct initialisation, fixed-width types and stack-allocated string buffers to ensure the allocator stays out of the way.
-
[YSTAR PRNG] — The bot utilises my custom ystar PRNG, which has been rigorously validated via Wald-Wolfowitz Runs and Lag-1 Correlation testing and more. Based on the tests, it has statistically sound randomness.
-
[BF INTERPRETER] — A native BF interpreter is implemented into the bot. It is immune to infinite loops and out-of-bounds errors as I’ve implemented a strict, large instruction cap. Thanks to the speed of C, the overhead of this mechanism is functionally invisible and execution remains effectively instantaneous.
-
[BANK PERSISTENCE] — No external DB engines were used. The banking system utilises a fundamentally inspectable flat-file architecture, stored as a plaintext file. The black box nature of binary blobs is avoided with this approach, although it is reasonably simple to switch to so in case it ever becomes a bottle neck.
-
[ZERO RACE CONDITIONS] — Race conditions related to bank persistence are mathematically impossible by design. This is due to the fact that the state is only committed (saved) when the
diecommand is executed, which is restricted to the master ID. The master is the mutex. -
[PEDANTIC C99 COMPLIANCE] — This bot is written in pure C99 and compiled with
-Wall -Wextra -Wpedantic. No GNU extensions were used, zero compiler tricks, and zero third-party dependencies outside of the Discord wrapper library concord. -
[EXTENSIBLE MODULAR DESIGN] — The command dispatch is handled through a clean
cmd_tregistry. This allows for good readability and provides a great maintainability experience without the spaghetti found in usual bot logic. -
[HARDENED BANKING OPERATIONS] — Banking operations that involve transaction of money undergoes rigorous runtime validation. This includes underflow protection, overflow protection, input sanitation and registration state checks where appropriate. The overhead caused by these runtime checks are practically nil, thanks to C’s speed.
-
[ECONOMIC THROTTLING] — Certain banking commands such as
workandgambleare equipped with cooldowns to prevent state exhaustion, cheating and quick earning via spamming. -
[SELF-DOCUMENTING] — This bot features a
helpcommand. The help system is dynamically generated at runtime from the internal command dispatch table. Due to this style of implementation, adding functionality does not require you to manually update string constants in five different files, you just define the logic and add a new entry to the table. -
[SELF-DOCUMENTING #2] — Certain commands that require one or more arguments are equipped with usage feedback. In case when a user provides an invalid set of arguments or uses such command incorrectly, the bot prints the proper syntax. This feature was inspired from ordinary CLI utility tools.
This project wasn’t really created to compete with existing discord bots, rather it was created as a fun project that gives me an excuse to evaluate the suitability of C for writing discord bots. So far as per my personal experience, the development process has proven that C is remarkably pleasant for writing discord bots. In my opinion this level of stability and efficiency is not achieved merely by the language itself (although that counts), rather I believe that it is achieved by being disciplined about writing code and maintaining a clean, manual approach to system resources.
This project is licensed under the GNU Affero General Public License version 3.0 or later.
NO WARRANTY PROVIDED
For more information, see LICENSE file or visit https://www.gnu.org/licenses/agpl-3.0.en.html.