-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cmd adapter and loader modules
- Loading branch information
Showing
33 changed files
with
442 additions
and
9,835 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,95 @@ | ||
import { Adapter, Api } from '@kotori-bot/kotori'; | ||
import QQApi from './api'; | ||
/* | ||
* @Author: Hotaru biyuehuya@gmail.com | ||
* @Blog: https://hotaru.icu | ||
* @Date: 2023-09-29 14:31:09 | ||
* @LastEditors: Hotaru biyuehuya@gmail.com | ||
* @LastEditTime: 2023-10-06 15:59:37 | ||
*/ | ||
import { Adapter, AdapterConfig, Events, Msg, eventDataMsgSender, isObj } from '@kotori-bot/kotori'; | ||
import CmdApi from './api'; | ||
|
||
export default class QQAdapter extends Adapter { | ||
protected Api: Api; | ||
interface Iconfig extends AdapterConfig { | ||
nickname: string; | ||
age: number; | ||
sex: eventDataMsgSender['sex']; | ||
} | ||
|
||
function checkConfig(config: unknown): config is Iconfig { | ||
if (!isObj(config)) return false; | ||
if (typeof config.nickname !== 'string') return false; | ||
if (typeof config.age !== 'number') return false; | ||
if (config.sex !== 'male' && config.sex !== 'female' && config.sex !== 'unknown') return false; | ||
return true; | ||
} | ||
|
||
export default class CmdAdapter extends Adapter<CmdApi> { | ||
private messageId = 0; | ||
|
||
public api: CmdApi = new CmdApi(this); | ||
|
||
public readonly platform: string = 'cmd'; | ||
|
||
public declare config: Iconfig; | ||
|
||
public constructor(config: object) { | ||
super(config); | ||
this.Api = new QQApi(this); | ||
public constructor(config: AdapterConfig, identity: string) { | ||
const defaultConfig = { | ||
nickname: 'Kotarou', | ||
age: 18, | ||
sex: 'male', | ||
}; | ||
const newConfig = Object.assign(defaultConfig, config); | ||
super(newConfig, identity); | ||
if (!checkConfig(newConfig)) throw new Error(`Bot '${identity}' config format error`); | ||
this.config = newConfig; | ||
process.stdin.on('data', data => this.handle(data)); | ||
} | ||
|
||
public handle = () => this; | ||
public handle = (data: Buffer) => { | ||
if (this.status.value !== 'online') return; | ||
const message = data.toString(); | ||
if (message === '\n' || message === '\r\n') return; | ||
Adapter.emit({ | ||
type: 'private_msg', | ||
messageId: this.messageId, | ||
message, | ||
userId: this.config.master, | ||
sender: { | ||
nickname: this.config.nickname, | ||
sex: this.config.sex, | ||
age: this.config.age, | ||
}, | ||
send: (message: Msg) => { | ||
this.api.send_private_msg(message, this.config.master); | ||
}, | ||
api: this.api, | ||
locale: this.locale, | ||
}); | ||
this.messageId += 1; | ||
}; | ||
|
||
public start = () => this; | ||
public start = () => { | ||
this.send = (action, params?) => { | ||
if (this.status.value !== 'online' || action !== 'send_private_msg' || !params) return; | ||
if (typeof (params as { message: string }).message !== 'string') return; | ||
if ((params as { user_id: unknown }).user_id !== this.config.master) return; | ||
process.stdout.write(`> ${(params as { message: string }).message} \r\n`); | ||
}; | ||
Events.emit({ | ||
type: 'connect', | ||
adapter: this, | ||
normal: true, | ||
info: `start cmd-line listen`, | ||
}); | ||
this.online(); | ||
}; | ||
|
||
public stop = () => this; | ||
public stop = () => { | ||
Events.emit({ | ||
type: 'disconnect', | ||
adapter: this, | ||
normal: true, | ||
info: `stop cmd-line listen`, | ||
}); | ||
this.offline(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,35 @@ | ||
/* | ||
* @Author: Hotaru biyuehuya@gmail.com | ||
* @Blog: https://hotaru.icu | ||
* @Date: 2023-09-29 14:31:13 | ||
* @LastEditors: Hotaru biyuehuya@gmail.com | ||
* @LastEditTime: 2023-10-06 15:57:29 | ||
*/ | ||
import { Api, Msg } from '@kotori-bot/kotori'; | ||
|
||
export default class QQApi extends Api { | ||
export default class CmdApi extends Api { | ||
public send_private_msg = (message: Msg, userId: number) => { | ||
this.send('send_private_msg', { user_id: userId, message, auto_escape: false }); | ||
/* handled msg... */ | ||
this.adapter.send('send_private_msg', { user_id: userId, message }); | ||
}; | ||
|
||
/** | ||
* @description: 发送私聊消息 | ||
* @param {Msg} message 要发送的内容 | ||
* @param {groupId} groupId 群号 | ||
* @return {void} | ||
*/ | ||
public send_group_msg = (message: Msg, groupId: number): void => { | ||
this.send('send_group_msg', { group_id: groupId, message, auto_escape: false }); | ||
}; | ||
public send_group_msg = () => this.adapter.send(''); | ||
|
||
/** | ||
* @description: 撤回消息 | ||
* @param {number} messageId 消息id | ||
* @return {void} | ||
*/ | ||
public delete_msg = (messageId: number): void => { | ||
this.send('delete_msg', { messageId }); | ||
}; | ||
public delete_msg = () => this.adapter.send(''); | ||
|
||
/** | ||
* @description: 设置群名 | ||
* @param {number} groupId 群号 | ||
* @param {string} groupName 新群名 | ||
* @return {void} | ||
*/ | ||
public set_group_name = (groupId: number, groupName: string): void => { | ||
this.send('set_group_name', { group_id: groupId, group_name: groupName }); | ||
}; | ||
public set_group_name = () => this.adapter.send(''); | ||
|
||
/** | ||
* @description: 设置群头像 | ||
* @param {number} groupId 群号 | ||
* @param {string} image 图片路径 | ||
* @return {void} | ||
*/ | ||
public set_group_avatar = (groupId: number, image: string): void => { | ||
this.send('set_group_portrait', { group_id: groupId, file: image, cache: false }); | ||
}; | ||
public set_group_avatar = () => this.adapter.send(''); | ||
|
||
/** | ||
* @description: 设置群管理员 | ||
* @param {number} groupId 群号 | ||
* @param {number} userId 要设置的管理员的QQ号 | ||
* @param {boolean} enable true为设置,false取消,默认true | ||
* @return {void} | ||
*/ | ||
public set_group_admin = (groupId: number, userId: number, enable: boolean = true): void => { | ||
this.send('set_group_admin', { group_id: groupId, user_id: userId, enable }); | ||
}; | ||
public set_group_admin = () => this.adapter.send(''); | ||
|
||
/** | ||
* @description: 设置群名片(群备注) | ||
* @param {number} groupId 群号 | ||
* @param {number} userId 要设置的QQ号 | ||
* @param {string} card 群名片内容,不填或空字符串表示删除群名片 | ||
* @return {void} | ||
*/ | ||
public set_group_card = (groupId: number, userId: number, card: string): void => { | ||
this.send('set_group_card', { group_id: groupId, user_id: userId, card }); | ||
}; | ||
public set_group_card = () => this.adapter.send(''); | ||
|
||
/** | ||
* @description: 群禁言 | ||
* @param {number} groupId 群号 | ||
* @param {number} userId 要禁言的QQ号,不填则为群禁言 | ||
* @param {number} time 禁言时长,单位秒,0表示取消禁言 | ||
* @return {void} | ||
*/ | ||
public set_group_ban = (groupId: number, userId?: number, time: number = 0): void => { | ||
if (userId) { | ||
this.send('set_group_ban', { group_id: groupId, user_id: userId, duration: time }); | ||
} else { | ||
this.send('set_group_whole_ban', { group_id: groupId, enable: !!time }); | ||
} | ||
}; | ||
public set_group_ban = () => this.adapter.send(''); | ||
|
||
/** | ||
* @description: 发送群公告 | ||
* @param {number} groupId 群号 | ||
* @param {string} content 公告内容 | ||
* @param {string} image 图片路径(可选) | ||
* @return {void} | ||
*/ | ||
public send_group_notice = (groupId: number, content: string, image?: string): void => { | ||
this.send('_send_group_notice', { group_id: groupId, content, image }); | ||
}; | ||
public send_group_notice = () => this.adapter.send(''); | ||
|
||
/** | ||
* @description: 群组踢人 | ||
* @param {number} groupId 群号 | ||
* @param {number} userId 要踢的QQ号 | ||
* @return {void} | ||
*/ | ||
public set_group_kick = (groupId: number, userId: number): void => { | ||
this.send('set_group_kick', { group_id: groupId, user_id: userId, reject_add_request: false }); | ||
}; | ||
public set_group_kick = () => this.adapter.send(''); | ||
|
||
/** | ||
* @description: 退出群组 | ||
* @param {number} groupId 群号 | ||
* @return {void} | ||
*/ | ||
public set_group_leave = (groupId: number): void => { | ||
this.send('set_group_leave', { group_id: groupId, is_dismiss: false }); | ||
}; | ||
public set_group_leave = () => this.adapter.send(''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import QQAdapter from './adapter'; | ||
|
||
export default QQAdapter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "@kotori-bot/plugin-testing", | ||
"version": "1.0.0", | ||
"description": "testing plugin", | ||
"main": "src/index.ts", | ||
"license": "GPL-3.0", | ||
"author": "Hotaru <biyuehuya@gmail.com>", | ||
"peerDependencies": { | ||
"kotori-bot": "workspace:^" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Kotori from '@kotori-bot/kotori'; | ||
|
||
Kotori.command('echo <content>').action(data => { | ||
console.log(data); | ||
}); |
Oops, something went wrong.