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

Commit

Permalink
fix #16 ユーザー画像がデプロイできない問題を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
yogarasu committed Aug 22, 2022
1 parent edc7c86 commit 58ca323
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 58 deletions.
2 changes: 1 addition & 1 deletion libs/category2.mts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class CategoryClient {
return await this.client.category.findFirst({
where: {
id: categoryId,
deployId: isDeployed ? { not: { equals: null } } : undefined,
deployId: isDeployed ? { not: { equals: null } } : { equals: null },
},
orderBy: [
{
Expand Down
15 changes: 9 additions & 6 deletions libs/channel2.mts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ export class ChannelClient {
// Deploy all category
await this.categoryClient.deployAllCategory(discordClient)
const category = option.isArchived
? await this.categoryClient.getCategory("ARCHIVE", true)
: await this.categoryClient.getCategory("CHANNEL", true)
? await this.categoryClient.getCategory("ARCHIVE_CATEGORY", true)
: await this.categoryClient.getCategory("DEFAULT_CATEGORY", true)
if (!category?.deployId)
throw new Error("Failed to get deployed init category")

Expand Down Expand Up @@ -139,12 +139,12 @@ export class ChannelClient {
/**
* Destroy single channel
* @param discordClient
* @param channelId
* @param channelDeployId
*/
async destroyChannel(discordClient: DiscordClient, channelId: string) {
async destroyChannel(discordClient: DiscordClient, channelDeployId: string) {
// Destroy discord channel
try {
await discordClient.channels.delete(channelId)
await discordClient.channels.delete(channelDeployId)
} catch (error) {
if (error instanceof DiscordAPIError && error.code == 10003) {
// Do not throw error if channel to be deleted does not exist
Expand All @@ -156,7 +156,7 @@ export class ChannelClient {
// Delete channel data
await this.client.channel.delete({
where: {
id: channelId,
deployId: channelDeployId,
},
})
}
Expand Down Expand Up @@ -309,14 +309,17 @@ export class ChannelClient {
id: channel.id,
},
update: {
deployId: channel.deployId,
name: channel.name,
categoryDeployId: channel.categoryDeployId,
type: channel.type,
topic: channel.topic,
isArchived: channel.isArchived,
pins: channel.pins,
},
create: {
id: channel.id,
deployId: channel.deployId,
name: channel.name,
categoryDeployId: channel.categoryDeployId,
type: channel.type,
Expand Down
24 changes: 9 additions & 15 deletions libs/message2.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaClient, DiscordMessage, SlackUser } from "@prisma/client"
import { PrismaClient, DiscordMessage, User } from "@prisma/client"
import { access, readFile, constants, readdir } from "node:fs/promises"
import { statSync } from "node:fs"
import { join } from "node:path"
Expand Down Expand Up @@ -77,17 +77,11 @@ export class MessageClient {
let messageType = 1

// Get message author
let author: SlackUser | null = null
let author: User | null = null
if (slackMessage.bot_id) {
author = await userClient.getSlackBot(
slackClient,
slackMessage.bot_id
)
author = await userClient.getBot(slackClient, slackMessage.bot_id)
} else if (slackMessage.user) {
author = await userClient.getSlackUser(
slackClient,
slackMessage.user
)
author = await userClient.getUser(slackClient, slackMessage.user)
}
if (!author) throw new Error("Failed to get message author")

Expand Down Expand Up @@ -124,21 +118,21 @@ export class MessageClient {
async deployAllMessage(discordClient: DiscordClient) {
// Get all slack channel data
const channelClient = new ChannelClient(this.client)
const slackChannels = await channelClient.getAllSlackChannel()
const slackChannels = await channelClient.getAllChannel()
for (const channel of slackChannels) {
const channelManager = discordClient.channels.cache.get(channel.channelId)
const channelManager = discordClient.channels.cache.get(channel.id)
if (
channelManager === undefined ||
channelManager.type !== ChannelType.GuildText
)
throw new Error(`Failed to get channel manager of ${channel.channelId}`)
throw new Error(`Failed to get channel manager of ${channel.id}`)

// Pagination message
const take = 100
let skip = 0
const total = await this.client.discordMessage.count({
where: {
channelId: channel.channelId,
channelId: channel.id,
},
})
while (skip < total) {
Expand Down Expand Up @@ -258,7 +252,7 @@ export class MessageClient {
mention.replace(/<@|>/g, "")
)
for (const userId of userIds) {
const username = await userClient.getSlackUsername(userId)
const username = await userClient.getUsername(userId)
if (username) {
newContent = newContent.replaceAll(`<@${userId}>`, `@${username}`)
} else {
Expand Down
46 changes: 10 additions & 36 deletions libs/user2.mts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ export class UserClient {
}

/**
* Get slack user
* Get user
* @param userId
*/
async getSlackUser(slackClient: SlackClient, userId: string) {
async getUser(slackClient: SlackClient, userId: string) {
let user: User | null = null

// Get slack bot
Expand Down Expand Up @@ -152,22 +152,10 @@ export class UserClient {
}

/**
* Get user
* @param userId
*/
async getUser(userId: string) {
return await this.client.user.findFirst({
where: {
id: userId,
},
})
}

/**
* Get slack username
* Get username
* @param userId
*/
async getSlackUsername(userId: string) {
async getUsername(userId: string) {
const user = await this.client.user.findFirst({
where: {
id: userId,
Expand Down Expand Up @@ -234,18 +222,7 @@ export class UserClient {
async deployUserImageChannel(discordClient: DiscordClient) {
// Deploy channel for hosting user image
const channelClient = new ChannelClient(this.client)
await channelClient.updateSlackChannel({
id: 0,
channelId: "C0000000000",
name: "mds-user",
type: 2,
topic: null,
isArchived: true,
pins: null,
createdAt: new Date(),
updatedAt: new Date(),
})
const userChannel = await channelClient.deployDiscordChannel(
const userChannel = await channelClient.deployChannel(
discordClient,
"mds-user",
"C0000000000",
Expand All @@ -256,7 +233,7 @@ export class UserClient {
}
)

// Get user data
// Get all user data
const users = await this.client.user.findMany()

// Deploy all user image
Expand Down Expand Up @@ -294,16 +271,13 @@ export class UserClient {
async destroyUserImageChannel(discordClient: DiscordClient) {
// Get channel for hosting user image
const channelClient = new ChannelClient(this.client)
const userChannel = await channelClient.getDiscordChannel("mds-user", 2)
if (!userChannel)
throw new Error("Failed to get channel for hosting user image")
const userChannel = await channelClient.getChannel("mds-user", 2)
if (!userChannel || !userChannel.deployId)
throw new Error("Failed to get deployed channel for hosting user image")

// TODO: Destroy all message for channel for hosting user image

// Destroy channel for hosting user image
await channelClient.destroyDiscordChannel(
discordClient,
userChannel.channelId
)
await channelClient.destroyChannel(discordClient, userChannel.deployId)
}
}

0 comments on commit 58ca323

Please sign in to comment.