{ "version": 3, "sources": ["../../../../server/chat-plugins/quotes.ts"], "sourcesContent": ["import {FS, Utils} from '../../lib';\r\n\r\nconst STORAGE_PATH = 'config/chat-plugins/quotes.json';\r\nconst MAX_QUOTES = 300;\r\n\r\ninterface Quote {\r\n\tuserid: string;\r\n\tquote: string;\r\n\tdate: number;\r\n}\r\n\r\nconst quotes: {[room: string]: Quote[]} = JSON.parse(FS(STORAGE_PATH).readIfExistsSync() || \"{}\");\r\n\r\n// migrate quotes out of roomsettings\r\nfunction convertOldQuotes() {\r\n\tfor (const room of Rooms.rooms.values()) {\r\n\t\t// @ts-ignore\r\n\t\tif (room.settings.quotes) {\r\n\t\t\t// @ts-ignore\r\n\t\t\tquotes[room.roomid] = room.settings.quotes;\r\n\t\t\t// @ts-ignore\r\n\t\t\tdelete room.settings.quotes;\r\n\t\t\troom.saveSettings();\r\n\t\t\tsaveQuotes();\r\n\t\t}\r\n\t}\r\n}\r\n\r\nfunction saveQuotes() {\r\n\tFS(STORAGE_PATH).writeUpdate(() => JSON.stringify(quotes));\r\n}\r\n\r\nconvertOldQuotes();\r\n\r\nexport const commands: Chat.ChatCommands = {\r\n\trandquote(target, room, user) {\r\n\t\troom = this.requireRoom();\r\n\t\tconst roomQuotes = quotes[room.roomid];\r\n\t\tif (!roomQuotes?.length) return this.errorReply(`This room has no quotes.`);\r\n\t\tthis.runBroadcast(true);\r\n\t\tconst {quote, date, userid} = roomQuotes[Math.floor(Math.random() * roomQuotes.length)];\r\n\t\tconst time = Chat.toTimestamp(new Date(date), {human: true});\r\n\t\tconst attribution = toID(target) === 'showauthor' ? `
Added by ${userid} on ${time}` : '';\r\n\t\treturn this.sendReplyBox(`${Chat.getReadmoreBlock(quote)}${attribution}`);\r\n\t},\r\n\trandquotehelp: [`/randquote [showauthor] - Show a random quote from the room. Add 'showauthor' to see who added it and when.`],\r\n\r\n\taddquote: 'quote',\r\n\tquote(target, room, user) {\r\n\t\troom = this.requireRoom();\r\n\t\tif (!room.persist) {\r\n\t\t\treturn this.errorReply(\"This command is unavailable in temporary rooms.\");\r\n\t\t}\r\n\t\ttarget = target.trim();\r\n\t\tthis.checkCan('mute', null, room);\r\n\t\tif (!target) {\r\n\t\t\treturn this.parse(`/help quote`);\r\n\t\t}\r\n\t\tif (!quotes[room.roomid]) quotes[room.roomid] = [];\r\n\r\n\t\tconst roomQuotes = quotes[room.roomid];\r\n\t\tif (this.filter(target) !== target) {\r\n\t\t\treturn this.errorReply(`Invalid quote.`);\r\n\t\t}\r\n\t\tif (roomQuotes.filter(item => item.quote === target).length) {\r\n\t\t\treturn this.errorReply(`\"${target}\" is already quoted in this room.`);\r\n\t\t}\r\n\t\tif (target.length > 8192) {\r\n\t\t\treturn this.errorReply(`Your quote cannot exceed 8192 characters.`);\r\n\t\t}\r\n\t\tif (room.settings.isPrivate && roomQuotes.length >= MAX_QUOTES) {\r\n\t\t\treturn this.errorReply(`This room already has ${MAX_QUOTES} quotes, which is the maximum for private rooms.`);\r\n\t\t}\r\n\t\troomQuotes.push({userid: user.id, quote: target, date: Date.now()});\r\n\t\tsaveQuotes();\r\n\t\tthis.refreshPage(`quotes-${room.roomid}`);\r\n\t\tconst collapsedQuote = target.replace(/\\n/g, ' ');\r\n\t\tthis.privateModAction(`${user.name} added a new quote: \"${collapsedQuote}\".`);\r\n\t\treturn this.modlog(`ADDQUOTE`, null, collapsedQuote);\r\n\t},\r\n\tquotehelp: [`/quote [quote] - Adds [quote] to the room's quotes. Requires: % @ # &`],\r\n\r\n\tremovequote(target, room, user) {\r\n\t\troom = this.requireRoom();\r\n\t\tthis.checkCan('mute', null, room);\r\n\t\tif (!quotes[room.roomid]?.length) return this.errorReply(`This room has no quotes.`);\r\n\t\tconst index = parseInt(target.trim());\r\n\t\tif (isNaN(index)) {\r\n\t\t\treturn this.errorReply(`Invalid index.`);\r\n\t\t}\r\n\t\tconst roomQuotes = quotes[room.roomid];\r\n\t\tif (!roomQuotes[index - 1]) {\r\n\t\t\treturn this.errorReply(`Quote not found.`);\r\n\t\t}\r\n\t\tconst [removed] = roomQuotes.splice(index - 1, 1);\r\n\t\tconst collapsedQuote = removed.quote.replace(/\\n/g, ' ');\r\n\t\tthis.privateModAction(`${user.name} removed quote indexed at ${index}: \"${collapsedQuote}\" (originally added by ${removed.userid}).`);\r\n\t\tthis.modlog(`REMOVEQUOTE`, null, collapsedQuote);\r\n\t\tsaveQuotes();\r\n\t\tthis.refreshPage(`quotes-${room.roomid}`);\r\n\t},\r\n\tremovequotehelp: [`/removequote [index] - Removes the quote from the room's quotes. Requires: % @ # &`],\r\n\r\n\tviewquote(target, room, user) {\r\n\t\troom = this.requireRoom();\r\n\t\tconst roomQuotes = quotes[room.roomid];\r\n\t\tif (!roomQuotes?.length) return this.errorReply(`This room has no quotes.`);\r\n\t\tconst [num, showAuthor] = Utils.splitFirst(target, ',');\r\n\t\tconst index = parseInt(num) - 1;\r\n\t\tif (isNaN(index)) {\r\n\t\t\treturn this.errorReply(`Invalid index.`);\r\n\t\t}\r\n\t\tif (!roomQuotes[index]) {\r\n\t\t\treturn this.errorReply(`Quote not found.`);\r\n\t\t}\r\n\t\tthis.runBroadcast(true);\r\n\t\tconst {quote, date, userid} = roomQuotes[index];\r\n\t\tconst time = Chat.toTimestamp(new Date(date), {human: true});\r\n\t\tconst attribution = toID(showAuthor) === 'showauthor' ? `
Added by ${userid} on ${time}` : '';\r\n\t\treturn this.sendReplyBox(`${Chat.formatText(quote, false, true)}${attribution}`);\r\n\t},\r\n\tviewquotehelp: [\r\n\t\t`/viewquote [index][, params] - View the quote from the room's quotes.`,\r\n\t\t`If 'showauthor' is used for the [params] argument, it shows who added the quote and when.`,\r\n\t\t`Requires: % @ # &`,\r\n\t],\r\n\r\n\tviewquotes: 'quotes',\r\n\tquotes(target, room) {\r\n\t\tconst targetRoom = target ? Rooms.search(target) : room;\r\n\t\tif (!targetRoom) return this.errorReply(`Invalid room.`);\r\n\t\tthis.parse(`/join view-quotes-${targetRoom.roomid}`);\r\n\t},\r\n\tquoteshelp: [`/quotes [room] - Shows all quotes for [room]. Defaults the room the command is used in.`],\r\n};\r\n\r\nexport const pages: Chat.PageTable = {\r\n\tquotes(args, user) {\r\n\t\tconst room = this.requireRoom();\r\n\t\tthis.title = `[Quotes]`;\r\n\t\t// allow it for users if they can access the room\r\n\t\tif (!room.checkModjoin(user)) {\r\n\t\t\treturn this.errorReply(`Access denied.`);\r\n\t\t}\r\n\t\tlet buffer = `
`;\r\n\t\tbuffer += ``;\r\n\r\n\t\tconst roomQuotes = quotes[room.roomid];\r\n\t\tif (!roomQuotes?.length) {\r\n\t\t\treturn `${buffer}

This room has no quotes.

`;\r\n\t\t}\r\n\r\n\t\tbuffer += Utils.html`

Quotes for ${room.title} (${roomQuotes.length}):

`;\r\n\t\tfor (const [i, quoteObj] of roomQuotes.entries()) {\r\n\t\t\tconst index = i + 1;\r\n\t\t\tconst {quote, userid, date} = quoteObj;\r\n\t\t\tbuffer += `
#${index}: ${Chat.formatText(quote, false, true)}`;\r\n\t\t\tbuffer += `

Added by ${userid} on ${Chat.toTimestamp(new Date(date), {human: true})}`;\r\n\t\t\tif (user.can('mute', null, room)) {\r\n\t\t\t\tbuffer += ` `;\r\n\t\t\t}\r\n\t\t\tbuffer += `
`;\r\n\t\t}\r\n\t\tbuffer += ``;\r\n\t\treturn buffer;\r\n\t},\r\n};\r\n\r\nexport const handlers: Chat.Handlers = {\r\n\tonRenameRoom(oldID, newID) {\r\n\t\tif (quotes[oldID]) {\r\n\t\t\tif (!quotes[newID]) quotes[newID] = [];\r\n\t\t\tquotes[newID].push(...quotes[oldID]);\r\n\t\t\tdelete quotes[oldID];\r\n\t\t\tsaveQuotes();\r\n\t\t}\r\n\t},\r\n};\r\n"], "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAwB;AAExB,MAAM,eAAe;AACrB,MAAM,aAAa;AAQnB,MAAM,SAAoC,KAAK,UAAM,eAAG,YAAY,EAAE,iB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