SoulFire LogoSoulFire
SDKRecipes

Reply to chat

Watch chat messages and respond from a bot.

This program starts a bot, watches for ping, and sends pong. It keeps running until you stop it. SoulFire reconnects the bot if its Minecraft connection drops.

reply-to-chat.ts
import { Effect, Stream } from "effect";
import { SoulFire } from "@soulfiremc/sdk";

function env(name: string): string {
  const value = process.env[name];
  if (!value) throw new Error(`Set ${name} before running this recipe`);
  return value;
}

const program = Effect.scoped(
  Effect.gen(function* () {
    const soulfire = yield* SoulFire.connect({
      baseUrl: env("SOULFIRE_URL"),
      token: env("SOULFIRE_TOKEN"),
    });

    const bot = soulfire
      .instance(env("SOULFIRE_INSTANCE_ID"))
      .bot(env("SOULFIRE_BOT_ID"));
    yield* bot.start();

    yield* bot.chat.watch("ping").pipe(
      Stream.runForEach((match) =>
        Effect.logInfo("Matched chat", match.event).pipe(
          Effect.andThen(bot.chat.send("pong")),
        ),
      ),
    );
  }),
);

await Effect.runPromise(program);

The matcher accepts a string, regular expression, or function. If the bot should respond only to a particular chat source, pass sources to watch. See chat events for the event shape.

How is this page?

Last updated on