Skip to content

Commit

Permalink
feat: Support fetch many messages
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Apr 16, 2023
1 parent c82d12a commit 39f3e9c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"ws": "^8.13.0"
},
"devDependencies": {
"@itchatapp/types": "^1.2.8",
"@itchatapp/types": "^1.2.10",
"@types/ws": "^8.5.4",
"prettier": "^2.8.7",
"typescript": "^5.0.4"
Expand Down
32 changes: 22 additions & 10 deletions src/managers/MessageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ export class MessageManager extends BaseManager<Message, APIMessage> {
super(channel.client)
}

async fetch(message: MessageResolvable): Promise<Message> {
const id = this.resolveId(message)

if (!id) {
throw new TypeError('INVALID_TYPE', 'message', 'MessageResolvable')
}
fetch(): Promise<Map<string, Message>>
fetch(message: MessageResolvable): Promise<Message>
async fetch(message?: MessageResolvable): Promise<Message | Map<string, Message>> {
if (!message) {
const data = await this.client.api.get(`/messages/${this.channel.id}`)

return data.reduce((cur, prev) => {
const msg = this.add(prev)
cur.set(msg.id, msg)
return cur
}, new Map<string, Message>())
} else {
const id = this.resolveId(message)

if (!id) {
throw new TypeError('INVALID_TYPE', 'message', 'MessageResolvable')
}

const data = await this.client.api.get(`/channels/${this.channel.id}/messages/${id}`)
const data = await this.client.api.get(`/messages/${this.channel.id}/${id}`)

return this.add(data)
return this.add(data)
}
}

async delete(message: MessageResolvable): Promise<void> {
Expand All @@ -39,7 +51,7 @@ export class MessageManager extends BaseManager<Message, APIMessage> {
throw new TypeError('INVALID_TYPE', 'message', 'MessageResolvable')
}

await this.client.api.delete(`/channels/${this.channel.id}/messages/${id}`)
await this.client.api.delete(`/messages/${this.channel.id}/${id}`)
}

async send(options: CreateMessageOptions): Promise<Message> {
Expand All @@ -58,7 +70,7 @@ export class MessageManager extends BaseManager<Message, APIMessage> {
throw new TypeError('INVALID_TYPE', 'message', 'MessageResolvable')
}

const data = await this.client.api.patch(`/channels/${this.channel.id}/messages/${id}`, {
const data = await this.client.api.patch(`/messages/${this.channel.id}/${id}`, {
body: {
content: typeof options === 'object' ? options.content : options
}
Expand Down

0 comments on commit 39f3e9c

Please sign in to comment.