|
| 1 | +import { toRoomID } from 'ps-client/tools'; |
| 2 | + |
| 3 | +import { deleteJoinphrase, fetchAllJoinphrases, getJoinphrase, setJoinphrase } from '@/database/joinphrases'; |
| 4 | +import { MAX_MESSAGE_LENGTH } from '@/ps/constants'; |
| 5 | +import { loadJoinphrases } from '@/ps/loaders/joinphrases'; |
| 6 | +import { ChatError } from '@/utils/chatError'; |
| 7 | +import { Username } from '@/utils/components'; |
| 8 | + |
| 9 | +import type { NoTranslate, PSMessageTranslated, ToTranslate } from '@/i18n/types'; |
| 10 | +import type { PSCommand } from '@/types/chat'; |
| 11 | + |
| 12 | +function validateJoinphrase(phrase: string): void { |
| 13 | + if (!phrase) throw new ChatError('A joinphrase cannot be empty!' as ToTranslate); |
| 14 | + if (phrase.length > MAX_MESSAGE_LENGTH) |
| 15 | + throw new ChatError(`A joinphrase cannot be longer than ${MAX_MESSAGE_LENGTH} characters!` as ToTranslate); |
| 16 | + |
| 17 | + // Security checks |
| 18 | + if (phrase.startsWith('!') || phrase.startsWith('/')) { |
| 19 | + const VALID_COMMANDS = ['!dt', '/me']; |
| 20 | + if (!VALID_COMMANDS.some(cmd => phrase.startsWith(cmd + ' '))) { |
| 21 | + throw new ChatError('A joinphrase cannot start with a command!' as ToTranslate); |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +async function getRoom(message: PSMessageTranslated, arg: string): Promise<string> { |
| 27 | + if (message.type === 'chat') return message.target.roomid; |
| 28 | + if (arg) return toRoomID(arg); |
| 29 | + const reply = await message.target.waitFor(msg => msg.content.length > 0 && !!msg.parent.getRoom(toRoomID(msg.content))); |
| 30 | + if (!reply) throw new ChatError('No room provided!' as ToTranslate); |
| 31 | + return toRoomID(reply.content); |
| 32 | +} |
| 33 | + |
| 34 | +export const command: PSCommand = { |
| 35 | + name: 'joinphrase', |
| 36 | + help: 'Joinphrases module! Joinphrases are messages that are sent when a user joins a room. The room must enable joinphrases to use these.', |
| 37 | + perms: ['room', 'driver'], |
| 38 | + syntax: 'CMD', |
| 39 | + aliases: ['jp', 'joinphrases'], |
| 40 | + categories: ['utility'], |
| 41 | + extendedAliases: { |
| 42 | + addjp: ['joinphrase', 'add'], |
| 43 | + addjoinphrase: ['joinphrase', 'add'], |
| 44 | + ajp: ['joinphrase', 'add'], |
| 45 | + deletejp: ['joinphrase', 'delete'], |
| 46 | + deletejoinphrase: ['joinphrase', 'delete'], |
| 47 | + djp: ['joinphrase', 'delete'], |
| 48 | + removejp: ['joinphrase', 'delete'], |
| 49 | + remjp: ['joinphrase', 'delete'], |
| 50 | + ejp: ['joinphrase', 'edit'], |
| 51 | + editjoinphrase: ['joinphrase', 'edit'], |
| 52 | + getjp: ['joinphrase', 'view'], |
| 53 | + showjp: ['joinphrase', 'view'], |
| 54 | + displayjp: ['joinphrase', 'view'], |
| 55 | + vjp: ['joinphrase', 'view'], |
| 56 | + viewjp: ['joinphrase', 'view'], |
| 57 | + }, |
| 58 | + children: { |
| 59 | + help: { |
| 60 | + name: 'help', |
| 61 | + help: 'Shows the help for the joinphrases command.', |
| 62 | + aliases: ['h'], |
| 63 | + syntax: 'CMD', |
| 64 | + async run({ run }) { |
| 65 | + run('help jp'); |
| 66 | + }, |
| 67 | + }, |
| 68 | + add: { |
| 69 | + name: 'add', |
| 70 | + help: 'Adds a new joinphrase for a given user.', |
| 71 | + flags: { allowPMs: false }, |
| 72 | + syntax: 'CMD [user], [joinphrase]', |
| 73 | + aliases: ['new', 'a', 'n'], |
| 74 | + async run({ message, arg, $T, hasFeature }) { |
| 75 | + if (!hasFeature('joinphrases')) throw new ChatError('Joinphrases are not enabled for this room.' as ToTranslate); |
| 76 | + if (!arg) throw new ChatError($T('INVALID_ARGUMENTS')); |
| 77 | + const [username, phrase] = arg.lazySplit(/\s*,\s*/, 1).map(s => s.trim()); |
| 78 | + if (!phrase) throw new ChatError($T('INVALID_ARGUMENTS')); |
| 79 | + const targetUser = username.trim(); |
| 80 | + if (await getJoinphrase(targetUser, message.target.id)) { |
| 81 | + throw new ChatError(`${targetUser} already has a joinphrase in ${message.target.title}...` as ToTranslate); |
| 82 | + } |
| 83 | + validateJoinphrase(phrase); |
| 84 | + await setJoinphrase(targetUser, message.target.id, phrase, message.author.name); |
| 85 | + message.reply('Joinphrase added!' as ToTranslate); |
| 86 | + loadJoinphrases(); |
| 87 | + }, |
| 88 | + }, |
| 89 | + view: { |
| 90 | + name: 'view', |
| 91 | + help: 'Displays a given joinphrase.', |
| 92 | + syntax: 'CMD [user]', |
| 93 | + flags: { allowPMs: false }, |
| 94 | + aliases: ['show', 'display', 'get'], |
| 95 | + async run({ message, arg, $T, hasFeature }) { |
| 96 | + if (!hasFeature('joinphrases')) throw new ChatError('Joinphrases are not enabled for this room.' as ToTranslate); |
| 97 | + if (!arg) throw new ChatError($T('INVALID_ARGUMENTS')); |
| 98 | + const targetUser = arg.trim(); |
| 99 | + |
| 100 | + const { phrase } = (await getJoinphrase(targetUser, message.target.id)) ?? {}; |
| 101 | + if (!phrase) throw new ChatError(`${targetUser} does not have a joinphrase in ${message.target.title}...` as ToTranslate); |
| 102 | + |
| 103 | + message.privateReply(`${phrase}` as NoTranslate); |
| 104 | + }, |
| 105 | + }, |
| 106 | + delete: { |
| 107 | + name: 'delete', |
| 108 | + help: "Deletes a user's joinphrase.", |
| 109 | + syntax: 'CMD [user]', |
| 110 | + flags: { allowPMs: false }, |
| 111 | + aliases: ['del', 'remove', 'rem', 'd', 'r'], |
| 112 | + async run({ message, arg, $T, hasFeature }) { |
| 113 | + if (!hasFeature('joinphrases')) throw new ChatError('Joinphrases are not enabled for this room.' as ToTranslate); |
| 114 | + if (!arg) throw new ChatError($T('INVALID_ARGUMENTS')); |
| 115 | + const targetUser = arg.trim(); |
| 116 | + |
| 117 | + await deleteJoinphrase(targetUser, message.target.id); |
| 118 | + message.reply('Joinphrase deleted.' as ToTranslate); |
| 119 | + loadJoinphrases(); |
| 120 | + }, |
| 121 | + }, |
| 122 | + list: { |
| 123 | + name: 'list', |
| 124 | + help: 'Lists all joinphrases for a given room.', |
| 125 | + syntax: 'CMD [user]', |
| 126 | + aliases: ['ls', 'l'], |
| 127 | + async run({ message, arg, hasFeature }) { |
| 128 | + if (!hasFeature('joinphrases')) throw new ChatError('Joinphrases are not enabled for this room.' as ToTranslate); |
| 129 | + const targetRoom = await getRoom(message, arg); |
| 130 | + const joinphrases = await fetchAllJoinphrases(targetRoom); |
| 131 | + |
| 132 | + message.replyHTML( |
| 133 | + <table> |
| 134 | + <tbody> |
| 135 | + {joinphrases.map(joinphrase => ( |
| 136 | + <tr key={joinphrase.id}> |
| 137 | + <td> |
| 138 | + <Username name={joinphrase.username} clickable /> |
| 139 | + </td> |
| 140 | + <td>{joinphrase.phrase}</td> |
| 141 | + </tr> |
| 142 | + ))} |
| 143 | + </tbody> |
| 144 | + </table> |
| 145 | + ); |
| 146 | + }, |
| 147 | + }, |
| 148 | + edit: { |
| 149 | + name: 'edit', |
| 150 | + help: "Edits a user's joinphrase.", |
| 151 | + syntax: 'CMD [user], [joinphrase]', |
| 152 | + flags: { allowPMs: false }, |
| 153 | + aliases: ['e', 'update'], |
| 154 | + async run({ message, arg, $T, hasFeature }) { |
| 155 | + if (!hasFeature('joinphrases')) throw new ChatError('Joinphrases are not enabled for this room.' as ToTranslate); |
| 156 | + if (!arg) throw new ChatError($T('INVALID_ARGUMENTS')); |
| 157 | + const [username, phrase] = arg.lazySplit(/\s*,\s*/, 1).map(s => s.trim()); |
| 158 | + if (!phrase) throw new ChatError($T('INVALID_ARGUMENTS')); |
| 159 | + const targetUser = username.trim(); |
| 160 | + if (!(await getJoinphrase(targetUser, message.target.id))) { |
| 161 | + throw new ChatError(`${targetUser} does not have a joinphrase in ${message.target.title}...` as ToTranslate); |
| 162 | + } |
| 163 | + validateJoinphrase(phrase); |
| 164 | + await setJoinphrase(targetUser, message.target.id, phrase, message.author.name); |
| 165 | + message.reply('Joinphrase edited.' as ToTranslate); |
| 166 | + loadJoinphrases(); |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + async run({ run, arg }) { |
| 171 | + if (arg) await run(`joinphrases view ${arg}`); |
| 172 | + else await run(`help joinphrases`); |
| 173 | + }, |
| 174 | +}; |
0 commit comments