MetalAdventures/src/discord/dice_plugin.rb

47 lines
1.5 KiB
Ruby
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class MetalAdventuresDiscord::DicePlugin
CUSTOM_PARSER = MetalAdventures::Dice.make_parser(prefix: "!roll ")
# https://unicode.org/emoji/charts/full-emoji-list.html
# EMOJI_ZERO = "\u0030".freeze
# EMOJI_ZERO = "\uFE0F".freeze
# EMOJI_ZERO = "\u20E3".freeze
EMOJI_ZERO = "0"
EMOJI_NUMS = Array.new(10) { |i| EMOJI_ZERO.unpack("UUU").tap { |unpacked| unpacked[0] += i }.pack("UUU") }
def initialize(bot)
@bot = bot
hook_roll!
end
def _error_syntax_embed
{
color: 0xFF0000,
title: "Mauvaise requête: !roll X</Y> <modifiers>",
description: "- X = nombre de dès,\n- /Y le nombre de succès à attendre,\n- <modifiers> = td, tf, sc, SC, e2f, pmf, plusieurs séparés par des espaces est autorisé",
}
end
def hook_roll!
@bot.message(with_text: /^!roll.+/) do |event|
if (dice = MetalAdventures::Dice.parse(event.message.content, parser: CUSTOM_PARSER))
dice.roll!
react_chars = dice.result.to_s.split("")
response_embed = {
title: "Rolled #{dice.result} success!",
description: dice.last_roll.join(' '),
}
if dice.description
response_embed[:footer] = { text: "Rolled for: #{dice.description}" }
end
event.message.reply! "", embed: response_embed
if react_chars.uniq.size == react_chars.size
react_chars.each { event.message.react EMOJI_NUMS[_1.to_i] }
end
else
event.message.reply! "", embed: _error_syntax_embed
end
end
end
end