这是开发中的下一个版本(9.2)的文档,正式发布前内容可能会有变化。

请查看当前正式版的同一页面。

模板用 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 访问)

注意

将 SDK 复制、捆绑或分发到 OneComme 相关作品中,无论收费与否,均无需单独咨询。请保留 SDK 的许可协议和版权声明。详情请参阅使用条款。

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 // 连接协议(Default: 'local')  
  port: number // 端口号 (Default: 11180)  
  host: string // 主机名称(Default: 'localhost')  
  pathname: string // API pathname (Default: '')  
  mode: 'all' | 'diff' // 评论获取模式 (Default: 'all') all=每次获取最近的全部评论(不含已删除的) diff=始终只获取最新的评论  
  disabledDelay: boolean // 将评论获取延迟设为 0 (Default: false)  
  intervalTime: number // 评论更新间隔 (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 等平台的 Super Chat 和会员资格的颜色信息

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 // 是否为赠品  
  isUpdate?: boolean // 是否为相同 data.id 的评论第二次及以后送达(显示更新)(9.2 起)  
  isStreak?: boolean // 是否为连续发送(连击)中第二条及以后的送达(9.2 起)  
}  
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'  
}

各分销平台存入 data 的字段,请参阅 SDK 参考中各平台的 CommentResponse
9.2 中新增了 Mirrativ、MIXCHANNEL、bilibili 的礼物数量(giftCount),Kick 的礼物金额(price),Twitch 的订阅信息(giftCount・subType・tier・plan,以及单次订阅礼品的接收人 giftReceivers)

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 中作为事件类型接收

类型何时收到传给 callback 的数据
connected连接成功时{ config?: Config; services?: Service[]; comments?: Comment[]; waitingList?: OrderItem[]; setList?: SetListAPIResponse }
comments收到普通评论时Comment[]
systemComment收到系统评论时
发送来源的分销平台可通过 data.fromService 判断(9.2 起)
SystemComment
clear执行清除评论时空数组 []
deleted评论被删除时{ id: string; message: string }[]
id 是被删除评论的 id(comment.data.id),message 是“已删除”等用于显示的文字
meta流媒体信息更新时{ type: ServiceType; service: Service; data: ServiceMeta }
meta.clear流媒体信息被清除时string(被清除的直播间 ID)
meta.clear.all所有直播间的流媒体信息被清除时null
configOneComme 的设置更新时Config
services直播间列表发生变化时Service[]
userDatas观众(用户)信息更新时UserStoreData
notification收到通知时{ type: string; message: string }
pinned评论的置顶状态发生变化时Comment(取消置顶时为 null)
bookmarked书签发生变化时{ key: string; commentId: string | null }
waitingList参与等候列表发生变化时OrderItem[]
setList歌单发生变化时SetListAPIResponse
setList.request点歌请求发生变化时RequestItem[]
reactions收到 YouTube 的反应或 NicoNico直播的情感时
effect 表示 OneComme 本体是否设置为显示特效
{ reactions: Reaction[]; effect: boolean }
events收到不会作为评论显示的平台事件时(Twitch 的突袭・订阅・观众里程碑,NicoNico直播的来访・巡航等)(9.2 起)
无论应用的通知设置如何都会收到
EventPayload
wp.updateWordParty 的设置更新时{ dir: string }
wp.execWordParty 的特效执行时WordPartyExecParams
yt.survey.start / yt.survey.update / yt.survey.finishYouTube 的问卷调查开始・更新・结束时YouTube.SurveyData
ni.survey.start / ni.survey.finishNicoNico直播的问卷调查开始・结束时{ state, meta }
vts.position收到 VTube Studio 对接的追踪位置时VTSPositionParams
toast显示应用内通知(Toast)时
※ 用于远程观看的推送,普通模板无法接收
CommentNotification
deck.layouts远程控制台的布局发生变化时(9.2 起)
※ 用于远程观看的推送,普通模板无法接收
DeckLayout[]

deleted / clear 可在 SDK 9.2.0 及以上版本接收
请使用 id 来确定被删除的评论
message 是用于显示的文字,内容并不固定

各事件可接收的数据类型请参阅 PublishActions,可指定的值列表请参阅 SendType

events 可接收的数据(EventPayload)如下

export interface EventPayload {
  id: string
  timestamp: string
  service: Pick<Service, 'id' | 'name' | 'url' | 'color'>
  serviceType: ServiceType
  event: CommentEvent // 突袭、订阅等事件的内容
  message: string // 作为通知显示的文本
  user?: { userId: string; name: string; screenName?: string }
}

权限预设

向 OneSDK.usePermission() 传入 OneSDK.PERM 的预设名称,即可批量指定

OneSDK.setup({
  permissions: OneSDK.usePermission([OneSDK.PERM.COMMENT, OneSDK.PERM.EVENT]),
})
预设包含的内容
DEFAULTcomments・deleted・clear・pinned・bookmarked・meta・meta.clear
COMMENTcomments・deleted・clear・pinned・bookmarked
METAmeta・meta.clear
ORDERwaitingList
REACTIONreactions
SETLISTsetList・setList.request
WORDPARTYwp.update・wp.exec
YT_SURVEYyt.survey.start・yt.survey.update・yt.survey.finish
NI_SURVEYni.survey.start・ni.survey.finish
CONFIGconfig
SERVICEservices
USERuserDatas
NOTIFICATIONnotification
EVENTevents(9.2 起)