模板用 SDK

用于模板开发的 JavaScript SDK

使用注意事项

注意

本 SDK 是 OneComme 模板专用的 SDK
仅限模板、插件等在 OneComme 中使用的场景使用
除与 OneComme 无关的用途,以及未获准作为公开 API 使用的 API 以外,均不得使用

请再次确认使用条款

关联应用程序、服务同样适用 OneComme 的使用条款

关于署名标注

在使用调用了 HTTP API 的应用程序、服务时,若为免费使用,也需要标注署名

分发应用程序时,请务必附上关于署名标注的说明

关于来自外部 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 进行初始设置
必须调用此函数

configPartial< 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

subscriberSubscriber
export type Subscriber = {  
  action: T // permissions 中指定的操作名称  
  callback: (response: PublishActions[T]) => void  
}

unsubscribe(subscriberId: number): void

根据订阅 ID 停止对该事件的订阅

subscriberIdnumber

reset():void

初始化已获取的评论等数据

getStyleVariable< T = any >(name: string, defaultValue: T, parser: (val:string) => T): T

用于获取 CSS 中 :root 内定义的 CSS 变量值的函数

namestring
defaultValueT
parser(val: string) ⇒ T

getCommentStyle(comment: Comment): {[key: string]: string}

根据评论返回 YouTube 等平台的醒目留言(超级留言)或会员资格的颜色信息

commentComment
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 应用的基本信息

AppInfoAppInfo
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 >

【临时实现】返回歌单列表

SetListAPIResponseSetListAPIResponse
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点歌请求发生变化时