From 6cbfe34966c54a88192dc4b839c5631e49ca4fae Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:49:26 +0300 Subject: [PATCH] Testing bot avatars thingy --- chatBot/bot.js | 3 ++- chatBot/includes/Client.js | 7 +++++++ chatBot/includes/IconMessage.js | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 chatBot/includes/IconMessage.js diff --git a/chatBot/bot.js b/chatBot/bot.js index 9757135..88cc5e9 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -39,7 +39,6 @@ loadCommands().then(commands => { // We use Event Emitter here to handle new messages bot.on('onMessage', (peer, message) => { - console.log(`Message received from ${message.peerName} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); console.log(message); @@ -64,6 +63,8 @@ loadCommands().then(commands => { }); 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 => { console.error('Error loading commands:', error); }); \ No newline at end of file diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 1ec2200..b40677e 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -5,6 +5,7 @@ import TextMessage from "./TextMessage.js"; import FileMessage from "./FileMessage.js"; import AudioMessage from "./AudioMessage.js"; import Message from "./Message.js"; +import IconMessage from "./IconMessage.js"; class Client extends EventEmitter { 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() { this.swarm.on('connection', (peer) => { peer.on('data', message => { diff --git a/chatBot/includes/IconMessage.js b/chatBot/includes/IconMessage.js new file mode 100644 index 0000000..6ae10b4 --- /dev/null +++ b/chatBot/includes/IconMessage.js @@ -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;