-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjoin.rb
57 lines (49 loc) · 1.33 KB
/
join.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module PickupBot::Commands
class Join
def self.run(telegram_bot, message)
new(telegram_bot, message).run
end
def initialize(telegram_bot, message)
@telegram_bot = telegram_bot
@message = message
end
def run
if game_exists?
current_player = Player.find_or_create_by(telegram_user_id: message.from.id)
attendence = Attendance.new(game: current_game, player: current_player)
attendence.save
telegram_bot.api.send_message(
chat_id: message.chat.id,
text: I18n.t(
"bot.joined_game",
username: username,
players: players
)
)
else
telegram_bot.api.send_message(
chat_id: message.chat.id,
text: I18n.t("bot.no_game", username: username)
)
end
end
private
attr_reader :telegram_bot, :message
def game_exists?
Game.active.exists?(chat_id: @message.chat.id)
end
def current_game
Game.active.find_by_chat_id(@message.chat.id)
end
def players
if current_game.required_players > 0
"#{current_game.players.count} / #{current_game.required_players}"
else
"#{current_game.players.count}"
end
end
def username
message.from.username || message.from.first_name
end
end
end