npm i donationalerts-api
import { EventName, DAWidget } from "donationalerts"
const token = "ON3g9GNujvQYu3g9GNQY" // secret token url param from any widget
const dawidget = new DAWidget(token, {
widgetBehavior: true,
alertDuration: 10000
})
dawidget.api.getUser().then(user => console.log(
"api:user",
user.id,
user.socketConnectionToken,
user.code,
user.mainCurrency,
user.balances
))
dawidget.socket.on(EventName.Donation, donation => console.log(
"socket:donation",
donation.username,
donation.message,
donation.amount,
donation.currency,
donation.additionalData.isCommissionCovered
))
import { EventName, DAWidget } from "donationalerts"
const token = "ON3g9GNujvQYu3g9GNQY" // secret token url param from any widget
const dasocket = new DASocket(token, {
socketUrl: 7
})
const daapi = new DAAPI(token, {
accessToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIyIiwianRpIjoiOTQ1NzI0MTM2MGIzOGE4YTRjZWU0OGU0YmZiNWMyN2FmMGViOWZkY2M3MGI4M2FkNjhmYjViZWYwNzc3ZjNmZjFhMzg0NzAzMmM5Yzc0NGUiLCJpYXQiOjE2MzIwNjgzNjAsIm5iZiI6MTYzMjA2ODM2MCwiZXhwIjozMjA5OTA1MTYwLCyIiwianRpIjoiOTQ1NzI0MTM2MGIzOGE4YTRjZWU0OGU0YmZiNWMyN2FmMGViOWZkY2M3MGI4M2FkNjhmYjViZWYwNzc3ZjNmZJzdWIiOiI0MjY1NjQ2Iiwic2NvcGVzIjpbIndpZGdldC1zdHJlYW1lciJiZWYwNzc3ZjNmZjFhMzg0NzAzMmM5Yzc0NGUiLCJpYXQiOjE2MzIwNjgzNjAsIm5iZiI6MTYzMjA2ODM2MCwiZXhMTM2MGIzOGE4YTRjZWU0OGU0YmZiNWMyN2FmMGViOWZkY2M3MGI4M2FkNjhmYjViZWYwNzc3ZjNmZjFhMzg0NzAzMmM5Yzc0NGU"
})
daapi.crawlWidget().then(data => console.log(
"api:crawl",
data.accessToken
))
daapi.requestApi("donationgoal", {
params: {
is_active: 0,
include_timestamps: 1
}
}).then(data => console.log(
"api:crawl",
data.accessToken
))
dasocket.mediaPause()
dasocket.on(EventName.AlertSkip, alert => console.log(
"socket:alert_skip"
alert.id,
alert.type
))
new DAWidget(token: string, {
widgetBehavior?: boolean,
alertDuration?: number,
autoOpen?: boolean
}?)
DonationAlerts Secret token, can be found as a param in any widget url
You can find the token at the dashboard
Default: true
See widgetBehavior property description
Default: 10000
See alertDuration property description
Default: true
Set to false
if you want to manually call open
readonly api: DAAPI
The instance of the DAAPI class, can be used to access API
/* Usage example */
/* ... */
const user = await dawidget.api.getUser()
readonly socket: DASocket
The instance of the DASocket class, can be used to access Socket
/* Usage example */
/* ... */
dawidget.socket.mediaPause()
widgetBehavior: boolean
When connected to socket and widgetBehavior
set to true
, acts as default DonationAlerts widget, so Last Alert Panel would work correctly (new alerts wouldn't have unplayed state)
/*
* Note:
* You can use lastdonation widget as the OBS panel without authorization, just add the token param at the end
* For instance, https://www.donationalerts.com/widget/lastdonations?alert_type=1,12&limit=100&token=ON3g9GNujvQYu3g9GNQY
*/
/* Usage example */
/* ... */
if(!dawidget.widgetBehavior) dawidget.widgetBehavior = true
alertDuration: number
Widget pretending duration (ms) to be shown at Last Alert Panel
/* Usage example */
/* ... */
dawidget.alertDuration = 20000 // 20s
async skipCurrentAlert(): Promise<void>
Skip currently playing alert
Works only if widgetBehavior
is true
and alert
/* Usage example */
/* ... */
dawidget.skipCurrentAlert()
async open(): Promise<void>
Crawl necessary data from widget page and init api + socket connections
/* Usage example */
const dawidget = new DAWidget({ autoOpen: false })
await dawidget.open()
async close(): Promise<void>
Close all opened connections
/* Usage example */
/* ... */
dawidget.close()
new DAAPI(token: string, {
accessToken?: string
}?)
See token property description
Default: undefined
You can provide your own accessToken to access public api
If not provided, will try to crawl it from widget page using crawlWidget method
readonly token: string
The token used to access internal DA api
See DAWidget's token property description
/* Usage example */
/* ... */
const token = daapi.token
async crawlWidget(): Promise<{prev: { accessToken?: string }, accessToken: string}>
Crawl necessary data from widget page to use public api
It is not necessary to call this method because it will be called automatically if accessData
is not specified by the constructor
/* Usage example */
const daapi = new DAAPI(...)
const info = await daapi.crawlWidget()
console.log(`prev: ${info.prev.accessToken}, new: ${info.accessToken}`)
async getUser(): Promise<User>
Public DA method api/v1/user
, gets info about the accessToken's owner
Response is parsed according to our guideline
Try to use it and check what data do to you have access to, because DA API can be changed everyday
If DA implements new properties in future and module is outdated, you can access those by using raw
property in response object
/* Usage example */
/* ... */
const user = await daapi.getUser()
console.log(
user.socketConnectionToken,
user.id,
user.code,
user.mainCurrency,
user.balances,
user.raw
)
async markAlertShown({ id: number, type: AlertType }): Promise<string>
Internal DA method api/markalertshow
, used to remove unplayed status of the alert
/* Usage example */
/* ... */
const donation = ... // raw id is alert_id, raw type is alert_type
daapi.markAlertShown({ id: donation.id, type: donation.type })
async skipAlert({ id: number, type: AlertType }): Promise<string>
async skipMedia(id: number): Promise<string>
async markMediaPlayed(id: number): Promise<string>
async getMedia(): Promise<{ raw: any }>
async getPollWidget(): Promise<{ raw: any }>
async getWidget(): Promise<{
alerts: Donation[],
settings: { raw: any },
ttsMins: { [x: Currency]: number},
raw: any
}>
async requestApi(endpoint: string, {
version?: number,
method?: Method,
data?: any,
params?: any
}?): Promise<{ [x: string]: any }>
Request public API (with accessToken
, url starts with /api/v1/...
)
async requestInternalApi(endpoint: string, {
method?: Method,
data?: any,
params?: any
}?): Promise<{ [x: string]: any }>
Request internal API (with token
, url starts with /api/...
)
new DASocket(token: string, {
socketUrl?: number | string,
autoConnect?: boolean
}?) extends Emittery
emittery is used as events manager, you are free to use it's api
See token property description
Default: undefined
Every streamer account belongs to the specified socket server, for instance wss://socket7.donationalerts.ru
If string
is specified, it will override the whole url to connect
If number
is specified, it will override only server number
If undefined
is specified, it will try to crawl url with crawlWidget method
Note: if you specify wrong server, connection would not work
Set to false
if you want to manually call connect
readonly token: string
The token used to access internal DA socket
See DAWidget's token property description
/* Usage example */
/* ... */
const token = dasocket.token
readonly connected: boolean
Defines if socket is successfully connected
on(name: EventName.Donation, listener: (event: Donation) => void): void
on(name: EventName.AlertStart, listener: (event: AlertShowStartEvent) => void): void
on(name: EventName.AlertEnd, listener: (event: AlertShowEvent) => void): void
on(name: EventName.AlertSkip, listener: (event: AlertShowEvent) => void): void
on(name: EventName.Media, listener: (event: RawData) => void): void
on(name: EventName.Stickers, listener: (event: RawData) => void): void
Subscribe to new events, listener must be a callback which will be called with an event param
/* Usage example */
const dasocket = new DASocket(...)
dasocket.on(EventName.Donation, donationEvent => console.log(
donationEvent.id,
donationEvent.type,
donationEvent.username,
donationEvent.amountMain,
donationEvent.currency,
donationEvent.message,
donationEvent.additionalData,
donationEvent.raw
))
dasocket.on(EventName.AlertSkip, alertSkipEvent => console.log(
alertSkipEvent.id,
alertSkipEvent.type,
alertSkipEvent.raw
))
dasocket.on(EventName.Media, mediaEvent => console.log(
mediaEvent.raw
))
async crawlWidget(): Promise<{prev: { socketUrl?: string }, socketUrl: string}>
Crawl necessary data from widget page to use internal DA socket
It is not necessary to call this method because it will be called automatically if socketUrl
is not specified by the constructor
/* Usage example */
const dasocket = new DASocket(...)
const info = await dasocket.crawlWidget()
console.log(`prev: ${info.prev.socketUrl}, new: ${info.socketUrl}`)
async connect({ addUser?: boolean, clientType?: ClientType }?): Promise<void>
/* Usage example */
const dasocket = new DASocket({ autoConnect: false })
await dasocket.connect({
addUser: true,
clientType: ClientType.AlertWidget // Minor is used by default
})
async disconnect(): Promise<void>
/* Usage example */
/* ... */
await dasocket.disconnect()
async addUser(type: ClientType): Promise<void>
/* Usage example */
const dasocket = new DASocket({ autoConnect: false })
await dasocket.connect({ addUser: false })
await dasocket.addUser(ClientType.AlertShow)
alertStart({
id: number,
type: AlertType,
groupId?: number,
duration?: number
}): void
alertEnd({ id: number, type: AlertType }): void
alertSkip({ id: number, type: AlertType }): void
mediaEnd(id: number): void
mediaShowWidget(): void
mediaHideWidget(): void
mediaPause(): void
mediaUnpause(): void
mediaGetPauseState(source: any): void
mediaGetCurrent(source: any): void
mediaGetVolumeOverride(volume: number): void
mediaReceiveVolumeOverride(volume: number): void
mediaReceivePauseState({
source: string,
media: any,
isPaused: boolean,
volumeOverride: number
}): void
mediaReceiveCurrentMedia({
source: string,
media: any,
isPaused: boolean,
volumeOverride: number
}): void
Released under MIT license