UNPKG

3.81 kBMarkdownView Raw
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
12Moderation bots often delete their own messages after a while to avoid
13cluttering 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
20const delay = require('delay')
21
22mp.chat('Meh limit: 5')
23 .then(delay(5000)) // Wait 5 seconds
24 .then((message) => message.delete())
25```
26
27## Lockskip
28
29A lockskip skips the current DJ, but puts them back in front of the waitlist
30so they can play again soon.
31
32```js
33const dj = mp.dj()
34const entry = mp.historyEntry()
35
36// Skip the DJ, then move them back to spot #2 on the waitlist.
37const lockskipPosition = 2
38dj.skip(entry.id).then(() => {
39 return dj.move(lockskipPosition)
40})
41```
42
43## Async Functions
44
45Async functions are a new JavaScript feature that aims to make Promise-based
46code a lot easier to read. The code in the [Lockskip](#lockskip) recipe looks
47like this with async functions:
48
49```js
50async 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
58Async functions are available by default starting in Node.js v7.6. If you're
59using miniplug with an earlier Node.js version, you can use the [async-to-gen][]
60module like below:
61
62```js
63require('async-to-gen/register')
64require('./bot')
65```
66
67This will compile async functions in your code to more widely compatible
68JavaScript before running it.
69
70## Extending Objects
71
72Miniplug uses `wrapObject` methods to instantiate objects of various types.
73Extending these methods can be useful if you are writing a custom plugin. A good
74way to extend wrapper methods is to use function composition, using the
75[compose-function][] module for example.
76
77```js
78import compose from 'compose-function'
79
80function 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
107The 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