forked from snxraven/LinkUp-P2P-Chat
Compare commits
2 Commits
14aa129f6d
...
6cbfe34966
Author | SHA1 | Date | |
---|---|---|---|
|
6cbfe34966 | ||
|
9b4357dd00 |
@ -39,9 +39,8 @@ loadCommands().then(commands => {
|
|||||||
|
|
||||||
// We use Event Emitter here to handle new messages
|
// We use Event Emitter here to handle new messages
|
||||||
bot.on('onMessage', (peer, message) => {
|
bot.on('onMessage', (peer, message) => {
|
||||||
// console.log(message);
|
console.log(`Message received from ${message.peerName} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`);
|
||||||
|
console.log(message);
|
||||||
console.log(`Message received from ${message.peerName}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`);
|
|
||||||
|
|
||||||
// Check if the message starts with a command prefix
|
// Check if the message starts with a command prefix
|
||||||
if (message.message.startsWith('!')) {
|
if (message.message.startsWith('!')) {
|
||||||
@ -64,6 +63,8 @@ loadCommands().then(commands => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
bot.joinChatRoom(process.env.LINKUP_ROOM_ID);
|
bot.joinChatRoom(process.env.LINKUP_ROOM_ID);
|
||||||
|
|
||||||
|
bot.fetchAvatar(`https://avatar.iran.liara.run/username?username=${this.botName}&background=f4d9b2&color=FF9800&size=40`); // Debugging avatar
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.error('Error loading commands:', error);
|
console.error('Error loading commands:', error);
|
||||||
});
|
});
|
@ -5,6 +5,7 @@ import TextMessage from "./TextMessage.js";
|
|||||||
import FileMessage from "./FileMessage.js";
|
import FileMessage from "./FileMessage.js";
|
||||||
import AudioMessage from "./AudioMessage.js";
|
import AudioMessage from "./AudioMessage.js";
|
||||||
import Message from "./Message.js";
|
import Message from "./Message.js";
|
||||||
|
import IconMessage from "./IconMessage.js";
|
||||||
|
|
||||||
class Client extends EventEmitter {
|
class Client extends EventEmitter {
|
||||||
constructor(botName) {
|
constructor(botName) {
|
||||||
@ -37,6 +38,12 @@ class Client extends EventEmitter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fetchAvatar(url) {
|
||||||
|
this.botAvatar = url;
|
||||||
|
const img = await fetch(url);
|
||||||
|
this.sendMessage(IconMessage.new(this, img));
|
||||||
|
}
|
||||||
|
|
||||||
setupSwarm() {
|
setupSwarm() {
|
||||||
this.swarm.on('connection', (peer) => {
|
this.swarm.on('connection', (peer) => {
|
||||||
peer.on('data', message => {
|
peer.on('data', message => {
|
||||||
|
19
chatBot/includes/IconMessage.js
Normal file
19
chatBot/includes/IconMessage.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import Message from "./Message.js";
|
||||||
|
|
||||||
|
class IconMessage extends Message {
|
||||||
|
async constructor(peerName, avatarBuffer, topic, timestamp) {
|
||||||
|
super("icon", peerName, avatarBuffer, topic, timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
toJsonString() {
|
||||||
|
return JSON.stringify({
|
||||||
|
...this.toJson()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static new(bot, avatarBuffer) {
|
||||||
|
return new IconMessage(bot.botName, avatarBuffer, bot.currentTopic, Date.now());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default IconMessage;
|
@ -1,6 +1,6 @@
|
|||||||
class Message {
|
class Message {
|
||||||
constructor(messageType, peerName, peerAvatar, topic, timestamp) {
|
constructor(messageType, peerName, peerAvatar, topic, timestamp) {
|
||||||
this.messageType = messageType;
|
this.type = messageType;
|
||||||
this.peerName = peerName;
|
this.peerName = peerName;
|
||||||
this.peerAvatar = peerAvatar;
|
this.peerAvatar = peerAvatar;
|
||||||
this.topic = topic;
|
this.topic = topic;
|
||||||
@ -9,7 +9,7 @@ class Message {
|
|||||||
|
|
||||||
toJson() {
|
toJson() {
|
||||||
return {
|
return {
|
||||||
type: this.messageType,
|
type: this.type,
|
||||||
name: this.peerName,
|
name: this.peerName,
|
||||||
avatar: this.peerAvatar,
|
avatar: this.peerAvatar,
|
||||||
topic: this.topic,
|
topic: this.topic,
|
||||||
|
Loading…
Reference in New Issue
Block a user