Skip to content

Commit

Permalink
Update framework version (#7)
Browse files Browse the repository at this point in the history
* changed client script path

* add web clients

* update discordjs lib

* add guild id

* add bot install link

* fix typescript lib

* update env file
  • Loading branch information
hoonsubin authored Sep 19, 2021
1 parent 5bc7249 commit 9b000f2
Show file tree
Hide file tree
Showing 17 changed files with 1,906 additions and 2,602 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/deploy_action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ on:
branches:
- "main"
- "master"
- "release/**"
- "production/**"
- "release/*"
- "production/*"
release: # triggers on all release events. Remove this if you're not using it
types: [published, created, edited]

jobs:
# build the source code
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
steps:
- uses: actions/setup-node@v2-beta
- uses: actions/setup-node@v2
- uses: actions/checkout@v2
with:
node-version: 12.x
node-version: ${{ matrix.node-version }}
- name: Build App
run: |
yarn
Expand Down
18 changes: 12 additions & 6 deletions .github/workflows/pr_action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ jobs:
run_tests:
name: Unit Tests
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js 12.x
uses: actions/setup-node@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: 12.x
node-version: ${{ matrix.node-version }}
- name: unit_test
run: |
yarn
Expand All @@ -19,12 +22,15 @@ jobs:
build_code:
name: Build Check
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js 12.x
uses: actions/setup-node@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: 12.x
node-version: ${{ matrix.node-version }}
- name: build
run: |
yarn
Expand Down
29 changes: 0 additions & 29 deletions .vscode/extensions.json

This file was deleted.

37 changes: 0 additions & 37 deletions .vscode/settings.json

This file was deleted.

13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ This starter is focused on developing a full distributable Discord server bot or
Before starting the server, you need to first create a `.env` file with the following variables.

```env
# used for server bots
DISCORD_TOKEN=<discord bot token>
# used for webhook app
WEBHOOK_ID=<discord channel webhook client id>
WEBHOOK_TOKEN=<discord channel webhook client token>
# Bot user app token
DISCORD_APP_TOKEN=<bot token>
# Bot user client ID
DISCORD_APP_CLIENT_ID=<bot client>
# Server ID for the bot to be installed
DISCORD_GUILD_ID=<guild id>
```

Next, ensure that you have a usable Discord app on your developer portal.
Expand All @@ -36,7 +37,7 @@ Discord bot token is used to login to Discord as a bot user.
![bot-token](img/bot-token.png)

Click on the 'Copy' button under the 'Tokens' section to get your bot token.
Then paste it as the value for `DISCORD_TOKEN` in your `.env` file.
Then paste it as the value for `DISCORD_APP_TOKEN` in your `.env` file.

For adding a channel webhook integration, you need to first create a webhook under the 'Channel Edit' -> 'Integration' settings.

Expand Down
53 changes: 28 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discord-bot-starter",
"version": "1.0.0",
"version": "1.1.0",
"description": "Discord bot starter project made with discord.js",
"private": true,
"keywords": [
Expand Down Expand Up @@ -28,38 +28,41 @@
"main": "build/index.js",
"scripts": {
"start": "yarn run serve",
"serve": "yarn run build && NODE_ENV=production node -r dotenv/config ./build/index.js",
"serve": "node ./build/index.js",
"dev": "ts-node-dev -r dotenv/config src/index.ts",
"build": "tsc --project tsconfig.json",
"lint": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix",
"test": "NODE_ENV=test jest --setupFiles dotenv/config --verbose --coverage && eslint '*/**/*.{js,ts,tsx}'",
"sandbox": "ts-node -r dotenv/config scripts/sandbox.ts"
"lint": "eslint '*/**/*.{js,ts}' --quiet --fix",
"lint:check": "eslint '*/**/*.{js,ts}'",
"test": "NODE_ENV=test jest --verbose --coverage"
},
"engines": {
"node": ">=12.x"
"node": ">=16.6.x"
},
"license": "MIT",
"devDependencies": {
"@types/jest": "^26.0.21",
"@types/node": "^14.14.35",
"@types/ws": "^7.4.0",
"@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0",
"eslint": "^7.22.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
"jest-config": "^26.6.3",
"nock": "^13.0.11",
"prettier": "^2.2.1",
"ts-jest": "^26.5.4",
"ts-node-dev": "^1.1.6",
"typescript": "^4.2.3"
"@types/express": "^4.17.13",
"@types/jest": "^27.0.1",
"@types/node": "^16.9.3",
"@types/node-fetch": "2.5.10",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"dotenv": "^10.0.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.2.0",
"jest-config": "^27.2.0",
"nock": "^13.1.3",
"prettier": "^2.4.1",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"ts-node-dev": "^1.1.8",
"typescript": "^4.4.3"
},
"dependencies": {
"discord.js": "^12.5.1",
"dotenv": "^8.2.0",
"node": "^15.10.0",
"ts-node": "^9.1.1"
"@discordjs/rest": "^0.1.0-canary.0",
"discord-api-types": "^0.23.1",
"discord.js": "^13.1.0",
"express": "^4.17.1"
}
}
5 changes: 0 additions & 5 deletions scripts/README.md

This file was deleted.

6 changes: 0 additions & 6 deletions scripts/sandbox.ts

This file was deleted.

85 changes: 50 additions & 35 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,66 @@
import Discord, { Message } from 'discord.js';

const TOKEN = process.env.DISCORD_TOKEN;
const WEBHOOK_CRED = { id: process.env.WEBHOOK_ID, token: process.env.WEBHOOK_TOKEN };
import { DISCORD_APP_TOKEN } from './config';
import { initDiscordApp, appOauthInstallUrl } from './clients';
import express from 'express';

/**
* the main entry function for running the discord application
*/
export default async function main() {
// if (!TOKEN) throw new Error('Please provide discord bot credentials');
// await discordBot(TOKEN);

if (!WEBHOOK_CRED.id || !WEBHOOK_CRED.token) throw new Error('Please provide discord channel webhook credentials');
await webhookIntegration(WEBHOOK_CRED.id, WEBHOOK_CRED.token);
export default async function app() {
await discordAppController();
await expressAppController();
}

async function discordBot(token: string) {
// Create an instance of a Discord client app
const client = new Discord.Client({ fetchAllMembers: true, disableMentions: 'all' });
/**
* Handles client request via Express.js. These are usually for custom endpoints or OAuth and app installation.
* We didn't hook this up to any database, so for out-of-the-box usage, you can hard-code the guild ID and other credentials in a .env file
*/
const expressAppController = async () => {
const app = express();

const port = process.env.PORT || 8080;

const installUrl = appOauthInstallUrl();

/**
* The ready event is vital, it means that only _after_ this will your bot start reacting to information
* received from Discord
*/
client.on('ready', async () => {
const applicationInfo = await client.fetchApplication();
// show application install link
app.get('/install', (_req, res) => {
// redirect to app install page
return res.redirect(installUrl);

// send the install link as a JSON response
//return res.status(200).json({ url: installUrl });
});

console.log(`${applicationInfo.name} has started`);
// add endpoint for OAuth installation with redirect URLs (https://discord.com/developers/docs/topics/oauth2#authorization-code-grant)
app.get('/oauth2', async ({ query }, res) => {
const { code } = query;
console.log(code);
});

client.on('message', async (message: Message) => {
const conextChannel = message.channel;
app.listen(port, () => console.log(`App listening at port ${port}`));
};

if (message.content.startsWith('ping')) {
conextChannel.send('pong');
//message.author.send('pong');
/**
* The main controller for Discord API requests. Everything that is done from Discord should be written here
*/
const discordAppController = async () => {
const clientApp = await initDiscordApp();

clientApp.on('ready', async () => {
if (clientApp.user) {
console.log(`${clientApp.user.tag} is ready!`);
} else {
console.log(`Failed to login as a user!`);
}
});

// Log our bot in using the token from https://discord.com/developers/applications
await client.login(token);
}

async function webhookIntegration(channelId: string, webhookToken: string) {
// Create a discord channel webhook client
const webhookClient = new Discord.WebhookClient(channelId, webhookToken);
// a ping-pong test
clientApp.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;

console.log('Discord webhook client is ready!');
if (interaction.commandName === 'ping') {
await interaction.reply('Pong!');
}
});

webhookClient.send('Discord webhook client is ready!');
}
await clientApp.login(DISCORD_APP_TOKEN);
};
Loading

0 comments on commit 9b000f2

Please sign in to comment.