Skip to content

Commit

Permalink
Initial source code
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuba committed Mar 31, 2023
0 parents commit 361f5f2
Show file tree
Hide file tree
Showing 9 changed files with 926 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules/
.envrc
README.md
135 changes: 135 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# 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
.cache

# 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.*

brain.json

# direnv
.envrc
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM node:16-alpine

RUN apk add --update --no-cache && \
rm -rf /var/cache/apk/ && mkdir /var/cache/apk/ && \
rm -rf /usr/share/man

ENV WORK_DIR=/discord-bot

RUN mkdir -p $WORK_DIR
WORKDIR $WORK_DIR

COPY package.json ./
COPY package-lock.json ./
RUN npm ci

COPY . .

ARG TOKEN
ARG ROLE
ARG GUILD

ENTRYPOINT [ "npm", "start"]
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
### Architecture
Add scripts or event hooks to the `/scripts/` folder. These should export an
object with two keys:
+ `trigger`, which is the
[Event](https://discord.js.org/#/docs/discord.js/main/typedef/Events) that
triggers the script.
+ `execute`, which is a function that takes in a `client` and returns a event
handler function. For a barebones example of this, check out
[logged-in.js](./scripts/logged-in.js).

### Local Development
1. Create your own bot using the [the discord developer portal](https://discord.com/developers/applications).
2. Make sure your bot has the proper server authorizations.
3. Create a server, invite the bot to it, and give it a good permissions.
4. Clone this repo, and set the following environment variables below. I
recommend [direnv](https://direnv.net/) to manage this.
5. Navigate to this directory.
6. `$ npm ci`
7. `$ node index.js`
8. You should be greeted with something that looks like:

```
Logged in as BotUser#123!
Successfully registered application commands.
```

### Environment Configuration

+ `TOKEN`: The token for the discord bot from the discord API.
+ `ROLE`: The role-id for threads registration.
+ `GUILD`: The id of the server. Any link to a discord message has the
following structure:
https://discord.com/channels/GUID_ID/CHANNEL_ID/MESSAGE_ID.
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const TOKEN = process.env.TOKEN

const { Client, Intents } = require("discord.js")
const fs = require("fs")
const path = require("path")

const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MEMBERS,
],
})

const scriptsPath = path.join(__dirname, "scripts")
const scriptFiles = fs
.readdirSync(scriptsPath)
.filter((file) => file.endsWith(".js"))
for (const file of scriptFiles) {
const filePath = path.join(scriptsPath, file)
const script = require(filePath)
client.on(script.trigger, script.execute(client))
}

client.login(TOKEN)
Loading

0 comments on commit 361f5f2

Please sign in to comment.