import { Game } from "npm:@gathertown/gather-game-client@42.0.0";
import { load } from "https://deno.land/std@0.197.0/dotenv/mod.ts";
const env = await load();
// 設定情報
const BOT_NAME = "TOD_BOT";
const API_KEY = env["API_KEY"];
const SPACE_ID = env["SPACE_ID"];
const SPACE_NAME = env["SPACE_NAME"];
const SPACE_INFO = `${SPACE_ID}\\${SPACE_NAME}`;
// 初期化
const game = new Game(SPACE_INFO, () => Promise.resolve({ apiKey: API_KEY }));
// 接続
game.connect();
// ボット参加
game.enter({
name: BOT_NAME,
isNpc: true,
});
// 接続時の処理
game.subscribeToConnection((connected) => {
console.log(`接続可否: ${connected}`);
});
// チャット動作時の処理
// イベント: playerChats
// URL: http://gather-game-client-docs.s3-website-us-west-2.amazonaws.com/classes/Game.html#subscribeToEvent
game.subscribeToEvent("playerChats", (data) => {
// 「/todbot」で発火
const contents = data.playerChats.contents;
if (!contents.startsWith("/todbot ")) {
return;
}
// 「/todbot」より右を抽出。
const text = contents.replace(/\S+\s+/, "");
console.log(text);
// 返信
// URL: http://gather-game-client-docs.s3-website-us-west-2.amazonaws.com/classes/Game.html#chat
game.chat(
data.playerChats.recipient,
[],
data.playerChats.roomId || "",
{
contents: text,
},
);
});