# Crate API
Once you've initialised a new constructor, you can manipulate it using the following methods
# Crate APIs
# Toggle
Definition
type toggle = (open?: boolean) => void
1
Toggles the widget's visibility, with an optional param to set it.
Usage
// Toggle
crate.toggle()
// Toggle open
crate.toggle(true)
// Toggle closed
crate.toggle(false)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Try it:
# Notify
Definition
type notify = (
content:
| string
| Notification & {
content: string
timeout?: number
}
) => { hide: Function; delete: Function }
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Displays a notification message to the user. Markdown supported
Usage
// Hello world
crate.notify(`**hello** world`)
// Display for 2 seconds + custom avatar
crate.notify({
content: '`2 seconds`',
timeout: 2000,
avatar: 'https://cdn.samdd.me/samdd-logo/variations/logo.png'
})
// Programmatically hide notification
const notification = crate.notify({
content: 'Test',
timeout: false
})
/* business logic */
notification.hide()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Try it:
# Update options
Definition
type setOptions = (newOptions: Options) => Options
1
Updates the options for crate in real time. Available options
Usage
// Update an option by mutating it
crate.options.color = '#'+Math.random().toString(16).slice(2, 8) // random color
// Update options by passing an object
crate.setOptions({
location: [-Math.random() * 200), 'right'], // Random position
})
1
2
3
4
5
6
7
2
3
4
5
6
7
Try it:
# Visibility
Definition
type hide = () => void
type show = () => void
1
2
2
Hides / un-hides the crate instance, eg. the button, notifications and widget
Usage
// Button will now disappear
crate.hide()
// Button will now re-appear
crate.show()
1
2
3
4
5
2
3
4
5
Try it:
# Embed API
WARNING
This is not currently implemented in the latest version of WidgetBot.
# Emit
Definition
type emit = <Event>(event: Event, data: Events[Event]) => void
1
Emits an event to the embed-api
Usage
// Send a message on the active Discord channel
crate.emit('sendMessage', 'hi')
// Send a message on a specific Discord channel
crate.emit('sendMessage', {
channel: '123456789',
message: 'hi'
})
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# On
Definition
type on = <Event>(event: Event, (data: Events[Event]) => void) => void
1
Listens for a specific event from the embed-api
Usage
// Listens for when the user has signed in
crate.on('signIn', (user) => {
console.log(`Guest signed in as ${user.name}`, user)
})
// Listen for discord message events
crate.on('message', ({ message, channel }) => {
console.log(`New message in ${channel}`, message)
})
// Listen for discord message delete events
crate.on('messageDelete', ({ channel, id }) => {
console.log(`Message in ${channel} with an ID of ${id}, was deleted`)
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14