Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

アーキテクチャの刷新 #6

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,109 +1,55 @@
package discord
package discord_client

import (
"context"
"fmt"
"strings"

"github.com/bwmarrin/discordgo"
"github.com/go-resty/resty/v2"
"github.com/kounoike/dtv-discord-go/config"
"github.com/kounoike/dtv-discord-go/db"
)

var (
recordingReactionEmoji = "📼"
okReactionEmoji = "🆗"
)

type DiscordClient struct {
ctx context.Context
cfg config.Config
queries *db.Queries
session *discordgo.Session
channelIDCache map[string]string
}

func NewDiscordClient(ctx context.Context, cfg config.Config, queries *db.Queries) (*DiscordClient, error) {
func NewDiscordClient(cfg config.Config, queries *db.Queries) (*DiscordClient, error) {
session, err := discordgo.New("Bot " + cfg.Discord.Token)
if err != nil {
return nil, err
}
return &DiscordClient{
ctx: ctx,
cfg: cfg,
queries: queries,
session: session,
channelIDCache: map[string]string{},
}, nil
}

func (d *DiscordClient) reactionAdd(discord *discordgo.Session, reaction *discordgo.MessageReactionAdd) {
fmt.Println("add", reaction.Emoji.Name, reaction.UserID, reaction.ChannelID)
func (d *DiscordClient) Session() *discordgo.Session {
return d.session
}

if reaction.Emoji.Name != recordingReactionEmoji {
return
}
func (d *DiscordClient) GetChannelMessage(channelID string, messageID string) (*discordgo.Message, error) {
return d.session.ChannelMessage(channelID, messageID)
}

msg, err := discord.ChannelMessage(reaction.ChannelID, reaction.MessageID)
if err != nil {
fmt.Println(err)
return
}
for _, r := range msg.Reactions {
if r.Emoji.Name == recordingReactionEmoji {
if r.Count == 1 {
// 録画しよう!
programMessage, err := d.queries.GetProgramMessageByMessageID(d.ctx, reaction.MessageID)
if err != nil {
fmt.Println(err)
return
}
url := fmt.Sprintf("http://%s:%d/api/recording/schedules", d.cfg.Mirakc.Host, d.cfg.Mirakc.Port)
postOption := fmt.Sprintf(`{"programId": %d, "options": {"contentPath": "%d.m2ts"}, "tags": ["manual"]}`, programMessage.ProgramID, programMessage.ProgramID)
client := resty.New()
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(postOption).
Post(url)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("録画予約: StatusCode:", resp.StatusCode())
if resp.StatusCode() == 201 {
// 録画予約成功?
err := d.session.MessageReactionAdd(reaction.ChannelID, reaction.MessageID, okReactionEmoji)
fmt.Println(`録画予約成功絵文字付加`, err)
}
}
break
}
}
func (d *DiscordClient) MessageReactionAdd(channelID string, messageID string, emoji string) error {
return d.session.MessageReactionAdd(channelID, messageID, emoji)
}

func (d *DiscordClient) reactionRemove(discord *discordgo.Session, reaction *discordgo.MessageReactionRemove) {
fmt.Println("remove", reaction.Emoji.Name, reaction.UserID, reaction.ChannelID)
msg, err := discord.ChannelMessage(reaction.ChannelID, reaction.MessageID)
if err != nil {
fmt.Println(err)
return
}
for _, r := range msg.Reactions {
fmt.Println(r.Emoji.Name, r.Count)
// if r.Emoji.Name == recordingReactionEmoji {
// break
// }
}
func (d *DiscordClient) AddHandler(handler interface{}) {
d.session.AddHandler(handler)
}

func (d *DiscordClient) Open() error {
err := d.session.Open()
if err != nil {
return err
}
d.session.AddHandler(d.reactionAdd)
d.session.AddHandler(d.reactionRemove)
return nil
}

Expand Down
54 changes: 54 additions & 0 deletions discord/discord_handler/discordhandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package discord_handler

import (
"context"
"fmt"

"github.com/bwmarrin/discordgo"
"github.com/kounoike/dtv-discord-go/discord"
"github.com/kounoike/dtv-discord-go/dtv"
)

type DiscordHandler struct {
dtv *dtv.DTVUsecase
session *discordgo.Session
}

func NewDiscordHandler(dtv *dtv.DTVUsecase, session *discordgo.Session) *DiscordHandler {
return &DiscordHandler{
dtv: dtv,
session: session,
}
}

func (h *DiscordHandler) reactionAdd(session *discordgo.Session, reaction *discordgo.MessageReactionAdd) {
fmt.Println("add", reaction.Emoji.Name, reaction.UserID, reaction.ChannelID)

if reaction.Emoji.Name == discord.RecordingReactionEmoji {
ctx := context.Background()
err := h.dtv.OnRecordingEmojiAdd(ctx, reaction)
if err != nil {
fmt.Println(err)
}
}
}

func (h *DiscordHandler) reactionRemove(session *discordgo.Session, reaction *discordgo.MessageReactionRemove) {
fmt.Println("remove", reaction.Emoji.Name, reaction.UserID, reaction.ChannelID)

if reaction.Emoji.Name == discord.RecordingReactionEmoji {
ctx := context.Background()
err := h.dtv.OnRecordingEmojiRemove(ctx, reaction)
if err != nil {
fmt.Println(err)
}
}
}

func (h *DiscordHandler) AddReactionAddHandler() {
h.session.AddHandler(h.reactionAdd)
}

func (h *DiscordHandler) AddReactionRemoveHandler() {
h.session.AddHandler(h.reactionRemove)
}
6 changes: 6 additions & 0 deletions discord/emoji.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package discord

var (
RecordingReactionEmoji = "📼"
OkReactionEmoji = "🆗"
)
21 changes: 21 additions & 0 deletions dtv/dtv_usecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dtv

import (
"github.com/kounoike/dtv-discord-go/db"
"github.com/kounoike/dtv-discord-go/discord/discord_client"
"github.com/kounoike/dtv-discord-go/mirakc/mirakc_client"
)

type DTVUsecase struct {
discord *discord_client.DiscordClient
mirakc *mirakc_client.MirakcClient
queries *db.Queries
}

func NewDTVUsecase(discordClient *discord_client.DiscordClient, mirakcClient *mirakc_client.MirakcClient, queries *db.Queries) *DTVUsecase {
return &DTVUsecase{
discord: discordClient,
mirakc: mirakcClient,
queries: queries,
}
}
67 changes: 67 additions & 0 deletions dtv/on_program_updated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dtv

import (
"bytes"
"context"
"database/sql"
"fmt"

"github.com/kounoike/dtv-discord-go/db"
"github.com/kounoike/dtv-discord-go/template"
"github.com/pkg/errors"
)

func (dtv *DTVUsecase) OnProgramsUpdated(serviceId uint) {
service, err := dtv.mirakc.GetService(serviceId)
_ = service
if err != nil {
fmt.Println(err)
return
}
_, err = dtv.discord.SendMessage("録画-情報", "動作ログ", fmt.Sprintf("programs updated: %s", service.Name))
if err != nil {
fmt.Println(err)
return
}
programs, err := dtv.mirakc.ListPrograms(serviceId)
if err != nil {
fmt.Println(err)
return
}
// if len(programs) > 0 {
// fmt.Println(channel, len(programs), programs[0])
// }
ctx := context.Background()
for _, p := range programs {
if p.Name == "" {
continue
}
program, err := dtv.queries.GetProgram(ctx, p.ID)
if errors.Cause(err) == sql.ErrNoRows {
msg, err := template.GetProgramMessage(p, *service)
if err != nil {
fmt.Println(err)
return
}
msgID, err := dtv.discord.SendMessage("録画-番組情報", service.Name, msg)
if err != nil {
fmt.Println(err)
return
}
p.InsertDb(ctx, *dtv.queries)
dtv.queries.InsertProgramMessage(ctx, db.InsertProgramMessageParams{MessageID: msgID, ProgramID: p.ID})
} else {
pJson, err := p.Json.MarshalJSON()
if err != nil {
continue
}
programJson, err := program.Json.MarshalJSON()
if err != nil {
continue
}
if !bytes.Equal(pJson, programJson) {
p.UpdateDb(ctx, *dtv.queries)
}
}
}
}
36 changes: 36 additions & 0 deletions dtv/on_recording_emoji_add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dtv

import (
"context"

"github.com/bwmarrin/discordgo"
"github.com/kounoike/dtv-discord-go/discord"
)

func (dtv *DTVUsecase) OnRecordingEmojiAdd(ctx context.Context, reaction *discordgo.MessageReactionAdd) error {
msg, err := dtv.discord.GetChannelMessage(reaction.ChannelID, reaction.MessageID)
if err != nil {
return err
}
for _, r := range msg.Reactions {
if r.Emoji.Name == discord.RecordingReactionEmoji {
if r.Count == 1 {
// 録画しよう!
programMessage, err := dtv.queries.GetProgramMessageByMessageID(ctx, reaction.MessageID)
if err != nil {
return err
}
err = dtv.mirakc.AddRecordingSchedule(programMessage.ProgramID)
if err != nil {
return err
}
err = dtv.discord.MessageReactionAdd(reaction.ChannelID, reaction.MessageID, discord.OkReactionEmoji)
if err != nil {
return err
}
}
break
}
}
return nil
}
23 changes: 23 additions & 0 deletions dtv/on_recording_emoji_remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dtv

import (
"context"
"fmt"

"github.com/bwmarrin/discordgo"
)

func (dtv *DTVUsecase) OnRecordingEmojiRemove(ctx context.Context, reaction *discordgo.MessageReactionRemove) error {
fmt.Println("remove", reaction.Emoji.Name, reaction.UserID, reaction.ChannelID)
msg, err := dtv.discord.GetChannelMessage(reaction.ChannelID, reaction.MessageID)
if err != nil {
return err
}
for _, r := range msg.Reactions {
fmt.Println(r.Emoji.Name, r.Count)
// if r.Emoji.Name == recordingReactionEmoji {
// break
// }
}
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pingcap/errors v0.11.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/r3labs/sse/v2 v2.10.0 // indirect
github.com/rubenv/sql-migrate v1.4.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTw
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down
Loading