dice.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. def dice_list
  2. # Fetch all saved dice
  3. dice = DieArray.all
  4. fields = []
  5. # Insert each die into a field
  6. dice.each do |die|
  7. fields.push({ name: die.name, value: die.sides.join(", ") })
  8. end
  9. # Create Embed
  10. embed = Embed.new(
  11. title: "Available Dice",
  12. )
  13. # Update with fields, or a message of no dice, and reply
  14. fields.empty? ? embed.description = 'No dice found' : embed.fields = fields
  15. embed
  16. end
  17. def roll_results(author, hash, combine=false)
  18. # Create standard embed
  19. embed = Embed.new(
  20. author: {
  21. name: author.nickname || author.name,
  22. icon_url: author.avatar_url
  23. },
  24. color: author.color&.combined
  25. )
  26. case hash[:sides]
  27. when Integer
  28. # Fill the fields with results unless set to combine
  29. embed = fill_result_fields(hash[:results], embed) unless combine
  30. # Fill the description with tallied total, and footer with the rolled dice
  31. total = hash[:modifier].to_i + hash[:results].sum
  32. embed.description = "Rolled a total Result of **#{total}**"
  33. embed.footer = { text: "Rolled #{hash[:number]} D#{hash[:sides]}, modifier: #{hash[:modifier] || 'none'}" }
  34. when Array
  35. # Fill the fields with results, and add the die to the footer
  36. embed = fill_result_fields(hash[:results], embed)
  37. embed.footer = { text: "Rolled #{hash[:sides].join(', ')}" }
  38. end
  39. embed
  40. end
  41. def fill_result_fields(results, embed)
  42. fields = []
  43. results.each do |r|
  44. fields.push({ name: 'Result', value: "#{r}", inline: true })
  45. end
  46. embed.fields = fields
  47. embed
  48. end