|
16 | 16 | Dotenv.load
|
17 | 17 | @total_shards = ENV['SHARD'].to_i
|
18 | 18 | # 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 |
20 | 21 | @bot.gateway.check_heartbeat_acks = false
|
21 | 22 | @shard = ARGV[0].to_i
|
22 | 23 | @launch_option = ARGV[1].to_s
|
|
25 | 26 | @request_option = false
|
26 | 27 |
|
27 | 28 | # 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' |
29 | 30 | # do nothing
|
30 | 31 | 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) |
33 | 34 | end
|
34 | 35 |
|
35 | 36 | mutex = Mutex.new
|
|
41 | 42 | # handle !dm <command>
|
42 | 43 | next if check_server_options(event) == true
|
43 | 44 |
|
44 |
| - #check the server request options |
| 45 | + # check the server request options |
45 | 46 | check_request_option(event)
|
46 | 47 |
|
47 | 48 | # check what prefix the server should be using
|
|
58 | 59 |
|
59 | 60 | check_roll_modes
|
60 | 61 |
|
61 |
| - if @ed |
62 |
| - next unless replace_earthdawn(event) |
63 |
| - end |
| 62 | + next if @ed && !replace_earthdawn(event) |
64 | 63 |
|
65 | 64 | @roll_set = nil
|
66 | 65 | next unless roll_sets_valid(event)
|
67 | 66 |
|
68 | 67 | if @input =~ /^\s*d/
|
69 | 68 | roll_to_one = @input.lstrip
|
70 |
| - roll_to_one.prepend("1") |
| 69 | + roll_to_one.prepend('1') |
71 | 70 | @input = roll_to_one
|
72 | 71 | end
|
73 | 72 |
|
|
87 | 86 |
|
88 | 87 | # Check for correct input
|
89 | 88 | if @roll.match?(/\dd\d/i)
|
90 |
| - event.channel.start_typing() |
| 89 | + event.channel.start_typing |
91 | 90 | next if check_roll(event) == true
|
92 | 91 |
|
93 | 92 | # Check for wrath roll
|
94 | 93 | check_wrath
|
95 | 94 | # 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 |
97 | 98 | @roll_set_results = ''
|
98 | 99 | @error_check_roll_set = ''
|
99 | 100 | roll_count = 0
|
|
114 | 115 | end
|
115 | 116 | next if error_encountered
|
116 | 117 |
|
117 |
| - if @launch_option == "debug" |
118 |
| - log_roll(event) |
119 |
| - end |
| 118 | + log_roll(event) if @launch_option == 'debug' |
120 | 119 | if @comment.to_s.empty? || @comment.to_s.nil?
|
121 | 120 | event.respond "#{@user} Rolls:\n#{@roll_set_results}"
|
122 | 121 | else
|
123 | 122 | event.respond "#{@user} Rolls:\n#{@roll_set_results} Reason: `#{@comment}`"
|
124 | 123 | end
|
125 | 124 | next
|
126 |
| - else |
127 |
| - next if do_roll(event) == true |
128 | 125 | end
|
129 | 126 |
|
130 | 127 | # Output aliasing
|
131 | 128 | @tally = alias_output_pass(@tally)
|
132 | 129 |
|
133 | 130 | # 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' |
137 | 132 |
|
138 | 133 | # Print dice result to Discord channel
|
139 | 134 | @has_comment = !@comment.to_s.empty? && !@comment.to_s.nil?
|
|
148 | 143 | next if check_help(event) == true
|
149 | 144 | next if check_bot_info(event) == true
|
150 | 145 | 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? |
155 | 148 | # 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? |
160 | 151 | 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`" |
161 | 154 | 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') |
163 | 156 | 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}" } |
165 | 158 | 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.") |
167 | 160 | end
|
168 | 161 | end
|
169 | 162 | mutex.unlock
|
170 | 163 | end
|
171 |
| -if @launch_option == "lite" |
| 164 | +if @launch_option == 'lite' |
172 | 165 | @bot.run
|
173 | 166 | else
|
174 | 167 | @bot.run :async
|
175 | 168 |
|
176 | 169 | # Sleep until bot is ready and then set listening status
|
177 | 170 | 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) |
179 | 172 | # Check every 5 minutes and log server count
|
180 | 173 | loop do
|
181 | 174 | sleep 300
|
|
191 | 184 | exit!
|
192 | 185 | end
|
193 | 186 | # 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) |
198 | 192 | end
|
199 | 193 | end
|
0 commit comments