Skip to content

Commit

Permalink
goop: Use time.After instead of time.Sleep to break earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsAD committed Jul 19, 2018
1 parent 52b9389 commit ecb941c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
11 changes: 8 additions & 3 deletions cmd/goop/bnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

type bnetConfig struct {
ReconnectDelay time.Duration
FirstChannel string
HomeChannel string
CommandTrigger string
AvatarURL string

Expand Down Expand Up @@ -113,7 +113,12 @@ func (b *BNetRealm) Run(ctx context.Context) error {

if reconnect && ctx.Err() == nil {
b.Fire(&network.AsyncError{Src: "Run[Logon]", Err: err})
time.Sleep(backoff)

select {
case <-time.After(backoff):
case <-ctx.Done():
}

backoff = time.Duration(float64(backoff) * 1.5)
continue
}
Expand All @@ -125,7 +130,7 @@ func (b *BNetRealm) Run(ctx context.Context) error {

var channel = b.Channel()
if channel == "" {
channel = b.FirstChannel
channel = b.HomeChannel
}
if channel != "" {
b.Say("/join " + channel)
Expand Down
16 changes: 6 additions & 10 deletions cmd/goop/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ func (d *DiscordRealm) Run(ctx context.Context) error {
}

d.Fire(&network.AsyncError{Src: "Run[Open]", Err: err})
time.Sleep(2 * time.Minute)

select {
case <-time.After(2 * time.Minute):
case <-ctx.Done():
}
}

if err != nil {
Expand All @@ -121,7 +125,6 @@ func (d *DiscordRealm) Run(ctx context.Context) error {
func (d *DiscordRealm) InitDefaultHandlers() {
d.AddHandler(d.onConnect)
d.AddHandler(d.onDisconnect)
d.AddHandler(d.onPresenceUpdate)
d.AddHandler(d.onMessageCreate)
}

Expand All @@ -141,13 +144,6 @@ func (d *DiscordRealm) onDisconnect(s *discordgo.Session, msg *discordgo.Disconn
d.Fire(Disconnected{})
}

func (d *DiscordRealm) onPresenceUpdate(s *discordgo.Session, msg *discordgo.PresenceUpdate) {
old, _ := d.Session.State.Presence(msg.GuildID, msg.User.ID)
if old == nil || msg.Presence.Status != old.Status {
fmt.Println(msg)
}
}

func (d *DiscordRealm) onMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
if msg.Author.Bot {
return
Expand Down Expand Up @@ -290,7 +286,7 @@ func (c *DiscordChannel) Relay(ev *network.Event, sender string) {
case *PrivateChat:
err = c.WebhookOrSay(&discordgo.WebhookParams{
Content: c.filter(msg.Content, msg.User.Rank),
Username: fmt.Sprintf("[DM] %s@%s", msg.User.Name, sender),
Username: fmt.Sprintf("%s@%s (Direct Message)", msg.User.Name, sender),
AvatarURL: msg.User.AvatarURL,
})
default:
Expand Down
2 changes: 1 addition & 1 deletion cmd/goop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func main() {

if *makeconf {
if err := toml.NewEncoder(os.Stdout).Encode(conf); err != nil {
logErr.Fatal(err)
logErr.Fatal("Configuration encoding error: ", err)
}
return
}
Expand Down

0 comments on commit ecb941c

Please sign in to comment.