Skip to content

Commit

Permalink
Merge pull request #66 from moonstar-x/bugfix/level-repl
Browse files Browse the repository at this point in the history
Bugfix/level repl
  • Loading branch information
moonstar-x authored Jan 27, 2022
2 parents 634c9ae + 14f76d7 commit 29084ce
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 19 deletions.
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,28 @@ Your file should look like this.
"testing_guild_id": null,
"provider_type": "level",
"redis_url": "redis://ip:port",
"enable_tts_channels": false
"enable_tts_channels": false,
"enable_keep_alive": false
}
```

You may also configure these options with environment variables. The settings set with the environment variables will take higher precedence than the ones in the config JSON file.

This table contains all the configuration settings you may specify with both environment variables and the JSON config file.

| Environment Variable | JSON Property | Required | Type | Description |
|------------------------------------|------------------------------|-----------------------------|----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| DISCORD_TOKEN | `token` | Yes. | `string` | The bot's token. |
| DISCORD_PREFIX | `prefix` | No. (Defaults to: `$`) | `string` | **Deprecated**: The bot's prefix. A prefix is no longer necessary because this bot uses the all new interactions (slash commands). |
| DISCORD_OWNER_ID | `owner_id` | No. (Defaults to: `null`) | `string` or `null` | The ID of the bot's owner. |
| DISCORD_OWNER_REPORTING | `owner_reporting` | No. (Defaults to: `false`) | `boolean` | Whether the bot should send error reports to the owner via DM when a command errors. |
| DISCORD_PRESENCE_REFRESH_INTERVAL | `presence_refresh_interval` | No. (Defaults to: `900000`) | `number` or `null` | The time interval in milliseconds in which the bot updates its presence. If set to `null` the presence auto update will be disabled. |
| DISCORD_DEFAULT_DISCONNECT_TIMEOUT | `default_disconnect_timeout` | No. (Defaults to: `5`) | `number` or `null` | The time it takes the bot to leave a voice channel when inactive by default on all servers. This setting can be customised per server and this will be used if a server has not set their own value. |
| DISCORD_TESTING_GUILD_ID | `testing_guild_id` | No. (Defaults to: `null`) | `string` or `null` | The ID of the testing guild. You do not need to set this to anything if you're not planning on developing the bot. |
| DISCORD_PROVIDER_TYPE | `provider_type` | No. (Defaults to: `level`) | Can be: `level` or `redis` | The type of data provider to use. [Level](https://github.com/google/leveldb) is a file based key-value store whereas [Redis](https://redis.io/) is a cache service. If you plan on just hosting the bot for a small server you should choose `level`, if you plan on sharding the client `redis` can be a better choice. |
| DISCORD_REDIS_URL | `redis_url` | No. (Defaults to: `null`) | `string` or `null` | The URL of the redis service. This is only required if you have set the provider type to `redis`. |
| DISCORD_ENABLE_TTS_CHANNELS | `enable_tts_channels` | No. (Defaults to: `false`) | `boolean` | Whether to enable the message-only TTS for specific channels. With this setting, you can send TTS messages by just sending messages to a channel that you have enabled a provider for. You need the privileged message intent (accessible in the `Bot` page of your bot's application page) for this feature to work properly. |
| Environment Variable | JSON Property | Required | Type | Description |
|------------------------------------|------------------------------|-----------------------------|----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| DISCORD_TOKEN | `token` | Yes. | `string` | The bot's token. |
| DISCORD_PREFIX | `prefix` | No. (Defaults to: `$`) | `string` | **Deprecated**: The bot's prefix. A prefix is no longer necessary because this bot uses the all new interactions (slash commands). |
| DISCORD_OWNER_ID | `owner_id` | No. (Defaults to: `null`) | `string` or `null` | The ID of the bot's owner. |
| DISCORD_OWNER_REPORTING | `owner_reporting` | No. (Defaults to: `false`) | `boolean` | Whether the bot should send error reports to the owner via DM when a command errors. |
| DISCORD_PRESENCE_REFRESH_INTERVAL | `presence_refresh_interval` | No. (Defaults to: `900000`) | `number` or `null` | The time interval in milliseconds in which the bot updates its presence. If set to `null` the presence auto update will be disabled. |
| DISCORD_DEFAULT_DISCONNECT_TIMEOUT | `default_disconnect_timeout` | No. (Defaults to: `5`) | `number` or `null` | The time it takes the bot to leave a voice channel when inactive by default on all servers. This setting can be customised per server and this will be used if a server has not set their own value. |
| DISCORD_TESTING_GUILD_ID | `testing_guild_id` | No. (Defaults to: `null`) | `string` or `null` | The ID of the testing guild. You do not need to set this to anything if you're not planning on developing the bot. |
| DISCORD_PROVIDER_TYPE | `provider_type` | No. (Defaults to: `level`) | Can be: `level` or `redis` | The type of data provider to use. [Level](https://github.com/google/leveldb) is a file based key-value store whereas [Redis](https://redis.io/) is a cache service. If you plan on just hosting the bot for a small server you should choose `level`, if you plan on sharding the client `redis` can be a better choice. |
| DISCORD_REDIS_URL | `redis_url` | No. (Defaults to: `null`) | `string` or `null` | The URL of the redis service. This is only required if you have set the provider type to `redis`. |
| DISCORD_ENABLE_TTS_CHANNELS | `enable_tts_channels` | No. (Defaults to: `false`) | `boolean` | Whether to enable the message-only TTS for specific channels. With this setting, you can send TTS messages by just sending messages to a channel that you have enabled a provider for. You need the privileged message intent (accessible in the `Bot` page of your bot's application page) for this feature to work properly. |
| DISCORD_ENABLE_KEEP_ALIVE | `enable_keep_alive` | No. (Defaults to: `false`) | `boolean` | Whether an HTTP server should be started. You should only use this option if you're planning to host the bot on something like Repl.it or Heroku. Additionally, you should use something like UptimeRobot to poll the associated address to keep the bot alive. If you need to specify the port to use you should set the `PORT` environment variable (Heroku uses this by default). |

> If you set `enable_tts_channels` to `true`, you must enable the message content privileged intent in your bot's [application page](https://discord.com/developers/applications/).
Expand Down Expand Up @@ -180,6 +182,8 @@ npm run deploy
npm start
```

