Skip to content

Commit

Permalink
feat: setup default discord bot
Browse files Browse the repository at this point in the history
  • Loading branch information
Khaaz committed Jan 14, 2024
1 parent 0f2eab9 commit c9a2541
Show file tree
Hide file tree
Showing 13 changed files with 849 additions and 64 deletions.
104 changes: 104 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-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/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# 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 variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# 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

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
19 changes: 19 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Deploy
# https://github.com/marketplace/actions/github-action-for-flyctl
# https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/
on:
push:
branches:
- main

jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
concurrency: deploy-group # optional: ensure only one action runs at a time
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
46 changes: 0 additions & 46 deletions .github/workflows/publish.yaml

This file was deleted.

5 changes: 5 additions & 0 deletions .infisical.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"workspaceId": "64c14ce918cf3f49e88326db",
"defaultEnvironment": "dev",
"gitBranchToEnvironmentMapping": null
}
41 changes: 41 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=20.9.0
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Node.js"

# Node.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"
ARG YARN_VERSION=1.22.11
RUN npm install -g yarn@$YARN_VERSION --force


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3

# Install node modules
COPY --link package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Copy application code
COPY --link . .


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
EXPOSE 8080
CMD [ "yarn", "run", "start" ]
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# template
# discord-admin

Template repo for future node projects
Discord admin bot for the Owlebot Discord server.
Use Owlebot open-api.

## Setup

Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# fly.toml app configuration file generated for discord-admin-owlebot on 2024-01-14T18:18:57+01:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "discord-admin-owlebot"
primary_region = "cdg"

[build]

[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ["app"]

[[vm]]
cpu_kind = "shared"
cpus = 1
memory_mb = 1024
25 changes: 16 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
{
"name": "@owlebot/template",
"name": "@owlebot/discord-admin",
"version": "0.1.0",
"description": "Owlebot template.",
"description": "Owlebot admin discord bot.",
"author": "Khaaz <[email protected]>, Xerstom <[email protected]>",
"link": "https://github.com/owlebot/template",
"link": "https://github.com/owlebot/discord-admin",
"main": "./src/index.js",
"type": "module",
"license": "MIT",
"engines": {
"node": ">=20.9.0"
},
"private": true,
"dependencies": {},
"dependencies": {
"body-parser": "^1.19.2",
"express": "^4.18.2",
"slash-create": "^5.11.0"
},
"devDependencies": {
"@flydotio/dockerfile": "^0.5.0",
"@owlebot/eslint-config": "^1.0.0",
"dotenv": "^16.3.1",
"eslint": "^8.33.0"
},
"scripts": {
"lint": "eslint src/**/*.js",
"test": "yarn run lint",
"secret:pull": "infisical export --path=\"discord-admin\" --env=prod > .env",
"start": "node src/index.js",
"start:watch": "node src/index.js --watch",
"start:debug": "node src/index.js --inspect"
"start:watch": "node --watch -r dotenv/config src/index.js",
"start:debug": "node --inspect -r dotenv/config src/index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/owlebot/template.git"
"url": "git+https://github.com/owlebot/discord-admin.git"
},
"bugs": {
"url": "https://github.com/owlebot/template/issues"
"url": "https://github.com/owlebot/discord-admin/issues"
},
"homepage": "https://github.com/owlebot/template#readme",
"homepage": "https://github.com/owlebot/discord-admin#readme",
"keywords": []
}
30 changes: 30 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { readdir } from "node:fs/promises";

import { Creator } from "slash-create";

async function findFilesWithExtension(path, extensions, names = [] ) {
const dir = await readdir(path);
for (const name of dir) {
names.push(name);
}

return names;
}

export class CustomSlashCreator extends Creator {
// eslint-disable-next-line no-unused-vars
async registerCommandsIn(commandPath, customExtensions = [] ) {
const commands = [];
const files = await findFilesWithExtension(commandPath);
for (const path of files) {
try {
commands.push(await import(`./commands/${path}`) );
} catch (error) {
this.emit("error", new Error(`Failed to load command ${path}: ${error}`) );
}
}

return this.registerCommands(commands, true);
}
}

39 changes: 39 additions & 0 deletions src/commands/community.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-disable import/no-default-export */
import { SlashCommand } from "slash-create";

export default class CommunityCommand extends SlashCommand {
#baseUrl = "https://api.owle.bot";

#headers = {
"X-Access-Token": process.env.OPEN_API_TOKEN,
};

constructor(creator) {
super(creator, {
name: "community",
description: "Get the number of members in your top community.",
} );
}

async run(ctx) {
const discordUser = ctx.user;

const url = new URL(`/v1/accounts/${discordUser.id}/user/communities`, this.#baseUrl);

try {
const res = await fetch(url.href, {
headers: this.#headers,
} );

const data = await res.json();

if (data.communities?.length > 0) {
data.communities.sort( (a, b) => b.members - a.members);
return ctx.send(`Your highest community has ${data.communities[0].members} members`);
}
return ctx.send("You don't have any community yet");
} catch (err) {
return ctx.send("Error communicating with Owlebot API.");
}
}
}
Loading

0 comments on commit c9a2541

Please sign in to comment.