fix: wrap loadQuotes in promise for CommonJS compatibility
This commit is contained in:
107
index.js
107
index.js
@@ -9,58 +9,69 @@ const client = new Client({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
async function loadQuotes() {
|
function loadQuotes() {
|
||||||
const quotesFile = './quotes.json';
|
return new Promise((resolve) => {
|
||||||
if (fs.existsSync(quotesFile)) {
|
setTimeout(() => {
|
||||||
return JSON.parse(fs.readFileSync(quotesFile, 'utf8'));
|
const quotesFile = './quotes.json';
|
||||||
}
|
if (fs.existsSync(quotesFile)) {
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const quotes = await loadQuotes();
|
|
||||||
|
|
||||||
if (quotes.length === 0) {
|
|
||||||
console.error('No quotes found! Please ensure quotes.json is present.');
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
let joinedRooms = new Set();
|
|
||||||
|
|
||||||
client.on('RoomMember#joined', (member) => {
|
|
||||||
const room = member.room;
|
|
||||||
if (!joinedRooms.has(room.roomId)) {
|
|
||||||
joinedRooms.add(room.roomId);
|
|
||||||
console.log(`Joined room: ${room.roomId}`);
|
|
||||||
|
|
||||||
room.on('EventMessage', async (event) => {
|
|
||||||
const messageContent = event.getContent();
|
|
||||||
|
|
||||||
if (!messageContent || !messageContent.body) return;
|
|
||||||
|
|
||||||
const body = messageContent.body.toLowerCase();
|
|
||||||
const botCommand = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
|
||||||
|
|
||||||
if (body.includes(botCommand)) {
|
|
||||||
const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
|
|
||||||
try {
|
try {
|
||||||
await room.sendMessage(randomQuote);
|
resolve(JSON.parse(fs.readFileSync(quotesFile, 'utf8')));
|
||||||
console.log(`Sent quote to ${room.roomId}`);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to send message:', err.message);
|
resolve([]);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
resolve([]);
|
||||||
}
|
}
|
||||||
});
|
}, 0);
|
||||||
|
});
|
||||||
room.on('RoomMember#left', () => {
|
}
|
||||||
joinedRooms.delete(room.roomId);
|
|
||||||
console.log(`Left room: ${room.roomId}`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
client.init().then(() => {
|
loadQuotes().then((quotes) => {
|
||||||
console.log('Bot is online');
|
if (quotes.length === 0) {
|
||||||
|
console.error('No quotes found! Please ensure quotes.json is present.');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let joinedRooms = new Set();
|
||||||
|
|
||||||
|
client.on('RoomMember#joined', (member) => {
|
||||||
|
const room = member.room;
|
||||||
|
if (!joinedRooms.has(room.roomId)) {
|
||||||
|
joinedRooms.add(room.roomId);
|
||||||
|
console.log(`Joined room: ${room.roomId}`);
|
||||||
|
|
||||||
|
room.on('EventMessage', async (event) => {
|
||||||
|
const messageContent = event.getContent();
|
||||||
|
|
||||||
|
if (!messageContent || !messageContent.body) return;
|
||||||
|
|
||||||
|
const body = messageContent.body.toLowerCase();
|
||||||
|
const botCommand = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
||||||
|
|
||||||
|
if (body.includes(botCommand)) {
|
||||||
|
const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
|
||||||
|
try {
|
||||||
|
await room.sendMessage(randomQuote);
|
||||||
|
console.log(`Sent quote to ${room.roomId}`);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to send message:', err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
room.on('RoomMember#left', () => {
|
||||||
|
joinedRooms.delete(room.roomId);
|
||||||
|
console.log(`Left room: ${room.roomId}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
client.init().then(() => {
|
||||||
|
console.log('Bot is online');
|
||||||
|
}).catch(err => {
|
||||||
|
console.error('Failed to initialize client:', err.message);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.error('Failed to initialize client:', err.message);
|
console.error('Failed to load quotes:', err.message);
|
||||||
process.exit(1);
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user