NodeJS TypeScript Setup (#1)

* bot can login to discord

* changed node and npm to iron lts

* added typescript runnables

* more dev scripts

* readme update on scripts

* event handling skeleton

* fixed compiler target issue
This commit is contained in:
Kevin Dang
2023-12-22 11:22:16 -08:00
committed by GitHub
parent f4af329d44
commit c9dfd3671d
14 changed files with 1614 additions and 126 deletions

268
.gitignore vendored
View File

@@ -1,132 +1,148 @@
# secret bot token
token.txt
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
# Credentials
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
.dev.env
# Spyder project settings
.spyderproject
.spyproject
# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node
# Rope project settings
.ropeproject
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# mkdocs documentation
/site
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Pyre type checker
.pyre/
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
### Node Patch ###
# Serverless Webpack directories
.webpack/
# Optional stylelint cache
# SvelteKit build / generate output
.svelte-kit
# End of https://www.toptal.com/developers/gitignore/api/node

View File

@@ -2,6 +2,11 @@
Ollama is an AI model management tool that allows users to install and use custom large language models locally. The goal is to create a discord bot that will utilize Ollama and chat with it on a Discord!
## Resources
* [NodeJS](https://nodejs.org/en)
* This project uses `v20.10.0` (npm `10.2.5`). Consider using [nvm](https://github.com/nvm-sh/nvm) for multiple NodeJS versions.
* To run dev in `ts-node`, using `v18.x.x` is recommended.
* To run dev with `tsx`, you can use `v20.10.0`.
* This project supports any NodeJS version above `16.x.x` to only allow ESModules.
* [Ollama](https://ollama.ai/)
* [Docker Documentation](https://docs.docker.com/?_gl=1*nof6f8*_ga*MTQxNTc1MTYxOS4xNzAxNzI1ODAx*_ga_XJWPQMJYHQ*MTcwMjQxODUzOS4yLjEuMTcwMjQxOTgyMC41OS4wLjA.)
* [Discord Developer Portal](https://discord.com/developers/docs/intro)

12
nodemon.json Normal file
View File

@@ -0,0 +1,12 @@
{
"restartable": "rs",
"ignore": ["node_modules/"],
"watch": ["src/"],
"execMap": {
"ts": "ts-node --esm"
},
"env": {
"NODE_ENV": "development"
},
"ext": "js,json,ts"
}

1291
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

31
package.json Normal file
View File

@@ -0,0 +1,31 @@
{
"name": "discord-ollama",
"version": "0.0.1",
"description": "Ollama Integration into discord",
"main": "dist/index.js",
"exports": "./dist/index.js",
"scripts": {
"dev-tsx": "tsx watch src/index.ts",
"dev-mon": "nodemon --config nodemon.json src/index.ts",
"build": "tsc",
"prod": "node .",
"start": "npm run build && npm run prod"
},
"author": "Kevin Dang",
"license": "ISC",
"dependencies": {
"discord.js": "^14.14.1",
"dotenv": "^16.3.1"
},
"devDependencies": {
"@types/node": "^20.10.5",
"nodemon": "^3.0.2",
"ts-node": "^10.9.2",
"tsx": "^4.6.2",
"typescript": "^5.3.3"
},
"type": "module",
"engines": {
"node": ">=16.0.0"
}
}

24
src/client.ts Normal file
View File

@@ -0,0 +1,24 @@
import { Client, GatewayIntentBits } from "discord.js";
import { registerEvents } from "./utils/events.js";
import Events from "./events/index.js";
// Import keys/tokens
import Keys from "./keys.js";
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
registerEvents(client, Events)
// Try to log in the client
client.login(Keys.clientToken)
.catch((error) => {
console.error('[Login Error]', error);
process.exit(1);
});

7
src/events/index.ts Normal file
View File

@@ -0,0 +1,7 @@
import { Event } from '../utils/index.js'
import ready from './ready.js'
// Centralized export for all events
export default [
ready
] as Event[] // staticly is better ts practice, dynamic exporting is possible

5
src/events/ready.ts Normal file
View File

@@ -0,0 +1,5 @@
import { event, Events } from '../utils/index.js'
export default event(Events.ClientReady, ({ log }, client) => {
return log(`Logged in as ${client.user.username}.`)
})

1
src/index.ts Normal file
View File

@@ -0,0 +1 @@
import('./client.js')

7
src/keys.ts Normal file
View File

@@ -0,0 +1,7 @@
import { getEnvVar } from "./utils/env.js"
export const Keys = {
clientToken: getEnvVar('CLIENT_TOKEN')
} as const // readonly keys
export default Keys

21
src/utils/env.ts Normal file
View File

@@ -0,0 +1,21 @@
import { resolve } from "path"
import { config } from "dotenv"
// Find config - ONLY WORKS WITH NODEMON
const envFile = process.env.NODE_ENV === "development" ? ".dev.env" : ".env"
// resolve config file
const envFilePath = resolve(process.cwd(), envFile)
// set current environment variable file
config({ path: envFilePath })
// Getter for environment variables
export function getEnvVar(name: string, fallback?: string): string {
const value = process.env[name] ?? fallback
if (value == undefined)
throw new Error(`Environment variable ${name} is not set.`)
// return env variable
return value
}

43
src/utils/events.ts Normal file
View File

@@ -0,0 +1,43 @@
import type { ClientEvents, Awaitable, Client } from 'discord.js';
// Export events through here to reduce amount of imports
export { Events } from 'discord.js';
export type LogMethod = (...args: unknown[]) => void;
export type EventKeys = keyof ClientEvents; // only wants keys of ClientEvents object
// Event properties
export interface EventProps {
client: Client;
log: LogMethod;
}
export type EventCallback<T extends EventKeys> = (
props: EventProps,
...args: ClientEvents[T]
) => Awaitable<unknown>; // Method can be synchronous or async, unknown so we can return anything
// Event interface
export interface Event<T extends EventKeys = EventKeys> {
key: T;
callback: EventCallback<T>;
}
export function event<T extends EventKeys>(key: T, callback: EventCallback<T>): Event<T> {
return { key, callback };
}
export function registerEvents(client: Client, events: Event[]): void {
for (const { key, callback } of events) {
client.on(key, (...args) => {
// Create a new log method for this event
const log = console.log.bind(console, `[Event: ${key}]`);
// Handle Errors, call callback, log errors as needed
try {
callback({ client, log }, ...args);
} catch (error) {
log('[Uncaught Error]', error);
}
});
}
}

3
src/utils/index.ts Normal file
View File

@@ -0,0 +1,3 @@
// Centralized import index
export * from './env.js';
export * from './events.js';

22
tsconfig.json Normal file
View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
// Dependent on node version
"target": "ES2020",
"module": "Node16",
"moduleResolution": "Node16",
"strict": true,
// We must set the type
"noImplicitAny": true,
"declaration": false,
// Will not go through node_modules
"skipDefaultLibCheck": true,
"strictNullChecks": true,
// We can import json files like JavaScript
"resolveJsonModule": true,
// Decompile .ts to .js into a folder named dist
"outDir": "dist"
},
// environment for env vars
"include": ["src/**/*"],
"exclude": ["node_modules"]
}