Skip to content

Commit b19ca69

Browse files
eslachancegitbook-bot
authored andcommitted
GitBook: [master] one page modified
1 parent df79787 commit b19ca69

File tree

1 file changed

+1
-23
lines changed

1 file changed

+1
-23
lines changed

first-bot/command-with-arguments.md

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -165,29 +165,7 @@ if(command === "say"){
165165
If you're thinking, "What if I have more than one argument with spaces?", yes that's a tougher problem. Ideally, if you need more than one argument with spaces in it, do not use spaces to split the arguments. For example, `!newtag First Var Second Var Third Var` won't work. But `!newtag First Var;Second Var;Third Var;` could work by removing the command, splitting by `;` then splitting by space. Not for the faint of heart!
166166
{% endhint %}
167167

168-
### Let's be fancy with ES6 again
169-
170-
Destructuring has a `...rest` feature that lets you take "the rest of the array" and put it in a single variable. To demonstrate this, let me show you part of a code I use in a "save message" command. Basically, I store a message to a database, with a name. I call this command using: `!quote <channelid> <messageID> quotename note`, where `quotename` is a single word and `note` may be multiple words. The _actual_ command code is unimportant. However, the way I process these arguments, is useful:
171-
172-
```javascript
173-
if(command === "quote") {
174-
const [channelid, messageid, quotename, ...note] = args.splice(1);
175-
// I also support "here" as a channelID using this:
176-
const channel = channelid == "here" ? message.channel : client.channels.get(channelid);
177-
// I do the same with message ID, which can be "last":
178-
const message = messageid === "last" ? msg.channel.messages.last(2)[0] : await channel.messages.get(messageid);
179-
// pretend for a second this is the rest of the function:
180-
insertInDB(quotename, channel.id, message.id, note.join(" "));
181-
}
182-
```
183-
184-
A few notes on this code, because I will admit there's some new concepts in it you might not know:
185-
186-
* `...note` is "the rest of the array arguments", so it would be `["this", "is", "a", "note"]`, that's why we .join\(" "\) when we use it.
187-
* `const myVar = condition ? codeWhenConditionTrue : codeWhenConditionFalse;` is called the "ternary operator" in javascript and makes some conditions much more simpler.
188-
* `<Collection>.last(2)[0]` is only available on discord.js\#master \(the 12.0 beta version\) which is not out at the time of writing. The alternative is complex, and beyond what this page tries to teach, so just know it gets "the second to last message".
189-
190168
## Going one step further
191169

192-
Now, there's most definitely always room for some optimization, and better code. At this point, "parsing arguments" becomes something you might realize is necessary for _all_ of your commands, and writing ".startsWith\(\)" for every command is dull and boring. So, as your next step, consider looking at making [A Basic Command Handler](a-basic-command-handler.md). This **greatly** simplifies the creation of new commands.
170+
Now, there's most definitely always room for some optimization, and better code. At this point, "parsing arguments" becomes something you might realize is necessary for _all_ of your commands, and writing "\(command === 'thing'\)" for every command is dull and boring. So, as your next step, consider looking at making [A Basic Command Handler](a-basic-command-handler.md). This **greatly** simplifies the creation of new commands.
193171

0 commit comments

Comments
 (0)