diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts index 30fe43d..e0d2584 100644 --- a/src/channel/Channel.ts +++ b/src/channel/Channel.ts @@ -725,6 +725,16 @@ export class Channel extends EventEmitter { return false; } + + public async clearChat() { + this.chatHistory = []; + await saveChatHistory(this.getID(), this.chatHistory); + + this.sendArray([{ + m: "c", + c: this.chatHistory + }]); + } } export default Channel; diff --git a/src/util/types.d.ts b/src/util/types.d.ts index ef9d531..fdf1eae 100644 --- a/src/util/types.d.ts +++ b/src/util/types.d.ts @@ -83,18 +83,18 @@ declare type Notification = Partial<{ declare type CustomTarget = { global?: boolean; } & ( - | { - mode: "subscribed"; - } - | { - mode: "ids"; - ids: string[]; - } - | { - mode: "id"; - id: string; - } -); + | { + mode: "subscribed"; + } + | { + mode: "ids"; + ids: string[]; + } + | { + mode: "id"; + id: string; + } + ); declare interface Crown { userId: string; @@ -230,6 +230,10 @@ declare interface ServerEvents { key: keyof UserFlags; value: UserFlags[keyof UserFlags]; }; + + clear_chat: { + m: "clear_chat" + } } declare interface ClientEvents { diff --git a/src/ws/events/admin/handlers/clear_chat.ts b/src/ws/events/admin/handlers/clear_chat.ts new file mode 100644 index 0000000..eef90a8 --- /dev/null +++ b/src/ws/events/admin/handlers/clear_chat.ts @@ -0,0 +1,12 @@ +import { ServerEventListener } from "../../../../util/types"; + +export const clear_chat: ServerEventListener<"clear_chat"> = { + id: "clear_chat", + callback: async (msg, socket) => { + // Reset chat in channel + const ch = socket.getCurrentChannel(); + if (!ch) return; + + await ch.clearChat(); + } +}; diff --git a/src/ws/events/admin/index.ts b/src/ws/events/admin/index.ts index aea81f7..e504770 100644 --- a/src/ws/events/admin/index.ts +++ b/src/ws/events/admin/index.ts @@ -1,4 +1,5 @@ import { EventGroup, eventGroups } from "../../events"; +import { clear_chat } from "./handlers/clear_chat"; export const EVENT_GROUP_ADMIN = new EventGroup("admin"); @@ -10,6 +11,6 @@ import { user_flag } from "./handlers/user_flag"; // EVENT_GROUP_ADMIN.add(name); // EVENT_GROUP_ADMIN.add(user_flag); -EVENT_GROUP_ADMIN.addMany(color, name, user_flag); +EVENT_GROUP_ADMIN.addMany(color, name, user_flag, clear_chat); eventGroups.push(EVENT_GROUP_ADMIN);