diff --git a/discord/commands.go b/discord/commands.go index fb0f4e5..8e803a6 100644 --- a/discord/commands.go +++ b/discord/commands.go @@ -21,18 +21,21 @@ const ( type Command string const ( - HelpCommand Command = "help" - ObserveCommand Command = "observe" - ObservationsCommand Command = "observations" - UnObserveCommand Command = "unobserve" - LostVillagesCommand Command = "lostvillages" - ConqueredVillagesCommand Command = "conqueredvillages" - TribeCommand Command = "tribe" - TopAttCommand Command = "topatt" - TopDefCommand Command = "topdef" - TopSuppCommand Command = "topsupp" - TopTotalCommand Command = "toptotal" - TopPointsCommand Command = "toppoints" + HelpCommand Command = "help" + ObserveCommand Command = "observe" + ObservationsCommand Command = "observations" + UnObserveCommand Command = "unobserve" + LostVillagesCommand Command = "lostvillages" + UnObserveLostVillagesCommand Command = "unobservelostvillages" + ConqueredVillagesCommand Command = "conqueredvillages" + UnObserveConqueredVillagesCommand Command = "unobserveconqueredvillages" + TribeCommand Command = "tribe" + TopAttCommand Command = "topatt" + TopDefCommand Command = "topdef" + TopSuppCommand Command = "topsupp" + TopTotalCommand Command = "toptotal" + TopPointsCommand Command = "toppoints" + AuthorCommand Command = "author" ) func (cmd Command) String() string { @@ -46,11 +49,12 @@ func (cmd Command) WithPrefix(prefix string) string { func (s *Session) handleHelpCommand(m *discordgo.MessageCreate) { tribeCMDWithPrefix := TribeCommand.WithPrefix(s.cfg.CommandPrefix) commandsForAll := fmt.Sprintf(` -- %s %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RA z plemion o podanych id -- %s %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RO z plemion o podanych id -- %s %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RW z plemion o podanych id -- %s %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największej liczbie pokonanych z plemion o podanych id -- %s %s [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największej liczbie punktów z plemion o podanych id +- **%s %s** [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RA z plemion o podanych id +- **%s %s** [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RO z plemion o podanych id +- **%s %s** [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największym RW z plemion o podanych id +- **%s %s** [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największej liczbie pokonanych z plemion o podanych id +- **%s %s** [serwer] [strona] [id1] [id2] [id3] [n id] - wyświetla graczy o największej liczbie punktów z plemion o podanych id +- **%s** - kontakt z autorem bota `, tribeCMDWithPrefix, TopAttCommand.String(), @@ -61,21 +65,28 @@ func (s *Session) handleHelpCommand(m *discordgo.MessageCreate) { tribeCMDWithPrefix, TopTotalCommand.String(), tribeCMDWithPrefix, - TopPointsCommand.String()) + TopPointsCommand.String(), + AuthorCommand.WithPrefix(s.cfg.CommandPrefix), + ) commandsForGuildAdmins := fmt.Sprintf(` -- %s [świat] [id] - dodaje plemię z danego świata do obserwowanych -- %s - wyświetla wszystkie obserwowane plemiona -- %s [id z %s] - usuwa plemię z obserwowanych -- %s - ustawia kanał na którym będą wyświetlać się informacje o straconych wioskach -- %s - ustawia kanał na którym będą wyświetlać się informacje o podbitych wioskach +- **%s** [świat] [id] - dodaje plemię z danego świata do obserwowanych +- **%s** - wyświetla wszystkie obserwowane plemiona +- **%s** [id z %s] - usuwa plemię z obserwowanych +- **%s** - ustawia kanał na którym będą wyświetlać się informacje o podbitych wioskach +- **%s** - informacje o podbitych wioskach na wybranym kanale nie będą się już pojawiały +- **%s** - ustawia kanał na którym będą wyświetlać się informacje o straconych wioskach +- **%s** - informacje o podbitych wioskach na wybranym kanale nie będą się już pojawiały `, ObserveCommand.WithPrefix(s.cfg.CommandPrefix), ObservationsCommand.WithPrefix(s.cfg.CommandPrefix), UnObserveCommand.WithPrefix(s.cfg.CommandPrefix), ObservationsCommand.WithPrefix(s.cfg.CommandPrefix), + ConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix), + UnObserveConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix), LostVillagesCommand.WithPrefix(s.cfg.CommandPrefix), - ConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix)) + UnObserveLostVillagesCommand.WithPrefix(s.cfg.CommandPrefix), + ) s.SendEmbed(m.ChannelID, NewEmbed(). SetTitle("Pomoc"). @@ -85,6 +96,10 @@ func (s *Session) handleHelpCommand(m *discordgo.MessageCreate) { MessageEmbed) } +func (s *Session) handleAuthorCommand(m *discordgo.MessageCreate) { + s.SendMessage(m.ChannelID, fmt.Sprintf("%s Discord: Kichiyaki#2064.", m.Author.Mention())) +} + func (s *Session) handleTribeCommand(m *discordgo.MessageCreate, args ...string) { argsLength := len(args) if argsLength < 4 { @@ -230,6 +245,50 @@ func (s *Session) handleTribeCommand(m *discordgo.MessageCreate, args ...string) MessageEmbed) } +func (s *Session) handleConqueredVillagesCommand(m *discordgo.MessageCreate) { + if m.GuildID == "" { + return + } + if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { + return + } + + server := &models.Server{ + ID: m.GuildID, + } + err := s.cfg.ServerRepository.Store(context.Background(), server) + if err != nil { + return + } + server.ConqueredVillagesChannelID = m.ChannelID + go s.cfg.ServerRepository.Update(context.Background(), server) + s.SendMessage(m.ChannelID, + fmt.Sprintf("%s Pomyślnie zmieniono kanał na którym będą się wyświetlać informacje o podbitych wioskach.", m.Author.Mention())) +} + +func (s *Session) handleUnObserveConqueredVillagesCommand(m *discordgo.MessageCreate) { + if m.GuildID == "" { + return + } + if has, err := s.memberHasPermission(m.GuildID, m.Author.ID, discordgo.PermissionAdministrator); err != nil || !has { + return + } + + server := &models.Server{ + ID: m.GuildID, + } + err := s.cfg.ServerRepository.Store(context.Background(), server) + if err != nil { + return + } + if server.ConqueredVillagesChannelID != "" { + server.ConqueredVillagesChannelID = "" + go s.cfg.ServerRepository.Update(context.Background(), server) + } + s.SendMessage(m.ChannelID, + fmt.Sprintf("%s Informacje o podbitych wioskach nie będą się już pojawiały.", m.Author.Mention())) +} + func (s *Session) handleLostVillagesCommand(m *discordgo.MessageCreate) { if m.GuildID == "" { return @@ -251,7 +310,7 @@ func (s *Session) handleLostVillagesCommand(m *discordgo.MessageCreate) { fmt.Sprintf("%s Pomyślnie zmieniono kanał na którym będą się wyświetlać informacje o straconych wioskach.", m.Author.Mention())) } -func (s *Session) handleConqueredVillagesCommand(m *discordgo.MessageCreate) { +func (s *Session) handleUnObserveLostVillagesCommand(m *discordgo.MessageCreate) { if m.GuildID == "" { return } @@ -266,10 +325,12 @@ func (s *Session) handleConqueredVillagesCommand(m *discordgo.MessageCreate) { if err != nil { return } - server.ConqueredVillagesChannelID = m.ChannelID - go s.cfg.ServerRepository.Update(context.Background(), server) + if server.LostVillagesChannelID != "" { + server.LostVillagesChannelID = "" + go s.cfg.ServerRepository.Update(context.Background(), server) + } s.SendMessage(m.ChannelID, - fmt.Sprintf("%s Pomyślnie zmieniono kanał na którym będą się wyświetlać informacje o podbitych wioskach.", m.Author.Mention())) + fmt.Sprintf("%s Informacje o straconych wioskach nie będą się już pojawiały.", m.Author.Mention())) } func (s *Session) handleObserveCommand(m *discordgo.MessageCreate, args ...string) { diff --git a/discord/discord.go b/discord/discord.go index 9f68539..d0524c2 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -79,16 +79,22 @@ func (s *Session) handleNewMessage(_ *discordgo.Session, m *discordgo.MessageCre switch splitted[0] { case HelpCommand.WithPrefix(s.cfg.CommandPrefix): s.handleHelpCommand(m) + case AuthorCommand.WithPrefix(s.cfg.CommandPrefix): + s.handleAuthorCommand(m) case ObserveCommand.WithPrefix(s.cfg.CommandPrefix): s.handleObserveCommand(m, args...) case UnObserveCommand.WithPrefix(s.cfg.CommandPrefix): s.handleUnObserveCommand(m, args...) case ObservationsCommand.WithPrefix(s.cfg.CommandPrefix): s.handleObservationsCommand(m) - case LostVillagesCommand.WithPrefix(s.cfg.CommandPrefix): - s.handleLostVillagesCommand(m) case ConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix): s.handleConqueredVillagesCommand(m) + case UnObserveConqueredVillagesCommand.WithPrefix(s.cfg.CommandPrefix): + s.handleUnObserveConqueredVillagesCommand(m) + case LostVillagesCommand.WithPrefix(s.cfg.CommandPrefix): + s.handleLostVillagesCommand(m) + case UnObserveLostVillagesCommand.WithPrefix(s.cfg.CommandPrefix): + s.handleUnObserveLostVillagesCommand(m) case TribeCommand.WithPrefix(s.cfg.CommandPrefix): s.handleTribeCommand(m, args...) } diff --git a/models/observation.go b/models/observation.go index e650b61..4f6c6f8 100644 --- a/models/observation.go +++ b/models/observation.go @@ -1,10 +1,12 @@ package models type Observation struct { + tableName struct{} `pg:",alias:observation"` + ID int `json:"id" gqlgen:"id"` - World string `pg:"unique:group_1" json:"world" gqlgen:"world"` - TribeID int `pg:"unique:group_1" json:"tribeID" gqlgen:"tribeID"` - ServerID string `pg:"on_delete:CASCADE,unique:group_1" json:"serverID" gqlgen:"serverID"` + World string `pg:"unique:group_1,use_zero" json:"world" gqlgen:"world"` + TribeID int `pg:"unique:group_1,use_zero" json:"tribeID" gqlgen:"tribeID"` + ServerID string `pg:"on_delete:CASCADE,unique:group_1,use_zero" json:"serverID" gqlgen:"serverID"` Server *Server `json:"server,omitempty" gqlgen:"server"` } diff --git a/models/server.go b/models/server.go index eb88f2f..2b0a7e5 100644 --- a/models/server.go +++ b/models/server.go @@ -1,9 +1,11 @@ package models type Server struct { + tableName struct{} `pg:",alias:server"` + ID string `pg:",pk" json:"id" gqlgen:"id"` - ConqueredVillagesChannelID string `json:"conqueredVillagesChannelID" gqlgen:"conqueredVillagesChannelID"` - LostVillagesChannelID string `json:"lostVillagesChannelID" gqlgen:"lostVillagesChannelID"` + ConqueredVillagesChannelID string `pg:",use_zero" json:"conqueredVillagesChannelID" gqlgen:"conqueredVillagesChannelID"` + LostVillagesChannelID string `pg:",use_zero" json:"lostVillagesChannelID" gqlgen:"lostVillagesChannelID"` Observations Observations `json:"observation,omitempty" gqlgen:"observation"` } diff --git a/server/repository/server_pg_repo.go b/server/repository/server_pg_repo.go index 2116847..f4945c6 100644 --- a/server/repository/server_pg_repo.go +++ b/server/repository/server_pg_repo.go @@ -43,7 +43,7 @@ func (repo *pgRepo) Update(ctx context.Context, server *models.Server) error { WherePK(). Returning("*"). Context(ctx). - UpdateNotZero(); err != nil { + Update(); err != nil { return err } return nil