Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
fix #11 メッセージにファイル情報を追加するように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
yogarasu committed Aug 17, 2022
1 parent 358c1b2 commit 36732e2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
4 changes: 2 additions & 2 deletions libs/bot.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFile } from "node:fs/promises"
import { Message as SlackMessage } from "@slack/web-api/dist/response/ChatPostMessageResponse"
import { Message as SlackBaseMessage } from "@slack/web-api/dist/response/ChatPostMessageResponse"
import { WebClient as SlackClient } from "@slack/web-api"
import type { Channel } from "./channel.mjs"

Expand Down Expand Up @@ -56,7 +56,7 @@ export const getMessageBotId = async (
for (const channel of channels) {
for (const messageFilePath of channel.slack.message_file_paths) {
const srcMessageFile = await readFile(messageFilePath, "utf8")
const srcMessage: SlackMessage[] = JSON.parse(srcMessageFile)
const srcMessage: SlackBaseMessage[] = JSON.parse(srcMessageFile)
botIds = [
...botIds,
...srcMessage
Expand Down
36 changes: 31 additions & 5 deletions libs/message.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,36 @@ import { access, readFile, readdir, mkdir, writeFile } from "node:fs/promises"
import { statSync, constants } from "node:fs"
import { dirname, join } from "node:path"
import { fromUnixTime, format } from "date-fns"
import { Message as SlackMessage } from "@slack/web-api/dist/response/ChatPostMessageResponse"
import {
Message as SlackBaseMessage,
FileElement,
} from "@slack/web-api/dist/response/ChatPostMessageResponse"
import { ChannelType, Guild } from "discord.js"
import type { User } from "./user.mjs"
import type { Channel } from "./channel.mjs"

export interface SlackMessage extends SlackBaseMessage {
files?: FileElement[]
}

export interface Message {
message_id?: string
channel_id?: string
guild_id?: string
text: string
timestamp?: number
files?: {
id: string
file_type: string
name: string
size: number
url: string
download_url: string
}[]
author?: {
id: string
is_bot: boolean
}
timestamp?: number
}

/**
Expand Down Expand Up @@ -106,8 +121,6 @@ export const buildMessageFile = async (
: ""
text += `${icon} **${name}** ${timestamp}\n`

// TODO: ここに添付ファイルのダウンロード処理を書く

// TODO: ここにサブタイプに応じて必要なら処理を書く
// "bot_add" | "bot_message" | "bot_remove" | "channel_join" | "channel_topic" | "channel_archive" | "channel_purpose"

Expand All @@ -125,8 +138,21 @@ export const buildMessageFile = async (
}
}

// 添付ファイルがあれば追加
const files: Message["files"] = message.files?.map((file) => {
return {
id: file.id || "",
file_type: file.filetype || "",
name: file.name || "",
size: file.size || 0,
url: file.url_private || "",
download_url: file.url_private_download || "",
}
})

newMessages.push({
text: text,
files: files,
})
}

Expand Down Expand Up @@ -240,12 +266,12 @@ export const createMessage = async (
message_id: result.id,
channel_id: result.channelId,
guild_id: result.guildId ? result.guildId : undefined,
timestamp: result.createdTimestamp,
anthor: {
id: result.author.id,
is_bot: result.author.bot,
name: result.author.username,
},
timestamp: result.createdTimestamp,
},
})
}
Expand Down

0 comments on commit 36732e2

Please sign in to comment.