Skip to content

Commit 90d9bcc

Browse files
committed
Update matrixBot.js
1 parent 8f16cfd commit 90d9bcc

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

lib/matrixBot.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const fs = require('fs');
34
const path = require('path');
45
const { getConfig } = require('./config.js');
56
const chatStore = require('./chatStore.js');
@@ -20,6 +21,25 @@ function normalizeHomeserverUrl(url) {
2021
return u;
2122
}
2223

24+
function getPersistentMatrixDeviceId() {
25+
const dataDir = path.join(__dirname, '..', 'data');
26+
const file = path.join(dataDir, 'matrix-device-id.txt');
27+
try {
28+
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });
29+
if (fs.existsSync(file)) {
30+
const existing = fs.readFileSync(file, 'utf8').trim();
31+
if (/^[A-Z0-9]{6,16}$/.test(existing)) return existing;
32+
}
33+
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
34+
let id = '';
35+
for (let i = 0; i < 10; i += 1) id += alphabet[Math.floor(Math.random() * alphabet.length)];
36+
fs.writeFileSync(file, id, 'utf8');
37+
return id;
38+
} catch (_) {
39+
return '';
40+
}
41+
}
42+
2343
function matrixUsernameForSender(sender) {
2444
if (!sender || typeof sender !== 'string') return 'matrix_unknown';
2545
const local = sender.replace(/^@/, '').replace(/:/g, '_').replace(/[^a-zA-Z0-9_-]/g, '_');
@@ -41,6 +61,7 @@ async function matrixLoginWithPassword(homeserverUrl, userId, password) {
4161
type: 'm.id.user',
4262
user: userId.trim()
4363
},
64+
device_id: getPersistentMatrixDeviceId(),
4465
password: String(password),
4566
initial_device_display_name: 'ShadowAI'
4667
};

0 commit comments

Comments
 (0)