Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#18, #8) reformat in ESM style, dedupe code #19

Merged
merged 12 commits into from
May 3, 2022

Conversation

davidzwa
Copy link
Collaborator

@davidzwa davidzwa commented May 2, 2022

This reworks the bot into an ESM module instead of CJS. This was so rigorous that the rest of the code got a brush over it and tackled #18.

The new package.json now looks like:

  "scripts": {
    "start": "node src/index.js",
    "dev": "nodemon src/index.js --mode=dev",
    "commands": "node src/index.js --mode=global",
    "commands-guild": "node src/index.js --mode=guild"
  },

I've installed yargs as a CLI parser, to be made stricter in future.

Description

By converting to ESM, the bot's code is slightly more readable and modern.

  "type": "module",

Related Issues

Fixes #8 and Fixes #18

Motivation and Context

We all love clean and reusable code ready for testing.

How Has This Been Tested?

  1. Mode dev (dev script): I've ran it till the TOKEN is rejected. This means the startup logic has run at least once and the database connection is established.
  2. Mode undefined (start script): runs same as 1)
  3. Mode global (commands script) runs with TOKEN rejection
  4. Mode purgeCommands (commands:purge script) runs with GUILD_ID missing rejection
  5. Mode guild (commands:guild script) runs with GUILD_ID missing rejection

Startup log

[nodemon] restarting due to changes...
[nodemon] starting `node src/index.js --mode=dev`
Starting in mode: dev
Dynamically importing file from ./commands/admin/welcome.js
Dynamically importing file from ./commands/general/ping.js
DiscordAPIError[0]: 401: Unauthorized
    at SequentialHandler.runRequest (file:///C:/Users/david/Projects/NickNelson/node_modules/@discordjs/rest/dist/index.mjs:669:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (file:///C:/Users/david/Projects/NickNelson/node_modules/@discordjs/rest/dist/index.mjs:472:14)
    at async deployCommands (file:///C:/Users/david/Projects/NickNelson/src/nick-bot-commands.js:39:9)
    at async file:///C:/Users/david/Projects/NickNelson/src/index.js:32:3 {
    at REST.put (file:///C:/Users/david/Projects/NickNelson/node_modules/@discordjs/rest/dist/index.mjs:891:17)
    at deployCommands (file:///C:/Users/david/Projects/NickNelson/src/nick-bot-commands.js:39:26)
    at async file:///C:/Users/david/Projects/NickNelson/src/index.js:32:3
C:\Users\david\Projects\NickNelson\node_modules\discord.js\src\client\Client.js:237
    if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');
                                                   ^

Error [TOKEN_INVALID]: An invalid token was provided.
    at Client.login (C:\Users\david\Projects\NickNelson\node_modules\discord.js\src\client\Client.js:237:52)
    at initBot (file:///C:/Users/david/Projects/NickNelson/src/nick-bot.js:89:18)
    at async file:///C:/Users/david/Projects/NickNelson/src/index.js:33:3 {
  [Symbol(code)]: 'TOKEN_INVALID'

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Copy link
Owner

@mkevenaar mkevenaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @davidzwa thank you for your work!

There are some things that I think that need to be changed. Also shouldn't there be changes to the files in the database folder?

Copy link
Owner

@mkevenaar mkevenaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mkevenaar mkevenaar requested a review from Lucxjo May 2, 2022 21:14
@mkevenaar
Copy link
Owner

@Lucxjo please review

Copy link
Collaborator

@Lucxjo Lucxjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the bit of copy/paste, I'm at work at the moment and just quickly logged in. PR looks good otherwise, just could you clear some bits up?

Thanks

Copy link
Collaborator

@Lucxjo Lucxjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR approved

@mkevenaar mkevenaar merged commit 2c0cb5e into mkevenaar:develop May 3, 2022
@mkevenaar
Copy link
Owner

@davidzwa your changes have been merged, thank you for your contribution!

@mkevenaar
Copy link
Owner

@all-contributors please add @davidzwa for bug

@allcontributors
Copy link
Contributor

@mkevenaar

I've put up a pull request to add @davidzwa! 🎉

@davidzwa davidzwa deleted the feature/8-esm-conversion branch May 3, 2022 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deduplicate code in index, guild-commands and commands entrypoints Convert bot to ESM
3 participants