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