Skip to content

Latest commit

 

History

History
218 lines (162 loc) · 4.38 KB

api.md

File metadata and controls

218 lines (162 loc) · 4.38 KB

Crate API

Once you've initialised a new constructor, you can manipulate it using the following methods

Crate APIs

Toggle

:::tip Definition

type toggle = (open?: boolean) => void

:::

Toggles the widget's visibility, with an optional param to set it.

::: tip Usage

// Toggle
crate.toggle()

// Toggle open
crate.toggle(true)

// Toggle closed
crate.toggle(false)

::: Try it: toggle() toggle(true) toggle(false)


Notify

::: tip Definition

type notify = (
  content:
    | string
    | Notification & {
      content: string
      timeout?: number
    }
  ) => { hide: Function; delete: Function }

::: Displays a notification message to the user. Markdown supported

::: tip Usage

// Hello world
crate.notify(`**hello** world`)

// Display for 2 seconds + custom avatar
crate.notify({
  content: '`2 seconds`',
  timeout: 2000,
  avatar: 'https://cdn.discordapp.com/avatars/293731150239891456/f7d78d0c7e6522ed296bfa315b3a1969.png'
})

// Programmatically hide notification
const notification = crate.notify({
  content: 'Test',
  timeout: false
})
/* business logic */
notification.hide()

::: Try it: hello world 2 seconds + avatar


Update options

::: tip Definition

type setOptions = (newOptions: Options) => Options

::: Updates the options for crate in real time. Available options

::: tip 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
})

::: Try it: change colors random position


Visibility

::: tip Definition

type hide = () => void
type show = () => void

::: Hides / un-hides the crate instance, eg. the button, notifications and widget

::: tip Usage

// Button will now disappear
crate.hide()

// Button will now re-appear
crate.show()

::: Try it: hide() show()


Navigation

::: tip Definition

type navigate = (channelID: string) => void

::: Navigates to a different channel and opens the widget if it's closed

::: tip Usage

// Navigates to #general
crate.navigate('368427726358446110')

// Navigates to #live-demo
crate.navigate('355719584830980096')

::: Try it: #general #live-demo


Embed API

Emit

::: tip Definition

type emit = <Event>(event: Event, data?: Events[Event]) => void

::: Emits an event to the embed-api

For details, see embed-api commands

::: tip Usage

// Navigate to a different server/channel
crate.emit('navigate', {
  guild: '299881420891881473',
  channel: '368427726358446110'
})

// Request login
crate.emit('login')

// Log out
crate.emit('logout')

:::

On

::: tip Definition

type on = <Event>(event: Event, (data?: Events[Event]) => void) => void

::: Listens for a specific event from the embed-api

For details, see embed-api events

::: tip Usage

// Listens for when the user has signed in
crate.on('signIn', (user) => {
  console.log(`User signed in as ${user.username}`, user)
})

// Listen for discord message events
crate.on('message', ({ message, channel }) => {
  console.log(`New message in #${channel.name}`, message)
})

// Listen for discord message delete events
crate.on('messageDelete', ({ channel, id }) => {
  console.log(`Message ${id} in #${channel.name} was deleted`)
})

:::