bedrock-portal 0.3.1 → 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
@@ -78,11 +78,41 @@ portal.use(Modules.autoFriendAdd);
78
78
  // or
79
79
 
80
80
  portal.use(Modules.autoFriendAdd, {
81
+ // When a friend is added, automatically invite them to the game
81
82
  inviteOnAdd: true,
82
- conditionToMeet: (player) => player.presenceState === 'Online', // Only add friends that are online and remove friends that are offline
83
- // checkInterval: 30000, // How often to check for friends to add/remove (optional - defaults to 30000ms)
84
- // addInterval: 2000, // How long to wait between adding friends (optional - defaults to 2000ms)
85
- // removeInterval: 2000, // How long to wait between removing friends (optional - defaults to 2000ms)
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,
86
116
  });
87
117
  ```
88
118
 
@@ -157,6 +187,9 @@ Emitted when a player is removed as a friend. This event is only emitted when th
157
187
  **Parameters**
158
188
  - player - [Player](#player) object
159
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
+
160
193
  ## Objects
161
194
 
162
195
  ### Player
@@ -23,10 +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
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)
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)
30
35
  });
31
36
 
32
37
  // Put your event listeners before portal.start() to ensure you don't miss any events
@@ -59,6 +64,10 @@ const main = async () => {
59
64
  console.log('Friend Removed: ', player);
60
65
  });
61
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
+
62
71
  await portal.start(); // Starts the session
63
72
 
64
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.1",
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": {
@@ -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,