diff --git a/config/locales/en.yml b/config/locales/en.yml index 28dfe51..858c433 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -62,6 +62,7 @@ en: game_status: "There's [a game](%{game_url}) with *%{players} players*. Type /join to *join*." joined_game: "@%{username} joined the game. There are now %{players} players." + kick_user: "This user was kicked from the game." left_game: "@%{username} left the game. There are now *%{players}* players." location_set: "The location for the current game has been set" not_attending: "You're not attending the current game, @%{username}" diff --git a/lib/pickup_bot.rb b/lib/pickup_bot.rb index b8552dd..0411689 100644 --- a/lib/pickup_bot.rb +++ b/lib/pickup_bot.rb @@ -35,6 +35,8 @@ def handle_text(message) PickupBot::Commands::Join.run(telegram_bot, message) when /leave/ PickupBot::Commands::Leave.run(telegram_bot, message) + when /kick/ + PickupBot::Commands::Kick.run(telegram_bot, message) else PickupBot::Commands::Error.run(telegram_bot, message, exception) end diff --git a/lib/pickup_bot/commands.rb b/lib/pickup_bot/commands.rb index 9e31be9..d722185 100644 --- a/lib/pickup_bot/commands.rb +++ b/lib/pickup_bot/commands.rb @@ -6,6 +6,7 @@ module Commands require 'pickup_bot/commands/error' require 'pickup_bot/commands/help' require 'pickup_bot/commands/join' +require 'pickup_bot/commands/kick' require 'pickup_bot/commands/leave' require 'pickup_bot/commands/set_location' require 'pickup_bot/commands/status' diff --git a/lib/pickup_bot/commands/join.rb b/lib/pickup_bot/commands/join.rb index 9b3d984..2e01c47 100644 --- a/lib/pickup_bot/commands/join.rb +++ b/lib/pickup_bot/commands/join.rb @@ -12,6 +12,9 @@ def initialize(telegram_bot, message) def run if game_exists? current_player = Player.find_or_create_by(telegram_user_id: message.from.id) + unless current_player.username + current_player.update(username: username) + end attendence = Attendance.new(game: current_game, player: current_player) attendence.save telegram_bot.api.send_message( diff --git a/lib/pickup_bot/commands/kick.rb b/lib/pickup_bot/commands/kick.rb new file mode 100644 index 0000000..1a5adb2 --- /dev/null +++ b/lib/pickup_bot/commands/kick.rb @@ -0,0 +1,44 @@ +module Commands + class Kick + 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 + return destroy_user_attendance if user_attendance + end + + private + + def kick_username + @message.text.split(" ").second + end + + def current_game + Game.active.find_by_chat_id(@message.chat.id) + end + + def destroy_user_attendance + user_attendance.destroy + + @telegram_bot.api.send_message( + chat_id: @message.chat.id, + text: I18n.t("bot.kick_user") + ) + end + + def user_attendance + Attendance.find_by(game: current_game, player: kicked_player) + end + + def kicked_player + Player.find_by(username: kick_username) + end + + end +end diff --git a/spec/lib/pickup_bot/commands/kick_game_spec.rb b/spec/lib/pickup_bot/commands/kick_game_spec.rb new file mode 100644 index 0000000..c585fe2 --- /dev/null +++ b/spec/lib/pickup_bot/commands/kick_game_spec.rb @@ -0,0 +1,34 @@ +require "spec_helper" +require 'telegram/bot' +require 'pickup_bot' + +feature "kicking a user" do + let(:bot) { Telegram::Bot::Client.new('fake-token') } + let(:pickup_bot) { PickupBot.new(bot) } + let(:telegram_user) { Telegram::Bot::Types::User.new(user_params) } + let(:telegram_chat) { Telegram::Bot::Types::Chat.new(chat_params) } + let(:message) { Telegram::Bot::Types::Message.new(message_params("/kick OneTrickPony")) } + + before :each do + stub_request(:any, /api.telegram.org/).to_return(status: 200, body:"[]", :headers => {}) + end + + scenario "kick another user from a game" do + game = Game.create(chat_id: fake_chat_id, required_players: 4) + kicked_player = Player.create(telegram_user_id: 12, username: "OneTrickPony") + Attendance.create(game: game, player: kicked_player) + + pickup_bot.run(message) + + expect(a_request(:post, "https://api.telegram.org/botfake-token/sendMessage"). + with(body: { + "chat_id" => "123", + "text" => I18n.t( + "bot.kick_user" + ) + } + )).to have_been_made.times(1) + + expect(Attendance.count).to eq(0) + end +end