@discordia/framework
Advanced tools
Comparing version 0.2.3 to 1.0.0
@@ -6,2 +6,18 @@ # Change Log | ||
# [1.0.0](https://github.com/mfasman95/discordia/compare/v0.2.3...v1.0.0) (2020-05-08) | ||
### Code Refactoring | ||
* **multiple-packages:** handle multi-word accessors ([#5](https://github.com/mfasman95/discordia/issues/5)) ([02f5133](https://github.com/mfasman95/discordia/commit/02f51333cfb137006f1a15719a69e250f5ddb3e4)) | ||
### BREAKING CHANGES | ||
* **multiple-packages:** Many changes to the ways parameters are passed between action checking and handlers. | ||
## [0.2.2](https://github.com/mfasman95/discordia/compare/v0.2.1...v0.2.2) (2020-05-06) | ||
@@ -8,0 +24,0 @@ |
@@ -6,3 +6,2 @@ const { isString, isArray, isFunction, isBoolean, isNull } = require('lodash'); | ||
const DiscordiaAction = require('@discordia/action'); | ||
const { parseMessageContent } = require('./utils'); | ||
const { | ||
@@ -65,3 +64,7 @@ DEFAULT_NAME, | ||
// Set the login function here to avoid storing `token` | ||
this.login = () => this.client.login(token); | ||
this.login = () => { | ||
this.client.login(token); | ||
this.botName = this.name || `@${this.client.user.username}`; | ||
this.startingIndex = this.botName.length + 1; | ||
}; | ||
this.validateToken(token); | ||
@@ -196,3 +199,3 @@ | ||
* name should be case sensitive | ||
* @param {string} botName The portion of the user message that would be the botName | ||
* @param {string} msgContent The full content of the message that just came in | ||
* @returns {boolean} whether or not this bot was being addressed | ||
@@ -202,16 +205,11 @@ * @private | ||
*/ | ||
shouldHandleMessage(botName) { | ||
let defaultNameMatch = false; | ||
let caseInsensitiveNameMatch = false; | ||
let caseSensitiveNameMatch = false; | ||
if (isString(this.name)) { | ||
caseInsensitiveNameMatch = botName.toLowerCase() === this.name.toLowerCase() && !this.caseSensitiveName; | ||
caseSensitiveNameMatch = botName === this.name && this.caseSensitiveName; | ||
} else { | ||
defaultNameMatch = `<@!${this.client.user.id}>` === botName; | ||
shouldHandleMessage(msgContent) { | ||
if (isString(this.name) && this.caseSensitiveName) { | ||
return msgContent.indexOf(this.name) === 0; | ||
} | ||
if (isString(this.name) && !this.caseSensitiveName) { | ||
return msgContent.toLowerCase().indexOf(this.name.toLowerCase()) === 0; | ||
} | ||
this.nameToSend = defaultNameMatch ? `@${this.client.user.username}` : this.name; | ||
return defaultNameMatch || caseInsensitiveNameMatch || caseSensitiveNameMatch; | ||
return msgContent.indexOf(`<@!${this.client.user.id}>`) === 0; | ||
} | ||
@@ -224,9 +222,6 @@ | ||
* String - Replies to the user with this.missingCommandMessage | ||
* Function - Passes userAction, userArgs, the | ||
* Function - Passes msgContent, the | ||
* <a href="https://discord.js.org/#/docs/main/stable/class/Message">Discord.js Message object</a>, | ||
* and the <a href="https://discord.js.org/#/docs/main/stable/class/Client">Discord.js Client object</a>, | ||
* as parameters. | ||
* | ||
* @param {string} userAction The action taken by the user | ||
* @param {Array<string>} userArgs Everything in the message after the userAction as an Array | ||
* and this class instance. | ||
* @param {string} msgContent The full content of the message that just came in | ||
* @param {any} msg The <a href="https://discord.js.org/#/docs/main/stable/class/Message">discord.js | ||
@@ -237,3 +232,3 @@ * message object</a> that triggered this action | ||
*/ | ||
handleMissingCommand(userAction, userArgs, msg) { | ||
handleMissingCommand(msgContent, msg) { | ||
switch (this.missingCommandMessageType) { | ||
@@ -248,3 +243,3 @@ // If the missing command message is a string | ||
case ENUM_MISSING_COMMAND_MESSAGE_TYPE.FUNCTION: { | ||
const result = this.missingCommandMessage(userAction, userArgs, msg, this.client); | ||
const result = this.missingCommandMessage(msgContent, msg, this); | ||
// If the result of the missingCommandMessage function is a string | ||
@@ -279,7 +274,5 @@ if (isString(result)) { | ||
handleMessage(msg) { | ||
const [botName, userAction, userArgs] = parseMessageContent(msg); | ||
if (this.shouldHandleMessage(msg.content)) { | ||
this.debug(msg.content); | ||
if (this.shouldHandleMessage(botName)) { | ||
this.debug(botName, userAction, userArgs); | ||
let actionHandled = false; | ||
@@ -289,3 +282,3 @@ | ||
// If any of the actions are handled, make sure to track it | ||
if (action.checkAccessor(userAction, msg, userArgs, this)) { | ||
if (action.checkAccessor(msg.content, msg, this)) { | ||
actionHandled = true; | ||
@@ -296,3 +289,3 @@ } | ||
if (!actionHandled) { | ||
this.handleMissingCommand(userAction, userArgs, msg); | ||
this.handleMissingCommand(msg.content, msg); | ||
} | ||
@@ -299,0 +292,0 @@ |
const DiscordiaAction = require('@discordia/action'); | ||
const discord = require('discord.js'); | ||
const DiscordiaFramework = require('./index'); | ||
const { parseMessageContent } = require('./utils'); | ||
const { | ||
@@ -145,3 +144,2 @@ DEFAULT_NAME, | ||
const mockUserId = 123; | ||
const mockUsername = 123; | ||
const defaultNamePattern = `<@!${mockUserId}>`; | ||
@@ -181,15 +179,2 @@ const mockName = 'MOCK_NAME'; | ||
}); | ||
test('should set this.nameToSend based on framework.client.user.id if options are default', () => { | ||
framework = new DiscordiaFramework(mockToken, mockActions); | ||
framework.client.user = { id: mockUserId, username: mockUsername }; | ||
framework.shouldHandleMessage(defaultNamePattern); | ||
expect(framework.nameToSend).toEqual(`@${mockUsername}`); | ||
}); | ||
test('should set this.nameToSend based on options.name if options.name is set', () => { | ||
framework = new DiscordiaFramework(mockToken, mockActions, { name: mockName }); | ||
framework.shouldHandleMessage(defaultNamePattern); | ||
expect(framework.nameToSend).toEqual(mockName); | ||
}); | ||
}); | ||
@@ -208,3 +193,4 @@ | ||
framework = new DiscordiaFramework(mockToken, mockActions, { missingCommandMessage: 'MOCK' }); | ||
framework.handleMissingCommand(mockUserAction, mockUserArgs, mockMsg); | ||
const msgContent = `${framework.name} ${mockUserAction} ${mockUserArgs.join(' ')}`; | ||
framework.handleMissingCommand(msgContent, mockMsg); | ||
expect(mockMsg.reply).toHaveBeenCalledWith(framework.missingCommandMessage); | ||
@@ -215,9 +201,5 @@ }); | ||
framework = new DiscordiaFramework(mockToken, mockActions, { missingCommandMessage: jest.fn() }); | ||
framework.handleMissingCommand(mockUserAction, mockUserArgs, mockMsg); | ||
expect(framework.missingCommandMessage).toHaveBeenCalledWith( | ||
mockUserAction, | ||
mockUserArgs, | ||
mockMsg, | ||
framework.client | ||
); | ||
const msgContent = `${framework.name} ${mockUserAction} ${mockUserArgs.join(' ')}`; | ||
framework.handleMissingCommand(msgContent, mockMsg); | ||
expect(framework.missingCommandMessage).toHaveBeenCalledWith(msgContent, mockMsg, framework); | ||
expect(mockMsg.reply).not.toHaveBeenCalled(); | ||
@@ -230,9 +212,5 @@ }); | ||
}); | ||
framework.handleMissingCommand(mockUserAction, mockUserArgs, mockMsg); | ||
expect(framework.missingCommandMessage).toHaveBeenCalledWith( | ||
mockUserAction, | ||
mockUserArgs, | ||
mockMsg, | ||
framework.client | ||
); | ||
const msgContent = `${framework.name} ${mockUserAction} ${mockUserArgs.join(' ')}`; | ||
framework.handleMissingCommand(msgContent, mockMsg); | ||
expect(framework.missingCommandMessage).toHaveBeenCalledWith(msgContent, mockMsg, framework); | ||
expect(mockMsg.reply).toHaveBeenCalledWith(framework.missingCommandMessage()); | ||
@@ -243,3 +221,5 @@ }); | ||
framework = new DiscordiaFramework(mockToken, mockActions, { missingCommandMessage: null }); | ||
expect(() => framework.handleMissingCommand(mockUserAction, mockUserArgs, mockMsg)).not.toThrow(); | ||
expect(() => | ||
framework.handleMissingCommand(`${framework.name} ${mockUserAction} ${mockUserArgs.join(' ')}`, mockMsg) | ||
).not.toThrow(); | ||
}); | ||
@@ -252,2 +232,3 @@ }); | ||
framework = new DiscordiaFramework(mockToken, mockActions, { name: mockName }); | ||
framework.startingIndex = mockName.length + 1; | ||
}); | ||
@@ -258,4 +239,5 @@ | ||
framework.shouldHandleMessage = jest.fn(); | ||
framework.handleMessage({ content: `${mockName} some message` }); | ||
expect(framework.shouldHandleMessage).toHaveBeenCalledWith(mockName); | ||
const mockMsg = { content: `${mockName} some message` }; | ||
framework.handleMessage(mockMsg); | ||
expect(framework.shouldHandleMessage).toHaveBeenCalledWith(mockMsg.content); | ||
framework.shouldHandleMessage = originalShouldHandleMessage; | ||
@@ -265,8 +247,7 @@ }); | ||
test('should call framework.handleMissingCommand if userAction would not trigger one of the provided actions', () => { | ||
const msg = { content: `${mockName} some message` }; | ||
const mockMsg = { content: `${mockName} some message` }; | ||
const originalHandleMissingCommand = framework.handleMissingCommand; | ||
framework.handleMissingCommand = jest.fn(); | ||
framework.handleMessage(msg); | ||
const [, userAction, userArgs] = parseMessageContent(msg); | ||
expect(framework.handleMissingCommand).toHaveBeenCalledWith(userAction, userArgs, msg); | ||
framework.handleMessage(mockMsg); | ||
expect(framework.handleMissingCommand).toHaveBeenCalledWith(mockMsg.content, mockMsg); | ||
framework.handleMissingCommand = originalHandleMissingCommand; | ||
@@ -310,2 +291,5 @@ }); | ||
const mockName = 'MOCK_NAME'; | ||
const mockUserId = 123; | ||
const mockUsername = 123; | ||
describe('start', () => { | ||
@@ -316,2 +300,3 @@ beforeEach(() => { | ||
framework.client.login = jest.fn(); | ||
framework.client.user = { id: mockUserId, username: mockUsername }; | ||
framework.start(); | ||
@@ -331,3 +316,16 @@ }); | ||
}); | ||
test('should set this.botName based on framework.client.user.id if options are default', () => { | ||
expect(framework.botName).toEqual(`@${mockUsername}`); | ||
}); | ||
}); | ||
test('start should set this.botName based on this.name if this.name is set', () => { | ||
framework = new DiscordiaFramework(mockToken, mockActions); | ||
framework.client.on = jest.fn(); | ||
framework.client.login = jest.fn(); | ||
framework.name = mockName; | ||
framework.start(); | ||
expect(framework.botName).toEqual(mockName); | ||
}); | ||
}); |
{ | ||
"name": "@discordia/framework", | ||
"version": "0.2.3", | ||
"version": "1.0.0", | ||
"description": "The main discordia framework", | ||
@@ -30,5 +30,5 @@ "keywords": [ | ||
"dependencies": { | ||
"@discordia/action": "^0.2.3", | ||
"@discordia/debug": "^0.2.3", | ||
"@discordia/default-help": "^0.2.3", | ||
"@discordia/action": "^1.0.0", | ||
"@discordia/debug": "^1.0.0", | ||
"@discordia/default-help": "^1.0.0", | ||
"chalk": "^4.0.0", | ||
@@ -46,3 +46,3 @@ "discord.js": "^12.2.0", | ||
}, | ||
"gitHead": "93616a4c2b1c2898dd466e9b7abd0b5a790a3faa" | ||
"gitHead": "9d996209f74b688ed6c2650d612361bdfcac508f" | ||
} |
- [Installation](#installation) | ||
- [Basic Example](#basic-example) | ||
- [Kitchen Sink Example](#kitchen-sink-example) | ||
- [Usage](#usage) | ||
@@ -21,3 +21,3 @@ - [Token](#token) | ||
# Basic Example | ||
# Kitchen Sink Example | ||
```js | ||
@@ -48,3 +48,3 @@ const DiscordiaFramework = require('@discordia/framework'); | ||
>⚠️This token is a sensitive piece of information - do not commit it with your source code and regenerate it if it ever becomes compromised⚠️ | ||
>⚠️This token is a sensitive piece of information. DO NOT commit it with your source code. Regenerate it if it ever becomes compromised.⚠️ | ||
@@ -54,3 +54,3 @@ ## Actions | ||
An array of [DiscordiaAction](api#DiscordiaAction) objects. See [our guide](create_an_action) on how to use the [@discordia/action](https://github.com/mfasman95/discordia/tree/master/packages/action) module to create your own actions and/or install them from third party modules. | ||
An array of [DiscordiaAction](api#DiscordiaAction) objects. See [our guide](gs_write_an_action) on how to use the [@discordia/action](https://github.com/mfasman95/discordia/tree/master/packages/action) module to create your own actions. You can also install them from third party modules. | ||
@@ -81,4 +81,2 @@ ## Options | ||
See [DiscordiaFramework.handleMissingCommand](api#DiscordiaAction.handleMissingCommand) for more details. | ||
The message to send if a server member tries to ask the bot to do an action it does not know how to handle. You can either customize the `string` or provide a `function` to give a more involved response. | ||
@@ -89,7 +87,5 @@ | ||
See [DiscordiaFramework.handleHelp](api#DiscordiaAction.handleHelp) for more details. | ||
The message to send if a server member tries to ask the bot for help. By default it will use [@discordia/default-help](https://github.com/mfasman95/discordia/tree/master/packages/default-help) to read your actions and send a response based on their [accessors](action#accessor) and [descriptions](action#description). You can provide `null` to disable the help message or provide a custom [DiscordiaAction](api#DiscordiaAction) to control what the message responds to and what it says. | ||
The message to send if a server member tries to ask the bot for help. By default it will use [@discordia/default-help](https://github.com/mfasman95/discordia/tree/master/packages/default-help) to read your actions and send a response based on their [accessors](create_an_action#accessor) and [descriptions](create_an_action#description). You can provide a static `string` as a response or provide a custom `function` to give a more involved response. | ||
# Testing | ||
🚧 Coming Soon! 🚧 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
36803
7
579
86
+ Added@discordia/action@1.5.0(transitive)
+ Added@discordia/debug@1.4.1(transitive)
+ Added@discordia/default-help@1.5.0(transitive)
- Removed@discordia/action@0.2.3(transitive)
- Removed@discordia/debug@0.2.3(transitive)
- Removed@discordia/default-help@0.2.3(transitive)
Updated@discordia/action@^1.0.0
Updated@discordia/debug@^1.0.0