Feat: Adding GUILDS! #12

Merged
snxraven merged 21 commits from guilds into main 2024-07-07 23:01:33 -04:00
Showing only changes of commit fd02cff23f - Show all commits

34
app.js
View File

@ -339,6 +339,14 @@ function handleIncomingMessage(messageObj) {
} }
} }
} }
} else if (messageObj.type === 'guildJoin') {
const { guildTopic, guildData } = messageObj;
if (!config.guilds[guildTopic]) {
config.guilds[guildTopic] = guildData;
writeConfigToFile("./config.json");
renderGuildList();
joinGuild(guildTopic);
}
} else { } else {
console.error('Received unknown message type:', messageObj); console.error('Received unknown message type:', messageObj);
} }
@ -380,7 +388,19 @@ async function handleConnection(connection, info) {
connection.on('data', (data) => { connection.on('data', (data) => {
const messageObj = JSON.parse(data.toString()); const messageObj = JSON.parse(data.toString());
if (messageObj.type === 'guildJoin') {
const guildData = config.guilds[messageObj.guildTopic];
if (guildData) {
const guildJoinMessage = JSON.stringify({
type: 'guildJoin',
guildTopic: messageObj.guildTopic,
guildData: guildData
});
connection.write(guildJoinMessage);
}
} else {
eventEmitter.emit('onMessage', messageObj); eventEmitter.emit('onMessage', messageObj);
}
}); });
connection.on('close', () => { connection.on('close', () => {
@ -555,6 +575,20 @@ async function joinChatRoom(e) {
const roomTopicBuffer = b4a.from(roomTopic, 'hex'); const roomTopicBuffer = b4a.from(roomTopic, 'hex');
addRoomToList(guildTopic, roomTopic); addRoomToList(guildTopic, roomTopic);
await joinSwarm(roomTopicBuffer); await joinSwarm(roomTopicBuffer);
// Fetch guild data from a peer
const guildJoinMessage = JSON.stringify({
type: 'guildJoin',
guildTopic: guildTopic
});
const guildSwarm = activeRooms.find(room => room.topic === guildTopic);
if (guildSwarm) {
const peers = [...guildSwarm.swarm.connections];
for (const peer of peers) {
peer.write(guildJoinMessage);
}
}
} }
async function joinSwarm(topicBuffer) { async function joinSwarm(topicBuffer) {