|
| 1 | +# `mirai-api-http-ts` - Simple and Intuitive TypeScript Wrapper for `mira-api-http` |
| 2 | + |
| 3 | +[简体中文](README.zh.md) | English |
| 4 | + |
| 5 | +Note: This is an ongoing project. We will try our best to keep |
| 6 | +backward compatibility, but breaking changes can occur. |
| 7 | +The version number conforms to [SemVer](https://semver.org/) as |
| 8 | +compatibility reference. |
| 9 | + |
| 10 | +## Motivation |
| 11 | + |
| 12 | +I just need a good enough TypeScript library for my bot. I guess |
| 13 | +you might need it too. |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +Get the library: |
| 18 | + |
| 19 | +``` |
| 20 | +$ yarn add @dousha99/mirai-api-http-ts |
| 21 | +- or - |
| 22 | +$ npm i --save @dousha99/mirai-api-http-ts |
| 23 | +``` |
| 24 | + |
| 25 | +Documentation in progress... |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +See `examples/` for working examples. Here is a simple echo bot example: |
| 30 | + |
| 31 | +```ts |
| 32 | +import { MiraiClient } from '../index'; |
| 33 | +import { MessageType } from '../src/objects/Message'; |
| 34 | +import { OutboundMessageChain } from '../src/objects/OutboundMessageChain'; |
| 35 | + |
| 36 | +const mirai = new MiraiClient({ |
| 37 | +connection: { |
| 38 | +tls: false, |
| 39 | +host: 'localhost', |
| 40 | +httpPort: 8080, |
| 41 | +websocketPort: 8080, |
| 42 | +useWebsocket: true, |
| 43 | +pollPeriod: 5000, |
| 44 | +pollCount: 5, |
| 45 | +}, |
| 46 | +account: { |
| 47 | +authKey: process.env['AUTH_KEY']!, |
| 48 | +account: Number(process.env['QQ']!), |
| 49 | +}, |
| 50 | +}); |
| 51 | +mirai.on('message', msg => { |
| 52 | +if (msg.message.type === MessageType.FRIEND_MESSAGE) { |
| 53 | +if (msg.isPlainTextMessage()) { |
| 54 | +const text = msg.getPlainText(); |
| 55 | +msg.reply(OutboundMessageChain.ofText(text)).catch(e => console.error(e)); |
| 56 | +if (text.trim() === 'stophammertime') { |
| 57 | +mirai.close(); |
| 58 | +} |
| 59 | +} |
| 60 | +} |
| 61 | +}); |
| 62 | +mirai.on('connect', () => { |
| 63 | +console.log('Ready'); |
| 64 | +}); |
| 65 | +``` |
0 commit comments