Skip to content

Commit 6c10710

Browse files
Introduce passive mode (TCCPP#107)
In passive mode, Wheatley will not advertise or react to commands, and only load components that are marked as passive. This is to allow additional secondary instances of the bot to coexist with a running primary instance on the same server without interference, e.g., for testing new features.
1 parent ee562cb commit 6c10710

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ Secrets and other bot info must be configured in the `auth.json` file. An exampl
5454
"host": "127.0.0.1", // optional
5555
"port": 27017 // optional
5656
},
57-
"freestanding": false
57+
"freestanding": false, // optional
58+
"passive": false // optional
5859
}
5960
```
6061

6162
Mongo credentials can be omitted locally if you don't need to work on components that use mongo. `freestanding: true`
6263
can be specified to turn on only components which don't rely on channels etc. specific to Together C & C++ to exist.
6364
Freestanding mode also disables connecting to MongoDB.
65+
`passive: true` can be specified to run the bot in passive mode, where it will not advertise or react to commands, and only load components that are marked as passive (useful for testing new features in a second instance without interfering with a running primary instance).
6466

6567
## Bot Component Abstraction
6668

@@ -71,6 +73,9 @@ export class BotComponent {
7173
static get is_freestanding() {
7274
return false;
7375
}
76+
static get is_passive() {
77+
return false;
78+
}
7479
// Add a command
7580
add_command<T extends unknown[]>(
7681
command:

src/bot-component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export class BotComponent {
1515
return false;
1616
}
1717

18+
static get is_passive() {
19+
return false;
20+
}
21+
1822
constructor(protected readonly wheatley: Wheatley) {
1923
wheatley.event_hub.on("wheatley_ready", this.wrap(this.on_wheatley_ready.bind(this)));
2024
}

src/wheatley.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export type wheatley_auth = {
6666
guild?: string;
6767
token: string;
6868
freestanding?: boolean;
69+
passive?: boolean;
6970
mongo?: wheatley_database_credentials;
7071
sentry?: string;
7172
virustotal?: string;
@@ -316,6 +317,8 @@ export class Wheatley {
316317
readonly guildId: string;
317318
// True if freestanding mode is enabled. Defaults to false.
318319
readonly freestanding: boolean;
320+
// True if passive mode is enabled. Defaults to false.
321+
readonly passive: boolean;
319322

320323
// Some emojis
321324
readonly pepereally = "<:pepereally:643881257624666112>";
@@ -426,6 +429,7 @@ export class Wheatley {
426429
) {
427430
this.id = auth.id;
428431
this.freestanding = auth.freestanding ?? false;
432+
this.passive = auth.passive ?? false;
429433
this.guildId = auth.guild ?? TCCPP_ID;
430434

431435
this.parameters = drop_token(auth);
@@ -602,8 +606,12 @@ export class Wheatley {
602606
await wrap(() => this.fetch_root_mod_list(this.client));
603607
}
604608

605-
async add_component<T extends BotComponent>(component: { new (w: Wheatley): T; get is_freestanding(): boolean }) {
606-
if (!this.freestanding || component.is_freestanding) {
609+
async add_component<T extends BotComponent>(component: {
610+
new (w: Wheatley): T;
611+
get is_freestanding(): boolean;
612+
get is_passive(): boolean;
613+
}) {
614+
if ((!this.passive || component.is_passive) && (!this.freestanding || component.is_freestanding)) {
607615
M.log(`Initializing ${component.name}`);
608616
assert(!this.components.has(component.name), "Duplicate component name");
609617
const instance = new component(this);
@@ -964,6 +972,9 @@ export class Wheatley {
964972
| MessageContextMenuInteractionBuilder<true>
965973
| ModalInteractionBuilder<true>,
966974
) {
975+
if (this.passive) {
976+
return;
977+
}
967978
if (command instanceof TextBasedCommandBuilder) {
968979
for (const descriptor of command.to_command_descriptors(this)) {
969980
assert(!(descriptor.name in this.text_commands));
@@ -986,6 +997,9 @@ export class Wheatley {
986997

987998
// returns false if the message was not a wheatley command
988999
async handle_text_command(message: Discord.Message, prev_command_obj?: TextBasedCommand) {
1000+
if (this.passive) {
1001+
return false;
1002+
}
9891003
const match = message.content.match(Wheatley.command_regex);
9901004
if (match) {
9911005
const command_name = match[1];

0 commit comments

Comments
 (0)