feat(plugins) Base plugins for rooms and permissions
This commit is contained in:
124
lib/WetSock/Plugins/Rooms/Room.js
Normal file
124
lib/WetSock/Plugins/Rooms/Room.js
Normal file
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* Represents a chat room.
|
||||
*/
|
||||
class Room {
|
||||
/**
|
||||
* Create a new room instance.
|
||||
* @param {string} id - The unique identifier of the room.
|
||||
* @param {Object} [config={}] - The configuration options for the room.
|
||||
*/
|
||||
constructor(id, config = {}) {
|
||||
this.id = id;
|
||||
this.members = new Map();
|
||||
this.config = {
|
||||
notifyMuteUnmute: config.notifyMuteUnmute || false,
|
||||
notifyKick: config.notifyKick || false,
|
||||
inactivityTimeout: config.inactivityTimeout || 60_000,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a member to the room.
|
||||
* @param {string} memberId - The unique identifier of the member.
|
||||
* @param {Object} memberData - Additional data associated with the member.
|
||||
*/
|
||||
add(memberId, memberData) {
|
||||
this.members.set(memberId, {
|
||||
...memberData,
|
||||
lastActivity: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a member from the room.
|
||||
* @param {string} memberId - The unique identifier of the member.
|
||||
*/
|
||||
remove(memberId) {
|
||||
this.members.delete(memberId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data of a member in the room.
|
||||
* @param {string} memberId - The unique identifier of the member.
|
||||
* @returns {Object|undefined} The member data or undefined if the member is not found.
|
||||
*/
|
||||
get(memberId) {
|
||||
return this.members.get(memberId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a member is muted in the room.
|
||||
* @param {string} memberId - The unique identifier of the member.
|
||||
* @returns {boolean} True if the member is muted, false otherwise.
|
||||
*/
|
||||
isMuted(memberId) {
|
||||
const memberData = this.get(memberId);
|
||||
return memberData ? memberData.muted : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a member is kicked from the room.
|
||||
* @param {string} memberId - The unique identifier of the member.
|
||||
* @returns {boolean} True if the member is kicked, false otherwise.
|
||||
*/
|
||||
isKicked(memberId) {
|
||||
const memberData = this.get(memberId);
|
||||
return memberData ? memberData.kicked : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mute a member in the room.
|
||||
* @param {string} memberId - The unique identifier of the member.
|
||||
*/
|
||||
mute(memberId) {
|
||||
const memberData = this.get(memberId);
|
||||
if (memberData) {
|
||||
memberData.muted = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmute a member in the room.
|
||||
* @param {string} memberId - The unique identifier of the member.
|
||||
*/
|
||||
unmute(memberId) {
|
||||
const memberData = this.get(memberId);
|
||||
if (memberData) {
|
||||
memberData.muted = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Kick a member from the room.
|
||||
* @param {string} memberId - The unique identifier of the member.
|
||||
*/
|
||||
kick(memberId) {
|
||||
const memberData = this.get(memberId);
|
||||
if (memberData) {
|
||||
memberData.kicked = Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the last activity timestamp of a member in the room.
|
||||
* @param {string} memberId - The unique identifier of the member.
|
||||
*/
|
||||
update(memberId) {
|
||||
const memberData = this.get(memberId);
|
||||
if (memberData) {
|
||||
memberData.lastActivity = Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge inactive members from the room based on the inactivity timeout.
|
||||
*/
|
||||
purge() {
|
||||
const now = Date.now();
|
||||
for (const [memberId, memberData] of this.members.entries()) {
|
||||
if (now - memberData.lastActivity > this.config.inactivityTimeout) {
|
||||
this.kick(memberId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user