Skip to content

Commit ecb941c

Browse files
committed
goop: Use time.After instead of time.Sleep to break earlier
1 parent 52b9389 commit ecb941c

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

cmd/goop/bnet.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
type bnetConfig struct {
2222
ReconnectDelay time.Duration
23-
FirstChannel string
23+
HomeChannel string
2424
CommandTrigger string
2525
AvatarURL string
2626

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

114114
if reconnect && ctx.Err() == nil {
115115
b.Fire(&network.AsyncError{Src: "Run[Logon]", Err: err})
116-
time.Sleep(backoff)
116+
117+
select {
118+
case <-time.After(backoff):
119+
case <-ctx.Done():
120+
}
121+
117122
backoff = time.Duration(float64(backoff) * 1.5)
118123
continue
119124
}
@@ -125,7 +130,7 @@ func (b *BNetRealm) Run(ctx context.Context) error {
125130

126131
var channel = b.Channel()
127132
if channel == "" {
128-
channel = b.FirstChannel
133+
channel = b.HomeChannel
129134
}
130135
if channel != "" {
131136
b.Say("/join " + channel)

cmd/goop/discord.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ func (d *DiscordRealm) Run(ctx context.Context) error {
104104
}
105105

106106
d.Fire(&network.AsyncError{Src: "Run[Open]", Err: err})
107-
time.Sleep(2 * time.Minute)
107+
108+
select {
109+
case <-time.After(2 * time.Minute):
110+
case <-ctx.Done():
111+
}
108112
}
109113

110114
if err != nil {
@@ -121,7 +125,6 @@ func (d *DiscordRealm) Run(ctx context.Context) error {
121125
func (d *DiscordRealm) InitDefaultHandlers() {
122126
d.AddHandler(d.onConnect)
123127
d.AddHandler(d.onDisconnect)
124-
d.AddHandler(d.onPresenceUpdate)
125128
d.AddHandler(d.onMessageCreate)
126129
}
127130

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

144-
func (d *DiscordRealm) onPresenceUpdate(s *discordgo.Session, msg *discordgo.PresenceUpdate) {
145-
old, _ := d.Session.State.Presence(msg.GuildID, msg.User.ID)
146-
if old == nil || msg.Presence.Status != old.Status {
147-
fmt.Println(msg)
148-
}
149-
}
150-
151147
func (d *DiscordRealm) onMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
152148
if msg.Author.Bot {
153149
return
@@ -290,7 +286,7 @@ func (c *DiscordChannel) Relay(ev *network.Event, sender string) {
290286
case *PrivateChat:
291287
err = c.WebhookOrSay(&discordgo.WebhookParams{
292288
Content: c.filter(msg.Content, msg.User.Rank),
293-
Username: fmt.Sprintf("[DM] %s@%s", msg.User.Name, sender),
289+
Username: fmt.Sprintf("%s@%s (Direct Message)", msg.User.Name, sender),
294290
AvatarURL: msg.User.AvatarURL,
295291
})
296292
default:

cmd/goop/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func main() {
8181

8282
if *makeconf {
8383
if err := toml.NewEncoder(os.Stdout).Encode(conf); err != nil {
84-
logErr.Fatal(err)
84+
logErr.Fatal("Configuration encoding error: ", err)
8585
}
8686
return
8787
}

0 commit comments

Comments
 (0)