diff --git a/.gitignore b/.gitignore index 6cf9853..19e4092 100644 --- a/.gitignore +++ b/.gitignore @@ -39,5 +39,5 @@ dist/**/* # ignore yarn.lock yarn.lock - +prod pnpm-lock.yaml diff --git a/README.md b/README.md index b0a2975..4d9ffc7 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Jana-Reza-Bot -To start +To start `npm run start` This will run the bot using ts-node -bot invite link +bot invite link https://v13.discordjs.guide/preparations/adding-your-bot-to-servers.html#bot-invite-links -add your bot token to env as +add your bot token to env as TOKEN="YOUR_TOKEN" in Discord Dev Portal under permissions add the needed permission to your bot dicord bot invite link to your server diff --git a/package-lock.json b/package-lock.json index dbd0300..5ea364e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,9 @@ "dependencies": { "dayjs": "^1.11.10", "discord.js": "^14.14.1", - "dotenv": "^16.4.5" + "dotenv": "^16.4.5", + "node-fetch": "^3.3.2", + "openai": "^4.38.3" }, "devDependencies": { "@eslint/js": "^9.0.0", @@ -384,6 +386,15 @@ "undici-types": "~5.26.4" } }, + "node_modules/@types/node-fetch": { + "version": "2.6.11", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.11.tgz", + "integrity": "sha512-24xFj9R5+rfQJLRyM56qh+wnVSYhyXC2tkoBndtY0U+vubqNsYXGjufB2nn8Q6gt0LrARwL6UBtMCSVCwl4B1g==", + "dependencies": { + "@types/node": "*", + "form-data": "^4.0.0" + } + }, "node_modules/@types/semver": { "version": "7.5.8", "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.8.tgz", @@ -825,6 +836,17 @@ "npm": ">=7.0.0" } }, + "node_modules/abort-controller": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", + "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", + "dependencies": { + "event-target-shim": "^5.0.0" + }, + "engines": { + "node": ">=6.5" + } + }, "node_modules/acorn": { "version": "8.11.3", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", @@ -855,6 +877,17 @@ "node": ">=0.4.0" } }, + "node_modules/agentkeepalive": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/agentkeepalive/-/agentkeepalive-4.5.0.tgz", + "integrity": "sha512-5GG/5IbQQpC9FpkRGsSvZI5QYeSCzlJHdpBQntCsuTOxhKD8lqKhrleg2Yi7yvMIf82Ycmmqln9U8V9qwEiJew==", + "dependencies": { + "humanize-ms": "^1.2.1" + }, + "engines": { + "node": ">= 8.0.0" + } + }, "node_modules/ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -916,6 +949,11 @@ "node": ">=8" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -987,6 +1025,17 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -1013,6 +1062,14 @@ "node": ">= 8" } }, + "node_modules/data-uri-to-buffer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", + "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", + "engines": { + "node": ">= 12" + } + }, "node_modules/dayjs": { "version": "1.11.10", "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.10.tgz", @@ -1041,6 +1098,14 @@ "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==", "dev": true }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/diff": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", @@ -1292,6 +1357,14 @@ "node": ">=0.10.0" } }, + "node_modules/event-target-shim": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", + "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==", + "engines": { + "node": ">=6" + } + }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -1346,6 +1419,28 @@ "reusify": "^1.0.4" } }, + "node_modules/fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, "node_modules/file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", @@ -1406,6 +1501,55 @@ "integrity": "sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw==", "dev": true }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/form-data-encoder": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/form-data-encoder/-/form-data-encoder-1.7.2.tgz", + "integrity": "sha512-qfqtYan3rxrnCk1VYaA4H+Ms9xdpPqvLZa6xmMgFvhO32x7/3J/ExcTd6qpxM0vH2GdMI+poehyBZvqfMTto8A==" + }, + "node_modules/formdata-node": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/formdata-node/-/formdata-node-4.4.1.tgz", + "integrity": "sha512-0iirZp3uVDjVGt9p49aTaqjk84TrglENEDuqfdlZQ1roC9CWlPk6Avf8EEnZNcAqPonwkG35x4n3ww/1THYAeQ==", + "dependencies": { + "node-domexception": "1.0.0", + "web-streams-polyfill": "4.0.0-beta.3" + }, + "engines": { + "node": ">= 12.20" + } + }, + "node_modules/formdata-node/node_modules/web-streams-polyfill": { + "version": "4.0.0-beta.3", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-4.0.0-beta.3.tgz", + "integrity": "sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug==", + "engines": { + "node": ">= 14" + } + }, + "node_modules/formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "dependencies": { + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": ">=12.20.0" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -1491,6 +1635,14 @@ "node": ">=8" } }, + "node_modules/humanize-ms": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/humanize-ms/-/humanize-ms-1.2.1.tgz", + "integrity": "sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==", + "dependencies": { + "ms": "^2.0.0" + } + }, "node_modules/ignore": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.1.tgz", @@ -1702,6 +1854,25 @@ "node": ">=8.6" } }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -1717,8 +1888,7 @@ "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/natural-compare": { "version": "1.4.0", @@ -1726,6 +1896,41 @@ "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", "dev": true }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ], + "engines": { + "node": ">=10.5.0" + } + }, + "node_modules/node-fetch": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz", + "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==", + "dependencies": { + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -1735,6 +1940,51 @@ "wrappy": "1" } }, + "node_modules/openai": { + "version": "4.38.3", + "resolved": "https://registry.npmjs.org/openai/-/openai-4.38.3.tgz", + "integrity": "sha512-mIL9WtrFNOanpx98mJ+X/wkoepcxdqqu0noWFoNQHl/yODQ47YM7NEYda7qp8JfjqpLFVxY9mQhshoS/Fqac0A==", + "dependencies": { + "@types/node": "^18.11.18", + "@types/node-fetch": "^2.6.4", + "abort-controller": "^3.0.0", + "agentkeepalive": "^4.2.1", + "form-data-encoder": "1.7.2", + "formdata-node": "^4.3.2", + "node-fetch": "^2.6.7", + "web-streams-polyfill": "^3.2.1" + }, + "bin": { + "openai": "bin/cli" + } + }, + "node_modules/openai/node_modules/@types/node": { + "version": "18.19.31", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.31.tgz", + "integrity": "sha512-ArgCD39YpyyrtFKIqMDvjz79jto5fcI/SVUs2HwB+f0dAzq68yqOdyaSivLiLugSziTpNXLQrVb7RZFmdZzbhA==", + "dependencies": { + "undici-types": "~5.26.4" + } + }, + "node_modules/openai/node_modules/node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, "node_modules/optionator": { "version": "0.9.3", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.3.tgz", @@ -2063,6 +2313,11 @@ "node": ">=8.0" } }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, "node_modules/ts-api-utils": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/ts-api-utils/-/ts-api-utils-1.3.0.tgz", @@ -2222,6 +2477,28 @@ "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", "dev": true }, + "node_modules/web-streams-polyfill": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", + "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==", + "engines": { + "node": ">= 8" + } + }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index 08ec74b..966a9de 100644 --- a/package.json +++ b/package.json @@ -1,50 +1,52 @@ { - "name": "jana-reza-bot", - "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "lint": "eslint src --ignore-path .eslintignore --ext .js,.ts", - "start": "ts-node src/index.ts" - }, - "repository": { - "type": "git", - "url": "git+ssh://github.com:fac31/Jana-Reza-Bot.git" - }, - "keywords": [], - "contributors": [ - { - "name": "Reza Ghaniloo", - "email": "email@email.com", - "url": "https://github.com/rghaniloo" + "name": "jana-reza-bot", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "lint": "eslint src --ignore-path .eslintignore --ext .js,.ts && prettier ./src --write ", + "start": "ts-node src/index.ts" }, - { - "name": "Jana Gerrard", - "email": "email@email.com", - "url": "https://github.com/sulphite" + "repository": { + "type": "git", + "url": "git+ssh://github.com:fac31/Jana-Reza-Bot.git" + }, + "keywords": [], + "contributors": [ + { + "name": "Reza Ghaniloo", + "email": "email@email.com", + "url": "https://github.com/rghaniloo" + }, + { + "name": "Jana Gerrard", + "email": "email@email.com", + "url": "https://github.com/sulphite" + } + ], + "license": "ISC", + "bug": { + "url": "https://github.com/fac31/Jana-Reza-Bot/issues" + }, + "homepage": "https://github.com/fac31/Jana-Reza-Bot#jana-reza-bot", + "dependencies": { + "chrono-node": "^2.7.5", + "dayjs": "^1.11.10", + "discord.js": "^14.14.1", + "dotenv": "^16.4.5", + "node-fetch": "^3.3.2", + "openai": "^4.38.3" + }, + "devDependencies": { + "@eslint/js": "^9.0.0", + "@typescript-eslint/eslint-plugin": "^7.6.0", + "@typescript-eslint/parser": "^7.6.0", + "eslint": "^8.57.0", + "globals": "^15.0.0", + "prettier": "^3.2.5", + "ts-node": "^10.9.2", + "typescript": "^5.4.5", + "typescript-eslint": "^7.6.0" } - ], - "license": "ISC", - "bug": { - "url": "https://github.com/fac31/Jana-Reza-Bot/issues" - }, - "homepage": "https://github.com/fac31/Jana-Reza-Bot#jana-reza-bot", - "dependencies": { - "chrono-node": "^2.7.5", - "dayjs": "^1.11.10", - "discord.js": "^14.14.1", - "dotenv": "^16.4.5" - }, - "devDependencies": { - "@eslint/js": "^9.0.0", - "@typescript-eslint/eslint-plugin": "^7.6.0", - "@typescript-eslint/parser": "^7.6.0", - "eslint": "^8.57.0", - "globals": "^15.0.0", - "prettier": "^3.2.5", - "ts-node": "^10.9.2", - "typescript": "^5.4.5", - "typescript-eslint": "^7.6.0" - } } diff --git a/sample.env b/sample.env new file mode 100644 index 0000000..ba41790 --- /dev/null +++ b/sample.env @@ -0,0 +1,5 @@ +#DISCORD +TOKEN="Discord Bot Token" + +#OPENAI +API_KEY="OPEN AI API Key" diff --git a/src/Command.ts b/src/Command.ts index bca76db..d7516d6 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -1,8 +1,12 @@ -import { CommandInteraction, ChatInputApplicationCommandData, Client } from "discord.js"; +import { + CommandInteraction, + ChatInputApplicationCommandData, + Client, +} from 'discord.js' /** - * defining the structure of the slash command + * defining the structure of the slash command */ export type Command = ChatInputApplicationCommandData & { - run: (client: Client, interaction: CommandInteraction) => void; -}; \ No newline at end of file + run: (client: Client, interaction: CommandInteraction) => void +} diff --git a/src/commandCenter/Commands.ts b/src/commandCenter/Commands.ts index 418d3b3..cb33192 100644 --- a/src/commandCenter/Commands.ts +++ b/src/commandCenter/Commands.ts @@ -1,7 +1,6 @@ -import { Command } from "../Command"; -import { Howdy } from "./commands/Howdy"; -import { Time } from "./commands/Time"; +import { Command } from '../Command' +import { Howdy } from './commands/Howdy' +import { Assistant } from './commands/Assistant' +import { Time } from './commands/Time' -export const Commands: Command[] = [ - Howdy, Time -]; +export const Commands: Command[] = [Howdy, Time, Assistant] diff --git a/src/commandCenter/commands/Assistant.ts b/src/commandCenter/commands/Assistant.ts new file mode 100644 index 0000000..bcda258 --- /dev/null +++ b/src/commandCenter/commands/Assistant.ts @@ -0,0 +1,41 @@ +import { + CommandInteraction, + Client, + ApplicationCommandType, + ApplicationCommandOptionType, +} from 'discord.js' +import { Command } from '../../Command' +import 'dotenv/config' +import { fetchOpenAIChatCompletion } from '../../openai' + +export const Assistant: Command = { + name: 'assistant', + description: 'assistant command', + type: ApplicationCommandType.ChatInput, + options: [ + { + name: 'query', + description: ' tell me a nerdy joke', + type: ApplicationCommandOptionType.String, + required: true, + }, + ], + run: async (_: Client, interaction: CommandInteraction) => { + const userInput = interaction.options.get('query')?.value as string + console.log(userInput) + if (userInput === null || userInput === undefined) { + // await interaction.reply('Please provide a query'); + return + } + + try { + const generatedText = await fetchOpenAIChatCompletion(userInput) + console.log(generatedText) + await interaction.followUp(generatedText) + } catch (error) { + console.error(error) + // Consider providing a more user-friendly error message here + // await interaction.reply('An error occurred. Please try again later.'); + } + }, +} diff --git a/src/commandCenter/commands/Howdy.ts b/src/commandCenter/commands/Howdy.ts index d8cc854..fc29288 100644 --- a/src/commandCenter/commands/Howdy.ts +++ b/src/commandCenter/commands/Howdy.ts @@ -1,17 +1,17 @@ -import { CommandInteraction, Client , ApplicationCommandType} from "discord.js"; -import { Command } from "../../Command"; +import { CommandInteraction, Client, ApplicationCommandType } from 'discord.js' +import { Command } from '../../Command' export const Howdy: Command = { - name: "howdy", - description: "Howdy command", + name: 'howdy', + description: 'Howdy command', type: ApplicationCommandType.ChatInput, run: async (client: Client, interaction: CommandInteraction) => { - const content = `Howdy!👋 How are you doing today ${client.user?.username}?`; + const content = `Howdy!👋 How are you doing today ${client.user?.username}?` await interaction.followUp({ //https://discordjs.guide/slash-commands/response-methods.html#ephemeral-responses ephemeral: true, - content - }); - } -} \ No newline at end of file + content, + }) + }, +} diff --git a/src/index.ts b/src/index.ts index fe464f9..fb3f3ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,14 @@ import 'dotenv/config' -import { Client} from "discord.js" -import ready from './listeners/ready'; -import interactionCreator from './listeners/interactionCreator'; +import { Client } from 'discord.js' +import ready from './listeners/ready' +import interactionCreator from './listeners/interactionCreator' -const token = process.env.TOKEN; +const token = process.env.TOKEN console.log('FACTROID IS starting .....') const client = new Client({ - intents: [] + intents: [], +}) -}); - -ready(client); // registering the listener -interactionCreator(client); //listerning to interactipon -client.login(token); \ No newline at end of file +ready(client) // registering the listener +interactionCreator(client) //listerning to interactipon +client.login(token) diff --git a/src/listeners/interactionCreator.ts b/src/listeners/interactionCreator.ts index 2a84bb5..ffea417 100644 --- a/src/listeners/interactionCreator.ts +++ b/src/listeners/interactionCreator.ts @@ -1,21 +1,24 @@ -import { CommandInteraction, Client, Interaction } from "discord.js"; -import { Commands } from "../commandCenter/Commands"; +import { CommandInteraction, Client, Interaction } from 'discord.js' +import { Commands } from '../commandCenter/Commands' export default (client: Client): void => { - client.on("interactionCreate", async (interaction: Interaction) => { + client.on('interactionCreate', async (interaction: Interaction) => { if (interaction.isCommand()) { await handleSlashCommand(client, interaction) } }) } -const handleSlashCommand = async (client: Client, interaction: CommandInteraction): Promise => { - const slashCommand = Commands.find(command => command.name === interaction.commandName); +const handleSlashCommand = async ( + client: Client, + interaction: CommandInteraction +): Promise => { + const slashCommand = Commands.find( + (command) => command.name === interaction.commandName + ) if (!slashCommand) { - interaction.followUp({content: 'we havea problem'}) - return; + interaction.followUp({ content: 'we havea problem' }) + return } - await interaction.deferReply(); - slashCommand.run(client, interaction); - - -}; \ No newline at end of file + await interaction.deferReply() + slashCommand.run(client, interaction) +} diff --git a/src/listeners/ready.ts b/src/listeners/ready.ts index d62a9c2..f7be493 100644 --- a/src/listeners/ready.ts +++ b/src/listeners/ready.ts @@ -1,22 +1,20 @@ -import { Client } from "discord.js"; +import { Client } from 'discord.js' // we register all the commands with the client in the ready event -import {Commands} from "../commandCenter/Commands"; - +import { Commands } from '../commandCenter/Commands' /** - * sepreating and registering a listner to listen to + * sepreating and registering a listner to listen to * variety of messages, user activities etc - * + * */ export default (client: Client): void => { // :void as its non value returning func - client.on("ready", async () => { + client.on('ready', async () => { if (!client.user || !client.application) { - return; + return } // registering all the commands - await client.application.commands.set(Commands); + await client.application.commands.set(Commands) console.log(`${client?.user?.username} is online and ready !`) - }); - -} \ No newline at end of file + }) +} diff --git a/src/openai.ts b/src/openai.ts new file mode 100644 index 0000000..545d78d --- /dev/null +++ b/src/openai.ts @@ -0,0 +1,36 @@ +import OpenAI from 'openai' +import 'dotenv/config' + +const API_KEY = process.env.API_KEY + +if (!API_KEY) { + throw new Error('API_URL or API_KEY is missing') +} +const openai = new OpenAI({ apiKey: process.env.API_KEY }) + +export async function fetchOpenAIChatCompletion( + userInput: string +): Promise { + try { + const completion = await openai.chat.completions.create({ + model: 'gpt-3.5-turbo', + messages: [ + { role: 'system', content: 'You are a helpful assistant.' }, + { role: 'user', content: userInput }, + ], + stream: true, // Enable streaming for chunks + }) + + let generatedText: string = '' // Initialize empty string + for await (const chunk of completion) { + if (chunk.choices[0]?.delta?.content === undefined) { + break + } + generatedText += chunk.choices[0]?.delta?.content + } + return generatedText + } catch (error) { + console.error(error) + throw new Error('An error occurred while processing your request.') + } +}