Skip to content

Commit

Permalink
discord: BCDice プラグインの Discord 版を実装
Browse files Browse the repository at this point in the history
  • Loading branch information
koi-chan committed Feb 20, 2019
1 parent fc2a58d commit 1c059a4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ gem install mailcatcher
| [DiceRoll](doc/plugins/dice_roll.md) | ダイスロール | o | o |
| [RandomGenerator](doc/plugins/random_generator.md) | ランダムジェネレータ | o | o |
| [Trpg::Detatoko](doc/plugins/trpg/detatoko.md) | 「でたとこサーガ」専用のダイス・表引きコマンド | o | o |
| [BCDice](doc/plugins/bcdice.md) | [ボーンズ&カーズ](https://github.com/torgtaitai/BCDice) のダイスコマンドを利用する | o | x |
| [BCDice](doc/plugins/bcdice.md) | [ボーンズ&カーズ](https://github.com/torgtaitai/BCDice) のダイスコマンドを利用する | o | o |

#### 情報検索・引用

Expand Down
84 changes: 84 additions & 0 deletions lib/rgrb/plugin/bcdice/discord_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# vim: fileencoding=utf-8

require 'rgrb/discord_plugin'
require 'rgrb/plugin/bcdice/constants'
require 'rgrb/plugin/bcdice/errors'
require 'rgrb/plugin/bcdice/generator'

require 'BCDice/src/cgiDiceBot'
require 'BCDice/src/diceBot/DiceBotLoader'
require 'BCDice/src/diceBot/DiceBotLoaderList'

module RGRB
module Plugin
module Bcdice
# Bcdice の Discord アダプター
class DiscordAdapter
include RGRB::DiscordPlugin

set(plugin_name: 'Bcdice')
self.prefix = '.bcdice'
match(BCDICE_RE, method: :bcdice)
match(/-version/, method: :version)

def initialize(*args)
super

@generator = Generator.new
@header = 'BCDice'
end

# BCDice でダイスを振る
# @param [Cinch::Message] m 送信されたメッセージ
# @param [String] command ダイスコマンド
# @param [String] specified_game_title 指定されたゲームタイトル
# @return [void]
def bcdice(m, command, specified_game_title)
log_incoming(m)

# 共通のヘッダ
header_common = "#{@header}[#{m.user.mention}]"

result =
begin
@generator.bcdice(command, specified_game_title)
rescue => e
notice_bcdice_error(m.channel, header_common, e)
return
end

# ゲームシステム名を含むヘッダ
header = "#{header_common}<#{result.game_name}>: "

send_channel(m.channel, result.message_lines, header)
end

# git submodule で組み込んでいる BCDice のバージョンを出力する
# @param [Cinch::Message] m
# @return [void]
def version(m)
log_incoming(m)

message = @generator.bcdice_version

send_channel(m.channel, message)
end

private

# BCDice 関連のエラーを NOTICE する
# @param [Cinch::Target] target 送信先
# @param [String] header_common 共通のヘッダ
# @param [StandardError] error エラー
# @return [void]
def notice_bcdice_error(target, header_common, error)
header = error.respond_to?(:game_name) ?
"#{header_common}<#{error.game_name}>" : header_common
message = "#{header}: #{error.message}"

send_channel(target, message)
end
end
end
end
end

0 comments on commit 1c059a4

Please sign in to comment.