Skip to content

Commit

Permalink
Turn GIFs on/off with set gifs on|off. Closes #57.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Jan 3, 2016
1 parent d3d6f1c commit dcd5d2f
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Changelog

* [#57](https://github.com/dblock/slack-gamebot/issues/57): Turn GIFs on/off with `set gifs on|off` - [@dblock](https://github.com/dblock).
* [#70](https://github.com/dblock/slack-gamebot/issues/70): Added an optional limit to `matches` - [@dblock](https://github.com/dblock).
* [#71](https://github.com/dblock/slack-gamebot/issues/71): Allow scores separated by dashes and commas - [@dblock](https://github.com/dblock).
* [#67](https://github.com/dblock/slack-gamebot/issues/67): Allow resetting seasons with team ID - [@dblock](https://github.com/dblock).
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source 'http://rubygems.org'

ruby '2.2.4'

gem 'slack-ruby-bot', '~> 0.5.3'
gem 'slack-ruby-bot', '~> 0.5.4'
gem 'mongoid', '~> 5.0.0'
gem 'ruby-enum'
gem 'puma'
Expand All @@ -17,6 +17,7 @@ gem 'rack-robotz'
gem 'newrelic_rpm'
gem 'newrelic-slack-ruby-bot'
gem 'rack-rewrite'
gem 'wannabe_bool'

group :development, :test do
gem 'rake', '~> 10.4'
Expand Down
10 changes: 6 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ GEM
newrelic_rpm (3.14.1.311)
nokogiri (1.6.7.1)
mini_portile2 (~> 2.0.0.rc2)
oj (2.14.2)
oj (2.14.3)
origin (2.1.1)
parser (2.2.3.0)
ast (>= 1.1, < 3.0)
Expand Down Expand Up @@ -219,7 +219,7 @@ GEM
ruby-progressbar (1.7.5)
rubyzip (1.1.7)
safe_yaml (1.0.4)
slack-ruby-bot (0.5.3)
slack-ruby-bot (0.5.4)
activesupport
faye-websocket
giphy (~> 2.0.2)
Expand All @@ -244,6 +244,7 @@ GEM
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)
wannabe_bool (0.4.0)
webmock (1.22.3)
addressable (>= 2.3.6)
crack (>= 0.3.2)
Expand Down Expand Up @@ -280,10 +281,11 @@ DEPENDENCIES
rspec
rubocop (= 0.34.2)
ruby-enum
slack-ruby-bot (~> 0.5.3)
slack-ruby-bot (~> 0.5.4)
time_ago_in_words
vcr
wannabe_bool
webmock

BUNDLED WITH
1.10.6
1.11.2
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ gamebot reset china
Welcome to the new season!
```

#### gamebot set gifs on|off

Enable/disable GIFs for your team.

```
gamebot set gifs off
Disabled GIFs for team China, thanks captain!
```

#### gamebot season

Display current season's leader and game totals.
Expand Down
1 change: 1 addition & 0 deletions slack-gamebot/api/presenters/team_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module TeamPresenter
property :name, type: String, desc: 'Team name.'
property :domain, type: String, desc: 'Team domain.'
property :active, type: Boolean, desc: 'Team is active.'
property :gifs, type: Boolean, desc: 'Team loves animated GIFs.'
property :created_at, type: DateTime, desc: 'Date/time when the team was created.'
property :updated_at, type: DateTime, desc: 'Date/time when the team was accepted, declined or canceled.'

Expand Down
5 changes: 5 additions & 0 deletions slack-gamebot/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def prepare!
migrate_from_single_game!
ensure_a_team_game!
purge_inactive_teams!
set_team_gifs_default!
end

def self.instance
Expand Down Expand Up @@ -93,5 +94,9 @@ def ensure_a_team_game!
def purge_inactive_teams!
Team.purge!
end

def set_team_gifs_default!
Team.where(gifs: nil).update_all(gifs: true)
end
end
end
1 change: 1 addition & 0 deletions slack-gamebot/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
require 'slack-gamebot/commands/promote'
require 'slack-gamebot/commands/demote'
require 'slack-gamebot/commands/team'
require 'slack-gamebot/commands/set'
1 change: 1 addition & 0 deletions slack-gamebot/commands/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Help < SlackRubyBot::Commands::Base
Captains
--------
set gifs on|off: enable/disable animated GIFs
reset <team>: reset all stats, start a new season
promote <player>: promote a user to captain
demote me: demote you from captain
Expand Down
33 changes: 33 additions & 0 deletions slack-gamebot/commands/set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module SlackGamebot
module Commands
class Set < SlackRubyBot::Commands::Base
def self.call(client, data, match)
user = ::User.find_create_or_update_by_slack_id!(client, data.user)
if !user.captain?
send_message_with_gif client, data.channel, "You're not a captain, sorry.", 'sorry'
logger.info "SET: #{client.team} - #{user.user_name}, failed, not captain"
elsif !match.names.include?('expression')
send_message_with_gif client, data.channel, 'Missing setting, eg. _set gifs off_.', 'help'
logger.info "SET: #{client.team} - #{user.user_name}, failed, missing setting"
else
k, v = match['expression'].split(/\W/, 2)
case k
when 'gifs'
client.team.update_attributes!(gifs: v.to_b)
client.send_gifs = client.team.gifs
if client.team.gifs?
send_message_with_gif client, data.channel, "Enabled GIFs for team #{client.team.name}, thanks captain!", 'fun'
logger.info "SET: #{client.team} - #{user.user_name} enabled GIFs."
else
send_message_with_gif client, data.channel, "Disabled GIFs for team #{client.team.name}, thanks captain!", 'boring'
logger.info "SET: #{client.team} - #{user.user_name} disabled GIFs."
end
else
send_message_with_gif client, data.channel, "Invalid setting #{k}, you can _set gifs on|off_.", 'help'
logger.info "SET: #{client.team} - #{user.user_name}, failed, invalid setting #{k}"
end
end
end
end
end
end
1 change: 1 addition & 0 deletions slack-gamebot/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Team
field :domain, type: String
field :token, type: String
field :active, type: Boolean, default: true
field :gifs, type: Boolean, default: true

scope :active, -> { where(active: true) }

Expand Down
1 change: 1 addition & 0 deletions slack-gamebot/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(attrs = {})
fail 'Missing team' unless @team
options = { token: @team.token }
options[:aliases] = ([team.game.name] + [team.game.aliases]).flatten.compact if team.game
options[:send_gifs] = team.gifs
super(options)
client.team = @team
end
Expand Down
50 changes: 50 additions & 0 deletions spec/slack-gamebot/commands/set_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'spec_helper'

describe SlackGamebot::Commands::Set, vcr: { cassette_name: 'user_info' } do
let!(:team) { Fabricate(:team) }
let(:app) { SlackGamebot::Server.new(team: team) }
let(:captain) { Fabricate(:user, team: team, user_name: 'username', captain: true) }
context 'captain' do
it 'gives help' do
expect(message: "#{SlackRubyBot.config.user} set").to respond_with_slack_message(
'Missing setting, eg. _set gifs off_.'
)
end
context 'gifs' do
it 'enables GIFs' do
team.update_attributes!(gifs: false)
expect(message: "#{SlackRubyBot.config.user} set gifs on").to respond_with_slack_message(
"Enabled GIFs for team #{team.name}, thanks captain!"
)
expect(team.reload.gifs).to be true
expect(app.send(:client).send_gifs?).to be true
end
it 'disables GIFs' do
team.update_attributes!(gifs: true)
expect(message: "#{SlackRubyBot.config.user} set gifs off").to respond_with_slack_message(
"Disabled GIFs for team #{team.name}, thanks captain!"
)
expect(team.reload.gifs).to be false
expect(app.send(:client).send_gifs?).to be false
end
end
context 'invalid' do
it 'error' do
expect(message: "#{SlackRubyBot.config.user} set invalid on").to respond_with_slack_message(
'Invalid setting invalid, you can _set gifs on|off_.'
)
end
end
end
context 'not captain' do
before do
Fabricate(:user, team: team, captain: true)
captain.demote!
end
it 'cannot set GIFs' do
expect(message: "#{SlackRubyBot.config.user} set gifs true").to respond_with_slack_message(
"You're not a captain, sorry."
)
end
end
end
25 changes: 25 additions & 0 deletions spec/slack-gamebot/server_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

describe SlackGamebot::Server do
let(:app) { SlackGamebot::Server.new(team: team) }
context 'send_gifs' do
context 'default' do
let(:team) { Fabricate(:team) }
it 'true' do
expect(app.send(:client).send_gifs?).to be true
end
end
context 'on' do
let(:team) { Fabricate(:team, gifs: true) }
it 'true' do
expect(app.send(:client).send_gifs?).to be true
end
end
context 'off' do
let(:team) { Fabricate(:team, gifs: false) }
it 'false' do
expect(app.send(:client).send_gifs?).to be false
end
end
end
end

0 comments on commit dcd5d2f

Please sign in to comment.