Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ This Mattermost plugin adds a `/roll` slash command to roll all kinds of virtual

![demo](doc/demo_many_dice.png)

- Add roll reason at newline:

```
/roll 3d1 +3 3d1
your roll reason
other roll reason
```

![demo](doc/demo_reason.png)

- **[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`.


Expand Down
Binary file added doc/demo_reason.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ func (p *Plugin) generateDicePost(query, userID, channelID, rootID string) (*mod
displayName = user.Username
}

reason := ""
queries := strings.SplitN(query, "\n", 2)
if len(queries) == 2 {
reason = queries[1]
}
query = queries[0]

text := fmt.Sprintf("**%s** rolls *%s* = ", displayName, query)
sum := 0
rollRequests := strings.Fields(query)
Expand Down Expand Up @@ -142,6 +149,11 @@ func (p *Plugin) generateDicePost(query, userID, channelID, rootID string) (*mod
text += fmt.Sprintf("\n- %s", strings.Join(formattedRollDetails, "\n- "))
}

// Display roll reason
if reason != "" {
text += fmt.Sprintf("\n```\n%s\n```", reason)
Copy link
Copy Markdown
Owner

@moussetc moussetc May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering, is it be better to show the roll reason before or after the roll? Eg.
image
VS
image

Instinctively, I think I would prefer have it before but may be missing something!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't seem to care about this; it makes no difference to me. :)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another look and feel question: why the codeblock with empty lines around?
Codeblock means we lose any formatting that might be added to the roll, and it takes a lot more height. I see you use a compact mode of chat display, so maybe that is on purpose to better read the rolls, in which case, I'd like to mention that as a plugin, we can do nifty attachments such as:
image

You'll find more examples on this documentation page. Do tell if that looks interesting for your use case.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the codeblock with empty lines around?

This might just be my habit—my Markdown auto-formatting and linting tools (based on MD031 rule) require me to add blank lines around code blocks.

I'd like to mention that as a plugin, we can do nifty attachments

This looks really cool—I wasn’t aware of this plugin before. It appears much better-looking than Codeblock. Maybe I’ll switch from Codeblock to Message Attachments later, though it’s probably not a priority for me right now (for various reasons, In fact, it's been a long time since I last played an RPG.).

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all your answers.
I can take over the PR to switch to Message Attachments, if that's ok with you.

Copy link
Copy Markdown
Author

@gitsang gitsang May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is certainly ok, thank you for the effort. If you have submitted the code, feel free to close this PR.

}

return &model.Post{
UserId: p.diceBotID,
ChannelId: channelID,
Expand Down
36 changes: 36 additions & 0 deletions server/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,42 @@ func TestGoodInputs(t *testing.T) {
}
}

func TestReason(t *testing.T) {
p, api := initTestPlugin()
var post *model.Post
api.On("CreatePost", mock.AnythingOfType("*model.Post")).Return(nil, nil).Run(func(args mock.Arguments) {
post = args.Get(0).(*model.Post)
})
assert.Nil(t, p.OnActivate())

testCases := []struct {
inputDiceRequest string
expectedText string
}{
{
inputDiceRequest: "d1\nRoll reason",
expectedText: "**User** rolls *d1* = **1**\n```\nRoll reason\n```",
},
{
inputDiceRequest: "2d1\nRoll reason\nother reason",
expectedText: "**User** rolls *2d1* = **2**\n- 2d1: 1 1\n```\nRoll reason\nother reason\n```",
},
}
for _, testCase := range testCases {
command := &model.CommandArgs{
Command: "/roll " + testCase.inputDiceRequest,
UserId: "userid",
}
response, err := p.ExecuteCommand(&plugin.Context{}, command)
testLabel := "Testing " + testCase.inputDiceRequest
assert.Nil(t, err, testLabel)
assert.NotNil(t, response, testLabel)
assert.NotNil(t, post, testLabel)
assert.NotNil(t, post.Message, testLabel)
assert.Equal(t, testCase.expectedText, strings.TrimSpace(post.Message), testLabel)
}
}

func initTestPlugin() (*Plugin, *plugintest.API) {
api := &plugintest.API{}
api.On("RegisterCommand", mock.Anything).Return(nil)
Expand Down