Skip to content

Commit

Permalink
added readme and split the bots (#2)
Browse files Browse the repository at this point in the history
* added readme and split the bots

* fixed jest types and prettier config
  • Loading branch information
hoonsubin authored Mar 19, 2021
1 parent 8a93168 commit bd1f1e3
Show file tree
Hide file tree
Showing 7 changed files with 1,024 additions and 946 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
extends: [
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
'prettier', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
'plugin:prettier/recommended', // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
],
parserOptions: {
Expand Down
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,58 @@

## About

This is a Discord bot starter project made with Discord.js.
This is a Discord bot starter project made with [Discord.js](https://discord.js.org/#/docs/main/stable/general/welcome).
This starter is focused on developing a full distributable Discord server bot or a light-weight webhook integration bot.

## Usage

### Dot Env
### Setting Up

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>
CALLBACK_SERVER_ID=<monitor server>
```

Next, ensure that you have a usable Discord app on your developer portal.
If you do not have a Discord app, you can create one from here: <https://discord.com/developers/applications>.

For creating channel-specific webhook integrations, you only need the channel editing permissions to add integrations.

Unlike a full Discord app, webhooks are light and simple, but limited in it's features.
Discord webhooks do not require any bot credentials, but they are only limited to a specific channel and do not have access to advanced RPCs.
If you want to create a simple message subscription feature to a certain channel without any reactivity (e.g., a bot for sending Slack messages to Discord), webhooks will do the job.
If you need advanced features like reading server (guild) information and user permissions or react to certain events, you will need to set up a full Discord bot.

### Obtaining Credentials

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.

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

![webhook-integration](img/channel-integration.png)

Click on the 'Copy Webhook URL' button to request the webhook URL.
The webhook URL has the following format `https://discord.com/api/webhooks/<webhook-id>/<webhook-token>`.

For example:

```url
https://discord.com/api/webhooks/12345678910/fewgg-PFEfew_efgg2-fgWEGWG33g-fewgewg-sadfewgw
```

### Scripts

```bash

# install all the dependencies
yarn

Expand All @@ -32,3 +67,10 @@ yarn dev
# transpile the project into production-optimized javascript
yarn build
```

## Further Readings

For more information, please refer to the official Discord developer portal or the Discord.js documentation.

- <https://discord.com/developers/docs/intro>
- <https://discord.js.org/#/docs/main/master/general/welcome>
Binary file added img/bot-token.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/channel-integration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,26 @@
},
"license": "MIT",
"devDependencies": {
"@types/node": "^14.14.14",
"@types/jest": "^26.0.21",
"@types/node": "^14.14.35",
"@types/ws": "^7.4.0",
"@typescript-eslint/eslint-plugin": "^4.6.0",
"@typescript-eslint/parser": "^4.6.0",
"eslint": "^7.12.0",
"eslint-config-prettier": "^6.14.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.6.1",
"jest-config": "^26.6.1",
"nock": "^13.0.5",
"prettier": "^2.1.2",
"ts-jest": "^26.4.2",
"ts-node-dev": "^1.1.1",
"typescript": "^4.1.3"
"@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"
},
"dependencies": {
"discord.js": "^12.5.1",
"dotenv": "^8.2.0",
"node": "^15.4.0",
"node": "^15.10.0",
"ts-node": "^9.1.1"
}
}
23 changes: 18 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import Discord, { Message } from 'discord.js';

const TOKEN = process.env.DISCORD_TOKEN;
const CALLBACK_SERVER_ID = process.env.CALLBACK_SERVER_ID;
const WEBHOOK_CRED = { id: process.env.WEBHOOK_ID, token: process.env.WEBHOOK_TOKEN };

/**
* the main entry function for running the discord application
*/
export default async function main() {
if (!TOKEN || !CALLBACK_SERVER_ID) throw new Error('Please provide discord bot credentials');
// Create a discord channel webhook client
//const webhookClient = new Discord.WebhookClient(process.env.WEBHOOK_ID, process.env.WEBHOOK_TOKEN);
// 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);
}

async function discordBot(token: string) {
// Create an instance of a Discord client app
const client = new Discord.Client({ fetchAllMembers: true, disableMentions: 'all' });

Expand All @@ -34,5 +38,14 @@ export default async function main() {
});

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

async function webhookIntegration(channelId: string, webhookToken: string) {
// Create a discord channel webhook client
const webhookClient = new Discord.WebhookClient(channelId, webhookToken);

console.log('Discord webhook client is ready!');

webhookClient.send('Discord webhook client is ready!');
}
Loading

0 comments on commit bd1f1e3

Please sign in to comment.