Make sure to have `enable_keep_alive` set to `true` and use UptimeRobot to poll the address that shows up for your repl to make sure it stays on 24/7.

## Deploying to Heroku

To deploy to Heroku, you can click on the image below and login to your account.
Expand Down
3 changes: 2 additions & 1 deletion config/settings.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"testing_guild_id": null,
"provider_type": "level",
"redis_url": "redis://ip:port",
"enable_tts_channels": false
"enable_tts_channels": false,
"enable_keep_alive": false
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discord-tts-bot",
"version": "4.0.4",
"version": "4.0.5",
"description": "A simple Discord TTS Bot that uses the Google Translate TTS API.",
"main": "./src/app.js",
"scripts": {
Expand Down
11 changes: 9 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const RedisDataProvider = require('@greencoast/discord.js-extended/dist/provider
const LevelDataProvider = require('@greencoast/discord.js-extended/dist/providers/LevelDataProvider').default;
const TTSClient = require('./classes/extensions/TTSClient');
const { locales } = require('./locales');
const { keepAlive } = require('./utils/keep-alive');
const { DISCONNECT_TIMEOUT } = require('./common/constants');
const pkg = require('../package.json');

Expand All @@ -21,7 +22,8 @@ const config = new ConfigProvider({
TESTING_GUILD_ID: null,
PROVIDER_TYPE: 'level',
REDIS_URL: null,
ENABLE_TTS_CHANNELS: false
ENABLE_TTS_CHANNELS: false,
ENABLE_KEEP_ALIVE: false
},
types: {
TOKEN: 'string',
Expand All @@ -33,7 +35,8 @@ const config = new ConfigProvider({
TESTING_GUILD_ID: ['string', 'null'],
PROVIDER_TYPE: 'string',
REDIS_URL: ['string', 'null'],
ENABLE_TTS_CHANNELS: 'boolean'
ENABLE_TTS_CHANNELS: 'boolean',
ENABLE_KEEP_ALIVE: 'boolean'
},
customValidators: {
PROVIDER_TYPE: (value) => {
Expand Down Expand Up @@ -120,6 +123,10 @@ client.once('ready', async() => {
client.ttsChannelHandler.initialize();
}

if (config.get('ENABLE_KEEP_ALIVE')) {
keepAlive({ port: process.env.PORT || 3000 });
}

client.on('guildCreate', async(guild) => {
await client.initializeDependenciesForGuild(guild);
});
Expand Down
7 changes: 6 additions & 1 deletion src/classes/tts/CachedTTSSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ class CachedTTSSettings {
return;
}

await this.delete(channel);
try {
await this.delete(channel);
} catch (error) {
this.client.emit('warn', `Could not delete settings for channel ${channel.id}. Most likely no settings were saved for this channel in the first place. If this is the case then it is safe to ignore this warning.`);
this.client.emit('error', error);
}
});
}

Expand Down
20 changes: 20 additions & 0 deletions src/utils/keep-alive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const http = require('http');
const logger = require('@greencoast/logger');

const keepAlive = ({ text, port }) => {
const portToUse = port || 3000;

const server = http.createServer((_, res) => {
res.writeHead(200);
res.end(text || "I'm alive.");
});
server.listen(portToUse);

logger.info(`Started an HTTP server on port ${portToUse}. You should use a service like UptimeRobot to poll the web address associated to this service to keep the bot alive.`);

return server;
};

module.exports = {
keepAlive
};

0 comments on commit 29084ce

Please sign in to comment.