bedrock-portal 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  [![NPM version](https://img.shields.io/npm/v/bedrock-portal.svg)](http://npmjs.com/package/bedrock-portal)
3
3
  [![Discord](https://img.shields.io/badge/chat-on%20discord-brightgreen.svg)](https://discord.gg/KTyd9HWuBD)
4
4
 
5
- Handles and creates a Minecraft Bedrock game session which will redirect players to the specified server
5
+ Handles and creates a Minecraft Bedrock game session which will redirect players to the specified server. Join our [Discord](https://discord.com/invite/KTyd9HWuBD) for support.
6
6
 
7
7
  ## Installation
8
8
  ```shell
@@ -57,6 +57,43 @@ main();
57
57
 
58
58
  Modules are used to extend the functionality of the BedrockPortal class.
59
59
 
60
+ ### redirectFromRealm
61
+
62
+ > Requires [bedrock-protocol](https://github.com/PrismarineJS/bedrock-protocol) to be installed. `npm install bedrock-protocol`
63
+
64
+ Invites players when they join a Realm to the specified server or if they use the chat command. `#.use(Modules.redirectFromRealm, options);`
65
+
66
+ Options:
67
+ - **clientOptions**: ClientOptions - The client options to use when connecting to the Realm. These are passed directly to a [bedrock-protocol createClient](https://github.com/PrismarineJS/bedrock-protocol/blob/master/docs/API.md#becreateclientoptions--client) function. See the documentation for more information.
68
+ - **chatCommand**: object - Options for the chat command
69
+ - **enabled**: boolean - Whether sending the command in chat should trigger an invite (default: true)
70
+ - **message**: string - The message to send in chat to run the command (default: 'invite')
71
+ - **cooldown**: number - The cooldown between being able to send the command in chat (default: 60000ms)
72
+
73
+ ```js
74
+ const { BedrockPortal, Modules } = require('bedrock-portal');
75
+
76
+ const portal = new BedrockPortal(auth, { ... })
77
+
78
+ portal.use(Modules.redirectFromRealm, {
79
+ // The client options to use when connecting to the Realm.
80
+ clientOptions: {
81
+ realms: {
82
+ realmInvite: ''
83
+ }
84
+ }
85
+ // Options for the chat command
86
+ chatCommand: {
87
+ // Whether sending the command in chat should trigger an invite (optional - defaults to true)
88
+ enabled: true,
89
+ // The message to send in chat to run the command (optional - defaults to 'invite')
90
+ message: 'invite',
91
+ // The cooldown between being able to send the command in chat (optional - defaults to 60000ms)
92
+ cooldown: 60000,
93
+ }
94
+ }
95
+ ```
96
+
60
97
  ### autoFriendAdd
61
98
 
62
99
  Automatically adds the account's followers as friends and invites them to the game. `#.use(Modules.autoFriendAdd);`
@@ -97,7 +134,7 @@ Automatically invites players to the game when they send a message in the chat.
97
134
 
98
135
  Options:
99
136
  - **command**: string - The command to use to invite players (default: 'invite')
100
- - **checkInterval**: number - How often to check for messages (default: 1000ms)
137
+ - **checkInterval**: number - How often to check for messages (default: 30000ms)
101
138
 
102
139
  ```js
103
140
  const { BedrockPortal, Modules } = require('bedrock-portal');
@@ -111,7 +148,7 @@ portal.use(Modules.inviteOnMessage);
111
148
  portal.use(Modules.inviteOnMessage, {
112
149
  // The command to use to invite players (optional - defaults to 'invite')
113
150
  command: 'invite',
114
- // How often to check for messages (optional - defaults to 1000ms)
151
+ // How often to check for messages (optional - defaults to 30000ms)
115
152
  checkInterval: 30000,
116
153
  });
117
154
  ```
@@ -0,0 +1,58 @@
1
+ /* eslint-disable no-multi-spaces */
2
+
3
+ const { BedrockPortal, Modules, Joinability } = require('bedrock-portal');
4
+ const { Authflow, Titles } = require('prismarine-auth');
5
+
6
+ const main = async () => {
7
+ const auth = new Authflow('example', './', { authTitle: Titles.MinecraftNintendoSwitch, deviceType: 'Nintendo', flow: 'live' });
8
+
9
+ const portal = new BedrockPortal(auth, {
10
+ ip: 'geyserconnect.net',
11
+ port: 19132, // The port of the server (optional - defaults to 19132)
12
+ joinability: Joinability.FriendsOfFriends, // The joinability of the session (optional - defaults to Joinability.FriendsOfFriends)
13
+ disableAltCheck: false, // Disables the alt check (optional - defaults to false)
14
+ });
15
+
16
+ portal.use(Modules.redirectFromRealm, { // Automatically invite players to the server when they join a Realm
17
+ clientOptions: { // Options for the bedrock-protocol client, see https://github.com/PrismarineJS/bedrock-protocol for options
18
+ realms: {
19
+ realmId: '', // The realm ID of the realm to invite players from
20
+ realmInvite: '', // The invite code of the realm to invite players from
21
+ pickRealm: (realms) => realms[0], // A function to pick a realm from the list of realms
22
+ },
23
+ onMsaCode: console.log, // A function to handle the MSA code
24
+ },
25
+ chatCommand: { // Options for the chat command
26
+ enabled: true, // Whether sending the command in chat should trigger an invite (optional - defaults to true)
27
+ cooldown: 60000, // The cooldown of the command in milliseconds (optional - defaults to 60000)
28
+ message: 'invite', // The message to send in chat to run the command (optional - defaults to 'invite')
29
+ },
30
+ });
31
+
32
+ // Put your event listeners before portal.start() to ensure you don't miss any events
33
+
34
+ portal.on('rtaEvent', (event) => { // Emits when an RTA event is received
35
+ console.log('RTA Event: ', event);
36
+ });
37
+
38
+ portal.on('sessionCreated', (session) => { // Emits when the session is created'
39
+ console.log('Session Created: ', session);
40
+ });
41
+
42
+ portal.on('sessionUpdated', (session) => { // Emits when the session is updated
43
+ console.log('Session Updated: ', session);
44
+ });
45
+
46
+ portal.on('playerJoin', (player) => { // Emits when a player joins the session
47
+ console.log('Player Join: ', player);
48
+ });
49
+
50
+ portal.on('playerLeave', (player) => { // Emits when a player leaves the session
51
+ console.log('Player Leave: ', player);
52
+ });
53
+
54
+ await portal.start(); // Starts the session
55
+
56
+ };
57
+
58
+ main();
package/index.d.ts CHANGED
@@ -173,6 +173,8 @@ declare module 'bedrock-portal' {
173
173
  * })
174
174
  */
175
175
  autoFriendAdd: InstanceType<typeof ExtendedModule>
176
+ inviteOnMessage: InstanceType<typeof ExtendedModule>
177
+ redirectFromRealm: InstanceType<typeof ExtendedModule>
176
178
  }
177
179
 
178
180
  export interface RTAEvent {
package/index.js CHANGED
@@ -4,6 +4,7 @@ module.exports = {
4
4
  Modules: {
5
5
  autoFriendAdd: require('./src/modules/autoFriendAdd'),
6
6
  inviteOnMessage: require('./src/modules/inviteOnMessage'),
7
+ redirectFromRealm: require('./src/modules/redirectFromRealm'),
7
8
  },
8
9
  Joinability: {
9
10
  InviteOnly: 'invite_only',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bedrock-portal",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Creates a joinable session in Minecraft Bedrock Edition that can route players to their a specified host",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,81 @@
1
+ const Module = require('../classes/Module.js');
2
+
3
+ const cd = new Set();
4
+
5
+ class RedirectFromRealm extends Module {
6
+ constructor() {
7
+ super('redirectFromRealm', 'Automatically invite players to the server when they join a Realm');
8
+ this.options = {
9
+ clientOptions: { }, // Options for the bedrock-protocol client
10
+ chatCommand: {
11
+ enabled: true,
12
+ cooldown: 60000,
13
+ message: 'invite',
14
+ },
15
+ };
16
+ try {
17
+ this.bedrock = require('bedrock-protocol');
18
+ }
19
+ catch (e) {
20
+ console.trace('bedrock-protocol is not installed, please run "npm i bedrock-protocol" to use this module');
21
+ process.exit(1);
22
+ }
23
+ }
24
+
25
+ async run(portal, { rest, rta }) {
26
+
27
+ const { clientOptions } = this.options;
28
+
29
+ const client = await this.initClient(clientOptions, portal).catch(error => ({ error }));
30
+
31
+ if (client.error) return console.error(`Error connecting to Realm - ${client.error.message}. BedrockPortal will continue to run, but will not be able to redirect players from this realm.`);
32
+
33
+ }
34
+
35
+ async initClient(options, portal) {
36
+ return new Promise((resolve, reject) => {
37
+
38
+ this.client = this.bedrock.createClient(options);
39
+
40
+ this.client.once('error', (e) => (this.client = null, reject(e)));
41
+ this.client.once('disconnect', (e) => (this.client = null, reject(e)));
42
+
43
+ this.client.once('spawn', () => {
44
+ this.heartbeat = setInterval(async () => {
45
+ this.debug(`Sending heartbeat to Realm - Status: ${this.client?.status}`);
46
+ if (this.client === null || this.client.status === 0) {
47
+ this.client = null;
48
+ clearInterval(this.heartbeat);
49
+ this.debug('Attempting to reconnect to Realm');
50
+ return await this.initClient(options, portal).catch(err => {
51
+ this.debug(`Error reconnecting to Realm - ${err.message}`);
52
+ });
53
+ }
54
+ }, 30000);
55
+ resolve(this.client);
56
+ });
57
+
58
+ this.client.on('player_list', async packet => {
59
+ if (packet.records.type === 'add') {
60
+ for (const record of packet.records.records) {
61
+ try { await await portal.invitePlayer(record.xbox_user_id); }
62
+ catch (e) { this.debug(`Failed to invite ${record.xbox_user_id} - ${e.message}`); }
63
+ }
64
+ }
65
+ });
66
+
67
+ this.client.on('text', async (packet) => {
68
+ if (!this.options.chatCommand.enabled || packet.type !== 'chat' || cd.has(packet.xuid)) return;
69
+ if (packet.message.toLowerCase() === this.options.chatCommand.message.toLowerCase()) {
70
+ cd.add(packet.xuid);
71
+ setTimeout(() => cd.delete(packet.xuid), this.options.chatCommand.cooldown);
72
+ try { await await portal.invitePlayer(packet.xuid); }
73
+ catch (e) { this.debug(`Failed to invite ${packet.xuid} - ${e.message}`); }
74
+ }
75
+ });
76
+
77
+ });
78
+ }
79
+ }
80
+
81
+ module.exports = RedirectFromRealm;