11import { Response } from 'centra' ;
2- import { get , post , UserLogger } from './bttps ' ;
2+ import { get , post , UserLogger } from './requests ' ;
33import fallbackData from './fallbackListData' ;
44import legacyIdsFallbackData from './legacyIdsFallbackData' ;
55
@@ -51,7 +51,13 @@ type DiscordJSClientFallback = {
5151 ) )
5252 | null ;
5353 guilds : {
54- cache : Collection < string , { shardID : number ; [ k : string ] : any } > ;
54+ cache : Collection <
55+ string ,
56+ (
57+ | { shardID : number ; shardId : undefined }
58+ | { shardId : number ; shardID : undefined }
59+ ) & { [ k : string ] : any }
60+ > ;
5561 } ;
5662 ws : {
5763 shards : Collection < number , { id : number ; [ k : string ] : any } > ;
@@ -60,17 +66,14 @@ type DiscordJSClientFallback = {
6066 [ k : string ] : any ;
6167} ;
6268
63-
64- type LogOptions = boolean | { extended ?: boolean , logger ?: UserLogger }
65-
69+ type LogOptions = boolean | { extended ?: boolean ; logger ?: UserLogger } ;
6670
6771let listData = fallbackData as listDataType ;
6872let legacyIds = legacyIdsFallbackData as legacyIdDataType ;
6973const listAge = new Date ( ) ;
7074let extendedLogging = false ;
7175let useBotblockAPI = true ;
7276
73-
7477/**
7578 * the userLogger variable will later be defined with the
7679 * logger supplied by the user if they supplied any
@@ -79,12 +82,17 @@ let useBotblockAPI = true;
7982let userLogger : UserLogger | undefined ;
8083
8184const log = {
82- info : ( msg : string ) => ( userLogger ? userLogger . info ( `BLAPI: ${ msg } ` ) : console . info ( `[INFO] BLAPI: ${ msg } ` ) ) ,
83- warn : ( msg : string ) => ( userLogger ? userLogger . warn ( `BLAPI: ${ msg } ` ) : console . warn ( `[WARN] BLAPI: ${ msg } ` ) ) ,
84- error : ( err : any ) => ( userLogger ? userLogger . error ( `BLAPI: ${ err } ` ) : console . error ( `[ERROR] BLAPI ${ err } ` ) ) ,
85+ info : ( msg : string ) => ( userLogger
86+ ? userLogger . info ( `BLAPI: ${ msg } ` )
87+ : console . info ( `[INFO] BLAPI: ${ msg } ` ) ) ,
88+ warn : ( msg : string ) => ( userLogger
89+ ? userLogger . warn ( `BLAPI: ${ msg } ` )
90+ : console . warn ( `[WARN] BLAPI: ${ msg } ` ) ) ,
91+ error : ( err : any ) => ( userLogger
92+ ? userLogger . error ( `BLAPI: ${ err } ` )
93+ : console . error ( `[ERROR] BLAPI ${ err } ` ) ) ,
8594} ;
8695
87-
8896function convertLegacyIds ( apiKeys : apiKeysObject ) {
8997 const newApiKeys : apiKeysObject = { ...apiKeys } ;
9098 Object . entries ( legacyIds ) . forEach ( ( [ list , newlist ] ) => {
@@ -191,7 +199,9 @@ async function postToAllLists(
191199 sendObj [ list . api_shards ] = shards ;
192200 }
193201
194- posts . push ( post ( apiPath , apiKeys [ listname ] , sendObj , extendedLogging , log ) ) ;
202+ posts . push (
203+ post ( apiPath , apiKeys [ listname ] , sendObj , extendedLogging , log ) ,
204+ ) ;
195205 }
196206 } ) ;
197207
@@ -249,14 +259,16 @@ async function handleInternal(
249259 // Get array of shards, loosing collection typings make this somewhat ugly
250260 shards = client . ws . shards . map (
251261 ( s : { id : number } ) => client . guilds . cache . filter (
252- ( g : { shardID : number } ) => g . shardID === s . id ,
262+ (
263+ g :
264+ | { shardID : number ; shardId : undefined }
265+ | { shardId : number ; shardID : undefined } ,
266+ ) => g . shardID === s . id || g . shardId === s . id ,
253267 ) . size ,
254268 ) as Array < number > ;
255269 if ( shards . length !== client . ws . shards . size ) {
256270 // If not all shards are up yet, we skip this run of handleInternal
257- log . info (
258- "Not all shards are up yet, so we're skipping this run." ,
259- ) ;
271+ log . info ( "Not all shards are up yet, so we're skipping this run." ) ;
260272 return ;
261273 }
262274 server_count = shards . reduce (
@@ -338,9 +350,9 @@ export function handle(
338350/**
339351 * For when you don't use discord.js or just want to post to manual times
340352 * @param guildCount Integer value of guilds your bot is serving
341- * @param botID Snowflake of the ID the user your bot is using
353+ * @param botId Snowflake of the ID the user your bot is using
342354 * @param apiKeys A JSON object formatted like: {"botlist name":"API Keys for that list", etc.}
343- * @param shardID (optional) The shard ID, which will be used to identify the
355+ * @param shardId (optional) The shard ID, which will be used to identify the
344356 * shards valid for posting
345357 * (and for super efficient posting with BLAPIs own distributer when not using botBlock)
346358 * @param shardCount (optional) The number of shards the bot has, which is posted to the lists
@@ -422,13 +434,31 @@ export async function manualPost(
422434export function setLogging ( logOptions : LogOptions ) : void {
423435 // we are setting extendedLogging to the passed in logOptions
424436 // so users can disable extended logging later on
425- if ( typeof logOptions === 'boolean' ) extendedLogging = logOptions ;
426- if ( typeof logOptions === 'object' && Object . prototype . hasOwnProperty . call ( logOptions , 'extended' ) && typeof logOptions . extended === 'boolean' ) extendedLogging = logOptions . extended ;
437+ if ( typeof logOptions === 'boolean' ) {
438+ extendedLogging = logOptions ;
439+ }
440+ if (
441+ typeof logOptions === 'object'
442+ && Object . prototype . hasOwnProperty . call ( logOptions , 'extended' )
443+ && typeof logOptions . extended === 'boolean'
444+ ) {
445+ extendedLogging = logOptions . extended ;
446+ }
427447 // no logger supplied by user
428- if ( ! Object . prototype . hasOwnProperty . call ( logOptions , 'logger' ) ) return ;
448+ if ( ! Object . prototype . hasOwnProperty . call ( logOptions , 'logger' ) ) {
449+ return ;
450+ }
451+ const { logger } = logOptions as any ; // we checked that it exists beforehand
429452 // making sure the logger supplied by the user has our required log levels (info, warn, error)
430- // @ts -ignore
431- if ( ( typeof logOptions . logger . info !== 'function' ) || ( typeof logOptions . logger . warn !== 'function' ) || ( typeof logOptions . logger . error !== 'function' ) ) throw new Error ( 'Your supplied logger does not seem to expose the log levels BLAPI needs to work. Make sure your logger offers the following methods: info() warn() error()' ) ;
453+ if (
454+ typeof logger . info !== 'function'
455+ || typeof logger . warn !== 'function'
456+ || typeof logger . error !== 'function'
457+ ) {
458+ throw new Error (
459+ 'Your supplied logger does not seem to expose the log levels BLAPI needs to work. Make sure your logger offers the following methods: info() warn() error()' ,
460+ ) ;
461+ }
432462 // @ts -ignore
433463 userLogger = logOptions . logger ;
434464}
0 commit comments