Skip to content

Commit 92576ac

Browse files
committed
Add stamp names to reply
1 parent 5c64269 commit 92576ac

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

pkg/bot/command.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func (c *CommandInstance) Execute(ctx domain.Context) error {
228228
err := cmd.Run()
229229
if err != nil {
230230
return ctx.ReplyFailure(
231-
fmt.Sprintf("exec failed: %v", err),
231+
fmt.Sprintf(":%s: exec failed: %v", ctx.StampNames().Failure, err),
232232
"```",
233233
utils.LimitLog(utils.SafeConvertString(buf.Bytes()), logLimit),
234234
"```",
@@ -237,6 +237,7 @@ func (c *CommandInstance) Execute(ctx domain.Context) error {
237237

238238
var replyMessage []string
239239
if buf.Len() > 0 {
240+
replyMessage = append(replyMessage, fmt.Sprintf(":%s:", ctx.StampNames().Success))
240241
replyMessage = append(replyMessage, "```")
241242
replyMessage = append(replyMessage, utils.LimitLog(utils.SafeConvertString(buf.Bytes()), logLimit))
242243
replyMessage = append(replyMessage, "```")

pkg/bot/slack/context.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ func (ctx *slackContext) MessageLimit() int {
4747
return 2500
4848
}
4949

50+
func (ctx *slackContext) StampNames() *domain.StampNames {
51+
return &domain.StampNames{
52+
BadCommand: config.C.Stamps.BadCommand,
53+
Forbid: config.C.Stamps.Forbid,
54+
Success: config.C.Stamps.Success,
55+
Failure: config.C.Stamps.Failure,
56+
Running: config.C.Stamps.Running,
57+
}
58+
}
59+
5060
func (ctx *slackContext) sendSlackMessage(channelID string, text string) error {
5161
api := ctx.api
5262
return utils.WithRetry(ctx, 10, func(ctx context.Context) error {

pkg/bot/traq/bot.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"fmt"
66
"github.com/kballard/go-shellquote"
7+
"github.com/samber/lo"
78
"github.com/traPtitech/DevOpsBot/pkg/config"
89
"github.com/traPtitech/DevOpsBot/pkg/domain"
10+
"github.com/traPtitech/go-traq"
911
traqwsbot "github.com/traPtitech/traq-ws-bot"
1012
"github.com/traPtitech/traq-ws-bot/payload"
1113
"go.uber.org/zap"
@@ -25,13 +27,37 @@ func NewBot(rootCmd domain.Command, logger *zap.Logger) (domain.Bot, error) {
2527
if err != nil {
2628
return nil, fmt.Errorf("creating bot: %w", err)
2729
}
28-
bot.OnMessageCreated(botMessageReceived(bot, logger, rootCmd))
30+
31+
stampNames, err := resolveStampNames(context.Background(), bot)
32+
if err != nil {
33+
return nil, fmt.Errorf("resolving stamp names: %w", err)
34+
}
35+
bot.OnMessageCreated(botMessageReceived(bot, logger, stampNames, rootCmd))
2936

3037
return &traqBot{
3138
bot: bot,
3239
}, nil
3340
}
3441

42+
func resolveStampNames(ctx context.Context, bot *traqwsbot.Bot) (*domain.StampNames, error) {
43+
stamps, _, err := bot.API().StampApi.GetStamps(ctx).Execute()
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
idToName := lo.SliceToMap(stamps, func(s traq.StampWithThumbnail) (string, string) {
49+
return s.Id, s.Name
50+
})
51+
52+
return &domain.StampNames{
53+
BadCommand: idToName[config.C.Stamps.BadCommand],
54+
Forbid: idToName[config.C.Stamps.Forbid],
55+
Success: idToName[config.C.Stamps.Success],
56+
Failure: idToName[config.C.Stamps.Failure],
57+
Running: idToName[config.C.Stamps.Running],
58+
}, nil
59+
}
60+
3561
func (b *traqBot) Start(_ context.Context) error {
3662
err := b.bot.Start()
3763
if err != nil {
@@ -41,7 +67,12 @@ func (b *traqBot) Start(_ context.Context) error {
4167
}
4268

4369
// botMessageReceived BOTのMESSAGE_CREATEDイベントハンドラ
44-
func botMessageReceived(bot *traqwsbot.Bot, logger *zap.Logger, rootCmd domain.Command) func(p *payload.MessageCreated) {
70+
func botMessageReceived(
71+
bot *traqwsbot.Bot,
72+
logger *zap.Logger,
73+
stampNames *domain.StampNames,
74+
rootCmd domain.Command,
75+
) func(p *payload.MessageCreated) {
4576
return func(p *payload.MessageCreated) {
4677
// Validate command execution context
4778
if p.Message.User.Bot {
@@ -57,10 +88,13 @@ func botMessageReceived(bot *traqwsbot.Bot, logger *zap.Logger, rootCmd domain.C
5788
// Prepare command args
5889
ctx := &traqContext{
5990
Context: context.Background(),
60-
api: bot.API(),
61-
logger: logger,
62-
p: p,
63-
args: nil,
91+
92+
api: bot.API(),
93+
logger: logger,
94+
stampNames: stampNames,
95+
96+
p: p,
97+
args: nil,
6498
}
6599
prefixStripped := strings.TrimPrefix(p.Message.PlainText, config.C.Prefix)
66100
args, err := shellquote.Split(prefixStripped)

pkg/bot/traq/context.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import (
1616
type traqContext struct {
1717
context.Context
1818

19-
api *traq.APIClient
20-
logger *zap.Logger
19+
api *traq.APIClient
20+
logger *zap.Logger
21+
stampNames *domain.StampNames
22+
2123
// p BOTが受信したMESSAGE_CREATEDイベントの生のペイロード
2224
p *payload.MessageCreated
2325
args []string
@@ -49,6 +51,10 @@ func (ctx *traqContext) MessageLimit() int {
4951
return 9900
5052
}
5153

54+
func (ctx *traqContext) StampNames() *domain.StampNames {
55+
return ctx.stampNames
56+
}
57+
5258
// sendTRAQMessage traQにメッセージ送信
5359
func (ctx *traqContext) sendTRAQMessage(channelID string, text string) error {
5460
api := ctx.api

pkg/domain/bot.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ type Bot interface {
1010
Start(ctx context.Context) error
1111
}
1212

13+
type StampNames struct {
14+
BadCommand string
15+
Forbid string
16+
Success string
17+
Failure string
18+
Running string
19+
}
20+
1321
// Context コマンド実行コンテキスト
1422
type Context interface {
1523
context.Context
@@ -23,7 +31,10 @@ type Context interface {
2331

2432
// L returns logger.
2533
L() *zap.Logger
34+
// MessageLimit returns reply character limit.
2635
MessageLimit() int
36+
// StampNames returns list of stamp names which can be used in raw text.
37+
StampNames() *StampNames
2738

2839
// ReplyBad コマンドメッセージにBadスタンプをつけて返信します
2940
ReplyBad(message ...string) error

0 commit comments

Comments
 (0)