forked from BeardBrewery/DiscordBot-KoA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
841 lines (718 loc) · 39.1 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
require 'fileutils'
require 'set'
require 'open-uri'
require 'yaml'
require 'chronic'
require 'chronic_duration'
require 'oj'
Oj.default_options = {
bigdecimal_load: :bigdecimal,
circular: false,
class_cache: true,
mode: :json,
indent: " "
}
require 'multi_json'
require 'discordrb'
require 'pry'
#require 'fuzzy_match'
def get_value_from_arguments(option_name: nil, env_key:nil, default:)
if option_name && option_index = ARGV.index(option_name)
ARGV.delete_at(option_index)
abort("Command line option '#{option_name}' should be followed by a another argument.") unless option_value = ARGV.delete_at(option_index)
return option_value
elsif env_key && ENV[env_key]
return ENV[env_key]
else
return default
end
end
DEFAULT_CONFIG = YAML.load_file(get_value_from_arguments(option_name: '--default-config', default:'./default_config.yaml')).freeze
def config_keys_changed_from_defaults?(loaded_config)
loaded_config.keys.size != DEFAULT_CONFIG.keys.size || loaded_config.keys.sort != DEFAULT_CONFIG.keys.sort
end
def config
return @config if @config
source_file = get_value_from_arguments(option_name: '--config-file', env_key: 'DISCORD_BOT_CONFIG_FILE', default: './.config.json')
unless @config = load_config(source_file: source_file, compare: true)
save_config(source_file: source_file, config: DEFAULT_CONFIG)
abort("Your config was empty. I've generated a valid configuration in its placed based on our defaults. Please edit (#{source_file}) and restart")
end
@config
end
def cli_help
"Command Example: `source YOUR_PROJECT.env && rbenv exec bundle exec ruby ./app.rb --config-file ./.config-YOUR_PROJECT.json --default-config ./default_config.yaml`"
end
abort(cli_help) if ARGV.delete('--help')
def compare_config_to_defaults(loaded_config)
msg = <<~CONFIG_EOS
Keys missing from loaded config:
#{DEFAULT_CONFIG.keys - loaded_config.keys}
Keys extra compared to DEFAULT_CONFIG:
#{loaded_config.keys - DEFAULT_CONFIG.keys}
DEFAULT_CONFIG:
#{MultiJson.dump(DEFAULT_CONFIG, pretty: true)}
Loaded Config:
#{MultiJson.dump(loaded_config, pretty: true)}
Suggested Merge:
#{MultiJson.dump(DEFAULT_CONFIG.merge(loaded_config), pretty: true)}
CONFIG_EOS
end
def load_config(source_file: ,compare: false)
source_file = File.expand_path(source_file)
return nil unless File.exist?(source_file)
loaded_config = MultiJson.load(open(source_file))
abort("Loaded config does not contain the same amount of keys as the default config. Please your config with the default config, make the appropriate changes, and try again.\n" + compare_config_to_defaults(loaded_config)) if compare && config_keys_changed_from_defaults?(loaded_config)
loaded_config
end
def save_config(source_file:, config:, pretty_print: true, backup: false)
source_file = File.expand_path(source_file)
FileUtils.cp(source_file, source_file + DateTime.now.strftime(".%Y%m%d%H%M%S")) if backup && File.exist?(source_file) #TODO: Allow for different backup methods (git?)
begin
content = MultiJson.dump(config, pretty: pretty_print)
rescue RuntimeError => e
puts "Error while trying to dump config:\n#{config}\n#{e.message}"
raise e
else
write_file(source_file: source_file, content: content)
end
config
end
def write_file(source_file:, content:)
File.open(source_file,'w+') do |f|
f.puts(content)
end
end
def channels(*filters)
return config['channels'].map{|type, channels| channels}.flatten.uniq if filters.delete('all')
channels = []
filters.each do |filter|
channels += config['channels'][filter] if config['channels'].has_key?(filter)
end
return channels.uniq
end
EVENTS_SOURCE = File.expand_path(get_value_from_arguments(option_name: '--events_source', env_key: 'EVENTS_SOURCE', default: 'events.json')).freeze
ENV['TZ'] = get_value_from_arguments(option_name: '--timezone', env_key: 'TZ', default: 'UTC').freeze # Chronic uses ENV['TZ'] for its timezone.
DISCORD_TOKEN = get_value_from_arguments(option_name: '--discord-token', env_key: 'DISCORD_BOT_TOKEN', default: nil).freeze
abort('DISCORD_TOKEN cannot be empty.') if DISCORD_TOKEN.nil? || DISCORD_TOKEN.empty?
DISCORD_CLIENT_ID = get_value_from_arguments(option_name: '--discord-client-id', env_key: 'DISCORD_BOT_CLIENT_ID', default: nil).freeze
abort('DISCORD_CLIENT_ID cannot be empty.') if DISCORD_CLIENT_ID.nil? || DISCORD_CLIENT_ID.empty?
REGEX_UPGRADE_CALCULATOR = /(upgrade:?|will it finish in time\??) (?<duration>((?<days>\d+)d ?)?(?<hours>\d+):(?<minutes>\d+):(?<seconds>\d+))(?<timer_help> (?<base_number_helps>\d+)\+(?<bonus_number_helps>\d+) (?<base_timer_help_duration>\d+)\+(?<bonus_timer_help_duration>\d+))?(?<next_event> -next>)?(?<restrict_type> GE-only)?/i.freeze
REGEX_SPEEDUPS = /how long are (?<m5>\d+)[: ](?<h1>\d+)[: ](?<h3>\d+) speedups\??/i.freeze
REGEX_WALL_CALCULATOR = /!wall(?<duration> (?:(?<days>\d)d ?)?(?<hours>\d+):(?<minutes>\d+):(?<seconds>\d+)?)?(?<wall_defense> (?<current_wall_defense>\d+)(?:\/(?<max_wall_defense>\d+))?)?/i.freeze
REGEX_GET_ALLIANCE_PORTAL = /^(?:!(alliance )?portal|when(?:’|'| i)s (?:(?<alliance_tag>#{config['alliances'].keys.join('|')}) )?(?:alliance )?portal|(?:(?<alliance_tag>#{config['alliances'].keys.join('|')}) )?(?:alliance )?portal soon\?)/i.freeze
REGEX_REMIND_ALLIANCE_PORTAL = /^!(announce|remind) (?:(?<alliance_tag>#{config['alliances'].keys.join('|')}) )?(alliance )?portal/i.freeze
REGEX_GET_FALLEN_KNIGHTS = /^(?:!fallen( knights)?|when(?:’|'| i)s (?:(?<alliance_tag>#{config['alliances'].keys.join('|')}) )?fallen(?: knights)?|(?:(?<alliance_tag>#{config['alliances'].keys.join('|')}) )?fallen(?: knights)? soon\?)/i.freeze
REGEX_SET_ALLIANCE_PORTAL = /^!set (?:(?<alliance_tag>#{config['alliances'].keys.join('|')}) )?(?:alliance )?portal (?<content>.+)$/i.freeze
REGEX_SET_FALLEN_KNIGHTS = /^!set (?:(?<alliance_tag>#{config['alliances'].keys.join('|')}) )?(?:fallen|fallen knights|fk) (?<content>.+)$/i.freeze
REGEX_RALLY_ROUNDS = /^[!?]?(fallen|rally) ?rounds\??$/i
REGEX_SET_GOLEM = /^!set (?<keyword>golem|kingdom threat) (?<content>.+)$/i.freeze
WALL_DMG_PER_MINUTE = (4.0).freeze
WALL_BURNING_DURATION_PER_HIT = ChronicDuration.parse('30 minutes').freeze
REPOSITORY_URL = "https://github.com/BeardBrewery/DiscordBot-KoA"
GOLD_EVENT_INTERVAL = '2 weeks from now'.freeze
ALLIANCE_EVENT_INTERVAL = '5 weeks from now'.freeze
# This statement creates a bot with the specified token and application ID. After this line, you can add events to the
# created bot, and eventually run it.
#
# If you don't yet have a token and application ID to put in here, you will need to create a bot account here:
# https://discordapp.com/developers/applications/me
# If you're wondering about what redirect URIs and RPC origins, you can ignore those for now. If that doesn't satisfy
# you, look here: https://github.com/meew0/discordrb/wiki/Redirect-URIs-and-RPC-origins
# After creating the bot, simply copy the token (*not* the OAuth2 secret) and the client ID and put it into the
# respective places.
bot = Discordrb::Commands::CommandBot.new token: DISCORD_TOKEN, client_id: DISCORD_CLIENT_ID.to_i, prefix: '!'
if ARGV.delete('--invite-url')
puts "This bot's invite URL is #{bot.invite_url}."
puts 'Click on it to invite it to your server.'
end
module JoinAnnouncer
extend Discordrb::EventContainer
member_join do |bot_event|
bot_event.server.general_channel.send_message(config['join_announcer']['welcome_message'] % {user_mention: bot_event.user.name})
end
end
def scheduled_events(clear_cache: false, include_expired: false, restrict_types: [], names: [], sort: :time, bot_event: nil, include_private: false)
if clear_cache || @scheduled_events.nil? || @scheduled_events[:cached] < Chronic.parse('2 hours ago')
# bot_event.respond "Scheduled Events cache cleared at request of #{bot_event.user.mention}!" if bot_event && clear_cache
@scheduled_events = {cached: Time.now}.merge(MultiJson.load(open(EVENTS_SOURCE)))
@scheduled_events['events'].each {|scheduled_event| scheduled_event[:time] = Chronic.parse(scheduled_event['ISO8601']) unless scheduled_event.has_key?(:time)}
end
events = @scheduled_events['events'].dup
events.reject!{|scheduled_event|(scheduled_event[:time] - Time.now).to_i < 0} unless include_expired
unless restrict_types.empty?
restrict_types.map!{|type|type.downcase}
events.select!{|scheduled_event|next unless scheduled_event.has_key?('type'); restrict_types.include?(scheduled_event['type'].downcase)}
end
unless include_private
events.reject!{|scheduled_event|scheduled_event['private']}
end
unless names.compact.empty?
names.map!{|name|name.downcase}
events.select!{|scheduled_event|next unless scheduled_event.has_key?('name'); names.include?(scheduled_event['name'].downcase)}
end
if sort == :time
events.sort_by! do |scheduled_event|
scheduled_event['ISO8601']
end
end
return events
end
def check_confirmed(target_event, msg)
msg += " Mind you, this time has not yet been confirmed, so it might change." if (target_event['status'].downcase == 'unconfirmed' rescue false)
msg
end
def time_difference(target_event, format: :textual)
difference_in_seconds = target_event[:time].to_i - Time.now.to_i
return difference_in_seconds if format == :seconds
ChronicDuration.output(difference_in_seconds)
end
def help_vars(bot_event)
{
user_mention: bot_event.user.mention,
first_alliance_tag: config['alliances'].keys.first,
first_alliance_name: config['alliances'].values.first,
last_alliance_tag: config['alliances'].keys.last,
last_alliance_name: config['alliances'].values.last,
TZ: ENV['TZ'],
}
end
bot.message(start_with: '!help') do |bot_event|
msg = open('help_general.md').read % help_vars(bot_event)
bot_event.respond(msg)
end
bot.message(start_with: '!calculators') do |bot_event|
msg = open('help_calculators.md').read % help_vars(bot_event)
bot_event.respond(msg)
end
bot.message(start_with: '!admin') do |bot_event|
if bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
msg = open('help_admin.md').read % help_vars(bot_event)
else
msg = "#{bot_event.user.mention}, you don't have access to this command"
end
bot_event.respond(msg)
end
bot.message(start_with: '!find ') do |bot_event|
begin
needle = bot_event.content.gsub(/!find /,'')
messages = bot_event.channel.history(100).select {|message| message.content.include?(needle)}
rescue
binding.pry
end
begin
bot_event.respond "#{bot_event.user.mention}: Found #{messages.reverse[0...5].count} results: #{messages.join("\n")[0...1800]}"
rescue
binding.pry
end
end
def alliance_portal_event(bot_event:, clear_cache: false, alliance: config['alliances'].keys.first.downcase, everyone: false, scheduled_by: nil)
event_key = alliance + '-portal'
target_event = scheduled_events(names: [config['events_map'][event_key]], include_expired: false, sort: :time, bot_event: bot_event, clear_cache: clear_cache, include_private: true).first
announce_event(bot_event: bot_event, clear_cache: clear_cache, target_event: target_event, event_name: config['events_map'][event_key], everyone: everyone, scheduled_by: scheduled_by)
end
def fallen_knights_event(bot_event:, clear_cache: false, alliance: config['alliances'].keys.first.downcase)
event_key = alliance + ' fallen knights'
unless event_name = config['events_map'][event_key]
bot_event.respond "#{bot_event.user.mention}: I don't know this alliance event ('No events map match for #{event_key}')"
else
target_event = scheduled_events(names: [event_name], include_expired: false, sort: :time, bot_event: bot_event, clear_cache: clear_cache, include_private: true).first
announce_event(bot_event: bot_event, clear_cache: clear_cache, target_event: target_event, event_name: event_name)
end
end
def golem_event(bot_event:, clear_cache: false)
target_event = scheduled_events(names: [config['events_map']['golem']], include_expired: false, sort: :time, bot_event: bot_event, clear_cache: clear_cache).first
announce_event(bot_event: bot_event, clear_cache: clear_cache, target_event: target_event, event_name: config['events_map']['golem'])
end
def announce_event(bot_event:, clear_cache: false, target_event:, event_name: nil, everyone: false, scheduled_by: nil)
if target_event
if everyone
msg = "@everyone, the #{target_event['name']} event is currently scheduled#{scheduled_by ? ' by ' + scheduled_by : ''} for #{target_event[:time]}, which is in about #{time_difference(target_event)}."
else
msg = "#{bot_event.user.mention}: The #{target_event['name']} event is currently scheduled for #{target_event[:time]}, which is in about #{time_difference(target_event)}."
end
bot_event.respond check_confirmed(target_event, msg)
else
bot_event.respond "Sorry #{bot_event.user.mention}, there's currently no next #{event_name} scheduled yet. Maybe check the event centre?"
end
end
def next_event(bot_event:, clear_cache: false)
if target_event = scheduled_events(include_expired: false, sort: :time, clear_cache: clear_cache, include_private: include_private?(bot_event)).first
msg = "#{bot_event.user.mention}: Next event is #{target_event['name']} and is currently scheduled for #{target_event[:time]}, which is in about #{time_difference(target_event)}."
msg += "\nThis does not include private alliance events as this is a public channel." unless include_private?(bot_event)
bot_event.respond check_confirmed(target_event, msg)
else
bot_event.respond "Sorry #{bot_event.user.mention}, there's currently no next event scheduled yet."
end
end
def set_event(bot_event:, event_key:, content:, event_type:)
unless bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
bot_event.respond "#{bot_event.user.mention} you don't have access to this command"
else
confirmed = !content.gsub!(/\s-unconfirmed/,'')
event_time = content
events = MultiJson.load(open(EVENTS_SOURCE))
target_event = events['events'].find do |event|
event['name'] == config['events_map'][event_key]
end
if target_event.nil? || target_event.empty?
if target_event = config['template_events'][event_type]
target_event['name'] = config['events_map'][event_key]
events['events'] << target_event
else
bot_event.respond "#{bot_event.user.mention}, I could not find an '#{event_key}'-event called '#{config['events_map'][event_key]}', nor could I find a template for it."
return false
end
end
begin
target_event['ISO8601'] = Chronic.parse(event_time).iso8601
rescue
bot_event.respond "#{bot_event.user.mention}, I'm sorry, but '#{event_time}' is not recognised as a valid time."
return false
end
target_event.delete('status')
target_event['status'] = 'unconfirmed' unless confirmed
store_events(events)
end
end
def store_events(events=nil)
if events.nil?
return false unless scheduled_events(clear_cache: true, include_expired: true, sort: :time, include_private: true)
events = @scheduled_events
end
events.delete(:cached)
events['events'].each do |event|
if event[:time]
event['ISO8601'] = event[:time].iso8601
event.delete(:time)
end
end
save_config(source_file: EVENTS_SOURCE, config: events, pretty_print: true, backup: true)
#Force a cache clear:
@scheduled_events = nil
end
bot.message(start_with: REGEX_GET_ALLIANCE_PORTAL, in: channels('alliance', 'control')) do |bot_event|
clear_cache = bot_event.content.include?('clearcache') && bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
md = bot_event.content.match(REGEX_GET_ALLIANCE_PORTAL)
alliance_tag = md['alliance_tag'] || config['alliances'].keys.first
alliance_portal_event(bot_event: bot_event, clear_cache: clear_cache, alliance: alliance_tag.downcase)
end
bot.message(start_with: REGEX_REMIND_ALLIANCE_PORTAL, in: channels('alliance', 'control')) do |bot_event|
clear_cache = bot_event.content.include?('clearcache') && bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
md = bot_event.content.match(REGEX_REMIND_ALLIANCE_PORTAL)
alliance_tag = md['alliance_tag'] || config['alliances'].keys.first
alliance_portal_event(bot_event: bot_event, clear_cache: clear_cache, alliance: alliance_tag.downcase, everyone: true)
end
bot.message(start_with: REGEX_GET_FALLEN_KNIGHTS, in: channels('alliance', 'control')) do |bot_event|
clear_cache = bot_event.content.include?('clearcache') && bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
md = bot_event.content.match(REGEX_GET_FALLEN_KNIGHTS)
alliance_tag = md['alliance_tag'] || config['alliances'].keys.first
fallen_knights_event(bot_event: bot_event, clear_cache: clear_cache, alliance: alliance_tag.downcase)
end
bot.message(start_with: '!golem', in: channels('all')) do |bot_event|
clear_cache = bot_event.content.include?('clearcache') && bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
golem_event(bot_event: bot_event, clear_cache: clear_cache)
end
bot.message(content: REGEX_SET_ALLIANCE_PORTAL, in: channels('alliance', 'control')) do |bot_event|
md = bot_event.content.match(REGEX_SET_ALLIANCE_PORTAL)
alliance_tag = md['alliance_tag'] || config['alliances'].keys.first
set_event(bot_event: bot_event, event_type: 'portal', event_key: "#{alliance_tag}-portal", content: md['content'])
alliance_portal_event(bot_event: bot_event, clear_cache: true, alliance: alliance_tag, everyone: true, scheduled_by: bot_event.user.mention)
end
bot.message(content: REGEX_SET_FALLEN_KNIGHTS, in: channels('alliance', 'control')) do |bot_event|
md = bot_event.content.match(REGEX_SET_FALLEN_KNIGHTS)
alliance_tag = md['alliance_tag'] || config['alliances'].keys.first
set_event(bot_event: bot_event, event_type: 'fallen knights', event_key: "#{alliance_tag} fallen knights", content: md['content'])
fallen_knights_event(bot_event: bot_event, clear_cache: true, alliance: alliance_tag)
end
bot.message(content: REGEX_SET_GOLEM, in: channels('all')) do |bot_event|
md = bot_event.content.match(REGEX_SET_GOLEM)
set_event(bot_event: bot_event, event_type: 'golem', event_key: 'golem', content: md['content'])
golem_event(bot_event: bot_event, clear_cache: true)
end
bot.message(content: REGEX_RALLY_ROUNDS, in: channels('alliance', 'control')) do |bot_event|
bot_event.respond "Fallen Knights Rally Rounds are at 7, 14 and 17, with Fort at rounds 10 and 20. Only those who already have high *individual* ranks are supposed to go into the fort"
end
bot.message(start_with: ['!next event', /(what|when)(’|'| i)s next event/], in: channels('all')) do |bot_event|
clear_cache = bot_event.content.include?('clearcache') && bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
next_event(bot_event: bot_event, clear_cache: clear_cache)
end
bot.message(start_with: [REGEX_SPEEDUPS], in: channels('spam', 'control')) do |bot_event|
md = bot_event.content.match(REGEX_SPEEDUPS).named_captures
md.each do |k,v|
md[k] = v.to_i
end
msg =<<~BOTRESPONSE
#{bot_event.user.mention}:
#{md['m5']} x 5m = #{ChronicDuration.output(md['m5'] * 5 * 60)}
#{md['h1']} x 1h = #{ChronicDuration.output(md['h1'] * 60 * 60)}
#{md['h3']} x 3h = #{ChronicDuration.output(md['h3'] * 3 * 60 * 60)}
= #{ChronicDuration.output((((md['h3'] * 3) + md['h1']) * 60 * 60) + (md['m5'] * 5 * 60))}
BOTRESPONSE
bot_event.respond msg
end
bot.message(start_with: [REGEX_UPGRADE_CALCULATOR], in: channels('spam', 'control')) do |bot_event|
clear_cache = bot_event.content.include?('clearcache') && bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
restrict_types = (bot_event.content.gsub!(/ GE-only/i, '') ? ['Gold Event'] : [])
index = (bot_event.content.gsub!(/ -next/i, '') ? 1 : 0)
upgrade_calculator(bot_event, restrict_types: restrict_types, index: index)
end
bot.message_edit(start_with: [REGEX_UPGRADE_CALCULATOR], in: channels('spam', 'control')) do |bot_event|
clear_cache = bot_event.content.include?('clearcache') && bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
restrict_types = (bot_event.content.gsub!(/ GE-only/i, '') ? ['Gold Event'] : [])
index = (bot_event.content.gsub!(/ -next/i, '') ? 1 : 0)
upgrade_calculator(bot_event, restrict_types: restrict_types, index: index)
end
def upgrade_calculator(bot_event, format: :default, restrict_types: [], index: 0)
events = scheduled_events(clear_cache: false, include_expired: false, names: ['Upgrade Stage'], restrict_types: restrict_types, sort: :time, bot_event: nil)
event = events[index]
md = bot_event.content.match(REGEX_UPGRADE_CALCULATOR)
unless md
bot_event.respond "Could not match your message against the expected format 1d 12:59:59 10+9 60+9"
else
upgrade_seconds = ChronicDuration.parse(md['duration'].strip)
total_timer_help = 0
if md['timer_help']
total_timer_help = (md['base_number_helps'].to_i + md['bonus_number_helps'].to_i) * (md['base_timer_help_duration'].to_i + md['bonus_timer_help_duration'].to_i)
end
total_upgrade_seconds = upgrade_seconds - total_timer_help
if event.nil?
bot_event.respond "No upcoming upgrade stage known. Maybe you need to *!reschedule* first?"
bot_event.respond "Time deducted for timer help: **#{ChronicDuration.output(total_timer_help)}**"
return false
end
#TODO: Loop through all events to check which event is the closest.
time_till_upgrade_stage = time_difference(event, format: :seconds)
time_difference = (total_upgrade_seconds - time_till_upgrade_stage)
speedups = {start_of_stage: {'3h' => 0, '1h' => 0, '5m' => 0, 'remainder' => 0}, end_of_stage: {'3h' => 0, '1h' => 0, '5m' => 0, 'remainder' => 0}}
if time_difference > 0
speedups[:start_of_stage]['3h'], remainder = (time_difference).divmod(60*60*3)
speedups[:start_of_stage]['1h'], remainder = remainder.divmod(60*60)
speedups[:start_of_stage]['5m'], speedups[:start_of_stage]['remainder'] = remainder.divmod(60*5)
speedups[:end_of_stage]['3h'], remainder = (time_difference - (24*60*60)).divmod(60*60*3)
speedups[:end_of_stage]['1h'], remainder = remainder.divmod(60*60)
speedups[:end_of_stage]['5m'], speedups[:end_of_stage]['remainder'] = remainder.divmod(60*5)
end
if format == :default
msg=<<~BOTRESPONSE
#{bot_event.user.mention}:
Your Upgrade will take:
***#{ChronicDuration.output(total_upgrade_seconds)}***
_(If you have the Development Lord Talent, you can probably cut this time down to about #{ChronicDuration.output((total_upgrade_seconds * 0.8).to_i)}, though this needs further testing...)_
BOTRESPONSE
if total_timer_help == 0
msg+="No timer help to deduct.\n\n"
else
msg+=<<~BOTRESPONSE
Time deducted for timer help:
**#{ChronicDuration.output(total_timer_help)}**
BOTRESPONSE
end
msg+=<<~BOTRESPONSE
Till next #{event['type']} Upgrade Stage:
***#{ChronicDuration.output(time_till_upgrade_stage)}***
It will finish:
***#{ChronicDuration.output(time_difference.abs)}***
#{time_difference > 0 ? '***after*** the stage has started! :clap: YAY! :D' : '***too soon*** :cry: Wait a bit, or try without your construction speed boosting equipment and/or heroes?'}
BOTRESPONSE
elsif format == :compact
msg=<<~BOTRESPONSE
#{bot_event.user.mention}:
***#{ChronicDuration.output(total_upgrade_seconds)}*** for your upgrade to finish after **#{ChronicDuration.output(total_timer_help)||'no'}** total timer help has been deducted.
***#{ChronicDuration.output(time_till_upgrade_stage)}*** till the next upgrade stage.
It will finish ***#{ChronicDuration.output(time_difference.abs)}*** #{time_difference > 0 ? '***after*** the stage has started! :clap: YAY! :D' : '***too soon*** :cry: Wait a bit, or try without your construction speed boosting equipment and/or heroes?'}
BOTRESPONSE
else
msg='UNDEFINED FORMAT'
end
if time_difference > 0
if time_difference >= (24 * 60 * 60)
msg += "It will take longer than 24 hours though, so unless you have enough speedups, it will finish **after the upgrade stage has ended** :cry:\n\n"
msg += "Speedups needed to finish it before the end of the stage:\n"
msg += "**#{speedups[:end_of_stage]['3h']}** x 3h (or #{speedups[:end_of_stage]['3h'] * 3} + #{speedups[:end_of_stage]['1h']} x 1h = #{speedups[:end_of_stage]['3h'] * 3 + speedups[:end_of_stage]['1h']} 1h)\n"
msg += "**#{speedups[:end_of_stage]['1h']}** x 1h\n"
msg += "**#{speedups[:end_of_stage]['5m']}** x 5m\n"
msg += "Remaining:#{speedups[:end_of_stage]['remainder'].divmod(60).join(' minutes ')} seconds. If there's a remainder, you need another 5m speedup at the very least.\n\n"
end
msg += "Speedups needed to finish it at the start of the stage:\n**#{speedups[:start_of_stage]['3h']}** x 3h\n**#{speedups[:start_of_stage]['1h']}** x 1h\n**#{speedups[:start_of_stage]['5m']}** x 5m\nRemaining:#{speedups[:start_of_stage]['remainder'].divmod(60).join(' minutes ')} seconds\n\n"
end
msg +=<<~BOTRESPONSE
Please note that this is based on the calculation:
(#{upgrade_seconds} - (#{md['base_number_helps'].to_i} + #{md['bonus_number_helps'].to_i}) * (#{md['base_timer_help_duration'].to_i} + #{md['bonus_timer_help_duration'].to_i})) - #{time_till_upgrade_stage}) = #{time_difference} seconds.
Or:
(#{md['duration']} base upgrade time - (#{md['base_number_helps'].to_i} + #{md['bonus_number_helps'].to_i} number of timer helps) * (#{md['base_timer_help_duration'].to_i} + #{md['bonus_timer_help_duration'].to_i} = #{ChronicDuration.output(md['base_timer_help_duration'].to_i + md['bonus_timer_help_duration'].to_i)} timer help duration)
= #{ChronicDuration.output(total_upgrade_seconds)} upgrade time) - #{ChronicDuration.output(time_till_upgrade_stage)} till upgrade stage ) = #{ChronicDuration.output(time_difference)} seconds.
Also, be careful to not accidentally trigger the Instant Building Speedup if there's not a lot of margin between the start of the stage and the remaining construction time.
Don't blame me if it finishes too soon! ;-)
BOTRESPONSE
bot_event.respond msg
bot_event.respond "THIS DOES NOT INCLUDE YOUR TIMER HELP DURATION! Please specify base number of timer helps + bonus number of timer helps and base timer help duration and bonus timer help duration in the format: 20+9 60+10. For example: \"will it finish in time? 1d 21:18:10 20+9 88+9\". You can find this info in your Embassy info screen." unless md['timer_help']
end
end
bot.message(start_with: ['!reschedule gold event', '!reschedule alliance event'], in: channels('all')) do |bot_event|
unless bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
bot_event.respond("#{bot_event.user.mention}: you're not authorised to reschedule this event")
else
msg = ""
md = bot_event.content.match(/!reschedule (?<event_type>gold|alliance) event/i)
event_type = md['event_type'].capitalize + ' Event'
interval = GOLD_EVENT_INTERVAL
interval = ALLIANCE_EVENT_INTERVAL if event_type == 'Alliance Event'
events = scheduled_events(clear_cache: false, include_expired: true, restrict_types: [event_type], sort: :time, bot_event: bot_event)
events.each do |event|
old_time = event[:time]
event[:time] = Chronic.parse(interval, now: old_time)
event['ISO8601'] = event[:time].iso8601
msg += "Rescheduling #{event['name']} from #{old_time} to #{event[:time]}\n"
end
store_events(@scheduled_events)
split_message(msg).each do |split_msg|
bot_event.respond(split_msg)
end
end
end
def split_message(msg, max_msg_size=1950)
return [msg] if msg.size <= max_msg_size
split_msg = []
msg.split(/\n/).each do |sentence|
current_msg = ''
sentence.split.each do |word|
if current_msg.size + word.size > max_msg_size
current_msg += '[...]'
split_msg << current_msg
current_msg = ''
end
current_msg += " #{word}"
current_msg.strip
end
split_msg << current_msg
end
return split_msg
end
def include_private?(bot_event)
channels('alliance', 'control').map{|c|c.downcase}.include?('#' + bot_event.channel.name.to_s.downcase)
end
bot.message(start_with: /!next \d+ events/, in: channels('all')) do |bot_event|
clear_cache = bot_event.content.include?('clearcache') && bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
amount = bot_event.content.match(/!next (\d+) events/)[1].to_i
amount = 1 if amount <= 0
amount = 10 if amount > 30
upcoming_events = scheduled_events(include_expired: false, sort: :time, clear_cache: clear_cache, include_private: include_private?(bot_event))[0,amount]
unless upcoming_events.empty?
msg = "#{bot_event.user.mention}: I found #{upcoming_events.count} events:\n"
upcoming_events.each do |target_event|
prefix = ''
prefix = '[AE] ' if target_event['type'] == 'Alliance Event'
prefix = '[GE] ' if target_event['type'] == 'Gold Event'
msg += "\n#{prefix}#{target_event['name']}: #{target_event[:time]} (about #{time_difference(target_event)} from now)."
msg += " [UNCONFIRMED]" if target_event['status'] == 'unconfirmed' rescue false
msg += "\n"
end
msg += "\nThis does not include private alliance events as this is a public channel." unless include_private?(bot_event)
split_message(msg, 1950).each do |msg|
bot_event.respond msg
end
else
bot_event.respond "Sorry #{bot_event.user.mention}, there's currently no next event scheduled yet."
end
end
bot.message(start_with: ['!next stage', /(what|when)(’|'| i)s next stage/, /what stage(’|'| i)s next/], in: channels('all')) do |bot_event|
clear_cache = bot_event.content.include?('clearcache') && bot_event.user.roles.any?{|role|['R4', 'R5'].include?(role.name)}
grouped_events = scheduled_events(include_expired: false, sort: :time, restrict_types: ['Gold Event', 'Alliance Event'], clear_cache: clear_cache).group_by{|scheduled_event|scheduled_event['type']}
grouped_events.each do |event_type, events|
if target_event = events.first
msg = "#{bot_event.user.mention}: Next #{event_type} stage is #{target_event['name']} and is currently scheduled for #{target_event[:time]}, which is in about #{time_difference(target_event)}."
bot_event.respond check_confirmed(target_event, msg)
else
bot_event.respond "Sorry #{bot_event.user.mention}, there's currently no next #{event_type} stage scheduled yet."
end
end
end
bot.message(start_with: [REGEX_WALL_CALCULATOR], in: channels('spam', 'control')) do |bot_event|
md = bot_event.content.match(REGEX_WALL_CALCULATOR)
msg = "#{bot_event.user.mention},\n"
if md['wall_defense']
seconds_to_zero_current_wall_defense = md['current_wall_defense'].to_i / WALL_DMG_PER_MINUTE * 60
nr_of_hits_to_zero_current_wall_defense = seconds_to_zero_current_wall_defense / 60 / 30
seconds_to_zero_max_wall_defense = md['max_wall_defense'].to_i / WALL_DMG_PER_MINUTE * 60 rescue nil
nr_of_hits_to_zero_max_wall_defense = seconds_to_zero_max_wall_defense / 60 / 30 rescue nil
msg += "Your wall's #{md['current_wall_defense'].to_i} *current* defense will reach zero in about #{nr_of_hits_to_zero_current_wall_defense.ceil} hits, which will keep it burning for #{ChronicDuration.output(seconds_to_zero_current_wall_defense)}.\n\n"
msg += "Your wall's #{md['max_wall_defense'].to_i} *max* defense will reach zero in about #{nr_of_hits_to_zero_max_wall_defense.ceil} hits, which will keep it burning for #{ChronicDuration.output(seconds_to_zero_max_wall_defense)}.\n\n" if seconds_to_zero_max_wall_defense
end
if md['duration']
duration = ChronicDuration.parse(md['duration'])
total_damage_over_time = ((duration / 60) * WALL_DMG_PER_MINUTE).to_i
msg += "Your wall will take ***#{total_damage_over_time} damage*** in total over the #{ChronicDuration.output(duration)} of burning.\n"
if md['wall_defense']
if seconds_to_zero_current_wall_defense <= duration
msg += "Your wall's current defenses will reach zero in #{ChronicDuration.output(seconds_to_zero_current_wall_defense)} if you don't stop it from burning.\n"
else
msg += "It looks like your wall would survive the burning. After it's finished burning, it will have #{md['current_wall_defense'].to_i - total_damage_over_time} defense points left.\n"
end
end
end
bot_event.respond msg
end
bot.message(content: '!about') do |bot_event|
bot_event.respond "This bot is created by FiXato (https://profile.fixato.org), and uses meew0's discordrb bot framework (https://github.com/meew0/discordrb), as well as the oj, multi-json, chronic and chronic-duration Ruby gems. If you like what this bot does, donations for its upkeep and development are welcome at <https://PayPal.Me/FiXatoNL> :joy:\nThe source code is available at: #{REPOSITORY_URL}"
end
bot.message(content: '!source') do |bot_event|
bot_event.respond "The source code is available at: #{REPOSITORY_URL}"
end
module CoordinateBookmarks
BOOKMARKS_SOURCE = get_value_from_arguments(option_name: '--bookmarks-source', env_key: 'BOOKMARKS_SOURCE', default: './.bookmarks.json').freeze
def self.bookmarks
return @bookmarks if @bookmarks
return @bookmarks = {} unless bookmarks = load_config(source_file: BOOKMARKS_SOURCE)
@bookmarks = {}
bookmarks.each do |coords, bookmark|
bookmark = Bookmark.json_create(bookmark)
@bookmarks[bookmark.coords] = bookmark
end
@bookmarks
end
class Bookmark
attr_reader :type, :name, :x, :y, :created_by, :created_on, :last_modified_by, :last_modified_on, :alliance_tag
def initialize(type:, name:, x:, y:, created_by:, created_on: Time.now, last_modified_by: nil, last_modified_on: Time.now, alliance_tag: nil)
@type = type
@name = name
@x = x
@y = y
@created_by = created_by
@created_on = created_on.kind_of?(Time) ? created_on : Chronic.parse(created_on)
@last_modified_by = last_modified_by
@last_modified_on = last_modified_on.kind_of?(Time) ? last_modified_on : Chronic.parse(last_modified_on)
@alliance_tag = alliance_tag
end
def id
"#{alliance_tag}#{type}#{name}"
end
def coords
[x, y].join(':')
end
def to_s
msg = "#{coords} [#{alliance_tag}] #{name} #{type}\n Created on #{created_on} by #{created_by}\n"
msg += " Last modified on #{last_modified_on} by #{last_modified_by}\n" if last_modified_by
msg
end
def to_json(*a)
a.first[:indent] = ' ' * a.first[:indent] if a.first[:indent].kind_of?(Integer)
{ json_class: self.class.name,
type: @type,
name: @name,
x: @x,
y: @y,
created_by: @created_by,
created_on: @created_on.to_s,
last_modified_by: @modified_by,
last_modified_on: @modified_on.to_s,
alliance_tag: @alliance_tag
}.to_json(*a)
end
def self.json_create(o)
from_json = new(
x: o['x'],
y: o['y'],
type: o['type'],
name: o['name'],
alliance_tag: o['alliance_tag'],
created_by: o['created_by'],
last_modified_by: o['last_modified_by'],
created_on: Chronic.parse(o['created_on']),
last_modified_on: Chronic.parse(o['last_modified_on'])
)
end
end
extend Discordrb::Commands::CommandContainer
def self.add_bookmark(keywords)
bookmark = Bookmark.new(**keywords)
bookmarks[bookmark.coords] = bookmark
save_config(source_file: BOOKMARKS_SOURCE, config: bookmarks, pretty_print: true, backup: true)
bookmark
end
def self.normalise_bookmark_type(bookmark_type)
if bookmark_type.match(/(?:alliance ?)?fort/i)
bookmark_type = 'fort'
elsif bookmark_type.match(/poi|point of interest/i)
bookmark_type = 'POI'
elsif bookmark_type.match(/(?:dragon ?)?alt[ae]r/i)
bookmark_type = 'altar'
elsif bookmark_type.match(/(?<storehouse>(?:(?:safe|store|storage) ?house)|storage)/i)
bookmark_type = 'storehouse'
end
return bookmark_type
end
# class Alliance
# attr_accessor :alliance_tag, :name, :motto, :rss_types
# def initialize(alliance_tag, name: nil, motto: nil, rss_types: nil)
# @alliance_tag = alliance_tag
# @name = name
# @motto = motto
# @rss_types = rss_types.select{|rss_type| [:food, :wood, :iron, :silver].include?(rss_type)}
# end
# end
# class AllianceGroup
# attr_accessor :name, :main_alliance, :sub_alliances
# def initialize(main_alliance:, name: nil, sub_alliances: [])
# @main_alliance = main_alliance
# @name = name || main_alliance.name
# @sub_alliances = [sub_alliances].flatten.compact
# end
# end
command :fort do |bot_event, alliance_tag, x, y, *name|
name = name.empty? ? "Alliance Fortress" : name.join(' ')
bookmark = add_bookmark({
type: normalise_bookmark_type('fort'),
name: name,
created_by: bot_event.user.mention,
x: x,
y: y,
alliance_tag: alliance_tag
})
bot_event.respond "#{bot_event.user.mention}, I've added the following bookmark:\n\n#{bookmark}"
end
command :whereare do |bot_event, alliance_tag|
results = bookmarks.select{|coords, bookmark|bookmark.alliance_tag.downcase == alliance_tag.downcase rescue false}
bot_event.respond "#{bot_event.user.mention}, I've found #{results.size} bookmarks:\n\n#{results.values.join("\n\n")}"
end
command :whereis do |bot_event, keyword|
results = bookmarks.select{|coords, bookmark|(bookmark.alliance_tag.downcase.include?(keyword.downcase) rescue false) || (bookmark.name.downcase.include?(keyword.downcase))}
bot_event.respond "#{bot_event.user.mention}, I've found #{results.size} bookmarks:\n\n#{results.values.join("\n\n")}"
end
command :bookmark do |bot_event, action, *args|
case action
when 'add'
if md = args.join(' ').match(/(?<type>(?:alliance ?)?fort|hive|castle|poi|point of interest|avalon|misc|portal|(?<storehouse>(?:(?:safe|store|storage) ?house)|storage)|(?:dragon ?)?alt[ae]r) (?<coords>(?<x>\d{1,4})[: ,](?<y>\d{1,4})) (?:\[(?<alliance_tag>[a-z0-9]{3})\] ?)?(?<name>.+)/i)
keywords = {created_by: bot_event.user.mention}
[:type, :name, :x, :y, :alliance_tag].each do |key|
keywords[key] = md[key.to_s]
end
keywords[:type] = normalise_bookmark_type(keywords[:type])
bookmark = add_bookmark(keywords)
bot_event.respond "#{bot_event.user.mention}, I've added the following bookmark:\n\n#{bookmark}"
else
bot_event.respond "#{bot_event.user.mention}, when adding bookmarks, request should be in the format:\n!bookmark add type coords [tag] name\nor:\n!bookmark add type coords name\nEx:\n\n!bookmark add fort 1234:567 [d0a] Dead 0r Alive"
end
break
when 'list'
if bookmarks.empty?
bot_event.respond "#{bot_event.user.mention}, I could not find any bookmarks"
else
bot_event.respond "#{bot_event.user.mention}, I've found the following bookmarks:\n\n#{bookmarks.values.join("\n\n")}"
end
break
when 'remove'
when 'delete'
when 'del'
when 'rm'
bot_event.respond "#{bot_event.user.mention}, placeholder for removing bookmarks."
break
else
bot_event.respond "#{bot_event.user.mention}, that's an unsupported action."
break
end
end
end
bot.include! JoinAnnouncer
bot.include! CoordinateBookmarks
bot.run