@@ -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+
3561func (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 )
0 commit comments