From 56f38b632a98df76ea28d4158264b45864e83733 Mon Sep 17 00:00:00 2001 From: Waren Gonzaga Date: Thu, 4 May 2023 06:40:51 +0000 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9C=A8=20TWEAK:=20ready=20and=20error=20?= =?UTF-8?q?as=20modular=20event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/bot.js | 34 ++++++++++++++++------------------ src/events/error.js | 9 +++++++++ src/events/ready.js | 16 ++++++++++++++++ yarn.lock | 28 ++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 src/events/error.js create mode 100644 src/events/ready.js diff --git a/package.json b/package.json index ac7e0bd..a2db79c 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "dependencies": { "discord.js": "^14.6.0", "dotenv": "^16.0.3", + "fs-extra": "^11.1.1", "google-spreadsheet": "^3.3.0", "moment": "^2.29.4" }, diff --git a/src/bot.js b/src/bot.js index 751225c..d3b3e16 100644 --- a/src/bot.js +++ b/src/bot.js @@ -1,5 +1,6 @@ -const { - ActivityType, +const fs = require('node:fs'); +const path = require('node:path'); +const { Client, ChannelType, GatewayIntentBits, @@ -292,22 +293,19 @@ const formatTime = (date) => { return moment.utc(date).utcOffset(config.utc_offset).format('M/DD/YYYY HH:mm:ss'); } -// discord error log event -client.on('error', (err) => { - console.log(err); -}); - -// discord log event -client.once('ready', bot => { - client.user?.setPresence({ - activities: [{ - name: 'for support.', - type: ActivityType.Watching - }] - }); - - console.log(`Ready! Logged in as ${bot.user.tag}`); -}); +// reading events file in discord.js +const eventsPath = path.join(__dirname, 'events'); +const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); + +for (const file of eventFiles) { + const filePath = path.join(eventsPath, file); + const event = require(filePath); + if (event.once) { + client.once(event.name, (...args) => event.execute(...args)); + } else { + client.on(event.name, (...args) => event.execute(...args)); + } +} // log in to Discord with your client's token client.login(token); diff --git a/src/events/error.js b/src/events/error.js new file mode 100644 index 0000000..a2ceccb --- /dev/null +++ b/src/events/error.js @@ -0,0 +1,9 @@ +const { Events } = require('discord.js'); + +module.exports = { + name: Events.Error, + once: false, + execute(error) { + console.log(`Not ready due to ${error}`); + }, +}; \ No newline at end of file diff --git a/src/events/ready.js b/src/events/ready.js new file mode 100644 index 0000000..eec1828 --- /dev/null +++ b/src/events/ready.js @@ -0,0 +1,16 @@ +const { Events, ActivityType } = require('discord.js'); + +module.exports = { + name: Events.ClientReady, + once: true, + execute(bot) { + bot.user?.setPresence({ + activities: [{ + name: 'for support.', + type: ActivityType.Watching + }] + }); + + console.log(`Ready! Logged in as ${bot.user.tag}`); + }, +}; \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 5a27337..49ac865 100644 --- a/yarn.lock +++ b/yarn.lock @@ -269,6 +269,15 @@ follow-redirects@^1.14.0: resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13" integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA== +fs-extra@^11.1.1: + version "11.1.1" + resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-11.1.1.tgz#da69f7c39f3b002378b0954bb6ae7efdc0876e2d" + integrity sha512-MGIE4HOvQCeUCzmlHs0vXpih4ysz4wg9qiSAu6cd42lVwPbTM1TjV7RusoyQqMmk/95gdQZX72u+YW+c3eEpFQ== + dependencies: + graceful-fs "^4.2.0" + jsonfile "^6.0.1" + universalify "^2.0.0" + fsevents@~2.3.2: version "2.3.2" resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a" @@ -331,6 +340,11 @@ google-spreadsheet@^3.3.0: google-auth-library "^6.1.3" lodash "^4.17.21" +graceful-fs@^4.1.6, graceful-fs@^4.2.0: + version "4.2.11" + resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.11.tgz#4183e4e8bf08bb6e05bbb2f7d2e0c8f712ca40e3" + integrity sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ== + gtoken@^5.0.4: version "5.3.2" resolved "https://registry.yarnpkg.com/gtoken/-/gtoken-5.3.2.tgz#deb7dc876abe002178e0515e383382ea9446d58f" @@ -404,6 +418,15 @@ json-bigint@^1.0.0: dependencies: bignumber.js "^9.0.0" +jsonfile@^6.0.1: + version "6.1.0" + resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae" + integrity sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ== + dependencies: + universalify "^2.0.0" + optionalDependencies: + graceful-fs "^4.1.6" + jwa@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/jwa/-/jwa-2.0.0.tgz#a7e9c3f29dae94027ebcaf49975c9345593410fc" @@ -636,6 +659,11 @@ undici@^5.13.0: dependencies: busboy "^1.6.0" +universalify@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717" + integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ== + util-deprecate@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" From 6bd684afd85c7e4bf30aee8b4e163f509d9b082f Mon Sep 17 00:00:00 2001 From: Waren Gonzaga Date: Thu, 4 May 2023 07:21:24 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=E2=98=95=EF=B8=8F=20CHORE:=20small=20updat?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bot.js b/src/bot.js index d3b3e16..1d05bd3 100644 --- a/src/bot.js +++ b/src/bot.js @@ -293,7 +293,7 @@ const formatTime = (date) => { return moment.utc(date).utcOffset(config.utc_offset).format('M/DD/YYYY HH:mm:ss'); } -// reading events file in discord.js +// reading events file const eventsPath = path.join(__dirname, 'events'); const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); From e7f5d460675f8177aaa0c30bb8e76d91b4657c0a Mon Sep 17 00:00:00 2001 From: Waren Gonzaga Date: Fri, 5 May 2023 02:01:46 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=E2=98=95=EF=B8=8F=20CHORE:=20update=20disc?= =?UTF-8?q?ord.js=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- yarn.lock | 176 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 104 insertions(+), 74 deletions(-) diff --git a/package.json b/package.json index a2db79c..715c132 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "license": "GPL-3.0", "private": true, "dependencies": { - "discord.js": "^14.6.0", + "discord.js": "^14.10.2", "dotenv": "^16.0.3", "fs-extra": "^11.1.1", "google-spreadsheet": "^3.3.0", diff --git a/yarn.lock b/yarn.lock index 49ac865..48b59ad 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,59 +2,82 @@ # yarn lockfile v1 -"@discordjs/builders@^1.4.0": - version "1.4.0" - resolved "https://registry.yarnpkg.com/@discordjs/builders/-/builders-1.4.0.tgz#b951b5e6ce4e459cd06174ce50dbd51c254c1d47" - integrity sha512-nEeTCheTTDw5kO93faM1j8ZJPonAX86qpq/QVoznnSa8WWcCgJpjlu6GylfINTDW6o7zZY0my2SYdxx2mfNwGA== - dependencies: - "@discordjs/util" "^0.1.0" - "@sapphire/shapeshift" "^3.7.1" - discord-api-types "^0.37.20" +"@discordjs/builders@^1.6.3": + version "1.6.3" + resolved "https://registry.yarnpkg.com/@discordjs/builders/-/builders-1.6.3.tgz#994b4fe57e77b47096f74bb5a1f664870a930a43" + integrity sha512-CTCh8NqED3iecTNuiz49mwSsrc2iQb4d0MjMdmS/8pb69Y4IlzJ/DIy/p5GFlgOrFbNO2WzMHkWKQSiJ3VNXaw== + dependencies: + "@discordjs/formatters" "^0.3.1" + "@discordjs/util" "^0.3.1" + "@sapphire/shapeshift" "^3.8.2" + discord-api-types "^0.37.41" fast-deep-equal "^3.1.3" - ts-mixer "^6.0.2" - tslib "^2.4.1" + ts-mixer "^6.0.3" + tslib "^2.5.0" -"@discordjs/collection@^1.3.0": - version "1.3.0" - resolved "https://registry.yarnpkg.com/@discordjs/collection/-/collection-1.3.0.tgz#65bf9674db72f38c25212be562bb28fa0dba6aa3" - integrity sha512-ylt2NyZ77bJbRij4h9u/wVy7qYw/aDqQLWnadjvDqW/WoWCxrsX6M3CIw9GVP5xcGCDxsrKj5e0r5evuFYwrKg== +"@discordjs/collection@^1.5.1": + version "1.5.1" + resolved "https://registry.yarnpkg.com/@discordjs/collection/-/collection-1.5.1.tgz#bc7ca557838dc29247bf19860426637f103bc383" + integrity sha512-aWEc9DCf3TMDe9iaJoOnO2+JVAjeRNuRxPZQA6GVvBf+Z3gqUuWYBy2NWh4+5CLYq5uoc3MOvUQ5H5m8CJBqOA== -"@discordjs/rest@^1.4.0": - version "1.5.0" - resolved "https://registry.yarnpkg.com/@discordjs/rest/-/rest-1.5.0.tgz#dc15474ab98cf6f31291bf61bbc72bcf4f30cea2" - integrity sha512-lXgNFqHnbmzp5u81W0+frdXN6Etf4EUi8FAPcWpSykKd8hmlWh1xy6BmE0bsJypU1pxohaA8lQCgp70NUI3uzA== +"@discordjs/formatters@^0.3.1": + version "0.3.1" + resolved "https://registry.yarnpkg.com/@discordjs/formatters/-/formatters-0.3.1.tgz#81393cf25e6e3223361061629752ea727475e842" + integrity sha512-M7X4IGiSeh4znwcRGcs+49B5tBkNDn4k5bmhxJDAUhRxRHTiFAOTVUNQ6yAKySu5jZTnCbSvTYHW3w0rAzV1MA== dependencies: - "@discordjs/collection" "^1.3.0" - "@discordjs/util" "^0.1.0" - "@sapphire/async-queue" "^1.5.0" - "@sapphire/snowflake" "^3.2.2" - discord-api-types "^0.37.23" - file-type "^18.0.0" - tslib "^2.4.1" - undici "^5.13.0" + discord-api-types "^0.37.41" -"@discordjs/util@^0.1.0": - version "0.1.0" - resolved "https://registry.yarnpkg.com/@discordjs/util/-/util-0.1.0.tgz#e42ca1bf407bc6d9adf252877d1b206e32ba369a" - integrity sha512-e7d+PaTLVQav6rOc2tojh2y6FE8S7REkqLldq1XF4soCx74XB/DIjbVbVLtBemf0nLW77ntz0v+o5DytKwFNLQ== +"@discordjs/rest@^1.7.1": + version "1.7.1" + resolved "https://registry.yarnpkg.com/@discordjs/rest/-/rest-1.7.1.tgz#eeef0e71a37c95fa27962129729b2aa9de8e3752" + integrity sha512-Ofa9UqT0U45G/eX86cURQnX7gzOJLG2oC28VhIk/G6IliYgQF7jFByBJEykPSHE4MxPhqCleYvmsrtfKh1nYmQ== + dependencies: + "@discordjs/collection" "^1.5.1" + "@discordjs/util" "^0.3.0" + "@sapphire/async-queue" "^1.5.0" + "@sapphire/snowflake" "^3.4.2" + discord-api-types "^0.37.41" + file-type "^18.3.0" + tslib "^2.5.0" + undici "^5.22.0" + +"@discordjs/util@^0.3.0", "@discordjs/util@^0.3.1": + version "0.3.1" + resolved "https://registry.yarnpkg.com/@discordjs/util/-/util-0.3.1.tgz#4e8737e1dcff7e9f5eccc3116fb44755b65b1e97" + integrity sha512-HxXKYKg7vohx2/OupUN/4Sd02Ev3PBJ5q0gtjdcvXb0ErCva8jNHWfe/v5sU3UKjIB/uxOhc+TDOnhqffj9pRA== + +"@discordjs/ws@^0.8.2": + version "0.8.2" + resolved "https://registry.yarnpkg.com/@discordjs/ws/-/ws-0.8.2.tgz#e76f013fe69e41641e9c4d9f0514763048f691d0" + integrity sha512-av2yboE9VrBn0xyObiMMCrBbls+ucjjR3MNI5hc78hdNnYLoZQuvEVLGN8h/I+W1Vr08WATxke69RXsNZS5FBQ== + dependencies: + "@discordjs/collection" "^1.5.1" + "@discordjs/rest" "^1.7.1" + "@discordjs/util" "^0.3.0" + "@sapphire/async-queue" "^1.5.0" + "@types/ws" "^8.5.4" + "@vladfrangu/async_event_emitter" "^2.2.1" + discord-api-types "^0.37.41" + tslib "^2.5.0" + ws "^8.13.0" "@sapphire/async-queue@^1.5.0": version "1.5.0" resolved "https://registry.yarnpkg.com/@sapphire/async-queue/-/async-queue-1.5.0.tgz#2f255a3f186635c4fb5a2381e375d3dfbc5312d8" integrity sha512-JkLdIsP8fPAdh9ZZjrbHWR/+mZj0wvKS5ICibcLrRI1j84UmLMshx5n9QmL8b95d4onJ2xxiyugTgSAX7AalmA== -"@sapphire/shapeshift@^3.7.1": - version "3.8.1" - resolved "https://registry.yarnpkg.com/@sapphire/shapeshift/-/shapeshift-3.8.1.tgz#b98dc6a7180f9b38219267917b2e6fa33f9ec656" - integrity sha512-xG1oXXBhCjPKbxrRTlox9ddaZTvVpOhYLmKmApD/vIWOV1xEYXnpoFs68zHIZBGbqztq6FrUPNPerIrO1Hqeaw== +"@sapphire/shapeshift@^3.8.2": + version "3.8.2" + resolved "https://registry.yarnpkg.com/@sapphire/shapeshift/-/shapeshift-3.8.2.tgz#f9f25cba74c710b56f8790de76a9642a9635e7db" + integrity sha512-NXpnJAsxN3/h9TqQPntOeVWZrpIuucqXI3IWF6tj2fWCoRLCuVK5wx7Dtg7pRrtkYfsMUbDqgKoX26vrC5iYfA== dependencies: fast-deep-equal "^3.1.3" lodash "^4.17.21" -"@sapphire/snowflake@^3.2.2": - version "3.4.0" - resolved "https://registry.yarnpkg.com/@sapphire/snowflake/-/snowflake-3.4.0.tgz#25c012158a9feea2256c718985dbd6c1859a5022" - integrity sha512-zZxymtVO6zeXVMPds+6d7gv/OfnCc25M1Z+7ZLB0oPmeMTPeRWVPQSS16oDJy5ZsyCOLj7M6mbZml5gWXcVRNw== +"@sapphire/snowflake@^3.4.2": + version "3.4.2" + resolved "https://registry.yarnpkg.com/@sapphire/snowflake/-/snowflake-3.4.2.tgz#365af8e7b57ada924ec8e85383b921280f81d128" + integrity sha512-KJwlv5gkGjs1uFV7/xx81n3tqgBwBJvH94n1xDyH3q+JSmtsMeSleJffarEBfG2yAFeJiFA4BnGOK6FFPHc19g== "@tokenizer/token@^0.3.0": version "0.3.0" @@ -66,13 +89,18 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-18.13.0.tgz#0400d1e6ce87e9d3032c19eb6c58205b0d3f7850" integrity sha512-gC3TazRzGoOnoKAhUx+Q0t8S9Tzs74z7m0ipwGpSqQrleP14hKxP4/JUeEQcD3W1/aIpnWl8pHowI7WokuZpXg== -"@types/ws@^8.5.3": +"@types/ws@^8.5.4": version "8.5.4" resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.4.tgz#bb10e36116d6e570dd943735f86c933c1587b8a5" integrity sha512-zdQDHKUgcX/zBc4GrwsE/7dVdAD8JR4EuiAXiiUhhfyIJXXb2+PrGshFyeXWQPMmmZ2XxgaqclgpIC7eTXc1mg== dependencies: "@types/node" "*" +"@vladfrangu/async_event_emitter@^2.2.1": + version "2.2.1" + resolved "https://registry.yarnpkg.com/@vladfrangu/async_event_emitter/-/async_event_emitter-2.2.1.tgz#5f44b3095d5edd695a9d12079f81b41826ad7831" + integrity sha512-XtUEAS0m6uVddXW+EImGunLiJZzWNWAZQBoQCUneowrYXPQ6y7c0iWEm/wVYyGpTixTIhUfLRSoYCwojL64htA== + abbrev@1: version "1.1.1" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" @@ -193,28 +221,30 @@ debug@^3.2.7: dependencies: ms "^2.1.1" -discord-api-types@^0.37.20, discord-api-types@^0.37.23: - version "0.37.31" - resolved "https://registry.yarnpkg.com/discord-api-types/-/discord-api-types-0.37.31.tgz#128d33d641fd9a92fba97a47d7052e1f5694ec27" - integrity sha512-k9DQQ7Wv+ehiF7901qk/FnP47k6O2MHm3meQFee4gUzi5dfGAVLf7SfLNtb4w7G2dmukJyWQtVJEDF9oMb9yuQ== - -discord.js@^14.6.0: - version "14.7.1" - resolved "https://registry.yarnpkg.com/discord.js/-/discord.js-14.7.1.tgz#26079d0ff4d27daf02480a403c456121f0682bd9" - integrity sha512-1FECvqJJjjeYcjSm0IGMnPxLqja/pmG1B0W2l3lUY2Gi4KXiyTeQmU1IxWcbXHn2k+ytP587mMWqva2IA87EbA== - dependencies: - "@discordjs/builders" "^1.4.0" - "@discordjs/collection" "^1.3.0" - "@discordjs/rest" "^1.4.0" - "@discordjs/util" "^0.1.0" - "@sapphire/snowflake" "^3.2.2" - "@types/ws" "^8.5.3" - discord-api-types "^0.37.20" +discord-api-types@^0.37.41: + version "0.37.41" + resolved "https://registry.yarnpkg.com/discord-api-types/-/discord-api-types-0.37.41.tgz#b2719aa25ba6c5794d08524742ab008224b03710" + integrity sha512-FaPGBK9hx3zqSRX1x3KQWj+OElAJKmcyyfcdCy+U4AKv+gYuIkRySM7zd1So2sE4gc1DikkghkSBgBgKh6pe4Q== + +discord.js@^14.10.2: + version "14.10.2" + resolved "https://registry.yarnpkg.com/discord.js/-/discord.js-14.10.2.tgz#0cc7eec422ab1589e1fdaf57fcb14cd063324610" + integrity sha512-yPMJ/vGSWJP8WbbfPX18WGU2wOLBjL+OKVOVKI1eWx3ICcUhDg5MvFkg5aNGiokjPfKlAgqnFIUH0jl59ONHrQ== + dependencies: + "@discordjs/builders" "^1.6.3" + "@discordjs/collection" "^1.5.1" + "@discordjs/formatters" "^0.3.1" + "@discordjs/rest" "^1.7.1" + "@discordjs/util" "^0.3.1" + "@discordjs/ws" "^0.8.2" + "@sapphire/snowflake" "^3.4.2" + "@types/ws" "^8.5.4" + discord-api-types "^0.37.41" fast-deep-equal "^3.1.3" lodash.snakecase "^4.1.1" - tslib "^2.4.1" - undici "^5.13.0" - ws "^8.11.0" + tslib "^2.5.0" + undici "^5.22.0" + ws "^8.13.0" dotenv@^16.0.3: version "16.0.3" @@ -248,10 +278,10 @@ fast-text-encoding@^1.0.0: resolved "https://registry.yarnpkg.com/fast-text-encoding/-/fast-text-encoding-1.0.6.tgz#0aa25f7f638222e3396d72bf936afcf1d42d6867" integrity sha512-VhXlQgj9ioXCqGstD37E/HBeqEGV/qOD/kmbVG8h5xKBYvM1L3lR1Zn4555cQ8GkYbJa8aJSipLPndE1k6zK2w== -file-type@^18.0.0: - version "18.2.0" - resolved "https://registry.yarnpkg.com/file-type/-/file-type-18.2.0.tgz#c2abec00d1af0f09151e1549e3588aab0bac5001" - integrity sha512-M3RQMWY3F2ykyWZ+IHwNCjpnUmukYhtdkGGC1ZVEUb0ve5REGF7NNJ4Q9ehCUabtQKtSVFOMbFTXgJlFb0DQIg== +file-type@^18.3.0: + version "18.3.0" + resolved "https://registry.yarnpkg.com/file-type/-/file-type-18.3.0.tgz#116d4df9120b7f17e973ad5c976b82a40407bd07" + integrity sha512-pkPZ5OGIq0TYb37b8bHDLNeQSe1H2KlaQ2ySGpJkkr2KZdaWsO4QhPzHA0mQcsUW2cSqJk+4gM/UyLz/UFbXdQ== dependencies: readable-web-to-node-stream "^3.0.2" strtok3 "^7.0.0" @@ -637,12 +667,12 @@ tr46@~0.0.3: resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw== -ts-mixer@^6.0.2: +ts-mixer@^6.0.3: version "6.0.3" resolved "https://registry.yarnpkg.com/ts-mixer/-/ts-mixer-6.0.3.tgz#69bd50f406ff39daa369885b16c77a6194c7cae6" integrity sha512-k43M7uCG1AkTyxgnmI5MPwKoUvS/bRvLvUb7+Pgpdlmok8AoqmUaZxUUw8zKM5B1lqZrt41GjYgnvAi0fppqgQ== -tslib@^2.4.1: +tslib@^2.5.0: version "2.5.0" resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.5.0.tgz#42bfed86f5787aeb41d031866c8f402429e0fddf" integrity sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg== @@ -652,10 +682,10 @@ undefsafe@^2.0.5: resolved "https://registry.yarnpkg.com/undefsafe/-/undefsafe-2.0.5.tgz#38733b9327bdcd226db889fb723a6efd162e6e2c" integrity sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA== -undici@^5.13.0: - version "5.21.0" - resolved "https://registry.yarnpkg.com/undici/-/undici-5.21.0.tgz#b00dfc381f202565ab7f52023222ab862bb2494f" - integrity sha512-HOjK8l6a57b2ZGXOcUsI5NLfoTrfmbOl90ixJDl0AEFG4wgHNDQxtZy15/ZQp7HhjkpaGlp/eneMgtsu1dIlUA== +undici@^5.22.0: + version "5.22.0" + resolved "https://registry.yarnpkg.com/undici/-/undici-5.22.0.tgz#5e205d82a5aecc003fc4388ccd3d2c6e8674a0ad" + integrity sha512-fR9RXCc+6Dxav4P9VV/sp5w3eFiSdOjJYsbtWfd4s5L5C4ogyuVpdKIVHeW0vV1MloM65/f7W45nR9ZxwVdyiA== dependencies: busboy "^1.6.0" @@ -682,10 +712,10 @@ whatwg-url@^5.0.0: tr46 "~0.0.3" webidl-conversions "^3.0.0" -ws@^8.11.0: - version "8.12.0" - resolved "https://registry.yarnpkg.com/ws/-/ws-8.12.0.tgz#485074cc392689da78e1828a9ff23585e06cddd8" - integrity sha512-kU62emKIdKVeEIOIKVegvqpXMSTAMLJozpHZaJNDYqBjzlSYXQGviYwN1osDLJ9av68qHd4a2oSjd7yD4pacig== +ws@^8.13.0: + version "8.13.0" + resolved "https://registry.yarnpkg.com/ws/-/ws-8.13.0.tgz#9a9fb92f93cf41512a0735c8f4dd09b8a1211cd0" + integrity sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA== yallist@^4.0.0: version "4.0.0" From da0e59ebdf2d6c10a8f37655fbe86d19fe31d101 Mon Sep 17 00:00:00 2001 From: Waren Gonzaga Date: Fri, 5 May 2023 03:32:53 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=E2=9C=A8=20TWEAK:=20modular=20utilities?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bot.js | 72 +++---------------------------------------- src/utils/core.js | 38 +++++++++++++++++++++++ src/utils/database.js | 41 ++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 68 deletions(-) create mode 100644 src/utils/core.js create mode 100644 src/utils/database.js diff --git a/src/bot.js b/src/bot.js index 7cc21fb..b3bd32e 100644 --- a/src/bot.js +++ b/src/bot.js @@ -4,11 +4,10 @@ const { Client, ChannelType, GatewayIntentBits, - Partials, - EmbedBuilder } = require('discord.js'); -const { GoogleSpreadsheet } = require('google-spreadsheet'); -const config = require(`${__dirname}/config.json`); -const moment = require('moment'); + Partials } = require('discord.js'); +const config = require('./config.json'); +const { sendEmbedMessage, formatTime } = require('./utils/core'); +const { sendData } = require('./utils/database'); require('dotenv').config(); @@ -34,29 +33,6 @@ const client = new Client({ ] }); -// load spreadsheet -const doc = new GoogleSpreadsheet(process.env.GOOGLE_SPREADSHEET_ID); - -/** - * send embed message - * @param {string} message - * @returns pre-defined embed style - */ -const sendEmbedMessage = (message) => { - return new EmbedBuilder() - .setDescription(message) - .setColor(`#f213a4`); -} - -/** - * get username from ownerid - * @param {number} id - * @returns - */ -const getUsernameFromId = async (id) => { - return (await client.users.fetch(id)).username; -} - // listen to post messages client.on('messageCreate', async (message) => { if (message.author.bot) return; @@ -253,46 +229,6 @@ client.on('threadCreate', async post => { }, config.datasheet_init); }); -/** - * sends data to the spreadsheet - * @param {object} data - data being added as row in the spreadsheet - * @param {string} datasheet - name of sheet where data being sent e.g. init, response, resolve - */ -const sendData = async (data, datasheet) => { - // authenticate - await doc.useServiceAccountAuth({ - client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL, - private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n"), - }); - // load the "initial" sheet - await doc.loadInfo(); - const sheet = doc.sheetsByTitle[datasheet]; - - // check if the data will be send to init sheet - if (datasheet === config.datasheet_init) { - await sheet.addRow(data); - }; - - // check if the data will be send to response sheet - if (datasheet === config.datasheet_response) { - await sheet.addRow(data); - } - - // check if the data will be send to resolve sheet - if (datasheet === config.datasheet_resolve) { - await sheet.addRow(data); - }; -} - -/** - * format time according to UTC - * @param {number} date - epoch timestamp - * @returns time and date format - */ -const formatTime = (date) => { - return moment.utc(date).utcOffset(config.utc_offset).format('M/DD/YYYY HH:mm:ss'); -} - // reading events file const eventsPath = path.join(__dirname, 'events'); const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); diff --git a/src/utils/core.js b/src/utils/core.js new file mode 100644 index 0000000..fdae6c1 --- /dev/null +++ b/src/utils/core.js @@ -0,0 +1,38 @@ +const { EmbedBuilder } = require('discord.js'); +const moment = require('moment'); +const config = require('../config.json'); + +/** + * send embed message + * @param {string} message + * @returns pre-defined embed style + */ +const sendEmbedMessage = (message) => { + return new EmbedBuilder() + .setDescription(message) + .setColor(`#f213a4`); +} + +/** + * format time according to UTC + * @param {number} date - epoch timestamp + * @returns time and date format + */ +const formatTime = (date) => { + return moment.utc(date).utcOffset(config.utc_offset).format('M/DD/YYYY HH:mm:ss'); +} + +/** + * get username from ownerid/author.id + * @param {number} id user's id + * @returns + */ +const getUsernameFromId = async (id) => { + return (await client.users.fetch(id)).username; +} + +module.exports = { + sendEmbedMessage, + formatTime, + getUsernameFromId +} \ No newline at end of file diff --git a/src/utils/database.js b/src/utils/database.js new file mode 100644 index 0000000..a8326f0 --- /dev/null +++ b/src/utils/database.js @@ -0,0 +1,41 @@ +const config = require('../config.json'); +const { GoogleSpreadsheet } = require('google-spreadsheet'); +require('dotenv').config(); + +// load the spreadsheet +const doc = new GoogleSpreadsheet(process.env.GOOGLE_SPREADSHEET_ID); + +/** + * sends data to the spreadsheet + * @param {object} data - data being added as row in the spreadsheet + * @param {string} datasheet - name of sheet where data being sent e.g. init, response, resolve + */ +const sendData = async (data, datasheet) => { + // authenticate + await doc.useServiceAccountAuth({ + client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL, + private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n"), + }); + // load the "initial" sheet + await doc.loadInfo(); + const sheet = doc.sheetsByTitle[datasheet]; + + // check if the data will be send to init sheet + if (datasheet === config.datasheet_init) { + await sheet.addRow(data); + }; + + // check if the data will be send to response sheet + if (datasheet === config.datasheet_response) { + await sheet.addRow(data); + } + + // check if the data will be send to resolve sheet + if (datasheet === config.datasheet_resolve) { + await sheet.addRow(data); + }; +} + +module.exports = { + sendData +} \ No newline at end of file From 2156fe51b82e7101ea2a94e7ff604bf1ada7af50 Mon Sep 17 00:00:00 2001 From: Waren Gonzaga Date: Fri, 5 May 2023 07:13:08 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20close=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bot.js | 121 ++++++++++++++++++++++++++++++------------ src/config.json | 4 ++ src/utils/database.js | 5 ++ 3 files changed, 96 insertions(+), 34 deletions(-) diff --git a/src/bot.js b/src/bot.js index b3bd32e..2be5a2c 100644 --- a/src/bot.js +++ b/src/bot.js @@ -66,15 +66,12 @@ client.on('messageCreate', async (message) => { if (typeof post.availableTags !== 'undefined') { // filter the tags to get the resolution tag name ID const resolutionTag = post.availableTags.filter((item) => { return item.name == config.tag_name_resolve }); + const closeTag = post.availableTags.filter((item) => { return item.name == config.tag_name_close }); // get the existing tags of the post const postTags = message.channel.appliedTags; - - // collect tags - let initialTags = [resolutionTag[0].id,...postTags]; - let tags = [...new Set(initialTags)]; // check if the command has the prefix and includes "resolve" - if (message.content.startsWith(config.command_prefix) && message.content.includes(config.command_resolve)) { + if (message.content.startsWith(config.command_prefix)) { await message.delete(); // delete the commmand message // check if the message is in the forum post and from the support role @@ -83,39 +80,91 @@ client.on('messageCreate', async (message) => { // check if the post has fewer tags if (postTags.length < 5) { - // send embed message before resolving the post - await message.channel.send({ embeds: [ - sendEmbedMessage(`${config.reminder_resolve}`) - ], - content: `🔔 <@${message.channel.ownerId}>` - }) - - // then archive and lock it - message.channel.edit({ - appliedTags: tags, - archived: true - }); - // gather data const postId = message.channel.id; const resolutionTime = formatTime(message.createdTimestamp); const resolvedBy = member.user.username; - // check if there's a mentioned user - if (mention.users.first()) { - // send the data, use the mentioned user as resolvedBy - sendData({ - post_id: postId, - resolution_time: resolutionTime, - resolved_by: mention.users.first().username, - }, config.datasheet_resolve); - } else { - // send the data with the one who sends the command - sendData({ - post_id: postId, - resolution_time: resolutionTime, - resolved_by: resolvedBy - }, config.datasheet_resolve); + // functions for resolve command + if (message.content.includes(config.command_resolve)) { + + // data for resolve command + // collect tags + let initialTags = [resolutionTag[0].id,...postTags]; + let tags = [...new Set(initialTags)]; + + // send embed message upon executing the resolve command + await message.channel.send({ + embeds: [ + sendEmbedMessage(`${config.reminder_resolve}`) + ], + content: `🔔 <@${message.channel.ownerId}>` + }); + + // then archive and lock it + message.channel.edit({ + appliedTags: tags, + archived: true + }); + + // check if there's a mentioned user + if (mention.users.first()) { + // send the data, use the mentioned user as resolvedBy + sendData({ + post_id: postId, + resolution_time: resolutionTime, + resolved_by: mention.users.first().username, + }, config.datasheet_resolve); + } else { + // send the data with the one who sends the command + sendData({ + post_id: postId, + resolution_time: resolutionTime, + resolved_by: resolvedBy + }, config.datasheet_resolve); + } + + } + + // functions for close command + if (message.content.includes(config.command_close)) { + + // data for resolve command + // collect tags + let initialTags = [closeTag[0].id,...postTags]; + let tags = [...new Set(initialTags)]; + + // send embed message upon executing the close command + await message.channel.send({ + embeds: [ + sendEmbedMessage(`${config.reminder_close}`) + ], + content: `🔔 <@${message.channel.ownerId}>` + }); + + // then archive and lock it + message.channel.edit({ + appliedTags: tags, + archived: true + }); + + // check if there's a mentioned user + if (mention.users.first()) { + // send the data, use the mentioned user as resolvedBy + sendData({ + post_id: postId, + close_time: resolutionTime, + closed_by: mention.users.first().username, + }, config.datasheet_close); + } else { + // send the data with the one who sends the command + sendData({ + post_id: postId, + close_time: resolutionTime, + closed_by: resolvedBy + }, config.datasheet_close); + } + } } else { @@ -211,8 +260,10 @@ client.on('threadCreate', async post => { const tags = forumTags.join(', '); const firstResponse = `=IFERROR(VLOOKUP(A2:A,${config.datasheet_response}!A2:B,2,0))`; const resolutionTime = `=IFERROR(VLOOKUP(A2:A,${config.datasheet_resolve}!A2:B,2,0))`; + const closeTime = `=IFERROR(VLOOKUP(A2:A,${config.datasheet_close}!A2:B,2,0))`; const responder = `=IFERROR(VLOOKUP(A2:A,{${config.datasheet_response}!A2:A,${config.datasheet_response}!C2:C},2,0))`; const resolvedBy = `=IFERROR(VLOOKUP(A2:A,{${config.datasheet_resolve}!A2:A,${config.datasheet_resolve}!C2:C},2,0))`; + const closedBy = `=IFERROR(VLOOKUP(A2:A,{${config.datasheet_close}!A2:A,${config.datasheet_close}!C2:C},2,0))`; // send the data sendData({ @@ -225,7 +276,9 @@ client.on('threadCreate', async post => { responder: responder, first_response: firstResponse, resolution_time: resolutionTime, - resolved_by: resolvedBy + resolved_by: resolvedBy, + close_time: closeTime, + closed_by: closedBy }, config.datasheet_init); }); diff --git a/src/config.json b/src/config.json index 7c8cefd..1a19a16 100644 --- a/src/config.json +++ b/src/config.json @@ -3,12 +3,16 @@ "command_resolve": "resolve", "command_close": "close", "command_escalate": "escalate", + "command_reopen": "reopen", "tag_name_resolve": "Solved", "tag_name_close": "Closed", "tag_name_escalate": "Escalated", "datasheet_init": "init", "datasheet_response": "response", "datasheet_resolve": "resolve", + "datasheet_close": "close", + "datasheet_escalate": "escalate", + "datasheet_reopen": "reopen", "utc_offset": -8, "reminder_mention": "Hey! If you need help, please read the information in <#1074862134284005396> and post your questions or issues in the <#1029543258822553680> channel. Our team and community members are always ready to help you out. Thank you for building with us!", "reminder_max_tags": "Max tags (5) exceeded. Please update the original post with fewer tags and try again.", diff --git a/src/utils/database.js b/src/utils/database.js index a8326f0..06b1d2a 100644 --- a/src/utils/database.js +++ b/src/utils/database.js @@ -34,6 +34,11 @@ const sendData = async (data, datasheet) => { if (datasheet === config.datasheet_resolve) { await sheet.addRow(data); }; + + // check if the data will be send to close sheet + if (datasheet === config.datasheet_close) { + await sheet.addRow(data); + }; } module.exports = {