Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit

Permalink
[feat] accept telegram as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
MKRhere committed Aug 19, 2020
1 parent 7188a33 commit 570337f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"bracketSpacing": true,
"jsxBracketSameLine": true,
"arrowParens": "avoid",
"printWidth": 120
"printWidth": 80
}
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
"type": "git",
"url": "[email protected]: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"
}
}
16 changes: 11 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 30 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -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.`,
);
}
});
},
Expand Down
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 570337f

Please sign in to comment.