Skip to content

Commit 2dac61e

Browse files
gitsangmoussetc
authored andcommitted
Roll reason support
1 parent 918ab1a commit 2dac61e

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ This Mattermost plugin adds a `/roll` slash command to roll all kinds of virtual
2727

2828
![demo](doc/demo_many_dice.png)
2929

30+
- Add roll reason at newline:
31+
32+
```
33+
/roll 3d1 +3 3d1
34+
your roll reason
35+
other roll reason
36+
```
37+
38+
![demo](doc/demo_reason.png)
39+
3040
- **[Up to version 3.0.x]** Add `sum` at the end to sum results automatically: `/roll 5 d8 13D20 sum`. In later versions, the sum is always displayed without having to add `sum`.
3141

3242

doc/demo_reason.png

10.3 KB
Loading

server/plugin.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ func (p *Plugin) generateDicePost(query, userID, channelID, rootID string) (*mod
9999
displayName = user.Username
100100
}
101101

102+
reason := ""
103+
queries := strings.SplitN(query, "\n", 2)
104+
if len(queries) == 2 {
105+
reason = queries[1]
106+
}
107+
query = queries[0]
108+
102109
text := fmt.Sprintf("**%s** rolls *%s* = ", displayName, query)
103110
sum := 0
104111
rollRequests := strings.Fields(query)
@@ -142,6 +149,11 @@ func (p *Plugin) generateDicePost(query, userID, channelID, rootID string) (*mod
142149
text += fmt.Sprintf("\n- %s", strings.Join(formattedRollDetails, "\n- "))
143150
}
144151

152+
// Display roll reason
153+
if reason != "" {
154+
text += fmt.Sprintf("\n```\n%s\n```", reason)
155+
}
156+
145157
return &model.Post{
146158
UserId: p.diceBotID,
147159
ChannelId: channelID,

server/plugin_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,42 @@ func TestGoodInputs(t *testing.T) {
8787
}
8888
}
8989

90+
func TestReason(t *testing.T) {
91+
p, api := initTestPlugin()
92+
var post *model.Post
93+
api.On("CreatePost", mock.AnythingOfType("*model.Post")).Return(nil, nil).Run(func(args mock.Arguments) {
94+
post = args.Get(0).(*model.Post)
95+
})
96+
assert.Nil(t, p.OnActivate())
97+
98+
testCases := []struct {
99+
inputDiceRequest string
100+
expectedText string
101+
}{
102+
{
103+
inputDiceRequest: "d1\nRoll reason",
104+
expectedText: "**User** rolls *d1* = **1**\n```\nRoll reason\n```",
105+
},
106+
{
107+
inputDiceRequest: "2d1\nRoll reason\nother reason",
108+
expectedText: "**User** rolls *2d1* = **2**\n- 2d1: 1 1\n```\nRoll reason\nother reason\n```",
109+
},
110+
}
111+
for _, testCase := range testCases {
112+
command := &model.CommandArgs{
113+
Command: "/roll " + testCase.inputDiceRequest,
114+
UserId: "userid",
115+
}
116+
response, err := p.ExecuteCommand(&plugin.Context{}, command)
117+
testLabel := "Testing " + testCase.inputDiceRequest
118+
assert.Nil(t, err, testLabel)
119+
assert.NotNil(t, response, testLabel)
120+
assert.NotNil(t, post, testLabel)
121+
assert.NotNil(t, post.Message, testLabel)
122+
assert.Equal(t, testCase.expectedText, strings.TrimSpace(post.Message), testLabel)
123+
}
124+
}
125+
90126
func initTestPlugin() (*Plugin, *plugintest.API) {
91127
api := &plugintest.API{}
92128
api.On("RegisterCommand", mock.Anything).Return(nil)

0 commit comments

Comments
 (0)