summaryrefslogtreecommitdiff
path: root/autofeur_nova/src/index.mts
blob: b808d4d829b76d618f633c7547f7d817b1655406 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import "source-map-support";
import {
  GatewayMessageCreateDispatch,
  RESTPostAPIChannelMessageJSONBody,
  Routes,
} from "discord-api-types/v10";
import { match } from "./algo.mjs";
import { Client } from "@discordnova/nova-js/src/lib/client.js";

export const NATS = process.env.NATS || "localhost:4222";
export const REST = process.env.REST || "http://localhost:8090/api";

(async () => {
  const emitter = new Client({
    transport: {
      additionalEvents: [],
      nats: {
        servers: [NATS],
      },
      queue: "nova-worker-common",
    },
    rest: {
      api: REST,
    },
  });

  emitter.on(
    "messageCreate",
    async (message: GatewayMessageCreateDispatch["d"]) => {
      let response = await match(message.content);
      if (response) {
        await emitter.rest.post(Routes.channelMessages(message.channel_id), {
          body: {
            content: response,
            message_reference: { message_id: message.id },
          } as RESTPostAPIChannelMessageJSONBody,
        });
      }
    }
  );

  // We connect ourselves to the nova nats broker.
  await emitter.start();
})();