模板用 SDK
用於模板開發的 JavaScript SDK
使用注意事項
注意
本 SDK 為 OneComme 模板專用 SDK
僅限用於模板、擴充功能等 OneComme 相關用途
除公開 API 允許使用的範圍外,不得用於與 OneComme 無關的用途
請再次確認使用條款
連動的應用程式與服務也適用 OneComme 的使用條款
關於標示credit
即使是使用 HTTP API 的應用程式或服務,在免費使用的情況下也需要標示credit
發布應用程式時,請務必附上關於credit標示的說明
關於從外部 IP 或網域存取
基於安全考量,OneComme 的 API 限制僅能從本機 IP 存取
若要從外部 IP 或網域存取 API,需要在「設定 > API 分頁」(設定頁面)中登記允許存取的主機名稱
注意
API 文件部分尚未完備
我們會在 Discord 伺服器的開發者專用頻道中交流新增內容與最新資訊
歡迎開發者加入 Discord,並告知我們您是開發者
使用方法
npm install @onecomme.com/onesdk -S
或使用模板資料夾內的 preset/__origin/js/onesdk.js
(也可透過 http://localhost:11180/templates/preset/__origin/js/onesdk.js 存取)
注意
若要上傳至伺服器或複製到模板資料夾等操作,請先與我們聯繫
API
ready():Promise< void >
當頁面載入完成時,會 resolve 該 Promise
若已經載入完成,則會立即 resolve
setup(config: Partial< OneSDKConfig >): void
進行 SDK 的初始設定
必須呼叫此函式
| config | Partial< OneSDKConfig > |
export type Protocol = 'ws' | 'local'
export interface OneSDKConfig {
protocol: Protocol // Connection protocol(Default: 'local')
port: number // Port number (Default: 11180)
host: string // Host name(Default: 'localhost')
pathname: string // API pathname (Default: '')
mode: 'all' | 'diff' // Comment fetch mode (Default: 'all') all=每次都取得目前已取得的所有留言(已刪除的除外) diff=永遠只取得最新留言
disabledDelay: boolean // 將留言取得延遲設為 0 (Default: false)
intervalTime: number // Comment update interval (Default: 5000)
maxQueueInterval: number // 連續留言時的間隔 (Default: 150)
reconnectInterval: number // 斷線時的重新連接間隔 (Default: 5000)
commentLimit: number // SDK 上保留的最大留言數 (Default: 100)
includes: string[] | null // 要取得的直播服務 (Default: null)
excludes: string[] | null // 要排除的直播服務 (Default: null)
includeIds: string[] | null // 要接收的使用者身分 (Default: null)
excludeIds: string[] | null // 要排除的使用者身分 (Default: null)
includeNames: string[] | null // 要取得的框架名稱 (Default: null)
excludeNames: string[] | null // 要取得的框架名稱 (Default: null)
lifeTime: number // 留言保留時間 (Default: Infinity)
permissions: SendType[] | null // API Permission
}
subscribe(subscriber: Subscriber): number
訂閱 OneComme 發生變化時的事件
會回傳解除訂閱時所需的訂閱 ID
| subscriber | Subscriber |
export type Subscriber = {
action: T // permissions 中指定的動作名稱
callback: (response: PublishActions[T]) => void
}
unsubscribe(subscriberId: number): void
根據訂閱 ID 停止訂閱事件
| subscriberId | number |
reset():void
將已取得的留言等資料初始化
getStyleVariable< T = any >(name: string, defaultValue: T, parser: (val:string) => T): T
用於取得 CSS 中 :root 所定義的 CSS 變數值的函式
| name | string |
| defaultValue | T |
| parser | (val: string) ⇒ T |
getCommentStyle(comment: Comment): {[key: string]: string}
根據留言內容,回傳 YouTube 等平台的超級留言(SC)或會員資格的顏色資訊
| comment | Comment |
export interface Comment {
id: string
service: ServiceType // 接收到的直播網站識別碼
name: string
url: string
color: RGBColor // 框架設定的顏色資訊{r,g,b}
meta?: BaseCommentMeta
data: CommentData
}
export interface BaseCommentMeta {
no?: number // 留言編號
tc?: number // 累計留言數
lc?: number // 留言數
interval?: number // 留言間隔
free?: boolean // 是否為贈品
}
export interface CommentData {
id: string
userId: string
liveId: string
name: string
screenName?: string
isOwner: boolean
isSupporter?: boolean
displayName?: string
nickname?: string
hasGift: boolean
autoModerated?: boolean
profileImage: string
originalProfileImage?: string
badges: BaseBadge[]
timestamp: string
comment: string
speechText?: string
isFirstTime?: boolean
isRepeater?: boolean
commentVisible?: boolean
meta?: CommentMeta
}
export interface BaseBadge {
url: string
label: string
type?: string
}
export interface CommentMeta {
hasNickname?: boolean
hasMemo?: boolean
anonymity?: boolean
label?: string
type?: 'system'
}
checkLicensed(): Promise< boolean >
回傳是否正在使用 PRO 版的旗標
connect(): Promise< void >
連接至 OneComme API 伺服器
必須呼叫此函式
getInfo(): Promise< AppInfo >
回傳 OneComme 應用程式的基本資訊
| AppInfo | AppInfo |
export interface AppInfo {
platform: 'win' | 'mac'
version: string
port: number
licensed: boolean
templatePath: string
templateUrl: string
}
getOrders(): Promise< OrderItem[] >
回傳參與型排隊等候的資訊清單
| OrderItem[] | OrderItem[] |
export interface OrderItem {
id: string // userid
commentId: string
service: ServiceType
serviceName: string
username: string
profileImage: string
timestamp: string
comment: string
count: number
completed: boolean
playing: boolean
anonymity: boolean
userData?: UserNameData
totalCount?: number
lastCompletedAt?: string
}
cancelOrder(orderId: string): Promise< OrderItem[] >
根據參與 ID 取消排隊等候
會回傳取消後最新的排隊等候清單
| OrderItem[] | OrderItem[] |
completeOrder(orderId: string): Promise< OrderItem[] >
根據參與 ID 完成排隊等候
會回傳完成後最新的排隊等候清單
| OrderItem[] | OrderItem[] |
getSetList(): Promise< SetListAPIResponse >
【暫定實作】回傳歌單曲目清單
| SetListAPIResponse | SetListAPIResponse |
export interface SetListAPIResponse {
id: number
name: string
items: MusicData[]
completed: MusicData[]
}
export interface MusicData {
id: number
box: number
name: string
link: string
tags: string[]
memo: string
}
Permission
為了從 SDK 接收各種資料,必須明確指定要接收的資料
可在 config 的 permissions 中以陣列指定多個項目
在 permissions 中指定的項目,即可在 subscribe 中作為事件類型接收
| connected | 連線正常建立時 |
| comments | 收到一般留言時 |
| systemComment | 收到系統留言時 |
| clear | 執行清除留言時 |
| deleted | 留言被刪除時 |
| meta | 直播情報更新時 |
| meta.clear | 直播情報被清除時 |
| notification | 收到通知時 |
| pinned | 留言的釘選狀態變化時 |
| waitingList | 參與等候清單變化時 |
| setList | 歌單曲目變化時 |
| setList.request | 點播請求變化時 |