From 49cb8980a068381ff2c778ba53515d95d97a9d85 Mon Sep 17 00:00:00 2001 From: Azuki-bar <42642269+Azuki-bar@users.noreply.github.com> Date: Wed, 3 Aug 2022 02:19:56 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E5=85=A5=E9=80=80=E5=AE=A4=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=A6=E3=83=BC=E3=82=B6=E3=82=92=E8=AA=AD=E3=81=BF?= =?UTF-8?q?=E4=B8=8A=E3=81=92=E3=82=8B=E6=A9=9F=E8=83=BD=E3=81=AB=E3=81=A4?= =?UTF-8?q?=E3=81=84=E3=81=A6=E3=83=97=E3=83=AD=E3=83=88=E3=82=BF=E3=82=A4?= =?UTF-8?q?=E3=83=97=E3=82=92=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 2 + pkg/ceviord/handleMsg.go | 3 ++ pkg/discord/user.go | 33 +++++++++++++++ pkg/joinVc/handler.go | 88 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 pkg/discord/user.go create mode 100644 pkg/joinVc/handler.go diff --git a/cmd/main.go b/cmd/main.go index a2953ba..cf3d875 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,6 +3,7 @@ package main import ( "database/sql" "fmt" + "github.com/azuki-bar/ceviord/pkg/joinVc" "github.com/azuki-bar/ceviord/pkg/slashCmd" "github.com/azuki-bar/ceviord/pkg/speech/grpc" "log" @@ -73,6 +74,7 @@ func main() { dgSess.AddHandler(func(s *discordgo.Session, _ *discordgo.Connect) { log.Println("connect to discord") }) dgSess.AddHandler(ceviord.MessageCreate) dgSess.AddHandler(slashCmd.InteractionHandler) + dgSess.AddHandler(joinVc.VoiceStateUpdateHandler) // dgSess.Debug = true gTalker, closer := grpc.NewTalker(&conf.auth.CeviordConn, &conf.param.Parameters[0]) defer closer() diff --git a/pkg/ceviord/handleMsg.go b/pkg/ceviord/handleMsg.go index ffacd8b..f77bcde 100644 --- a/pkg/ceviord/handleMsg.go +++ b/pkg/ceviord/handleMsg.go @@ -272,6 +272,9 @@ func SendMsg(msg string, session *discordgo.Session, guildId string) error { func SendEmbedMsg(embed *discordgo.MessageEmbed, session *discordgo.Session, guildId string) error { cev, err := Cache.Channels.GetChannel(guildId) + if cev == nil { + return err + } isJoined, err := cev.IsActorJoined(session) if err != nil || !isJoined { return err diff --git a/pkg/discord/user.go b/pkg/discord/user.go new file mode 100644 index 0000000..3e0dbd7 --- /dev/null +++ b/pkg/discord/user.go @@ -0,0 +1,33 @@ +package discord + +import "github.com/bwmarrin/discordgo" + +type User struct { + *discordgo.User + sess *discordgo.Session + guildId string +} + +func NewUser(userId string, s *discordgo.Session, guildId string) (User, error) { + u, err := s.User(userId) + if err != nil { + return User{}, err + } + return User{ + User: u, + sess: s, + guildId: guildId, + }, nil +} + +// ScreenName returns NickName if defined, and returns Username in else. +func (u User) ScreenName() (string, error) { + m, err := u.sess.GuildMember(u.guildId, u.ID) + if err != nil { + return "", err + } + if m.Nick != "" { + return m.Nick, nil + } + return u.Username, nil +} diff --git a/pkg/joinVc/handler.go b/pkg/joinVc/handler.go new file mode 100644 index 0000000..aec4da9 --- /dev/null +++ b/pkg/joinVc/handler.go @@ -0,0 +1,88 @@ +package joinVc + +import ( + "fmt" + "github.com/azuki-bar/ceviord/pkg/ceviord" + "github.com/azuki-bar/ceviord/pkg/discord" + "github.com/azuki-bar/ceviord/pkg/logging" + "github.com/bwmarrin/discordgo" +) + +type handler struct { + *discordgo.VoiceStateUpdate + session *discordgo.Session + changeState ChangeRoomState + user discord.User +} + +func (h *handler) handle() error { + switch h.changeState.(type) { + case outOfScope: + return nil + default: + return ceviord.RawSpeak(h.changeState.GetText(), h.VoiceStateUpdate.GuildID, h.session) + } +} + +type Handler interface { + handle() error +} + +type ChangeRoomState interface { + GetText() string +} + +func NewChangeRoomState(vsu *discordgo.VoiceStateUpdate) ChangeRoomState { + if vsu.BeforeUpdate == nil && vsu.VoiceState != nil { + return intoRoom{} + } else if vsu.BeforeUpdate != nil && vsu.VoiceState.ChannelID == "" { + return outRoom{} + } + return outOfScope{} +} + +type intoRoom struct{ screenName string } + +func (r intoRoom) GetText() string { + return fmt.Sprintf("%sさんが入室しました。", r.screenName) +} + +type outRoom struct{ screenName string } + +func (r outRoom) GetText() string { + return fmt.Sprintf("%sさんが退室しました。", r.screenName) +} + +type outOfScope struct{ ChangeRoomState } + +func NewHandler(s *discordgo.Session, vsu *discordgo.VoiceStateUpdate) (Handler, error) { + u, err := discord.NewUser(vsu.UserID, s, vsu.GuildID) + if err != nil { + return nil, err + } + cs := NewChangeRoomState(vsu) + if cs == nil { + // ignore not covered event + return nil, nil + } + return &handler{ + session: s, + VoiceStateUpdate: vsu, + user: u, + changeState: NewChangeRoomState(vsu), + }, nil +} + +func VoiceStateUpdateHandler(s *discordgo.Session, vsu *discordgo.VoiceStateUpdate) { + h, err := NewHandler(s, vsu) + if err != nil { + ceviord.Logger.Log(logging.WARN, err) + return + } + err = h.handle() + if err != nil { + ceviord.Logger.Log(logging.WARN, err) + return + } + return +} From aaca79423f18617c26a5470062d904fa595ef002 Mon Sep 17 00:00:00 2001 From: Azuki-bar <42642269+Azuki-bar@users.noreply.github.com> Date: Wed, 3 Aug 2022 15:49:38 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E3=83=A6=E3=83=BC=E3=82=B6=E3=81=AE?= =?UTF-8?q?=E5=85=A5=E9=80=80=E5=AE=A4=E6=99=82=E3=81=AB=E3=80=81=E3=83=A6?= =?UTF-8?q?=E3=83=BC=E3=82=B6=E5=90=8D=E3=82=92=E8=AA=AD=E3=81=BF=E4=B8=8A?= =?UTF-8?q?=E3=81=92=E3=82=8B=E6=A9=9F=E8=83=BD=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/ceviord/handleMsg.go | 6 ++++ pkg/joinVc/handler.go | 61 +++++++++++++++++++++++++++++++--------- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/pkg/ceviord/handleMsg.go b/pkg/ceviord/handleMsg.go index f77bcde..e587110 100644 --- a/pkg/ceviord/handleMsg.go +++ b/pkg/ceviord/handleMsg.go @@ -227,6 +227,12 @@ func MessageCreate(sess *discordgo.Session, msg *discordgo.MessageCreate) { func RawSpeak(text string, guildId string, sess *discordgo.Session) error { cev, err := Cache.Channels.GetChannel(guildId) + if cev == nil { + return fmt.Errorf("get channel failed") + } + if err != nil { + return err + } isJoined, err := cev.IsActorJoined(sess) if err != nil || !isJoined { return err diff --git a/pkg/joinVc/handler.go b/pkg/joinVc/handler.go index aec4da9..0f6f41b 100644 --- a/pkg/joinVc/handler.go +++ b/pkg/joinVc/handler.go @@ -10,17 +10,18 @@ import ( type handler struct { *discordgo.VoiceStateUpdate - session *discordgo.Session - changeState ChangeRoomState - user discord.User + session *discordgo.Session + changeState ChangeRoomState + user discord.User + joinedChannels ceviord.Channels } func (h *handler) handle() error { switch h.changeState.(type) { - case outOfScope: - return nil - default: + case intoRoom, outRoom: return ceviord.RawSpeak(h.changeState.GetText(), h.VoiceStateUpdate.GuildID, h.session) + default: + return nil } } @@ -32,11 +33,44 @@ type ChangeRoomState interface { GetText() string } -func NewChangeRoomState(vsu *discordgo.VoiceStateUpdate) ChangeRoomState { - if vsu.BeforeUpdate == nil && vsu.VoiceState != nil { - return intoRoom{} - } else if vsu.BeforeUpdate != nil && vsu.VoiceState.ChannelID == "" { - return outRoom{} +func NewChangeRoomState(vsu *discordgo.VoiceStateUpdate, s *discordgo.Session, cs *ceviord.Channels) ChangeRoomState { + if !cs.IsExistChannel(vsu.GuildID) { + return outOfScope{} + } + c, err := cs.GetChannel(vsu.GuildID) + if err != nil { + return outOfScope{} + } + u, err := discord.NewUser(vsu.UserID, s, vsu.GuildID) + if u.Bot { + return outOfScope{} + } + if err != nil { + ceviord.Logger.Log(logging.WARN, err) + return outOfScope{} + } + scn, err := u.ScreenName() + if err != nil { + ceviord.Logger.Log(logging.WARN, err) + return outOfScope{} + } + var state ChangeRoomState = outOfScope{} + if (vsu.BeforeUpdate == nil || vsu.BeforeUpdate.ChannelID != c.VoiceConn.ChannelID) && vsu.VoiceState != nil { + state = intoRoom{screenName: scn} + } else if vsu.BeforeUpdate != nil && vsu.VoiceState.ChannelID != c.VoiceConn.ChannelID { + state = outRoom{screenName: scn} + } + switch state.(type) { + case intoRoom: + if vsu.ChannelID == c.VoiceConn.ChannelID { + return state + } + case outRoom: + if vsu.BeforeUpdate.ChannelID == c.VoiceConn.ChannelID { + return state + } + default: + return outOfScope{} } return outOfScope{} } @@ -60,7 +94,7 @@ func NewHandler(s *discordgo.Session, vsu *discordgo.VoiceStateUpdate) (Handler, if err != nil { return nil, err } - cs := NewChangeRoomState(vsu) + cs := NewChangeRoomState(vsu, s, &ceviord.Cache.Channels) if cs == nil { // ignore not covered event return nil, nil @@ -69,7 +103,8 @@ func NewHandler(s *discordgo.Session, vsu *discordgo.VoiceStateUpdate) (Handler, session: s, VoiceStateUpdate: vsu, user: u, - changeState: NewChangeRoomState(vsu), + changeState: cs, + joinedChannels: ceviord.Cache.Channels, }, nil } From 15f1c417299b5cc45e09ff27d4c9f2b604b11e5c Mon Sep 17 00:00:00 2001 From: Azuki-bar <42642269+Azuki-bar@users.noreply.github.com> Date: Wed, 3 Aug 2022 16:26:36 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E3=83=86=E3=82=AD=E3=82=B9=E3=83=88?= =?UTF-8?q?=E8=AA=AD=E3=81=BF=E3=81=82=E3=81=92=E9=96=A2=E6=95=B0=E3=82=92?= =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89=E6=B3=A8=E5=85=A5=E3=81=A7?= =?UTF-8?q?=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/joinVc/handler.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/joinVc/handler.go b/pkg/joinVc/handler.go index 0f6f41b..3699dbf 100644 --- a/pkg/joinVc/handler.go +++ b/pkg/joinVc/handler.go @@ -16,19 +16,19 @@ type handler struct { joinedChannels ceviord.Channels } -func (h *handler) handle() error { +type Handler interface { + handle(speaker func(text, guildId string, session *discordgo.Session) error) error +} + +func (h *handler) handle(speaker func(text string, guildId string, session *discordgo.Session) error) error { switch h.changeState.(type) { case intoRoom, outRoom: - return ceviord.RawSpeak(h.changeState.GetText(), h.VoiceStateUpdate.GuildID, h.session) + return speaker(h.changeState.GetText(), h.VoiceStateUpdate.GuildID, h.session) default: return nil } } -type Handler interface { - handle() error -} - type ChangeRoomState interface { GetText() string } @@ -114,7 +114,7 @@ func VoiceStateUpdateHandler(s *discordgo.Session, vsu *discordgo.VoiceStateUpda ceviord.Logger.Log(logging.WARN, err) return } - err = h.handle() + err = h.handle(ceviord.RawSpeak) if err != nil { ceviord.Logger.Log(logging.WARN, err) return From a2e8152cb637d1a1f174ed6a5ac1dd24264b1779 Mon Sep 17 00:00:00 2001 From: Azuki-bar <42642269+Azuki-bar@users.noreply.github.com> Date: Wed, 3 Aug 2022 16:36:40 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=83=96=E3=83=A9?= =?UTF-8?q?=E3=83=AA=E3=81=AE=E3=82=A8=E3=83=A9=E3=83=BC=E5=87=BA=E5=8A=9B?= =?UTF-8?q?=E3=82=92=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/dgvoice/dgvoice.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/dgvoice/dgvoice.go b/pkg/dgvoice/dgvoice.go index c13eab6..94c20e8 100644 --- a/pkg/dgvoice/dgvoice.go +++ b/pkg/dgvoice/dgvoice.go @@ -47,9 +47,9 @@ var OnError = func(str string, err error) { prefix := "dgVoice: " + str if err != nil { - os.Stderr.WriteString(prefix + ": " + err.Error()) + os.Stderr.WriteString(prefix + ": " + err.Error() + "\n") } else { - os.Stderr.WriteString(prefix) + os.Stderr.WriteString(prefix + "\n") } }