Skip to content

Commit 172d1cc

Browse files
authored
Add rubocop, rubocop rules and run autofix (Humblemonk#155)
* Add the Year Zero system as an alias * Add rubocop, rubocop rules and run autofix * Add ci linting
1 parent c1501ac commit 172d1cc

7 files changed

+401
-252
lines changed

.github/workflows/linting.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Linting
2+
3+
on: [push]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: ruby/setup-ruby@v1
11+
with:
12+
ruby-version: 2.6
13+
- run: bundle install
14+
- name: Rubocop
15+
run: rubocop

.rubocop.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
inherit_from: .rubocop_todo.yml

.rubocop_todo.yml

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2021-04-17 22:49:30 UTC using RuboCop version 1.12.1.
4+
# The point is for the user to remove these configuration records
5+
# one by one as the offenses are removed from the code base.
6+
# Note that changes in the inspected code, or installation of new
7+
# versions of RuboCop, may require this file to be generated again.
8+
9+
# Offense count: 2
10+
# Configuration parameters: AllowComments.
11+
Lint/EmptyConditionalBody:
12+
Exclude:
13+
- 'dice_maiden_logic.rb'
14+
15+
# Offense count: 1
16+
Lint/RescueException:
17+
Exclude:
18+
- 'dice_maiden_logic.rb'
19+
20+
# Offense count: 3
21+
Lint/UselessAssignment:
22+
Exclude:
23+
- 'dice_maiden.rb'
24+
25+
# Offense count: 8
26+
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
27+
Metrics/AbcSize:
28+
Max: 50
29+
30+
# Offense count: 1
31+
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
32+
# IgnoredMethods: refine
33+
Metrics/BlockLength:
34+
Max: 93
35+
36+
# Offense count: 3
37+
# Configuration parameters: CountBlocks.
38+
Metrics/BlockNesting:
39+
Max: 4
40+
41+
# Offense count: 5
42+
# Configuration parameters: IgnoredMethods.
43+
Metrics/CyclomaticComplexity:
44+
Max: 15
45+
46+
# Offense count: 18
47+
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
48+
Metrics/MethodLength:
49+
Max: 47
50+
51+
# Offense count: 5
52+
# Configuration parameters: IgnoredMethods.
53+
Metrics/PerceivedComplexity:
54+
Max: 17
55+
56+
# Offense count: 1
57+
Naming/AccessorMethodName:
58+
Exclude:
59+
- 'dice_maiden_logic.rb'
60+
61+
# Offense count: 2
62+
# Configuration parameters: IgnoredPatterns.
63+
# SupportedStyles: snake_case, camelCase
64+
Naming/MethodName:
65+
EnforcedStyle: snake_case
66+
67+
# Offense count: 2
68+
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
69+
# AllowedNames: at, by, db, id, in, io, ip, of, on, os, pp, to
70+
Naming/MethodParameterName:
71+
Exclude:
72+
- 'dice_maiden_logic.rb'
73+
74+
# Offense count: 15
75+
# Configuration parameters: AllowedIdentifiers.
76+
# SupportedStyles: snake_case, camelCase
77+
Naming/VariableName:
78+
EnforcedStyle: snake_case
79+
80+
# Offense count: 4
81+
# Cop supports --auto-correct.
82+
Style/CaseLikeIf:
83+
Exclude:
84+
- 'dice_maiden_logic.rb'
85+
86+
# Offense count: 5
87+
# Cop supports --auto-correct.
88+
# Configuration parameters: EnforcedStyle.
89+
# SupportedStyles: always, always_true, never
90+
Style/FrozenStringLiteralComment:
91+
Exclude:
92+
- 'Gemfile'
93+
- 'dice_maiden.rb'
94+
- 'dice_maiden_logic.rb'
95+
- 'earthdawn_logic.rb'
96+
- 'scripts/dice_maiden_quota.rb'
97+
98+
# Offense count: 13
99+
# Configuration parameters: AllowedVariables.
100+
Style/GlobalVars:
101+
Exclude:
102+
- 'dice_maiden.rb'
103+
- 'dice_maiden_logic.rb'
104+
105+
# Offense count: 7
106+
# Configuration parameters: MinBodyLength.
107+
Style/GuardClause:
108+
Exclude:
109+
- 'dice_maiden_logic.rb'
110+
111+
# Offense count: 10
112+
Style/IdenticalConditionalBranches:
113+
Exclude:
114+
- 'dice_maiden_logic.rb'
115+
116+
# Offense count: 1
117+
# Cop supports --auto-correct.
118+
Style/IfUnlessModifier:
119+
Exclude:
120+
- 'dice_maiden_logic.rb'
121+
122+
# Offense count: 10
123+
# Cop supports --auto-correct.
124+
# Configuration parameters: EnforcedStyle, IgnoredMethods.
125+
# SupportedStyles: predicate, comparison
126+
Style/NumericPredicate:
127+
Exclude:
128+
- 'spec/**/*'
129+
- 'dice_maiden.rb'
130+
- 'dice_maiden_logic.rb'
131+
132+
# Offense count: 1
133+
# Cop supports --auto-correct.
134+
# Configuration parameters: EnforcedStyle.
135+
# SupportedStyles: short, verbose
136+
Style/PreferredHashMethods:
137+
Exclude:
138+
- 'dice_maiden_logic.rb'
139+
140+
# Offense count: 7
141+
# Cop supports --auto-correct.
142+
Style/StringConcatenation:
143+
Exclude:
144+
- 'dice_maiden.rb'
145+
- 'dice_maiden_logic.rb'
146+
- 'earthdawn_logic.rb'
147+
148+
# Offense count: 6
149+
# Cop supports --auto-correct.
150+
Style/ZeroLengthPredicate:
151+
Exclude:
152+
- 'dice_maiden_logic.rb'
153+
154+
# Offense count: 19
155+
# Cop supports --auto-correct.
156+
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
157+
# URISchemes: http, https
158+
Layout/LineLength:
159+
Max: 722

Gemfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
source 'https://rubygems.org'
22

3+
gem 'dicebag', git: 'https://github.com/Humblemonk/Dice-Bag.git', branch: 'master'
34
gem 'discordrb', github: 'shardlab/discordrb', branch: 'main'
4-
gem 'dicebag', :git => "https://github.com/Humblemonk/Dice-Bag.git", :branch => "master"
55
gem 'dotenv', '~> 2.5'
66
gem 'rest-client', '~> 2.0', '>= 2.0.2'
7+
gem 'rubocop'
78
gem 'sqlite3', '~> 1.3', '>= 1.3.11'

dice_maiden.rb

+30-36
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
Dotenv.load
1717
@total_shards = ENV['SHARD'].to_i
1818
# Add API token
19-
@bot = Discordrb::Bot.new token: ENV['TOKEN'], num_shards: @total_shards, shard_id: ARGV[0].to_i, intents: [:servers, :server_messages, :direct_messages], ignore_bots: true, fancy_log: true
19+
@bot = Discordrb::Bot.new token: ENV['TOKEN'], num_shards: @total_shards, shard_id: ARGV[0].to_i,
20+
intents: %i[servers server_messages direct_messages], ignore_bots: true, fancy_log: true
2021
@bot.gateway.check_heartbeat_acks = false
2122
@shard = ARGV[0].to_i
2223
@launch_option = ARGV[1].to_s
@@ -25,11 +26,11 @@
2526
@request_option = false
2627

2728
# open connection to sqlite db and set timeout to 10s if the database is busy
28-
if @launch_option == "lite"
29+
if @launch_option == 'lite'
2930
# do nothing
3031
else
31-
$db = SQLite3::Database.new "main.db"
32-
$db.busy_timeout=(10000)
32+
$db = SQLite3::Database.new 'main.db'
33+
$db.busy_timeout = (10_000)
3334
end
3435

3536
mutex = Mutex.new
@@ -41,7 +42,7 @@
4142
# handle !dm <command>
4243
next if check_server_options(event) == true
4344

44-
#check the server request options
45+
# check the server request options
4546
check_request_option(event)
4647

4748
# check what prefix the server should be using
@@ -58,16 +59,14 @@
5859

5960
check_roll_modes
6061

61-
if @ed
62-
next unless replace_earthdawn(event)
63-
end
62+
next if @ed && !replace_earthdawn(event)
6463

6564
@roll_set = nil
6665
next unless roll_sets_valid(event)
6766

6867
if @input =~ /^\s*d/
6968
roll_to_one = @input.lstrip
70-
roll_to_one.prepend("1")
69+
roll_to_one.prepend('1')
7170
@input = roll_to_one
7271
end
7372

@@ -87,13 +86,15 @@
8786

8887
# Check for correct input
8988
if @roll.match?(/\dd\d/i)
90-
event.channel.start_typing()
89+
event.channel.start_typing
9190
next if check_roll(event) == true
9291

9392
# Check for wrath roll
9493
check_wrath
9594
# Grab dice roll, create roll, grab results
96-
unless @roll_set.nil?
95+
if @roll_set.nil?
96+
next if do_roll(event) == true
97+
else
9798
@roll_set_results = ''
9899
@error_check_roll_set = ''
99100
roll_count = 0
@@ -114,26 +115,20 @@
114115
end
115116
next if error_encountered
116117

117-
if @launch_option == "debug"
118-
log_roll(event)
119-
end
118+
log_roll(event) if @launch_option == 'debug'
120119
if @comment.to_s.empty? || @comment.to_s.nil?
121120
event.respond "#{@user} Rolls:\n#{@roll_set_results}"
122121
else
123122
event.respond "#{@user} Rolls:\n#{@roll_set_results} Reason: `#{@comment}`"
124123
end
125124
next
126-
else
127-
next if do_roll(event) == true
128125
end
129126

130127
# Output aliasing
131128
@tally = alias_output_pass(@tally)
132129

133130
# Grab event user name, server name and timestamp for roll and log it
134-
if @launch_option == "debug"
135-
log_roll(event)
136-
end
131+
log_roll(event) if @launch_option == 'debug'
137132

138133
# Print dice result to Discord channel
139134
@has_comment = !@comment.to_s.empty? && !@comment.to_s.nil?
@@ -148,34 +143,32 @@
148143
next if check_help(event) == true
149144
next if check_bot_info(event) == true
150145
next if check_purge(event) == false
151-
rescue StandardError => error ## The worst that should happen is that we catch the error and return its message.
152-
if(error.message == nil )
153-
error.message = "NIL MESSAGE!"
154-
end
146+
rescue StandardError => e ## The worst that should happen is that we catch the error and return its message.
147+
e.message = 'NIL MESSAGE!' if e.message.nil?
155148
# Simplify roll and send it again if we error out due to character limit
156-
if (error.message.include? "Message over the character limit") || (error.message.include? "Invalid Form Body")
157-
unless @roll_set.nil?
158-
event.respond "#{@user} Rolls:\n#{@error_check_roll_set}Reason: `Simplified roll due to character limit`"
159-
else
149+
if (e.message.include? 'Message over the character limit') || (e.message.include? 'Invalid Form Body')
150+
if @roll_set.nil?
160151
event.respond "#{@user} Roll #{@dice_result} Reason: `Simplified roll due to character limit`"
152+
else
153+
event.respond "#{@user} Rolls:\n#{@error_check_roll_set}Reason: `Simplified roll due to character limit`"
161154
end
162-
elsif (error.message.include? "undefined method `join' for nil:NilClass") || (error.message.include? "The bot doesn't have the required permission to do this!") || (error.message.include? "500 Internal Server Error")
155+
elsif (e.message.include? "undefined method `join' for nil:NilClass") || (e.message.include? "The bot doesn't have the required permission to do this!") || (e.message.include? '500 Internal Server Error')
163156
time = Time.now.getutc
164-
File.open('dice_rolls.log', 'a') { |f| f.puts "#{time} ERROR: #{error.message}" }
157+
File.open('dice_rolls.log', 'a') { |f| f.puts "#{time} ERROR: #{e.message}" }
165158
else
166-
event.respond("Unexpected exception thrown! (" + error.message + ")\n\nPlease drop us a message in the #support channel on the dice maiden server, or create an issue on Github.")
159+
event.respond('Unexpected exception thrown! (' + e.message + ")\n\nPlease drop us a message in the #support channel on the dice maiden server, or create an issue on Github.")
167160
end
168161
end
169162
mutex.unlock
170163
end
171-
if @launch_option == "lite"
164+
if @launch_option == 'lite'
172165
@bot.run
173166
else
174167
@bot.run :async
175168

176169
# Sleep until bot is ready and then set listening status
177170
sleep(1) until @bot.ready
178-
@bot.update_status("online", "!roll", nil, since = 0, afk = false, activity_type = 2)
171+
@bot.update_status('online', '!roll', nil, since = 0, afk = false, activity_type = 2)
179172
# Check every 5 minutes and log server count
180173
loop do
181174
sleep 300
@@ -191,9 +184,10 @@
191184
exit!
192185
end
193186
# Limit HTTP POST to shard 0. We do not need every shard hitting the discordbots API
194-
if @shard == 0
195-
servers = $db.execute "select sum(server_count) from shard_stats;"
196-
RestClient.post("https://top.gg/api/bots/377701707943116800/stats", {"shard_count": @total_shards, "server_count": servers.join.to_i}, :'Authorization' => ENV['API'], :'Content-Type' => :json);
197-
end
187+
next unless @shard == 0
188+
189+
servers = $db.execute 'select sum(server_count) from shard_stats;'
190+
RestClient.post('https://top.gg/api/bots/377701707943116800/stats',
191+
{ "shard_count": @total_shards, "server_count": servers.join.to_i }, 'Authorization': ENV['API'], 'Content-Type': :json)
198192
end
199193
end

0 commit comments

Comments
 (0)