Skip to content

Commit 1b60287

Browse files
committed
message: don't require target to format
1 parent db6b9b3 commit 1b60287

File tree

7 files changed

+57
-53
lines changed

7 files changed

+57
-53
lines changed

command/marriage.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ func Affection(ctx context.Context, robo *Robot, call *Invocation) {
8888
// Check for the broadcaster. They get special treatment.
8989
if strings.EqualFold(call.Message.Sender.Name, strings.TrimPrefix(call.Channel.Name, "#")) {
9090
if _, ok := call.Channel.Extra.LoadOrStore(broadcasterAffectionKey{}, struct{}{}); ok {
91-
call.Channel.Message(ctx, message.Format("", "Don't make me repeat myself, it's embarrassing! %s", e).AsReply(call.Message.ID))
91+
call.Channel.Message(ctx, message.Format("Don't make me repeat myself, it's embarrassing! %s", e).AsReply(call.Message.ID))
9292
return
9393
}
9494
const funnyMessage = `It's a bit awkward to think of you like that, streamer... But, well, it's so fun to be here, and I have you to thank for that! So I'd say a whole bunch! %s`
95-
call.Channel.Message(ctx, message.Format("", funnyMessage, e).AsReply(call.Message.ID))
95+
call.Channel.Message(ctx, message.Format(funnyMessage, e).AsReply(call.Message.ID))
9696
return
9797
}
9898
// possible!
99-
call.Channel.Message(ctx, message.Format("", "literally zero %s", e).AsReply(call.Message.ID))
99+
call.Channel.Message(ctx, message.Format("literally zero %s", e).AsReply(call.Message.ID))
100100
return
101101
}
102102
s := affections.Pick(rand.Uint32())
@@ -123,15 +123,15 @@ func Marry(ctx context.Context, robo *Robot, call *Invocation) {
123123
e := call.Channel.Emotes.Pick(rand.Uint32())
124124
broadcaster := strings.EqualFold(call.Message.Sender.Name, strings.TrimPrefix(call.Channel.Name, "#")) && x == 0
125125
if x < 10 && !broadcaster {
126-
call.Channel.Message(ctx, message.Format("", "no %s", e).AsReply(call.Message.ID))
126+
call.Channel.Message(ctx, message.Format("no %s", e).AsReply(call.Message.ID))
127127
return
128128
}
129129
me := &partner{who: call.Message.Sender.ID, until: call.Message.Time().Add(time.Hour)}
130130
for {
131131
l, ok := call.Channel.Extra.LoadOrStore(partnerKey{}, me)
132132
if !ok {
133133
// No competition. We're a shoo-in.
134-
call.Channel.Message(ctx, message.Format("", "sure why not %s", e).AsReply(call.Message.ID))
134+
call.Channel.Message(ctx, message.Format("sure why not %s", e).AsReply(call.Message.ID))
135135
return
136136
}
137137
cur := l.(*partner)
@@ -143,19 +143,19 @@ func Marry(ctx context.Context, robo *Robot, call *Invocation) {
143143
// but start over anyway.
144144
continue
145145
}
146-
call.Channel.Message(ctx, message.Format("", "How could you forget we're already together? I hate you! Unsubbed, unfollowed, unloved! %s", e).AsReply(call.Message.ID))
146+
call.Channel.Message(ctx, message.Format("How could you forget we're already together? I hate you! Unsubbed, unfollowed, unloved! %s", e).AsReply(call.Message.ID))
147147
return
148148
}
149-
call.Channel.Message(ctx, message.Format("", "We're already together, silly! You're so funny and cute haha. %s", e).AsReply(call.Message.ID))
149+
call.Channel.Message(ctx, message.Format("We're already together, silly! You're so funny and cute haha. %s", e).AsReply(call.Message.ID))
150150
return
151151
}
152152
if call.Message.Time().Before(cur.until) {
153-
call.Channel.Message(ctx, message.Format("", "My heart yet belongs to another... %s", e).AsReply(call.Message.ID))
153+
call.Channel.Message(ctx, message.Format("My heart yet belongs to another... %s", e).AsReply(call.Message.ID))
154154
return
155155
}
156156
y, _, _, _, _ := score(robo.Log, &call.Channel.History, cur.who)
157157
if x < y && !broadcaster {
158-
call.Channel.Message(ctx, message.Format("", "I'm touched, but I must decline. I'm in love with someone else. %s", e).AsReply(call.Message.ID))
158+
call.Channel.Message(ctx, message.Format("I'm touched, but I must decline. I'm in love with someone else. %s", e).AsReply(call.Message.ID))
159159
return
160160
}
161161
if !call.Channel.Extra.CompareAndSwap(partnerKey{}, cur, me) {
@@ -165,9 +165,9 @@ func Marry(ctx context.Context, robo *Robot, call *Invocation) {
165165
// We win. Now just decide which message to send.
166166
// TODO(zeph): since pick.Dist exists now, we could randomize
167167
if call.Args["partnership"] != "" {
168-
call.Channel.Message(ctx, message.Format("", "Yes! I'll be your %s! %s", call.Args["partnership"], e).AsReply(call.Message.ID))
168+
call.Channel.Message(ctx, message.Format("Yes! I'll be your %s! %s", call.Args["partnership"], e).AsReply(call.Message.ID))
169169
} else {
170-
call.Channel.Message(ctx, message.Format("", "Yes! I'll marry you! %s", e).AsReply(call.Message.ID))
170+
call.Channel.Message(ctx, message.Format("Yes! I'll marry you! %s", e).AsReply(call.Message.ID))
171171
}
172172
return
173173
}
@@ -177,7 +177,7 @@ func Marry(ctx context.Context, robo *Robot, call *Invocation) {
177177
// No args.
178178
func DescribeMarriage(ctx context.Context, robo *Robot, call *Invocation) {
179179
if t := call.Channel.SilentTime(); call.Message.Time().Before(t) {
180-
call.Channel.Message(ctx, message.Format("", "I'm being quiet for the next %v, so the marriage system is disabled until then.", t.Sub(call.Message.Time())).AsReply(call.Message.ID))
180+
call.Channel.Message(ctx, message.Format("I'm being quiet for the next %v, so the marriage system is disabled until then.", t.Sub(call.Message.Time())).AsReply(call.Message.ID))
181181
return
182182
}
183183
const s = `I am looking for a long series of short-term relationships and am holding a ranked competitive how-much-I-like-you tournament to decide my suitors! Politely ask me to marry you (or become your partner) and I'll evaluate your score. I like copypasta, memes, and long walks in the chat.`

command/moderate.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ func Forget(ctx context.Context, robo *Robot, call *Invocation) {
3939
var r message.Sent
4040
switch n {
4141
case 0:
42-
r = message.Format("", "No messages contained %q.", term)
42+
r = message.Format("No messages contained %q.", term)
4343
case 1:
44-
r = message.Format("", "Forgot 1 message.")
44+
r = message.Format("Forgot 1 message.")
4545
default:
46-
r = message.Format("", "Forgot %d messages.", n)
46+
r = message.Format("Forgot %d messages.", n)
4747
}
4848
call.Channel.Message(ctx, r.AsReply(call.Message.ID))
4949
}
@@ -75,7 +75,7 @@ func Quiet(ctx context.Context, robo *Robot, call *Invocation) {
7575
n, err := strconv.Atoi(m[1])
7676
if err != nil {
7777
// Should be impossible.
78-
call.Channel.Message(ctx, message.Format("", `sorry? (%v)`, err).AsReply(call.Message.ID))
78+
call.Channel.Message(ctx, message.Format(`sorry? (%v)`, err).AsReply(call.Message.ID))
7979
return
8080
}
8181
switch m[2][0] {
@@ -89,7 +89,7 @@ func Quiet(ctx context.Context, robo *Robot, call *Invocation) {
8989
var err error
9090
dur, err = time.ParseDuration(call.Args["dur"])
9191
if err != nil {
92-
call.Channel.Message(ctx, message.Format("", `sorry? (%v)`, err).AsReply(call.Message.ID))
92+
call.Channel.Message(ctx, message.Format(`sorry? (%v)`, err).AsReply(call.Message.ID))
9393
return
9494
}
9595
}
@@ -102,7 +102,7 @@ func Quiet(ctx context.Context, robo *Robot, call *Invocation) {
102102
// Only do the spiel if the timer isn't very short.
103103
// Otherwise it's likely just clearing an existing silent time.
104104
if dur > 5*time.Second {
105-
call.Channel.Message(ctx, message.Format("", `I won't talk or learn for %v. Some commands relating to moderation and privacy will still make me talk. I'll mention when quiet time is up.`, dur).AsReply(call.Message.ID))
105+
call.Channel.Message(ctx, message.Format(`I won't talk or learn for %v. Some commands relating to moderation and privacy will still make me talk. I'll mention when quiet time is up.`, dur).AsReply(call.Message.ID))
106106
}
107107
t := time.NewTimer(dur)
108108
defer t.Stop()
@@ -116,7 +116,7 @@ func Quiet(ctx context.Context, robo *Robot, call *Invocation) {
116116
if call.Channel.Silent.Load() != n {
117117
return
118118
}
119-
call.Channel.Message(ctx, message.Format("", `@%s My quiet time has ended.`, call.Message.Sender.Name))
119+
call.Channel.Message(ctx, message.Format(`@%s My quiet time has ended.`, call.Message.Sender.Name))
120120
}
121121
}
122122

command/privacy.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ func Private(ctx context.Context, robo *Robot, call *Invocation) {
1212
err := robo.Privacy.Add(ctx, call.Message.Sender.ID)
1313
if err != nil {
1414
robo.Log.ErrorContext(ctx, "privacy add failed", slog.Any("err", err), slog.String("channel", call.Channel.Name))
15-
call.Channel.Message(ctx, message.Format("", "Something went wrong while trying to add you to the privacy list. Try again. Sorry!").AsReply(call.Message.ID))
15+
call.Channel.Message(ctx, message.Format("Something went wrong while trying to add you to the privacy list. Try again. Sorry!").AsReply(call.Message.ID))
1616
return
1717
}
1818
e := call.Channel.Emotes.Pick(rand.Uint32())
19-
call.Channel.Message(ctx, message.Format("", `Sure, I won't learn from your messages. Most of my functionality will still work for you. If you'd like to have me learn from you again, just tell me, "learn from me again." %s`, e).AsReply(call.Message.ID))
19+
call.Channel.Message(ctx, message.Format(`Sure, I won't learn from your messages. Most of my functionality will still work for you. If you'd like to have me learn from you again, just tell me, "learn from me again." %s`, e).AsReply(call.Message.ID))
2020
}
2121

2222
func Unprivate(ctx context.Context, robo *Robot, call *Invocation) {
2323
err := robo.Privacy.Remove(ctx, call.Message.Sender.ID)
2424
if err != nil {
2525
robo.Log.ErrorContext(ctx, "privacy remove failed", slog.Any("err", err), slog.String("channel", call.Channel.Name))
26-
call.Channel.Message(ctx, message.Format("", "Something went wrong while trying to add you to the privacy list. Try again. Sorry!").AsReply(call.Message.ID))
26+
call.Channel.Message(ctx, message.Format("Something went wrong while trying to add you to the privacy list. Try again. Sorry!").AsReply(call.Message.ID))
2727
return
2828
}
2929
e := call.Channel.Emotes.Pick(rand.Uint32())
30-
call.Channel.Message(ctx, message.Format("", `Sure, I'll learn from you again! %s`, e).AsReply(call.Message.ID))
30+
call.Channel.Message(ctx, message.Format(`Sure, I'll learn from you again! %s`, e).AsReply(call.Message.ID))
3131
}
3232

3333
func DescribePrivacy(ctx context.Context, robo *Robot, call *Invocation) {
3434
// TODO(zeph): describe privacy
35-
call.Channel.Message(ctx, message.Format("", `See here for a description of what information I collect, and how to opt out of all collection: https://github.com/zephyrtronium/robot#what-data-does-robot-store`).AsReply(call.Message.ID))
35+
call.Channel.Message(ctx, message.Format(`See here for a description of what information I collect, and how to opt out of all collection: https://github.com/zephyrtronium/robot#what-data-does-robot-store`).AsReply(call.Message.ID))
3636
}

command/talk.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func Rawr(ctx context.Context, robo *Robot, call *Invocation) {
142142
r.CancelAt(t)
143143
return
144144
}
145-
call.Channel.Message(ctx, message.Format("", "rawr %s", e).AsReply(call.Message.ID))
145+
call.Channel.Message(ctx, message.Format("rawr %s", e).AsReply(call.Message.ID))
146146
}
147147

148148
// HappyBirthdayToYou wishes the robot a happy birthday.
@@ -169,32 +169,32 @@ func HappyBirthdayToYou(ctx context.Context, robo *Robot, call *Invocation) {
169169
var m message.Sent
170170
switch t.Month() {
171171
case time.January:
172-
m = message.Format("", "No no no my birthday is next month. %s", e)
172+
m = message.Format("No no no my birthday is next month. %s", e)
173173
case time.February:
174174
switch t.Day() {
175175
case 1, 2, 3, 4, 5:
176-
m = message.Format("", "Oh, but my birthday is later this month. %s", e)
176+
m = message.Format("Oh, but my birthday is later this month. %s", e)
177177
case 6:
178-
m = message.Format("", "My birthday is just a week away! I am so excited about this information. %s", e)
178+
m = message.Format("My birthday is just a week away! I am so excited about this information. %s", e)
179179
case 7, 8, 9, 10:
180-
m = message.Format("", "My birthday is still less than a week away. %s", e)
180+
m = message.Format("My birthday is still less than a week away. %s", e)
181181
case 11:
182-
m = message.Format("", "Two days away...! %s", e)
182+
m = message.Format("Two days away...! %s", e)
183183
case 12:
184-
m = message.Format("", "My birthday is tomorrow! At least in my timezone. %s", e)
184+
m = message.Format("My birthday is tomorrow! At least in my timezone. %s", e)
185185
case 13:
186-
m = message.Format("", "Thank you! Happy my birthday to you, too! %s", e)
186+
m = message.Format("Thank you! Happy my birthday to you, too! %s", e)
187187
case 14:
188-
m = message.Format("", "You missed it. My birthday was yesterday. You are disqualified from being my valentine. %s", e)
188+
m = message.Format("You missed it. My birthday was yesterday. You are disqualified from being my valentine. %s", e)
189189
case 15, 16, 17, 18, 19, 20:
190-
m = message.Format("", "My birthday was the other day, actually, but I appreciate the sentiment. %s", e)
190+
m = message.Format("My birthday was the other day, actually, but I appreciate the sentiment. %s", e)
191191
default:
192-
m = message.Format("", "My birthday was earlier this month, actually, but I appreciate the sentiment. %s", e)
192+
m = message.Format("My birthday was earlier this month, actually, but I appreciate the sentiment. %s", e)
193193
}
194194
case time.March:
195-
m = message.Format("", "No no no my birthday was last month. %s", e)
195+
m = message.Format("No no no my birthday was last month. %s", e)
196196
default:
197-
m = message.Format("", "My birthday is in February, silly %s", e)
197+
m = message.Format("My birthday is in February, silly %s", e)
198198
}
199199
call.Channel.Message(ctx, m.AsReply(call.Message.ID))
200200
}
@@ -211,11 +211,11 @@ func Source(ctx context.Context, robo *Robot, call *Invocation) {
211211
func Who(ctx context.Context, robo *Robot, call *Invocation) {
212212
const whoMessage = `I'm a Markov chain bot! I learn from things people say in chat, then spew vaguely intelligible memes back. More info at: https://github.com/zephyrtronium/robot#how-robot-works %s`
213213
e := call.Channel.Emotes.Pick(rand.Uint32())
214-
call.Channel.Message(ctx, message.Format("", whoMessage, e).AsReply(call.Message.ID))
214+
call.Channel.Message(ctx, message.Format(whoMessage, e).AsReply(call.Message.ID))
215215
}
216216

217217
// Contact gives information on how to contact the bot owner.
218218
func Contact(ctx context.Context, robo *Robot, call *Invocation) {
219219
e := call.Channel.Emotes.Pick(rand.Uint32())
220-
call.Channel.Message(ctx, message.Format("", "My operator is %[1]s. %[2]s is the best way to contact %[1]s. %[3]s", robo.Owner, robo.Contact, e).AsReply(call.Message.ID))
220+
call.Channel.Message(ctx, message.Format("My operator is %[1]s. %[2]s is the best way to contact %[1]s. %[3]s", robo.Owner, robo.Contact, e).AsReply(call.Message.ID))
221221
}

command/tamagotchi.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func Tamagotchi(ctx context.Context, robo *Robot, call *Invocation) {
6666
e := call.Channel.Emotes.Pick(rand.Uint32())
6767
sat := robo.Pet.Satisfaction(call.Message.Time())
6868
_, m := satmsg(sat)
69-
call.Channel.Message(ctx, message.Format("", "%s %s", m, e).AsReply(call.Message.ID))
69+
call.Channel.Message(ctx, message.Format("%s %s", m, e).AsReply(call.Message.ID))
7070
}
7171

7272
type dinner struct {
@@ -154,12 +154,12 @@ func Eat(ctx context.Context, robo *Robot, call *Invocation) {
154154
)
155155
if !ok {
156156
s := fullmsgs.Pick(rand.Uint32())
157-
call.Channel.Message(ctx, message.Format("", "%s %s", s, e).AsReply(call.Message.ID))
157+
call.Channel.Message(ctx, message.Format("%s %s", s, e).AsReply(call.Message.ID))
158158
return
159159
}
160160
c, m := satmsg(sat)
161161
chew := chewmsgs.Pick(rand.Uint32())
162-
call.Channel.Message(ctx, message.Format("", "%s %s %s %s %s%s %s %s", chew[0], menu[0].name, menu[1].name, menu[2].name, chew[1], c, m, e).AsReply(call.Message.ID))
162+
call.Channel.Message(ctx, message.Format("%s %s %s %s %s%s %s %s", chew[0], menu[0].name, menu[1].name, menu[2].name, chew[1], c, m, e).AsReply(call.Message.ID))
163163
}
164164

165165
var cleancounts = pick.New([]pick.Case[int]{
@@ -208,15 +208,15 @@ func Clean(ctx context.Context, robo *Robot, call *Invocation) {
208208
var msg message.Sent
209209
switch len(rooms) {
210210
case 0:
211-
msg = message.Format("", "Everything's already clean! %s %s", m, e)
211+
msg = message.Format("Everything's already clean! %s %s", m, e)
212212
case 1:
213-
msg = message.Format("", "%s %s%s Now %s %s", clean[0], rooms[0], clean[1], m, e)
213+
msg = message.Format("%s %s%s Now %s %s", clean[0], rooms[0], clean[1], m, e)
214214
case 2:
215-
msg = message.Format("", "%s %s and %s%s Now %s %s", clean[0], rooms[0], rooms[1], clean[1], m, e)
215+
msg = message.Format("%s %s and %s%s Now %s %s", clean[0], rooms[0], rooms[1], clean[1], m, e)
216216
case 3:
217-
msg = message.Format("", "%s %s, %s, and %s%s Now %s %s", clean[0], rooms[0], rooms[1], rooms[2], clean[1], m, e)
217+
msg = message.Format("%s %s, %s, and %s%s Now %s %s", clean[0], rooms[0], rooms[1], rooms[2], clean[1], m, e)
218218
case 4:
219-
msg = message.Format("", "%s whole home%s Now %s %s", clean[0], clean[1], m, e)
219+
msg = message.Format("%s whole home%s Now %s %s", clean[0], clean[1], m, e)
220220
}
221221
call.Channel.Message(ctx, msg.AsReply(call.Message.ID))
222222
}
@@ -280,5 +280,5 @@ func Pat(ctx context.Context, robo *Robot, call *Invocation) {
280280
)
281281
sat := robo.Pet.Pat(call.Message.Time(), pat.love)
282282
_, m := satmsg(sat)
283-
call.Channel.Message(ctx, message.Format("", "%s %s %s", pat.where, m, e).AsReply(call.Message.ID))
283+
call.Channel.Message(ctx, message.Format("%s %s %s", pat.where, m, e).AsReply(call.Message.ID))
284284
}

message/message.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type Sent struct {
4949
// Reply is a message to reply to. If empty, the message is not interpreted
5050
// as a reply.
5151
Reply string
52-
// To is the channel to whom the message is sent.
52+
// To is the channel to which the message is sent.
5353
To string
5454
// Text is the message text.
5555
Text string
@@ -60,14 +60,18 @@ func (m Sent) AsReply(reply string) Sent {
6060
return m
6161
}
6262

63+
func (m Sent) SendTo(to string) Sent {
64+
m.To = to
65+
return m
66+
}
67+
6368
// formatString is a type to prevent misuse of format strings passed to [Format].
6469
type formatString string
6570

6671
// Format constructs a message to send from a format string literal and
6772
// formatting arguments.
68-
func Format(to string, f formatString, args ...any) Sent {
73+
func Format(f formatString, args ...any) Sent {
6974
return Sent{
70-
To: to,
7175
Text: strings.TrimSpace(fmt.Sprintf(string(f), args...)),
7276
}
7377
}

privmsg.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (robo *Robot) tmiMessage(ctx context.Context, send chan<- *tmi.Message, msg
107107
slog.String("text", s),
108108
slog.String("effect", f),
109109
)
110-
msg := message.Format(ch.Name, "%s", s)
110+
msg := message.Format("%s", s).SendTo(ch.Name)
111111
robo.sendTMI(ctx, send, msg)
112112
return
113113
default:
@@ -165,7 +165,7 @@ func (robo *Robot) tmiMessage(ctx context.Context, send chan<- *tmi.Message, msg
165165
r.CancelAt(t)
166166
return
167167
}
168-
out := message.Format(ch.Name, "%s", sef)
168+
out := message.Format("%s", sef).SendTo(ch.Name)
169169
robo.sendTMI(ctx, send, out)
170170
}
171171

0 commit comments

Comments
 (0)