bedrock-portal 0.3.0 → 0.4.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
@@ -64,6 +64,9 @@ Automatically adds the account's followers as friends and invites them to the ga
64
64
  Options:
65
65
  - **inviteOnAdd**: boolean - Automatically invites recently added friends to the game (default: false)
66
66
  - **conditionToMeet**: (player: RawPlayer) => boolean - A function that returns a boolean. If the function returns true, followers will be added as a friend and the friends that don't will be removed (default: () => true)
67
+ - **checkInterval**: number - How often to check for friends to add/remove (default: 30000ms)
68
+ - **addInterval**: number - How long to wait between adding friends (default: 2000ms)
69
+ - **removeInterval**: number - How long to wait between removing friends (default: 2000ms)
67
70
 
68
71
  ```js
69
72
  const { BedrockPortal, Modules } = require('bedrock-portal');
@@ -75,8 +78,41 @@ portal.use(Modules.autoFriendAdd);
75
78
  // or
76
79
 
77
80
  portal.use(Modules.autoFriendAdd, {
81
+ // When a friend is added, automatically invite them to the game
78
82
  inviteOnAdd: true,
79
- conditionToMeet: (player) => player.presenceState === 'Online', // Only add friends that are online and remove friends that are offline
83
+ // Only add friends that are online and remove friends that are offline
84
+ conditionToMeet: (player) => player.presenceState === 'Online',
85
+ // How often to check for friends to add/remove (optional - defaults to 30000ms)
86
+ checkInterval: 30000,
87
+ // How long to wait between adding friends (optional - defaults to 2000ms)
88
+ addInterval: 2000,
89
+ // How long to wait between removing friends (optional - defaults to 2000ms)
90
+ removeInterval: 2000,
91
+ });
92
+ ```
93
+
94
+ ### inviteOnMessage
95
+
96
+ Automatically invites players to the game when they send a message in the chat. `#.use(Modules.inviteOnMessage);`
97
+
98
+ Options:
99
+ - **command**: string - The command to use to invite players (default: 'invite')
100
+ - **checkInterval**: number - How often to check for messages (default: 1000ms)
101
+
102
+ ```js
103
+ const { BedrockPortal, Modules } = require('bedrock-portal');
104
+
105
+ const portal = new BedrockPortal(auth, { ... })
106
+
107
+ portal.use(Modules.inviteOnMessage);
108
+
109
+ // or
110
+
111
+ portal.use(Modules.inviteOnMessage, {
112
+ // The command to use to invite players (optional - defaults to 'invite')
113
+ command: 'invite',
114
+ // How often to check for messages (optional - defaults to 1000ms)
115
+ checkInterval: 30000,
80
116
  });
81
117
  ```
82
118
 
@@ -151,6 +187,9 @@ Emitted when a player is removed as a friend. This event is only emitted when th
151
187
  **Parameters**
152
188
  - player - [Player](#player) object
153
189
 
190
+ ### portal.on('messageRecieved', (message) => {})
191
+ Emitted when a message is recieved from a player. This event is only emitted when the `inviteOnMessage` module is enabled.
192
+
154
193
  ## Objects
155
194
 
156
195
  ### Player
@@ -23,7 +23,15 @@ const main = async () => {
23
23
 
24
24
  portal.use(Modules.autoFriendAdd, { // Automatically adds friends to the session
25
25
  inviteOnAdd: true,
26
- conditionToMeet: (player) => player.presenceState === 'Online', // Only add friends that are online and remove friends that are offline
26
+ // conditionToMeet: (player) => player.presenceState === 'Online', // Only add friends that are online and remove friends that are offline
27
+ // checkInterval: 30000, // How often to check for friends to add/remove (optional - defaults to 30000ms)
28
+ // addInterval: 2000, // How long to wait between adding friends (optional - defaults to 2000ms)
29
+ // removeInterval: 2000, // How long to wait between removing friends (optional - defaults to 2000ms)
30
+ });
31
+
32
+ portal.use(Modules.inviteOnMessage, { // Automatically invites players when they send a message with the specified command
33
+ command: 'invite', // The command to use to invite players (optional - defaults to 'invite')
34
+ checkInterval: 30000, // How often to check for messages (optional - defaults to 30000ms)
27
35
  });
28
36
 
29
37
  // Put your event listeners before portal.start() to ensure you don't miss any events
@@ -56,6 +64,10 @@ const main = async () => {
56
64
  console.log('Friend Removed: ', player);
57
65
  });
58
66
 
67
+ portal.on('messageRecieved', (message) => { // Emits when a message is received. Only emitted if inviteOnMessage module is used
68
+ console.log('Message Received: ', message);
69
+ });
70
+
59
71
  await portal.start(); // Starts the session
60
72
 
61
73
  await portal.invitePlayer('p3'); // gamertag or xuid to invite
package/index.js CHANGED
@@ -3,6 +3,7 @@ module.exports = {
3
3
  Module : require('./src/classes/Module'),
4
4
  Modules: {
5
5
  autoFriendAdd: require('./src/modules/autoFriendAdd'),
6
+ inviteOnMessage: require('./src/modules/inviteOnMessage'),
6
7
  },
7
8
  Joinability: {
8
9
  InviteOnly: 'invite_only',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bedrock-portal",
3
- "version": "0.3.0",
3
+ "version": "0.4.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": {
@@ -8,6 +8,9 @@ class AutoFriendAdd extends Module {
8
8
  this.options = {
9
9
  inviteOnAdd: false,
10
10
  conditionToMeet: () => true,
11
+ checkInterval: 30000,
12
+ addInterval: 2000,
13
+ removeInterval: 2000,
11
14
  };
12
15
  }
13
16
 
@@ -42,7 +45,7 @@ class AutoFriendAdd extends Module {
42
45
 
43
46
  this.debug(`Added & invited ${account.gamertag}`);
44
47
 
45
- await new Promise(resolve => setTimeout(resolve, 2000));
48
+ await new Promise(resolve => setTimeout(resolve, this.options.addInterval));
46
49
  }
47
50
 
48
51
  this.debug('Checking for friends to remove');
@@ -65,7 +68,7 @@ class AutoFriendAdd extends Module {
65
68
 
66
69
  this.debug(`Removed ${account.gamertag}`);
67
70
 
68
- await new Promise(resolve => setTimeout(resolve, 2000));
71
+ await new Promise(resolve => setTimeout(resolve, this.options.removeInterval));
69
72
  }
70
73
 
71
74
  }
@@ -73,7 +76,7 @@ class AutoFriendAdd extends Module {
73
76
  this.debug(`Error: ${error.message}`, error);
74
77
  }
75
78
 
76
- await new Promise(resolve => setTimeout(resolve, 30000));
79
+ await new Promise(resolve => setTimeout(resolve, this.options.checkInterval));
77
80
  }
78
81
  }
79
82
  }
