Skip to content

Commit

Permalink
Removed help message spam on startup.
Browse files Browse the repository at this point in the history
Adds two methods to database package that adds server IDs to joined
server set and checks if those IDs exist in the set.

Adds server joined logic to joinHandler. Adds dependency on the bot
object in joinHandler.
  • Loading branch information
MrFlynn committed Mar 18, 2021
1 parent d2e0307 commit 785a60c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
23 changes: 21 additions & 2 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/go-redis/redis/v7"
)

const joinedServerKey = "servers"

// Database type acts as the control interface for the Redis datastore.
type Database struct {
uri string
Expand Down Expand Up @@ -42,7 +44,7 @@ func New(uri string) Database {

// GetBestCandidateWord returns the best replacement synonym for supplied word.
// The return order should be in the defined lexeme order in Lexeme.go.
func (d Database) GetBestCandidateWord(word string) string {
func (d *Database) GetBestCandidateWord(word string) string {
results := make([]*redis.StringCmd, 4)

_, err := d.client.TxPipelined(func(pipe redis.Pipeliner) error {
Expand Down Expand Up @@ -73,7 +75,7 @@ func (d Database) GetBestCandidateWord(word string) string {
}

// WaitForReady waits for `ready` status message in `status` pubsub channel.
func (d Database) WaitForReady(timeout int) error {
func (d *Database) WaitForReady(timeout int) error {
if timeout == 0 {
log.Println("Skipping database check...")
return nil
Expand All @@ -96,3 +98,20 @@ func (d Database) WaitForReady(timeout int) error {
}
}
}

// AddJoinedServer adds a server to the `servers` set.
func (d *Database) AddJoinedServer(id string) error {
result := d.client.SAdd(joinedServerKey, id)
return result.Err()
}

// IsServerJoined checks if a specific guild is in the `servers` set.
func (d *Database) IsServerJoined(id string) (bool, error) {
result := d.client.SIsMember(joinedServerKey, id)

if result.Err() != nil {
return false, result.Err()
}

return true, nil
}
2 changes: 1 addition & 1 deletion internal/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func Run(ctx *cli.Context, skip bool) error {
}

bot.serviceHandler.AddHandler(bot.commandHandler)
bot.serviceHandler.AddHandler(joinHandler)
bot.serviceHandler.AddHandler(bot.joinHandler)
bot.serviceHandler.AddHandler(func(s *discordgo.Session, e *discordgo.Ready) {
s.UpdateGameStatus(0, "Reading a Thesaurus")
})
Expand Down
12 changes: 11 additions & 1 deletion internal/discord/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,27 @@ func mentionParser(s *discordgo.Session, u *discordgo.User, channelID string) (s
}
}

func joinHandler(s *discordgo.Session, c *discordgo.GuildCreate) {
func (b *bot) joinHandler(s *discordgo.Session, c *discordgo.GuildCreate) {
if c.Guild.Unavailable {
log.Printf("guild %s unavailable", c.Guild.Name)

return
}

joined, err := b.database.IsServerJoined(c.Guild.ID)
if err != nil {
log.Printf("Could not check if server was in joined set %s", err)
}

if joined {
return
}

for _, channel := range c.Guild.Channels {
if channel.Type == discordgo.ChannelTypeGuildText {
_, err := s.ChannelMessageSendEmbed(channel.ID, helpEmbed)
if err == nil {
b.database.AddJoinedServer(c.Guild.ID)
return // If channel delivery was successful, exit.
}
}
Expand Down

0 comments on commit 785a60c

Please sign in to comment.