-
Notifications
You must be signed in to change notification settings - Fork 6
Room Plugin Example: Writing the TypingList Plugin
This guide provides a step-by-step process to develop the TypingListPlugin
, a room-specific plugin for the SkyChat application. This plugin tracks and manages the list of users currently typing in a chat room.
-
Create TypingListPlugin Class:
-
Extend the
TypingListPlugin
class fromRoomPlugin
. -
Define the command name as
t
.export class TypingListPlugin extends RoomPlugin { static readonly commandName = 't'; // ... }
-
-
Set Command Rules:
-
Define minimum and maximum parameter counts.
-
Specify a pattern for the action parameter (
on
,off
,clear
).readonly rules = { // Usage: // /t on // /t off // /t clear (OP only) t: { minCount: 1, maxCount: 1, params: [{ name: 'action', pattern: /^(on|off|clear)$/ }], }, };
-
-
Handle Different Actions:
-
Clear the typing list if the
clear
command is used (with OP rights check). -
Register a user as typing with the
on
command. -
Remove a user from the typing list with the
off
command.async run(alias: string, param: string, connection: Connection): Promise<void> { // Implement action handling logic here... }
-
-
Maintain Typing List:
-
Use a private variable
typingList
to track users and their typing start time.private typingList: { [identifier: string]: { startedDate: Date; user: User } } = {};
-
-
Implement Sync Method:
-
Create a private method
sync
to update the room with the current typing list.private sync(): void { // Synchronization logic here... }
-
-
Use Hooks:
-
Implement
onConnectionJoinedRoom
to sync the typing list when a new connection joins the room. -
Implement
onConnectionLeftRoom
to remove a user from the typing list when they leave the room.async onConnectionJoinedRoom(): Promise<void> { this.sync(); } async onConnectionLeftRoom(connection: Connection): Promise<void> { delete this.typingList[connection.session.identifier]; }
-
- Ensure the plugin behaves as expected in different scenarios:
- When users start and stop typing.
- When users join or leave the room.
- When the typing list is cleared by an operator.