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

Commit

Permalink
fix #16 Channelテーブルを統合
Browse files Browse the repository at this point in the history
  • Loading branch information
yogarasu committed Aug 22, 2022
1 parent 3042cc6 commit edc7c86
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 237 deletions.
2 changes: 1 addition & 1 deletion command/deploy/channel2.mts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ interface Options {

spinner.loading("Deploy channel")
try {
await channelClient.deployAllDiscordChannel(discordClient)
await channelClient.deployAllChannel(discordClient)
} catch (error) {
spinner.failed(null, error)
process.exit(1)
Expand Down
2 changes: 1 addition & 1 deletion command/destroy/channel2.mts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface Options {

spinner.loading("Destroy channel")
try {
await channelClient.destroyAllDiscordChannel(discordClient)
await channelClient.destroyAllChannel(discordClient)
} catch (error) {
spinner.failed(null, error)
process.exit(1)
Expand Down
99 changes: 62 additions & 37 deletions libs/category2.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChannelType, DiscordAPIError } from "discord.js"
import type { Guild as DiscordClient } from "discord.js"
import retry from "async-retry"
import { PrismaClient } from "@prisma/client"
import type { DiscordCategory } from "@prisma/client"
import type { Category } from "@prisma/client"

export class CategoryClient {
client: PrismaClient
Expand All @@ -11,52 +11,51 @@ export class CategoryClient {
}

/**
* Deploy many discord category
* Deploy all category
* @param discordClient
* @param categoryNames
*/
async deployManyDiscordCategory(
discordClient: DiscordClient,
categoryNames: string[]
) {
// Create many discord category
const discordCategories: DiscordCategory[] = await Promise.all(
categoryNames.map(async (categoryName) => {
async deployAllCategory(discordClient: DiscordClient) {
// Get all category data
const categories = await this.getAllCategory()

// Create all category
const newCategories: Category[] = await Promise.all(
categories.map(async (category) => {
const newCategory = await retry(
async () =>
await discordClient.channels.create({
name: categoryName,
name: category.name,
type: ChannelType.GuildCategory,
})
)
return {
id: 0,
categoryId: newCategory.id,
id: category.id,
deployId: newCategory.id,
name: newCategory.name,
createdAt: newCategory.createdAt,
updatedAt: newCategory.createdAt,
}
})
)

// Update many discord category data
await this.updateManyDiscordCategory(discordCategories)

return discordCategories
// Update all category data
await this.updateManyCategory(newCategories)
}

/**
* Destroy all discord category
* Destroy all category
*/
async destroyAllDiscordCategory(discordClient: DiscordClient) {
// Get all discord category data
const discordCategories = await this.client.discordCategory.findMany()
async destroyAllCategory(discordClient: DiscordClient) {
// Get all deployed category data
const categories = await this.getAllCategory(true)

// Destroy all discord category
await Promise.all(
discordCategories.map(async (category) => {
categories.map(async (category) => {
try {
await discordClient.channels.delete(category.categoryId)
if (!category.deployId)
throw new Error("Failed to get is deployed category id")
await discordClient.channels.delete(category.deployId)
} catch (error) {
if (error instanceof DiscordAPIError && error.code == 10003) {
// Do not throw error if category to be deleted does not exist
Expand All @@ -67,27 +66,35 @@ export class CategoryClient {
})
)

// Delete all discord category data
await this.client.discordCategory.deleteMany()
// Delete all category data
await this.client.category.deleteMany({
where: {
deployId: {
not: {
equals: null,
},
},
},
})
}

/**
* Update many discord category
* Update many category
* @param categories
*/
async updateManyDiscordCategory(categories: DiscordCategory[]) {
async updateManyCategory(categories: Category[]) {
const query = categories.map((category) =>
this.client.discordCategory.upsert({
this.client.category.upsert({
where: {
categoryId: category.categoryId,
id: category.id,
},
update: {
categoryId: category.categoryId,
deployId: category.deployId,
name: category.name,
updatedAt: category.updatedAt,
},
create: {
categoryId: category.categoryId,
id: category.id,
deployId: category.deployId,
name: category.name,
createdAt: category.createdAt,
updatedAt: category.updatedAt,
Expand All @@ -98,13 +105,31 @@ export class CategoryClient {
}

/**
* Get single discord category data
* @param categoryName
* Get single category data
* @param categoryId
*/
async getCategory(categoryId: string, isDeployed: boolean = false) {
return await this.client.category.findFirst({
where: {
id: categoryId,
deployId: isDeployed ? { not: { equals: null } } : undefined,
},
orderBy: [
{
updatedAt: "desc",
},
],
})
}

/**
* Get all category data
* @param isDeployed
*/
async getDiscordCategory(categoryName: string) {
return await this.client.discordCategory.findFirst({
async getAllCategory(isDeployed: boolean = false) {
return await this.client.category.findMany({
where: {
name: categoryName,
deployId: isDeployed ? { not: { equals: null } } : { equals: null },
},
orderBy: [
{
Expand Down
Loading

0 comments on commit edc7c86

Please sign in to comment.