@@ -0,0 +1,68 @@
1
+ /* eslint-disable no-constant-condition */
2
+ const Module = require('../classes/Module.js');
3
+
4
+ class IniteOnMessage extends Module {
5
+ constructor() {
6
+ super('initeOnMessage', 'Automatically invite players to the server when they message you');
7
+ this.options = {
8
+ command: 'invite',
9
+ checkInterval: 30000,
10
+ };
11
+ this.cache = [];
12
+ }
13
+
14
+ async run(portal, { rest, rta }) {
15
+
16
+ const initeOnMessage = async () => {
17
+ const primaryMessages = await rest.get('https://xblmessaging.xboxlive.com/network/xbox/users/me/inbox/primary', { contractVersion: 1 });
18
+ const secondaryMessages = await rest.get('https://xblmessaging.xboxlive.com/network/xbox/users/me/inbox/secondary', { contractVersion: 1 });
19
+
20
+ const messages = [...primaryMessages.conversations, ...secondaryMessages.conversations];
21
+
22
+ const contentMessages = messages.filter(message => message.lastMessage.type === 'ContentMessage');
23
+
24
+ this.debug(`Found ${contentMessages.length} content message(s)`);
25
+
26
+ if (this.cache.length === 0) return this.cache = contentMessages;
27
+
28
+ // use #.lastMessage.messageId
29
+ const newMessages = contentMessages.filter(message => this.cache.some(cacheMessage => cacheMessage.lastMessage.messageId !== message.lastMessage.messageId && cacheMessage.conversationId === message.conversationId));
30
+
31
+ this.debug(`Found ${newMessages.length} new message(s)`);
32
+
33
+ if (newMessages.length === 0) return;
34
+
35
+ for (const message of newMessages) {
36
+ portal.emit('messageRecieved', message.lastMessage);
37
+
38
+ const senderXuid = message.lastMessage.sender;
39
+
40
+ if (senderXuid === '0') continue;
41
+
42
+ this.debug(`Received message from ${senderXuid}`);
43
+
44
+ const content = message.lastMessage.contentPayload.content.parts.find(part => part.contentType === 'text')?.text;
45
+
46
+ if (!content) continue;
47
+
48
+ if (content.toLowerCase() === this.options.command.toLowerCase()) {
49
+ await portal.invitePlayer(senderXuid);
50
+ }
51
+ }
52
+
53
+ this.cache = contentMessages;
54
+ };
55
+
56
+ while (!this.stopped) {
57
+ try {
58
+ await initeOnMessage();
59
+ await new Promise(resolve => setTimeout(resolve, this.options.checkInterval));
60
+ }
61
+ catch (error) {
62
+ this.debug(`Error: ${error.message}`, error);
63
+ }
64
+ }
65
+ }
66
+ }
67
+
68
+ module.exports = IniteOnMessage;
package/src/rest.js CHANGED
@@ -81,7 +81,9 @@ module.exports = class Rest {
81
81
  const response = await this.get(`https://profile.xboxlive.com/users/${target}/profile/settings`, { params: { settings }, contractVersion: 2 });
82
82
 
83
83
  const [avatar, gamerscore, gamertag, tier, reputation, colour, realname, bio, location, modernGamertag, modernGamertagSuffix, uniqueModernGamertag, realnameOverride, tenureLevel, watermarks, isQuarantined, linkedAccounts] = response.profileUsers[0].settings.map(e=> e.value);
84
- const colourData = await axios.get(colour).then(e => e.data);
84
+ const colourData = await axios.get(colour, { timeout: 5000 })
85
+ .then(e => e.data)
86
+ .catch(() => ({ primaryColor: '000000', secondaryColor: '000000', tertiaryColor: '000000' }));
85
87
 
86
88
  return {
87
89
  xuid: response.profileUsers[0].id,