1- import assert from 'node:assert' ;
21import { uploadToPastie } from 'ps-client/tools' ;
32
4- import { PSRoomConfigs } from '@/cache' ;
3+ import { PSPointsNonce , PSRoomConfigs } from '@/cache' ;
54import {
65 type BulkPointsDataInput ,
76 type Model as PointsModel ,
@@ -137,9 +136,9 @@ export const command: PSCommand[] = [
137136 } ,
138137 } ,
139138 {
140- name : 'addjson ' ,
139+ name : 'addnonce ' ,
141140 help : null ,
142- syntax : 'CMD [json data ]' ,
141+ syntax : 'CMD [nonce ]' ,
143142 perms : 'driver' ,
144143 flags : { conceal : true , noDisplay : true , routePMs : true } ,
145144 categories : [ 'points' ] ,
@@ -149,39 +148,27 @@ export const command: PSCommand[] = [
149148 const roomConfig = PSRoomConfigs [ message . target . id ] ;
150149 if ( ! roomConfig . points ) throw new ChatError ( $T ( 'COMMANDS.POINTS.ROOM_NO_POINTS' , { room : message . target . title } ) ) ;
151150
152- let json ;
153- try {
154- json = JSON . parse ( arg ) ;
155- } catch {
156- throw new ChatError ( 'Invalid JSON.' as ToTranslate ) ;
157- }
158- try {
159- // TODO: Maybe use zod here?
160- assert . equal ( typeof json , 'object' ) ;
161- assert . equal ( Array . isArray ( json ) , false ) ;
162- Object . entries ( json ) . forEach ( ( [ user , value ] ) => {
163- assert . ok ( toId ( user ) ) ;
164- assert . equal ( typeof value , 'object' ) ;
165- assert . equal ( Array . isArray ( value ) , false ) ;
166- Object . entries ( value ! ) . forEach ( ( [ type , amount ] ) => {
167- assert ( roomConfig . points ?. types [ type ] ) ;
168- assert . equal ( typeof amount , 'number' ) ;
169- assert . equal ( Math . round ( amount ) , amount ) ;
170- } ) ;
171- } ) ;
172- } catch {
173- throw new ChatError ( $T ( 'INVALID_ARGUMENTS' ) ) ;
174- }
151+ const nonce = arg . trim ( ) ;
152+ if ( ! nonce ) throw new ChatError ( 'Nonce not provided.' as ToTranslate ) ;
153+ const data = PSPointsNonce [ nonce ] ;
154+
155+ if ( data === null ) throw new ChatError ( `Already added points for ${ nonce } !` as ToTranslate ) ;
156+ if ( ! data ) throw new ChatError ( `Invalid nonce ${ nonce } .` as ToTranslate ) ;
175157
176- const data : Record < string , Record < string , number > > = json ;
177158 const pointsData : BulkPointsDataInput = Object . fromEntries (
178159 Object . entries ( data ) . map ( ( [ name , points ] ) => {
179160 const id = toId ( name ) ;
180161 return [ id , { id, name, points } ] ;
181162 } )
182163 ) ;
183164
184- await bulkAddPoints ( pointsData , message . target . id ) ;
165+ PSPointsNonce [ nonce ] = null ;
166+ try {
167+ await bulkAddPoints ( pointsData , message . target . id ) ;
168+ } catch ( err ) {
169+ PSPointsNonce [ nonce ] = data ;
170+ throw err ;
171+ }
185172 broadcast ( 'Added points!' as ToTranslate ) ;
186173 } ,
187174 } ,
0 commit comments