From ce2942879c025a364c799cafbdf4641330df88be Mon Sep 17 00:00:00 2001 From: Isaac Date: Wed, 17 Feb 2021 13:24:33 +0000 Subject: [PATCH] Clean up --- package.json | 4 +-- pnpm-lock.yaml | 76 ++++++++++++++++++++++------------------ src/database/index.js | 11 +----- src/index.js | 31 +++++++++------- src/listeners/ready.js | 20 ++++++++++- src/modules/listeners.js | 3 +- src/modules/plugins.js | 2 +- src/updater.js | 35 ++++++++++++++++++ user/example.config.js | 2 ++ 9 files changed, 121 insertions(+), 63 deletions(-) create mode 100644 src/updater.js diff --git a/package.json b/package.json index 212319d..8a8c699 100644 --- a/package.json +++ b/package.json @@ -29,13 +29,13 @@ "funding": "https://github.com/eartharoid/DiscordTickets/?sponsor=1", "dependencies": { "@eartharoid/i18n": "^0.0.1", - "command-exists": "^1.2.9", + "boxen": "^5.0.0", "discord.js": "^12.5.1", "dotenv": "^8.2.0", - "keyv": "^4.0.3", "leeks.js": "^0.0.9", "leekslazylogger": "^2.0.7", "node-fetch": "^2.6.1", + "semver": "^7.3.4", "sequelize": "^6.5.0", "terminal-link": "^2.1.1" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6ca3165..eb71741 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,12 +1,12 @@ dependencies: '@eartharoid/i18n': 0.0.1 - command-exists: 1.2.9 + boxen: 5.0.0 discord.js: 12.5.1 dotenv: 8.2.0 - keyv: 4.0.3 leeks.js: 0.0.9 leekslazylogger: 2.0.7 node-fetch: 2.6.1 + semver: 7.3.4 sequelize: 6.5.0_fb66e8c649bde1be622cc06164c9e22d terminal-link: 2.1.1 devDependencies: @@ -242,7 +242,6 @@ packages: /ansi-align/3.0.0: dependencies: string-width: 3.1.0 - dev: true resolution: integrity: sha512-ZpClVKqXN3RGBmKibdfWzqCY4lnjEuoNzU5T0oEFpfd/z5qJHVarukridD4juLO2FXMiwUQxr9WqQtaYa8XRYw== /ansi-colors/4.1.1: @@ -267,13 +266,11 @@ packages: resolution: integrity: sha1-w7M6te42DYbg5ijwRorn7yfWVN8= /ansi-regex/4.1.0: - dev: true engines: node: '>=6' resolution: integrity: sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg== /ansi-regex/5.0.0: - dev: true engines: node: '>=8' resolution: @@ -289,7 +286,6 @@ packages: /ansi-styles/4.3.0: dependencies: color-convert: 2.0.1 - dev: true engines: node: '>=8' resolution: @@ -417,6 +413,21 @@ packages: node: '>=8' resolution: integrity: sha512-eB4uT9RGzg2odpER62bBwSLvUeGC+WbRjjyyFhGsKnc8wp/m0+hQsMUvUe3H2V0D5vw0nBdO1hCJoZo5mKeuIQ== + /boxen/5.0.0: + dependencies: + ansi-align: 3.0.0 + camelcase: 6.2.0 + chalk: 4.1.0 + cli-boxes: 2.2.1 + string-width: 4.2.0 + type-fest: 0.20.2 + widest-line: 3.1.0 + wrap-ansi: 7.0.0 + dev: false + engines: + node: '>=10' + resolution: + integrity: sha512-5bvsqw+hhgUi3oYGK0Vf4WpIkyemp60WBInn7+WNfoISzAqk/HX4L7WNROq38E6UR/y3YADpv6pEm4BfkeEAdA== /brace-expansion/1.1.11: dependencies: balanced-match: 1.0.0 @@ -474,6 +485,12 @@ packages: node: '>=6' resolution: integrity: sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg== + /camelcase/6.2.0: + dev: false + engines: + node: '>=10' + resolution: + integrity: sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg== /caseless/0.12.0: resolution: integrity: sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw= @@ -500,7 +517,6 @@ packages: dependencies: ansi-styles: 4.3.0 supports-color: 7.2.0 - dev: true engines: node: '>=10' resolution: @@ -531,7 +547,6 @@ packages: resolution: integrity: sha512-5tK7EtrZ0N+OLFMthtqOj4fI2Jeb88C4CAZPu25LDVUgXJ0A3Js4PMGqrn0JU1W0Mh1/Z8wZzYPxqUrXeBboCQ== /cli-boxes/2.2.1: - dev: true engines: node: '>=6' resolution: @@ -558,7 +573,6 @@ packages: /color-convert/2.0.1: dependencies: color-name: 1.1.4 - dev: true engines: node: '>=7.0.0' resolution: @@ -568,7 +582,6 @@ packages: resolution: integrity: sha1-p9BVi9icQveV3UIyj3QIMcpTvCU= /color-name/1.1.4: - dev: true resolution: integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== /combined-stream/1.0.8: @@ -578,10 +591,6 @@ packages: node: '>= 0.8' resolution: integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== - /command-exists/1.2.9: - dev: false - resolution: - integrity: sha512-LTQ/SGc+s0Xc0Fu5WaKnR0YiygZkm9eKFvyS+fRsU7/ZWFF8ykFM6Pc9aCVf1+xasOOZpO3BAVgVrKvsqKHV7w== /concat-map/0.0.1: resolution: integrity: sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= @@ -767,11 +776,9 @@ packages: resolution: integrity: sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ== /emoji-regex/7.0.3: - dev: true resolution: integrity: sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA== /emoji-regex/8.0.0: - dev: true resolution: integrity: sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A== /end-of-stream/1.4.4: @@ -1319,13 +1326,11 @@ packages: resolution: integrity: sha1-754xOG8DGn8NZDr4L95QxFfvAMs= /is-fullwidth-code-point/2.0.0: - dev: true engines: node: '>=4' resolution: integrity: sha1-o7MKXE8ZkYMWeqq5O+764937ZU8= /is-fullwidth-code-point/3.0.0: - dev: true engines: node: '>=8' resolution: @@ -1416,10 +1421,6 @@ packages: dev: true resolution: integrity: sha1-Wx85evx11ne96Lz8Dkfh+aPZqJg= - /json-buffer/3.0.1: - dev: false - resolution: - integrity: sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ== /json-schema-traverse/0.4.1: resolution: integrity: sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg== @@ -1468,12 +1469,6 @@ packages: dev: true resolution: integrity: sha512-9ykJ/46SN/9KPM/sichzQ7OvXyGDYKGTaDlKMGCAlg2UK8KRy4jb0d8sFc+0Tt0YYnThq8X2RZgCg74RPxgcVA== - /keyv/4.0.3: - dependencies: - json-buffer: 3.0.1 - dev: false - resolution: - integrity: sha512-zdGa2TOpSZPq5mU6iowDARnMBZgtCqJ11dJROFi6tg6kTn4nuUdU09lFyLFSaHrWqpIJ+EBq4E8/Dc0Vx5vLdA== /latest-version/5.1.0: dependencies: package-json: 6.5.0 @@ -2428,7 +2423,6 @@ packages: emoji-regex: 7.0.3 is-fullwidth-code-point: 2.0.0 strip-ansi: 5.2.0 - dev: true engines: node: '>=6' resolution: @@ -2438,7 +2432,6 @@ packages: emoji-regex: 8.0.0 is-fullwidth-code-point: 3.0.0 strip-ansi: 6.0.0 - dev: true engines: node: '>=8' resolution: @@ -2468,7 +2461,6 @@ packages: /strip-ansi/5.2.0: dependencies: ansi-regex: 4.1.0 - dev: true engines: node: '>=6' resolution: @@ -2476,7 +2468,6 @@ packages: /strip-ansi/6.0.0: dependencies: ansi-regex: 5.0.0 - dev: true engines: node: '>=8' resolution: @@ -2672,6 +2663,12 @@ packages: node: '>=8' resolution: integrity: sha512-OdjXJxnCN1AvyLSzeKIgXTXxV+99ZuXl3Hpo9XpJAv9MBcHrrJOQ5kV7ypXOuQie+AmWG25hLbiKdwYTifzcfQ== + /type-fest/0.20.2: + dev: false + engines: + node: '>=10' + resolution: + integrity: sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ== /type-fest/0.8.1: dev: true engines: @@ -2793,7 +2790,6 @@ packages: /widest-line/3.1.0: dependencies: string-width: 4.2.0 - dev: true engines: node: '>=8' resolution: @@ -2810,6 +2806,16 @@ packages: node: '>=0.10.0' resolution: integrity: sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ== + /wrap-ansi/7.0.0: + dependencies: + ansi-styles: 4.3.0 + string-width: 4.2.0 + strip-ansi: 6.0.0 + dev: false + engines: + node: '>=10' + resolution: + integrity: sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== /wrappy/1.0.2: resolution: integrity: sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8= @@ -2889,11 +2895,10 @@ packages: integrity: sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== specifiers: '@eartharoid/i18n': ^0.0.1 - command-exists: ^1.2.9 + boxen: ^5.0.0 discord.js: ^12.5.1 dotenv: ^8.2.0 eslint: ^7.20.0 - keyv: ^4.0.3 leeks.js: ^0.0.9 leekslazylogger: ^2.0.7 mariadb: ^2.5.2 @@ -2902,6 +2907,7 @@ specifiers: nodemon: ^2.0.7 pg: ^8.5.1 pg-hstore: ^2.3.3 + semver: ^7.3.4 sequelize: ^6.5.0 sqlite3: ^5.0.2 tedious: ^11.0.3 diff --git a/src/database/index.js b/src/database/index.js index 1699287..802f192 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -60,15 +60,6 @@ module.exports = async (log) => { return process.exit(); } - /* let models = {}; - let files = fs.readdirSync(path('src/database/models/')).filter(file => file.endsWith('.js')); - - for (let file of files) { - let table = require(`./models/${file}`); - let model = sequelize.define(table.name, table.model); - models[table.name] = model; - } */ - const Guild = sequelize.define('Guild', { id: { type: DataTypes.CHAR(18), @@ -123,5 +114,5 @@ module.exports = async (log) => { sequelize.sync(); - return sequelize.models; + return sequelize; }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index e56ebda..7023855 100644 --- a/src/index.js +++ b/src/index.js @@ -22,13 +22,17 @@ */ const node_version = Number(process.versions.node.split('.')[0]); -if (node_version < 14) return console.log(`Error: DiscordTickets does not work on Node v${node_version}. Please upgrade to v14 or above.`); -const { version } = require('../package.json'); +if (node_version < 14) + return console.log(`Error: DiscordTickets does not work on Node v${node_version}. Please upgrade to v14 or above.`); +const { version } = require('../package.json'); const fs = require('fs'); const { path } = require('./utils/fs'); -if (!fs.existsSync(path('./.env'))) return console.log('Please make a copy of \'example.env\' called \'.env\''); -if (!fs.existsSync(path('./user/config.js'))) return console.log('Please make a copy of \'user/example.config.js\' called \'user/config.js\''); + +if (!fs.existsSync(path('./.env'))) + return console.log('Please make a copy of \'example.env\' called \'.env\''); +if (!fs.existsSync(path('./user/config.js'))) + return console.log('Please make a copy of \'user/example.config.js\' called \'user/config.js\''); require('dotenv').config({ path: path('./.env') @@ -82,21 +86,22 @@ class Bot extends Client { }); Object.assign(this, { + commands: new CommandManager(this), config, - db: require('./database')(log), + db: require('./database')(log), // this.db.models.Ticket... log, i18n: new I18n(path('./src/locales'), 'en-GB') }); - (async () => { - this.listeners = require('./modules/listeners')(this); - this.commands = new CommandManager(this); - this.plugins = await require('./modules/plugins')(this); + this.setMaxListeners(this.config.max_listeners); - this.log.info('Connecting to Discord API'); + require('./updater')(this); + require('./modules/listeners')(this); + require('./modules/plugins')(this); - this.login(); - })(); + this.log.info('Connecting to Discord API...'); + + this.login(); } } @@ -105,7 +110,7 @@ new Bot(); process.on('unhandledRejection', error => { log.notice('PLEASE INCLUDE THIS INFORMATION:'); - log.warn(`Discord Tickets v${version}, Node ${process.versions.node} (${process.platform})`); + log.warn(`Discord Tickets v${version}, Node v${process.versions.node} on ${process.platform}`); log.warn('An error was not caught'); if (error instanceof Error) log.warn(`Uncaught ${error.name}: ${error}`); log.error(error); diff --git a/src/listeners/ready.js b/src/listeners/ready.js index 5fc0d3e..68f0a76 100644 --- a/src/listeners/ready.js +++ b/src/listeners/ready.js @@ -1,6 +1,24 @@ module.exports = { event: 'ready', + once: true, execute: (client) => { - client.log.success(`Connected to Discord as "${client.user.tag}"`); + const { log } = client; + + log.success(`Connected to Discord as "${client.user.tag}"`); + + /** + * OH NO, TELEMETRY!? + * Relax, it just counts how many people are using DiscordTickets. + */ + if (client.config.super_secret_setting) { + const fetch = require('node-fetch'); + fetch(`https://discordtickets-telemetry.eartharoid.repl.co/?id=${client.user.id}`, { + method: 'post', + }).catch(e => { + // fail quietly, it doesn't really matter if it didn't work + log.debug('Failed to post to discordtickets-telemetry'); + log.debug(e); + }); + } } }; \ No newline at end of file diff --git a/src/modules/listeners.js b/src/modules/listeners.js index d68fdb8..7b80cf8 100644 --- a/src/modules/listeners.js +++ b/src/modules/listeners.js @@ -7,6 +7,7 @@ module.exports = client => { for (const file of files) { const listener = require(`../listeners/${file}`); - client.on(listener.event, (...args) => listener.execute(client, ...args)); + let on = listener.once ? 'once' : 'on'; + client[on](listener.event, (...args) => listener.execute(client, ...args)); } }; \ No newline at end of file diff --git a/src/modules/plugins.js b/src/modules/plugins.js index d16164e..0dc6bf6 100644 --- a/src/modules/plugins.js +++ b/src/modules/plugins.js @@ -1,4 +1,4 @@ -module.exports = async client => { +module.exports = client => { client.config.plugins.forEach(plugin => { try { let package = require(`${plugin}/package.json`); diff --git a/src/updater.js b/src/updater.js new file mode 100644 index 0000000..bf45f00 --- /dev/null +++ b/src/updater.js @@ -0,0 +1,35 @@ +const fetch = require('node-fetch'); +const boxen = require('boxen'); +const link = require('terminal-link'); +const semver = require('semver'); + +let { version: current } = require('../package.json'); + +module.exports = async client => { + if (!client.config.update_notice) return; + const json = await (await fetch('https://api.github.com/repos/eartharoid/DiscordTickets/releases')).json(); + const update = json[0]; + + let latest = semver.coerce(update.tag_name); + + if (!semver.valid(latest)) return; + + if (semver.lt(current, latest)) { + client.log.notice(client.log.f(`There is an update available for Discord Tickets (${current} -> ${update.tag_name})`)); + + let notice = []; + notice.push(`&6You are currently using &c${current}&6, the latest is &a${update.tag_name}&6.`); + notice.push(`&6Download "&f${update.name}&6" from`); + notice.push(link('&6the GitHub releases page', 'https://github.com/eartharoid/DiscordTickets/releases/')); + + console.log( + boxen(client.log.f(notice.join('\n')), { + padding: 1, + margin: 1, + align: 'center', + borderColor: 'yellow', + borderStyle: 'round' + }) + ); + } +}; \ No newline at end of file diff --git a/user/example.config.js b/user/example.config.js index d23db19..fd04b1e 100644 --- a/user/example.config.js +++ b/user/example.config.js @@ -32,6 +32,7 @@ module.exports = { enabled: true, keep_for: 30 }, + max_listeners: 10, plugins: [ // 'dsctickets.plugin-name' 'discordtickets-portal' @@ -54,5 +55,6 @@ module.exports = { type: 'WATCHING' }, ], + super_secret_setting: true, update_notice: true, }; \ No newline at end of file