diff --git a/cmd/bot.go b/cmd/bot.go index 7de5f82..ce84cb1 100644 --- a/cmd/bot.go +++ b/cmd/bot.go @@ -289,6 +289,7 @@ func (c *BotCommand) Execute(ctx context.Context, f *flag.FlagSet, args ...inter discordHandler.AddReactionAddHandler() discordHandler.AddReactionRemoveHandler() discordHandler.RegisterCommand() + defer discordHandler.UnregisterCommand() logger.Info("AddDiscordHandle done. start subscribe to SSE events.") diff --git a/db/auto_search_found_message.sql.go b/db/auto_search_found_message.sql.go index 6824a71..bbdaea2 100644 --- a/db/auto_search_found_message.sql.go +++ b/db/auto_search_found_message.sql.go @@ -9,17 +9,20 @@ import ( "context" ) -const countAutoSearchFoundMessagesByProgramID = `-- name: CountAutoSearchFoundMessagesByProgramID :one +const countAutoSearchFoundMessagesWithRecordByProgramID = `-- name: CountAutoSearchFoundMessagesWithRecordByProgramID :one SELECT count(*) FROM ` + "`" + `auto_search_found_message` + "`" + ` +JOIN + ` + "`" + `auto_search` + "`" + ` ON ` + "`" + `auto_search_found_message` + "`" + `.` + "`" + `thread_id` + "`" + ` = ` + "`" + `auto_search` + "`" + `.` + "`" + `thread_id` + "`" + ` WHERE - ` + "`" + `program_id` + "`" + ` = ? + ` + "`" + `auto_search` + "`" + `.` + "`" + `record` + "`" + ` = 1 + AND ` + "`" + `program_id` + "`" + ` = ? ` -func (q *Queries) CountAutoSearchFoundMessagesByProgramID(ctx context.Context, programID int64) (int64, error) { - row := q.db.QueryRowContext(ctx, countAutoSearchFoundMessagesByProgramID, programID) +func (q *Queries) CountAutoSearchFoundMessagesWithRecordByProgramID(ctx context.Context, programID int64) (int64, error) { + row := q.db.QueryRowContext(ctx, countAutoSearchFoundMessagesWithRecordByProgramID, programID) var count int64 err := row.Scan(&count) return count, err diff --git a/db/auto_search_message.sql.go b/db/auto_search_message.sql.go deleted file mode 100644 index 9b664b5..0000000 --- a/db/auto_search_message.sql.go +++ /dev/null @@ -1,49 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.18.0 -// source: auto_search_message.sql - -package db - -import ( - "context" -) - -const getAutoSearchMessageByMessageID = `-- name: GetAutoSearchMessageByMessageID :one -SELECT - id, thread_id, message_id, created_at, updated_at -FROM - ` + "`" + `auto_search_message` + "`" + ` -WHERE - ` + "`" + `message_id` + "`" + ` = ? -` - -func (q *Queries) GetAutoSearchMessageByMessageID(ctx context.Context, messageID string) (AutoSearchMessage, error) { - row := q.db.QueryRowContext(ctx, getAutoSearchMessageByMessageID, messageID) - var i AutoSearchMessage - err := row.Scan( - &i.ID, - &i.ThreadID, - &i.MessageID, - &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} - -const insertAutoSearchMessage = `-- name: InsertAutoSearchMessage :exec -INSERT INTO ` + "`" + `auto_search_message` + "`" + ` ( - ` + "`" + `thread_id` + "`" + `, - ` + "`" + `message_id` + "`" + ` -) VALUES (?, ?) -` - -type InsertAutoSearchMessageParams struct { - ThreadID string `json:"threadID"` - MessageID string `json:"messageID"` -} - -func (q *Queries) InsertAutoSearchMessage(ctx context.Context, arg InsertAutoSearchMessageParams) error { - _, err := q.db.ExecContext(ctx, insertAutoSearchMessage, arg.ThreadID, arg.MessageID) - return err -} diff --git a/db/models.go b/db/models.go index c6e2c9e..49ca167 100644 --- a/db/models.go +++ b/db/models.go @@ -33,14 +33,6 @@ type AutoSearchFoundMessage struct { UpdatedAt time.Time `json:"updatedAt"` } -type AutoSearchMessage struct { - ID int32 `json:"id"` - ThreadID string `json:"threadID"` - MessageID string `json:"messageID"` - CreatedAt time.Time `json:"createdAt"` - UpdatedAt time.Time `json:"updatedAt"` -} - type ComponentVersion struct { ID int32 `json:"id"` Component string `json:"component"` diff --git a/db/queries/auto_search_found_message.sql b/db/queries/auto_search_found_message.sql index 678ffda..dd91983 100644 --- a/db/queries/auto_search_found_message.sql +++ b/db/queries/auto_search_found_message.sql @@ -13,13 +13,16 @@ FROM WHERE `thread_id` = ?; --- name: CountAutoSearchFoundMessagesByProgramID :one +-- name: CountAutoSearchFoundMessagesWithRecordByProgramID :one SELECT count(*) FROM `auto_search_found_message` +JOIN + `auto_search` ON `auto_search_found_message`.`thread_id` = `auto_search`.`thread_id` WHERE - `program_id` = ? + `auto_search`.`record` = 1 + AND `program_id` = ? ; -- name: DeleteAutoSearchFoundMessagesByThreadID :exec diff --git a/discord/discord_handler/command_handler.go b/discord/discord_handler/command_handler.go index 7fa2513..0e27305 100644 --- a/discord/discord_handler/command_handler.go +++ b/discord/discord_handler/command_handler.go @@ -251,7 +251,7 @@ func (h *DiscordHandler) CommandHandler(s *discordgo.Session, i *discordgo.Inter if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Content: fmt.Sprintf("> %s\nスレッド名:%s\nタイトル:%s\nチャンネル:%s\nジャンル:%s\n録画:%s\nこれでよろしいですか?", + Content: fmt.Sprintf("> %s\n**スレッド名:**%s\n**タイトル:**%s\n**チャンネル:**%s\n**ジャンル:**%s\n**録画:**%s\nこれでよろしいですか?", jsonStr, data.Name, data.Title, @@ -343,3 +343,9 @@ func (h *DiscordHandler) RegisterCommand() { h.logger.Error("ApplicationCommandCreate error", zap.Error(err)) } } + +func (h *DiscordHandler) UnregisterCommand() { + h.session.ApplicationCommandDelete(h.session.State.User.ID, h.session.State.Guilds[0].ID, "index") + h.session.ApplicationCommandDelete(h.session.State.User.ID, h.session.State.Guilds[0].ID, "create") + h.session.ApplicationCommandDelete(h.session.State.User.ID, h.session.State.Guilds[0].ID, "delete") +} diff --git a/dtv/create_auto_seach.go b/dtv/create_auto_seach.go index 4404464..93237f3 100644 --- a/dtv/create_auto_seach.go +++ b/dtv/create_auto_seach.go @@ -135,7 +135,7 @@ func (dtv *DTVUsecase) CreateAutoSearch(userID string, name string, title string } if err := dtv.mirakc.AddRecordingSchedule(program.ID, contentPath); err != nil { dtv.logger.Warn("AddRecordingSchedule error", zap.Error(err)) - continue + // 重複登録の場合もあるので、エラーは無視して継続 } } if err := dtv.queries.InsertAutoSearchFoundMessage(ctx, db.InsertAutoSearchFoundMessageParams{MessageID: msg.ID, ThreadID: thID, ProgramID: program.ID}); err != nil { diff --git a/dtv/delete_auto_search.go b/dtv/delete_auto_search.go index d4008d3..430656f 100644 --- a/dtv/delete_auto_search.go +++ b/dtv/delete_auto_search.go @@ -19,7 +19,7 @@ func (dtv *DTVUsecase) DeleteAutoSearch(ctx context.Context, threadID string) er } for _, found := range founds { programID := found.ProgramID - cnt, err := dtv.queries.CountAutoSearchFoundMessagesByProgramID(ctx, programID) + cnt, err := dtv.queries.CountAutoSearchFoundMessagesWithRecordByProgramID(ctx, programID) if err != nil { return err } diff --git a/mirakc/mirakc_client/mirakc-client.go b/mirakc/mirakc_client/mirakc-client.go index ec4a1d8..c3e6ed8 100644 --- a/mirakc/mirakc_client/mirakc-client.go +++ b/mirakc/mirakc_client/mirakc-client.go @@ -117,6 +117,19 @@ type scheduleData struct { func (m *MirakcClient) AddRecordingSchedule(programID int64, contentPath string) error { url := fmt.Sprintf("http://%s:%d/api/recording/schedules", m.host, m.port) + getUrl := fmt.Sprintf("%s/%d", url, programID) + + client := resty.New() + resp, err := client.R(). + Get(getUrl) + if err != nil { + return err + } + if resp.StatusCode() == 200 { + // 既に予約済みなので何もしない + return nil + } + data := scheduleData{ ProgramID: programID, Options: scheduleOptions{ @@ -129,8 +142,7 @@ func (m *MirakcClient) AddRecordingSchedule(programID int64, contentPath string) return err } // postOption := fmt.Sprintf(`{"programId": %d, "options": {"contentPath": "%d.m2ts"}, "tags": ["manual"]}`, programID, programID) - client := resty.New() - resp, err := client.R(). + resp, err = client.R(). SetHeader("Content-Type", "application/json"). SetBody(dataJson). Post(url)