-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from Azuki-bar/feat/77-inout-tts
- Loading branch information
Showing
5 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
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 | ||
joinedChannels ceviord.Channels | ||
} | ||
|
||
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 speaker(h.changeState.GetText(), h.VoiceStateUpdate.GuildID, h.session) | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
type ChangeRoomState interface { | ||
GetText() string | ||
} | ||
|
||
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{} | ||
} | ||
|
||
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, s, &ceviord.Cache.Channels) | ||
if cs == nil { | ||
// ignore not covered event | ||
return nil, nil | ||
} | ||
return &handler{ | ||
session: s, | ||
VoiceStateUpdate: vsu, | ||
user: u, | ||
changeState: cs, | ||
joinedChannels: ceviord.Cache.Channels, | ||
}, 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(ceviord.RawSpeak) | ||
if err != nil { | ||
ceviord.Logger.Log(logging.WARN, err) | ||
return | ||
} | ||
return | ||
} |