1 | # Recipes
|
2 |
|
3 | > A collection of short examples using Miniplug to accomplish common tasks.
|
4 |
|
5 | - [Self-deleting messages](#self-deleting-messages)
|
6 | - [Lockskip](#lockskip)
|
7 | - [Async Functions](#async-functions)
|
8 | - [Extending Objects](#extending-objects)
|
9 |
|
10 | ## Self-deleting messages
|
11 |
|
12 | Moderation bots often delete their own messages after a while to avoid
|
13 | cluttering everybody's chat.
|
14 |
|
15 | [`mp.chat`](./API.md#mp-chat) returns a Promise for the chat message. Using the
|
16 | [`delay`](https://npmjs.com/package/delay) module from npm, we can call the
|
17 | [`delete`](./API.md#message-delete) method on the message after some time:
|
18 |
|
19 | ```js
|
20 | const delay = require('delay')
|
21 |
|
22 | mp.chat('Meh limit: 5')
|
23 | .then(delay(5000)) // Wait 5 seconds
|
24 | .then((message) => message.delete())
|
25 | ```
|
26 |
|
27 | ## Lockskip
|
28 |
|
29 | A lockskip skips the current DJ, but puts them back in front of the waitlist
|
30 | so they can play again soon.
|
31 |
|
32 | ```js
|
33 | const dj = mp.dj()
|
34 | const entry = mp.historyEntry()
|
35 |
|
36 | // Skip the DJ, then move them back to spot #2 on the waitlist.
|
37 | const lockskipPosition = 2
|
38 | dj.skip(entry.id).then(() => {
|
39 | return dj.move(lockskipPosition)
|
40 | })
|
41 | ```
|
42 |
|
43 | ## Async Functions
|
44 |
|
45 | Async functions are a new JavaScript feature that aims to make Promise-based
|
46 | code a lot easier to read. The code in the [Lockskip](#lockskip) recipe looks
|
47 | like this with async functions:
|
48 |
|
49 | ```js
|
50 | async function lockskip () {
|
51 | // Skip the DJ, then move them back to spot #2 on the waitlist.
|
52 | const lockskipPosition = 2
|
53 | await dj.skip(entry.id)
|
54 | await dj.move(lockskipPosition)
|
55 | }
|
56 | ```
|
57 |
|
58 | Async functions are available by default starting in Node.js v7.6. If you're
|
59 | using miniplug with an earlier Node.js version, you can use the [async-to-gen][]
|
60 | module like below:
|
61 |
|
62 | ```js
|
63 | require('async-to-gen/register')
|
64 | require('./bot')
|
65 | ```
|
66 |
|
67 | This will compile async functions in your code to more widely compatible
|
68 | JavaScript before running it.
|
69 |
|
70 | ## Extending Objects
|
71 |
|
72 | Miniplug uses `wrapObject` methods to instantiate objects of various types.
|
73 | Extending these methods can be useful if you are writing a custom plugin. A good
|
74 | way to extend wrapper methods is to use function composition, using the
|
75 | [compose-function][] module for example.
|
76 |
|
77 | ```js
|
78 | import compose from 'compose-function'
|
79 |
|
80 | function afkPlugin () {
|
81 | return (mp) => {
|
82 | function decorateUser (user) {
|
83 | // Make sure to return the modified user object!
|
84 | return Object.assign(user, {
|
85 | // Add a `removeAfk` method to user objects, that will remove the user
|
86 | // from the waitlist and tell them why they've been removed.
|
87 | removeAfk: () =>
|
88 | user.remove().then(() => {
|
89 | return user.send(
|
90 | 'You have been AFK for too long. ' +
|
91 | 'You have been removed from the waitlist.'
|
92 | )
|
93 | })
|
94 | })
|
95 | }
|
96 |
|
97 | // mp.wrapUser will now first call the old method, and pass the result to
|
98 | // `decorateUser`.
|
99 | mp.wrapUser = compose(decorateUser, mp.wrapUser)
|
100 |
|
101 | // So now you can do:
|
102 | mp.me().removeAfk()
|
103 | }
|
104 | }
|
105 | ```
|
106 |
|
107 | The wrapper methods are:
|
108 |
|
109 | - `wrapMessage`, for [ChatMessage](./API.md#class-chatmessage)s
|
110 | - `wrapHistoryEntry`, for [HistoryEntrie](./API.md#class-historyentry)s
|
111 | - `wrapInventoryProduct`, for [InventoryProduct](./API.md#class-inventoryproduct)s
|
112 | - `wrapMedia`, for [Media](./API.md#class-media)
|
113 | - `wrapNotification`, for [Notification](./API.md#class-notification)s
|
114 | - `wrapPlaylist`, for [Playlist](./API.md#class-playlist)s
|
115 | - `wrapRoom`, for [Room](./API.md#class-room)s
|
116 | - `wrapStoreProduct`, for [StoreProduct](./API.md#class-storeproduct)s
|
117 | - `wrapUser`, for [User](./API.md#class-user)s
|
118 | - `wrapWaitlist`, for the [Waitlist](./API.md#class-waitlist)
|
119 | - `wrapWaitlistBan`, for [WaitlistBan](./API.md#class-waitlistban)s
|
120 |
|
121 | [async-to-gen]: https://github.com/leebyron/async-to-gen
|
122 | [compose-function]: https://npmjs.com/package/compose-function
|