bedrock-portal 0.8.0 → 0.8.1

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
@@ -135,6 +135,40 @@ portal.use(Modules.RedirectFromRealm, {
135
135
 
136
136
  ### AutoFriendAdd
137
137
 
138
+ Automatically adds the account's followers as friends and invites them to the game. `#.use(Modules.autoFriendAdd);`
139
+
140
+ Options:
141
+ - **inviteOnAdd**: boolean - Automatically invites recently added friends to the game (default: false)
142
+ - **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)
143
+ - **checkInterval**: number - How often to check for friends to add/remove (default: 30000ms)
144
+ - **addInterval**: number - How long to wait between adding friends (default: 2000ms)
145
+ - **removeInterval**: number - How long to wait between removing friends (default: 2000ms)
146
+
147
+ ```js
148
+ const { BedrockPortal, Modules } = require('bedrock-portal');
149
+
150
+ const portal = new BedrockPortal(auth, { ... })
151
+
152
+ portal.use(Modules.AutoFriendAdd);
153
+
154
+ // or
155
+
156
+ portal.use(Modules.AutoFriendAdd, {
157
+ // When a friend is added, automatically invite them to the game
158
+ inviteOnAdd: true,
159
+ // Only add friends that are online and remove friends that are offline
160
+ conditionToMeet: (player) => player.presenceState === 'Online',
161
+ // How often to check for friends to add/remove (optional - defaults to 30000ms)
162
+ checkInterval: 30000,
163
+ // How long to wait between adding friends (optional - defaults to 2000ms)
164
+ addInterval: 2000,
165
+ // How long to wait between removing friends (optional - defaults to 2000ms)
166
+ removeInterval: 2000,
167
+ });
168
+ ```
169
+
170
+ ### AutoFriendAccept
171
+
138
172
  Automatically accepts friend requests sent to the account. `#.use(Modules.autoFriendAdd);`
139
173
 
140
174
  Options:
@@ -146,11 +180,11 @@ const { BedrockPortal, Modules } = require('bedrock-portal');
146
180
 
147
181
  const portal = new BedrockPortal(auth, { ... })
148
182
 
149
- portal.use(Modules.AutoFriendAdd);
183
+ portal.use(Modules.AutoFriendAccept);
150
184
 
151
185
  // or
152
186
 
153
- portal.use(Modules.AutoFriendAdd, {
187
+ portal.use(Modules.AutoFriendAccept, {
154
188
  // When a friend is added invite them to the game
155
189
  inviteOnAdd: true,
156
190
  // Only accept friends that have 'elite' in their gamertag
@@ -236,6 +270,9 @@ Emitted when a player leaves the session.
236
270
  ### portal.on('friendAdded', (player) => void)
237
271
  Emitted when a player is added as a friend. This event is only emitted when the `autoFriendAdd` module is enabled.
238
272
 
273
+ ### portal.on('friendRemoved', (player) => void)
274
+ Emitted when a player is removed as a friend. This event is only emitted when the `autoFriendAdd` module is enabled.
275
+
239
276
  ### portal.on('messageRecieved', (message) => void)
240
277
  Emitted when a message is recieved from a player. This event is only emitted when the `inviteOnMessage` module is enabled.
241
278
 
package/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@ import Host from './classes/Host';
7
7
  import Player from './classes/Player';
8
8
  import Module from './classes/Module';
9
9
  import { Joinability } from './common/constants';
10
- import AutoFriendAdd from './modules/autoFriendAdd';
10
+ import AutoFriendAdd from './modules/autoFriendAccept';
11
11
  import InviteOnMessage from './modules/inviteOnMessage';
12
12
  import RedirectFromRealm from './modules/redirectFromRealm';
13
13
  import MultipleAccounts from './modules/multipleAccounts';
@@ -149,5 +149,6 @@ declare const Modules: {
149
149
  InviteOnMessage: typeof InviteOnMessage;
150
150
  RedirectFromRealm: typeof RedirectFromRealm;
151
151
  MultipleAccounts: typeof MultipleAccounts;
152
+ AutoFriendAccept: typeof AutoFriendAdd;
152
153
  };
153
154
  export { Modules };
package/dist/index.js CHANGED
@@ -13,10 +13,11 @@ const Module_1 = __importDefault(require("./classes/Module"));
13
13
  exports.Module = Module_1.default;
14
14
  const constants_1 = require("./common/constants");
15
15
  const Event_1 = __importDefault(require("./handlers/Event"));
16
- const autoFriendAdd_1 = __importDefault(require("./modules/autoFriendAdd"));
16
+ const autoFriendAccept_1 = __importDefault(require("./modules/autoFriendAccept"));
17
17
  const inviteOnMessage_1 = __importDefault(require("./modules/inviteOnMessage"));
18
18
  const redirectFromRealm_1 = __importDefault(require("./modules/redirectFromRealm"));
19
19
  const multipleAccounts_1 = __importDefault(require("./modules/multipleAccounts"));
20
+ const autoFriendAccept_2 = __importDefault(require("./modules/autoFriendAccept"));
20
21
  const start_game_1 = require("./common/start_game");
21
22
  const util_1 = require("./common/util");
22
23
  const debug = (0, debug_1.default)('bedrock-portal');
@@ -248,9 +249,10 @@ exports.BedrockPortal = BedrockPortal;
248
249
  var constants_2 = require("./common/constants");
249
250
  Object.defineProperty(exports, "Joinability", { enumerable: true, get: function () { return constants_2.Joinability; } });
250
251
  const Modules = {
251
- AutoFriendAdd: autoFriendAdd_1.default,
252
+ AutoFriendAdd: autoFriendAccept_1.default,
252
253
  InviteOnMessage: inviteOnMessage_1.default,
253
254
  RedirectFromRealm: redirectFromRealm_1.default,
254
255
  MultipleAccounts: multipleAccounts_1.default,
256
+ AutoFriendAccept: autoFriendAccept_2.default,
255
257
  };
256
258
  exports.Modules = Modules;
@@ -0,0 +1,7 @@
1
+ import type { BedrockPortal } from '../index';
2
+ import Module from '../classes/Module';
3
+ export default class AutoFriendAdd extends Module {
4
+ constructor();
5
+ run(portal: BedrockPortal): Promise<void>;
6
+ stop(): Promise<void>;
7
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const Module_1 = __importDefault(require("../classes/Module"));
7
+ const Player_1 = __importDefault(require("../classes/Player"));
8
+ const multipleAccounts_1 = __importDefault(require("./multipleAccounts"));
9
+ class AutoFriendAdd extends Module_1.default {
10
+ constructor() {
11
+ super('autoFriendAdd', 'Automatically accept friend requests');
12
+ this.options = {
13
+ inviteOnAdd: false,
14
+ conditionToMeet: () => true,
15
+ };
16
+ }
17
+ async run(portal) {
18
+ const addXboxFriend = async (host) => {
19
+ host.rta?.on('event', async (event) => {
20
+ if (event.data.NotificationType !== 'IncomingFriendRequestCountChanged')
21
+ return;
22
+ this.debug('Received Friend RTA event', event);
23
+ const requests = await host.rest.getFriendRequestsReceived()
24
+ .then(res => res.filter(this.options.conditionToMeet));
25
+ this.debug(`Received ${requests.length} friend request(s)`);
26
+ if (!requests.length)
27
+ return;
28
+ const accept = await host.rest.acceptFriendRequests(requests.map(req => req.xuid));
29
+ this.debug(`Accepted ${accept.updatedPeople.length} friend request(s)`);
30
+ for (const person of requests.filter(req => accept.updatedPeople.includes(req.xuid))) {
31
+ portal.emit('friendAdded', new Player_1.default(person, null));
32
+ this.debug(`Accepted ${person.gamertag}`);
33
+ if (this.options.inviteOnAdd) {
34
+ await portal.invitePlayer(person.xuid).catch(error => this.debug(`Error: Failed to invite ${person.gamertag}`, error));
35
+ }
36
+ }
37
+ });
38
+ await host.rta?.subscribe(`https://social.xboxlive.com/users/xuid(${host.profile?.xuid})/friends`);
39
+ };
40
+ const multipleAccounts = portal.modules.get('multipleAccounts');
41
+ if (multipleAccounts && multipleAccounts instanceof multipleAccounts_1.default) {
42
+ for (const account of multipleAccounts.peers.values()) {
43
+ addXboxFriend(account)
44
+ .catch(error => this.debug(`Error: ${error.message}`, error));
45
+ }
46
+ }
47
+ addXboxFriend(portal.host)
48
+ .catch(error => this.debug(`Error: ${error.message}`, error));
49
+ }
50
+ async stop() {
51
+ super.stop();
52
+ }
53
+ }
54
+ exports.default = AutoFriendAdd;
@@ -1,6 +1,7 @@
1
1
  import type { BedrockPortal } from '../index';
2
2
  import Module from '../classes/Module';
3
3
  export default class AutoFriendAdd extends Module {
4
+ interval: NodeJS.Timeout | null;
4
5
  constructor();
5
6
  run(portal: BedrockPortal): Promise<void>;
6
7
  stop(): Promise<void>;
@@ -3,52 +3,75 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const promises_1 = require("timers/promises");
6
7
  const Module_1 = __importDefault(require("../classes/Module"));
7
8
  const Player_1 = __importDefault(require("../classes/Player"));
8
9
  const multipleAccounts_1 = __importDefault(require("./multipleAccounts"));
9
10
  class AutoFriendAdd extends Module_1.default {
11
+ interval = null;
10
12
  constructor() {
11
- super('autoFriendAdd', 'Automatically accept friend requests');
13
+ super('autoFriendAdd', 'Automatically adds followers as friends');
12
14
  this.options = {
13
15
  inviteOnAdd: false,
14
16
  conditionToMeet: () => true,
17
+ checkInterval: 30000,
18
+ addInterval: 2000,
19
+ removeInterval: 2000,
15
20
  };
16
21
  }
17
22
  async run(portal) {
18
23
  const addXboxFriend = async (host) => {
19
- host.rta?.on('event', async (event) => {
20
- if (event.data.NotificationType !== 'IncomingFriendRequestCountChanged')
21
- return;
22
- this.debug('Received Friend RTA event', event);
23
- const requests = await host.rest.getFriendRequestsReceived()
24
- .then(res => res.filter(this.options.conditionToMeet));
25
- this.debug(`Received ${requests.length} friend request(s)`);
26
- if (!requests.length)
27
- return;
28
- const accept = await host.rest.acceptFriendRequests(requests.map(req => req.xuid));
29
- this.debug(`Accepted ${accept.updatedPeople.length} friend request(s)`);
30
- for (const person of requests.filter(req => accept.updatedPeople.includes(req.xuid))) {
31
- portal.emit('friendAdded', new Player_1.default(person, null));
32
- this.debug(`Accepted ${person.gamertag}`);
33
- if (this.options.inviteOnAdd) {
34
- await portal.invitePlayer(person.xuid).catch(error => this.debug(`Error: Failed to invite ${person.gamertag}`, error));
35
- }
24
+ this.debug('Checking for followers to add');
25
+ const followers = await host.rest.getXboxFollowers()
26
+ .catch(() => []);
27
+ this.debug(`Found ${followers.length} follower(s)`);
28
+ const needsAdding = followers.filter(res => !res.isFollowedByCaller && this.options.conditionToMeet(res));
29
+ this.debug(`Adding ${needsAdding.length} account(s) [${needsAdding.map(res => res.gamertag).join(', ')}]`);
30
+ for (const account of needsAdding) {
31
+ await host.rest.addXboxFriend(account.xuid).catch(() => {
32
+ throw Error(`Failed to add ${account.gamertag}`);
33
+ });
34
+ if (this.options.inviteOnAdd) {
35
+ await portal.invitePlayer(account.xuid).catch(() => {
36
+ throw Error(`Failed to invite ${account.gamertag}`);
37
+ });
36
38
  }
37
- });
38
- await host.rta?.subscribe(`https://social.xboxlive.com/users/xuid(${host.profile?.xuid})/friends`);
39
+ portal.emit('friendAdded', new Player_1.default(account, null));
40
+ this.debug(`Added & invited ${account.gamertag}`);
41
+ await (0, promises_1.setTimeout)(this.options.addInterval);
42
+ }
43
+ this.debug('Checking for friends to remove');
44
+ const friends = await host.rest.getXboxFriends()
45
+ .catch(() => []);
46
+ this.debug(`Found ${friends.length} friend(s)`);
47
+ const needsRemoving = friends.filter(res => !res.isFollowingCaller || !this.options.conditionToMeet(res));
48
+ this.debug(`Removing ${needsRemoving.length} account(s) [${needsRemoving.map(res => res.gamertag).join(', ')}]`);
49
+ for (const account of needsRemoving) {
50
+ await host.rest.removeXboxFriend(account.xuid).catch(() => {
51
+ throw Error(`Failed to remove ${account.gamertag}`);
52
+ });
53
+ portal.emit('friendRemoved', new Player_1.default(account, null));
54
+ this.debug(`Removed ${account.gamertag}`);
55
+ await (0, promises_1.setTimeout)(this.options.removeInterval);
56
+ }
39
57
  };
40
- const multipleAccounts = portal.modules.get('multipleAccounts');
41
- if (multipleAccounts && multipleAccounts instanceof multipleAccounts_1.default) {
42
- for (const account of multipleAccounts.peers.values()) {
43
- addXboxFriend(account)
44
- .catch(error => this.debug(`Error: ${error.message}`, error));
58
+ this.interval = setInterval(() => {
59
+ const multipleAccounts = portal.modules.get('multipleAccounts');
60
+ if (multipleAccounts && multipleAccounts instanceof multipleAccounts_1.default) {
61
+ for (const account of multipleAccounts.peers.values()) {
62
+ addXboxFriend(account)
63
+ .catch(error => this.debug(`Error: ${error.message}`, error));
64
+ }
45
65
  }
46
- }
47
- addXboxFriend(portal.host)
48
- .catch(error => this.debug(`Error: ${error.message}`, error));
66
+ addXboxFriend(portal.host)
67
+ .catch(error => this.debug(`Error: ${error.message}`, error));
68
+ }, this.options.checkInterval);
49
69
  }
50
70
  async stop() {
51
71
  super.stop();
72
+ if (this.interval) {
73
+ clearInterval(this.interval);
74
+ }
52
75
  }
53
76
  }
54
77
  exports.default = AutoFriendAdd;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bedrock-portal",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Creates a joinable session in Minecraft Bedrock Edition that can route players to their a specified host",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",