diff --git a/.prettierrc b/.prettierrc index 8810d52..d0bb80a 100644 --- a/.prettierrc +++ b/.prettierrc @@ -8,5 +8,5 @@ "bracketSpacing": true, "jsxBracketSameLine": true, "arrowParens": "avoid", - "printWidth": 120 + "printWidth": 80 } diff --git a/package.json b/package.json index 250cf37..5e0b1ce 100644 --- a/package.json +++ b/package.json @@ -11,17 +11,18 @@ "type": "git", "url": "git@github.com:telecraft/plugin" }, - "author": "Feathers Studio <@feathersstudio> (https://feathersstudio.com)", + "author": "Feathers Studio <@feathersstudio> (https://feathers.studio)", "contributors": [ "Muthu Kumar <@MKRhere> (https://mkr.pw)" ], "license": "MIT", "devDependencies": { - "tslib": "^2.0.0", - "typescript": "^3.9.5", - "@types/node": "^14.0.14" + "@types/node": "^14.0.14", + "typescript": "^3.9.5" }, "dependencies": { - "telegraf": "^3.38.0" + "@telecraft/types": "^0.2.0", + "telegraf": "^3.38.0", + "tslib": "^2.0.1" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8e864d2..0463e2c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,11 +1,16 @@ dependencies: + '@telecraft/types': 0.2.0 telegraf: 3.38.0 + tslib: 2.0.1 devDependencies: '@types/node': 14.0.14 - tslib: 2.0.0 typescript: 3.9.5 lockfileVersion: 5.1 packages: + /@telecraft/types/0.2.0: + dev: false + resolution: + integrity: sha512-PFmyF4bxn/Vaf8lji34iuqQwpMQLLqC9d6rWf36ifghpB5EhVhqF/ZAwJRbVBqnLsocRH4s4GKgjwBuSvlHbOQ== /@types/node/14.0.14: dev: true resolution: @@ -58,10 +63,10 @@ packages: dev: false resolution: integrity: sha512-njVv1EAhIZnmQVLocZEADYUyqA1WIXuVcDYlsp+mXua/XB0pxx+PKtMSPeZ/EE4wPWTw9h/hA9ASTT6yQelkiw== - /tslib/2.0.0: - dev: true + /tslib/2.0.1: + dev: false resolution: - integrity: sha512-lTqkx847PI7xEDYJntxZH89L2/aXInsyF2luSafe/+0fHOMjlBNXdH6th7f70qxLDhul7KZK0zC8V5ZIyHl0/g== + integrity: sha512-SgIkNheinmEBgx1IUNirK0TUD4X9yjjBRTqqjggWCU3pUEqIk3/Uwl3yRixYKT6WjQuGiwDv4NomL3wqRCj+CQ== /typescript/3.9.5: dev: true engines: @@ -70,7 +75,8 @@ packages: resolution: integrity: sha512-hSAifV3k+i6lEoCJ2k6R2Z/rp/H3+8sdmcn5NrS3/3kE7+RyZXm9aqvxWqjEXHAd8b0pShatpcdMTvEdvAJltQ== specifiers: + '@telecraft/types': ^0.2.0 '@types/node': ^14.0.14 telegraf: ^3.38.0 - tslib: ^2.0.0 + tslib: ^2.0.1 typescript: ^3.9.5 diff --git a/src/index.ts b/src/index.ts index fcf2d71..683cb5a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,12 +5,23 @@ const rand = () => String(Math.ceil(Math.random() * 10000)); const gamemodes = ["survival", "creative", "adventure", "survival"]; -const auth: Plugin = { +const auth: Plugin< + { enable: boolean }, + { telegram: { send: (user: string, msg: string) => void } } +> = { name: "telegram-auth", - plugin: (config, events, store, server) => { + plugin: ({ config = {}, dependencies: { telegram = null } = {} } = {}) => ( + events, + store, + server, + ) => { if (!config.enable) return; + if (!telegram) + throw new Error( + "[@telecraft/telegram-auth] Plugin was enabled, but dependency 'telegram' was not passed", + ); - events.on(["minecraft", "join"], async ctx => { + events.on("minecraft:join", async ctx => { const tgUser = await store.get(["minecraft", ctx.user, "tgUser"]); server.send(`data get entity ${ctx.user} playerGameType`); @@ -21,43 +32,48 @@ const auth: Plugin = { server.send(`gamemode spectator ${ctx.user}`); if (tgUser) { - events.emit(["telegram", "send"], { user: tgUser, msg: "Send /auth to authenticate yourself." }); + telegram.send(tgUser, "Send /auth to authenticate yourself."); } else { server.send(`tellraw ${ctx.user} Send /link to the Telegram bot.`); } }); - events.on(["telegram", "link"], async ctx => { + events.on("telegram:link", async ctx => { const token = rand(); const tgUser = ctx.user; await store.set(["telegram", token, "tgUser"], tgUser); - events.emit(["telegram", "send"], { user: tgUser, msg: `Send !link ${token} in Minecraft chat to link.` }); + telegram.send(tgUser, `Send !link ${token} in Minecraft chat to link.`); }); - events.on(["minecraft", "message"], async ctx => { + events.on("minecraft:message", async ctx => { const [cmd, ...rest] = ctx.msg.split(" "); if (cmd === "!link") { const [token] = rest; const tgUser = await store.get(["telegram", token, "tgUser"]); if (token && tgUser) { await store.set(["telegram", tgUser, "user"], ctx.user); - server.send(`tellraw ${ctx.user} Successfully linked with Telegram user.`); - events.emit(["telegram", "send"], { - user: tgUser, - msg: `Successfully linked with Minecraft player ${ctx.user}.`, - }); + server.send( + `tellraw ${ctx.user} Successfully linked with Telegram user.`, + ); + telegram.send( + tgUser, + `Successfully linked with Minecraft player ${ctx.user}.`, + ); } } }); - events.on(["telegram", "auth"], async ctx => { + events.on("telegram:auth", async ctx => { const tgUser = ctx.user; const player = await store.get(["telegram", tgUser, "user"]); if (!player) { - events.emit(["telegram", "send"], { user: tgUser, msg: `Not linked to a Minecraft player. Send /link first.` }); + telegram.send( + tgUser, + `Not linked to a Minecraft player. Send /link first.`, + ); } }); }, diff --git a/tsconfig.json b/tsconfig.json index 6172beb..2b5224e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,11 +4,11 @@ "target": "es2018" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, "lib": ["es2018"] /* Specify library files to be included in the compilation. */, - "sourceMap": true /* Generates corresponding '.map' file. */, "strict": true /* Enable all strict type-checking options. */, - "noErrorTruncation": true, - "moduleResolution": "node", - "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + "declaration": true /* Generates corresponding d.ts files. */, + "noErrorTruncation": true /* Generates corresponding d.ts files. */, + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, + "moduleResolution": "node" }, "include": ["./src", "./src/**/*.ts"], "exclude": ["node_modules"]