From c3a0dd5b8e41120f3752c3b79266697b08e8762a Mon Sep 17 00:00:00 2001
From: IdiNium <47635037+idinium96@users.noreply.github.com>
Date: Tue, 2 Aug 2022 06:21:03 +0800
Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20unhandled=20rejection=20er?=
 =?UTF-8?q?ror=20that=20crashes=20the=20bot?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/classes/MyHandler/MyHandler.ts            | 38 +++++++++----------
 .../MyHandler/utils/craftClassWeapons.ts      |  4 +-
 2 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/src/classes/MyHandler/MyHandler.ts b/src/classes/MyHandler/MyHandler.ts
index 153fe829a..41dd56e62 100644
--- a/src/classes/MyHandler/MyHandler.ts
+++ b/src/classes/MyHandler/MyHandler.ts
@@ -258,13 +258,13 @@ export default class MyHandler extends Handler {
             keepMetalSupply(this.bot, this.minimumScrap, this.minimumReclaimed, this.combineThreshold);
 
             // Craft duplicate weapons
-            void craftDuplicateWeapons(this.bot);
-
-            // Craft class weapons
-            this.classWeaponsTimeout = setTimeout(() => {
-                // called after 5 seconds to craft metals and duplicated weapons first.
-                void craftClassWeapons(this.bot);
-            }, 5 * 1000);
+            craftDuplicateWeapons(this.bot)
+                .then(() => {
+                    return craftClassWeapons(this.bot);
+                })
+                .catch(err => {
+                    log.warn('Failed to craft duplicated craft/class weapons', err);
+                });
         }
 
         if (this.isDeletingUntradableJunk) {
@@ -2205,18 +2205,14 @@ export default class MyHandler extends Handler {
                 // Smelt / combine metal
                 keepMetalSupply(this.bot, this.minimumScrap, this.minimumReclaimed, this.combineThreshold);
 
-                // Craft duplicated weapons
-                void craftDuplicateWeapons(this.bot);
-
-                this.classWeaponsTimeout = setTimeout(() => {
-                    // called after 5 second to craft metals and duplicated weapons first.
-                    void craftClassWeapons(this.bot);
-                }, 5 * 1000);
-            }
-
-            if (this.isDeletingUntradableJunk) {
-                // Delete untradable junk
-                this.deleteUntradableJunk();
+                // Craft duplicate weapons
+                craftDuplicateWeapons(this.bot)
+                    .then(() => {
+                        return craftClassWeapons(this.bot);
+                    })
+                    .catch(err => {
+                        log.warn('Failed to craft duplicated craft/class weapons', err);
+                    });
             }
 
             // Sort inventory
@@ -2546,7 +2542,9 @@ export default class MyHandler extends Handler {
 
         for (const assetid of assetidsToDelete) {
             log.debug(`Deleting junk item ${assetid}`);
-            this.bot.tf2gc.deleteItem(assetid);
+            this.bot.tf2gc.deleteItem(assetid, err => {
+                log.warn('Error deleting untradable junk', err);
+            });
         }
     }
 
diff --git a/src/classes/MyHandler/utils/craftClassWeapons.ts b/src/classes/MyHandler/utils/craftClassWeapons.ts
index e8a780419..db698947d 100644
--- a/src/classes/MyHandler/utils/craftClassWeapons.ts
+++ b/src/classes/MyHandler/utils/craftClassWeapons.ts
@@ -17,13 +17,13 @@ export type ClassesForCraftableWeapons =
 
 export type SlotsForCraftableWeapons = 'primary' | 'secondary' | 'melee' | 'pda2';
 
-export default function craftClassWeapons(bot: Bot): Promise<void> {
+export default async function craftClassWeapons(bot: Bot): Promise<void> {
     if (!bot.options.crafting.weapons.enable) {
         return;
     }
     const currencies = bot.inventoryManager.getInventory.getCurrencies(bot.craftWeapons, false);
 
-    void Promise.all(
+    await Promise.all(
         ['scout', 'soldier', 'pyro', 'demoman', 'heavy', 'engineer', 'medic', 'sniper', 'spy'].map(classChar =>
             craftEachClassWeapons(bot, bot.craftWeaponsByClass[classChar as ClassesForCraftableWeapons], currencies)
         )