forked from snxraven/LinkUp-P2P-Chat
Merge pull request 'Reworking bot code. Again...' (#7) from MiTask/LinkUp-P2P-Chat:main into main
Reviewed-on: snxraven/LinkUp-P2P-Chat#7
This commit is contained in:
commit
9a14c10bac
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ storage
|
|||||||
chatBot/.env
|
chatBot/.env
|
||||||
chatBot/commands/ai.js
|
chatBot/commands/ai.js
|
||||||
config.json
|
config.json
|
||||||
|
.idea
|
||||||
|
9
app.js
9
app.js
@ -390,20 +390,13 @@ function sendMessage(e) {
|
|||||||
|
|
||||||
onMessageAdded(config.userName, message, config.userAvatar, topic);
|
onMessageAdded(config.userName, message, config.userAvatar, topic);
|
||||||
|
|
||||||
let peersPublicKeys = [];
|
|
||||||
peersPublicKeys.push([...swarm.connections].map(peer => peer.remotePublicKey.toString('hex')));
|
|
||||||
peersPublicKeys = peersPublicKeys.flat(1);
|
|
||||||
peersPublicKeys.push(swarm.keyPair.publicKey.toString('hex'));
|
|
||||||
|
|
||||||
const messageObj = JSON.stringify({
|
const messageObj = JSON.stringify({
|
||||||
type: 'message',
|
type: 'message',
|
||||||
name: config.userName,
|
name: config.userName,
|
||||||
message,
|
message,
|
||||||
avatar: config.userAvatar,
|
avatar: config.userAvatar,
|
||||||
topic: topic,
|
topic: topic,
|
||||||
peers: peersPublicKeys, // Deprecated. To be deleted in future updates
|
timestamp: Date.now()
|
||||||
timestamp: Date.now(),
|
|
||||||
readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const peers = [...swarm.connections];
|
const peers = [...swarm.connections];
|
||||||
|
@ -60,7 +60,7 @@ loadCommands().then(commands => {
|
|||||||
|
|
||||||
bot.on('onBotJoinRoom', () => {
|
bot.on('onBotJoinRoom', () => {
|
||||||
console.log("Bot is ready!");
|
console.log("Bot is ready!");
|
||||||
bot.sendMessageToAll(process.env.ON_READY_MESSAGE);
|
bot.sendTextMessage(process.env.ON_READY_MESSAGE);
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.joinChatRoom(process.env.LINKUP_ROOM_ID);
|
bot.joinChatRoom(process.env.LINKUP_ROOM_ID);
|
||||||
|
@ -4,6 +4,6 @@ export default {
|
|||||||
handler: function(bot, args, message) {
|
handler: function(bot, args, message) {
|
||||||
const responses = ['It is certain.', 'It is decidedly so.', 'Without a doubt.', 'Yes - definitely.', 'You may rely on it.', 'As I see it, yes.', 'Most likely.', 'Outlook good.', 'Yes.', 'Signs point to yes.', 'Reply hazy, try again.', 'Ask again later.', 'Better not tell you now.', 'Cannot predict now.', 'Concentrate and ask again.', 'Don\'t count on it.', 'My reply is no.', 'My sources say no.', 'Outlook not so good.', 'Very doubtful.'];
|
const responses = ['It is certain.', 'It is decidedly so.', 'Without a doubt.', 'Yes - definitely.', 'You may rely on it.', 'As I see it, yes.', 'Most likely.', 'Outlook good.', 'Yes.', 'Signs point to yes.', 'Reply hazy, try again.', 'Ask again later.', 'Better not tell you now.', 'Cannot predict now.', 'Concentrate and ask again.', 'Don\'t count on it.', 'My reply is no.', 'My sources say no.', 'Outlook not so good.', 'Very doubtful.'];
|
||||||
const randomIndex = Math.floor(Math.random() * responses.length);
|
const randomIndex = Math.floor(Math.random() * responses.length);
|
||||||
bot.sendMessage(message.peers, responses[randomIndex]);
|
bot.sendTextMessage(responses[randomIndex]);
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -3,6 +3,6 @@ export default {
|
|||||||
description: 'Greet the user',
|
description: 'Greet the user',
|
||||||
handler: function(bot, args, message) {
|
handler: function(bot, args, message) {
|
||||||
const userName = message.name;
|
const userName = message.name;
|
||||||
bot.sendMessage(message.peers, `Hello, ${userName}! How can I assist you today?`);
|
bot.sendTextMessage(`Hello, ${userName}! How can I assist you today?`);
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
handler: function(bot, args, message) {
|
handler: function(bot, args, message) {
|
||||||
bot.sendMessage(message.peers, 'Pong!');
|
bot.sendTextMessage('Pong!');
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -1,8 +0,0 @@
|
|||||||
class ChatRoom {
|
|
||||||
public ChatRoom(topic, peers) {
|
|
||||||
this.topic = topic;
|
|
||||||
this.peers = peers;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default ChatRoom;
|
|
@ -1,6 +1,8 @@
|
|||||||
import Hyperswarm from 'hyperswarm';
|
import Hyperswarm from 'hyperswarm';
|
||||||
import EventEmitter from 'node:events';
|
import EventEmitter from 'node:events';
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
|
import TextMessage from "./TextMessage.js";
|
||||||
|
import FileMessage from "./FileMessage.js";
|
||||||
|
|
||||||
class Client extends EventEmitter {
|
class Client extends EventEmitter {
|
||||||
constructor(botName) {
|
constructor(botName) {
|
||||||
@ -11,6 +13,10 @@ class Client extends EventEmitter {
|
|||||||
this.joinedRooms = new Set(); // Track the rooms the bot has joined
|
this.joinedRooms = new Set(); // Track the rooms the bot has joined
|
||||||
this.currentTopic = null; // Track the current topic
|
this.currentTopic = null; // Track the current topic
|
||||||
this.setupSwarm();
|
this.setupSwarm();
|
||||||
|
|
||||||
|
process.on('exit', () => {
|
||||||
|
this.destroy();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setupSwarm() {
|
setupSwarm() {
|
||||||
@ -19,9 +25,14 @@ class Client extends EventEmitter {
|
|||||||
const messageObj = JSON.parse(message.toString());
|
const messageObj = JSON.parse(message.toString());
|
||||||
if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room
|
if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room
|
||||||
this.currentTopic = messageObj.topic; // Set the current topic from the incoming message
|
this.currentTopic = messageObj.topic; // Set the current topic from the incoming message
|
||||||
if (messageObj.type === "message") this.emit('onMessage', peer, messageObj);
|
if (messageObj.type === "message")
|
||||||
if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj);
|
this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.message, messageObj.timestamp));
|
||||||
if (messageObj.type === "file") this.emit('onFile', peer, messageObj);
|
|
||||||
|
if (messageObj.type === "file")
|
||||||
|
this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar, messageObj.topic, messageObj.timestamp));
|
||||||
|
|
||||||
|
if (messageObj.type === "icon")
|
||||||
|
this.emit('onIcon', peer, messageObj);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -46,32 +57,13 @@ class Client extends EventEmitter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(roomPeers, message) {
|
sendTextMessage(message) {
|
||||||
console.log('Bot name:', this.botName);
|
this.sendMessage(TextMessage.new(this, message));
|
||||||
const timestamp = Date.now();
|
|
||||||
const messageObj = {
|
|
||||||
type: 'message',
|
|
||||||
name: this.botName,
|
|
||||||
message,
|
|
||||||
timestamp,
|
|
||||||
topic: this.currentTopic // Include the current topic
|
|
||||||
};
|
|
||||||
const data = JSON.stringify(messageObj);
|
|
||||||
const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex')));
|
|
||||||
for (const peer of peers) peer.write(data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessageToAll(message) {
|
sendMessage(message) {
|
||||||
console.log('Bot name:', this.botName);
|
console.log('Bot name:', this.botName);
|
||||||
const timestamp = Date.now();
|
const data = message.toJsonString();
|
||||||
const messageObj = {
|
|
||||||
type: 'message',
|
|
||||||
name: this.botName,
|
|
||||||
message,
|
|
||||||
timestamp,
|
|
||||||
topic: this.currentTopic // Include the current topic
|
|
||||||
};
|
|
||||||
const data = JSON.stringify(messageObj);
|
|
||||||
const peers = [...this.swarm.connections];
|
const peers = [...this.swarm.connections];
|
||||||
for (const peer of peers) peer.write(data);
|
for (const peer of peers) peer.write(data);
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,26 @@
|
|||||||
class FileMessage {
|
class FileMessage {
|
||||||
public FileMessage(chatRoom, userPeer, fileName, fileUrl, fileType, timestamp) {
|
public FileMessage(peerName, fileName, fileUrl, fileType, peerAvatar, topic, timestamp) {
|
||||||
this.chatRoom = chatRoom;
|
this.peerName = peerName;
|
||||||
this.userPeer = userPeer;
|
|
||||||
this.fileName = fileName;
|
this.fileName = fileName;
|
||||||
this.fileUrl = fileUrl;
|
this.fileUrl = fileUrl;
|
||||||
this.fileType = fileType;
|
this.fileType = fileType;
|
||||||
|
this.peerAvatar = peerAvatar;
|
||||||
|
this.topic = topic;
|
||||||
this.timestamp = timestamp;
|
this.timestamp = timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public toJsonString() {
|
||||||
|
return JSON.stringify({
|
||||||
|
type: 'file',
|
||||||
|
name: this.peerName,
|
||||||
|
fileName: this.fileName,
|
||||||
|
fileUrl: this.fileUrl,
|
||||||
|
fileType: this.fileType,
|
||||||
|
avatar: this.peerAvatar,
|
||||||
|
topic: this.topic,
|
||||||
|
timestamp: this.timestamp,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default FileMessage;
|
export default FileMessage;
|
@ -1,10 +1,26 @@
|
|||||||
class TextMessage {
|
class TextMessage {
|
||||||
public TextMessage(chatRoom, userPeer, message, timestamp) {
|
public TextMessage(peerName, peerAvatar, topic, message, timestamp) {
|
||||||
this.chatRoom = chatRoom;
|
this.peerName = peerName;
|
||||||
this.userPeer = userPeer;
|
this.peerAvatar = peerAvatar;
|
||||||
|
this.topic = topic;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
this.timestamp = timestamp;
|
this.timestamp = timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public toJsonString() {
|
||||||
|
return JSON.stringify({
|
||||||
|
type: 'message',
|
||||||
|
name: this.peerName,
|
||||||
|
message: this.message,
|
||||||
|
avatar: this.peerAvatar,
|
||||||
|
topic: this.topic,
|
||||||
|
timestamp: this.timestamp
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static new(bot, message) {
|
||||||
|
return new TextMessage(bot.botName, "", bot.currentTopic, message, Date.now());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default TextMessage;
|
export default TextMessage;
|
@ -1,10 +0,0 @@
|
|||||||
class UserPeer {
|
|
||||||
public UserPeer(peer, topics, username, avatar) {
|
|
||||||
this.peer = peer;
|
|
||||||
this.topics = topics;
|
|
||||||
this.username = username;
|
|
||||||
this.avatar = avatar;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default UserPeer;
|
|
Loading…
Reference in New Issue
Block a user