-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.rb
493 lines (458 loc) · 15.4 KB
/
command.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
# frozen_string_literal: true
require 'dotenv/load'
require 'discordrb'
require 'i18n'
require_relative './lib/pokecord/wild_pokemon'
require_relative './lib/pokecord/starter_pokemons'
require_relative './lib/pokecord/commands/pick'
require_relative './lib/pokecord/commands/catch'
require_relative './lib/pokecord/commands/select'
require_relative './lib/pokecord/commands/info'
require_relative './lib/pokecord/commands/nickname'
require_relative './lib/pokecord/commands/name_rival'
require_relative './lib/pokecord/commands/fight'
require_relative './lib/pokecord/commands/initiate_trade'
require_relative './lib/pokecord/commands/accept_trade'
require_relative './lib/pokecord/commands/modify_trade'
require_relative './lib/pokecord/commands/confirm_trade'
require_relative './lib/pokecord/commands/execute_trade'
require_relative './lib/pokecord/commands/list_pokemons'
require_relative './lib/pokecord/commands/alter_fav'
require_relative './lib/pokecord/commands/balance'
require_relative './lib/pokecord/commands/buy'
require_relative './lib/pokecord/commands/list_inventory'
require_relative './lib/pokecord/commands/use'
require_relative './lib/pokecord/commands/hold'
require_relative './lib/pokecord/commands/take'
# dnd commands
require_relative './lib/dnd/commands/assign_party_role'
# admin commands
require_relative './lib/pokecord/commands/admin/reset_balances'
# embed templates
require_relative './lib/pokecord/embed_templates/info'
require_relative './lib/pokecord/embed_templates/trade'
require_relative './lib/pokecord/embed_templates/shop_landing_page'
require_relative './lib/pokecord/embed_templates/shop_items_page'
require_relative './lib/pokecord/embed_templates/inventory_list'
require_relative './lib/pokecord/embed_templates/spawn_list'
require_relative './lib/callbacks/update_trade'
I18n.load_path << Dir[File.expand_path("config/locales") + "/*.yml"]
I18n.default_locale = :en
bot = Discordrb::Commands::CommandBot.new(
token: ENV["DISCORD_TOKEN"],
prefix: 'p!'
)
if ENV['DEVELOPER_ID']
bot.set_user_permission(ENV['DEVELOPER_ID'].to_i, 5)
end
bot.command :start do |event|
event.channel.send_embed do |embed|
embed.color = Pokecord::EmbedTemplates::EMBED_COLOR
embed.title = 'Welcome to Pokecord'
embed.description = <<~DESC
This is a Discord bot that
allows users to catch, trade,
and train Pokemon! First,
you need a starter Pokemon.
Pick one of the following
starters with the command
`p!pick [pokemon name]`
DESC
Pokecord::StarterPokemons.new.to_h.each do |region, pokemons|
poke_names = pokemons.map { |poke| poke.name }.join(', ')
embed.add_field(name: region, value: poke_names)
end
end
starter_file = File.expand_path('assets/starter_heart.jpg', File.dirname(__FILE__))
event.send_file(File.open(starter_file, 'r'), caption: 'Pick your pokemon')
end
bot.command(:pick) do |event, *args|
if args.length.zero?
"Correct usage of this command is `p!pick [name of a starter pokemon]`."
else
poke_name = args.join(' ').capitalize
pick_cmd = Pokecord::Commands::Pick.new(event.user.id.to_s, poke_name)
if pick_cmd.already_picked_starter?
"You have already picked a starter Pokemon!"
elsif pick_cmd.name_incorrect?
"Could not find a Pokemon with the name \"#{poke_name}\""
elsif pick_cmd.is_not_starter?
"Unfortunately, #{poke_name} is not a starter Pokemon. Please select a different Pokemon from the `p!start` list."
else
picked_pokemon = pick_cmd.call
"Congratulations, #{event.user.mention}! You have successfully picked a #{picked_pokemon.name} as your starting Pokemon!"
end
end
end
bot.command(:catch) do |event, *args|
if args.length.zero?
'Correct usage of this command is `p!catch [pokemon name]`. A wild Pokemon must be present in order for you to catch one.'
else
name_guess = args.join(' ')
catch_cmd = Pokecord::Commands::Catch.new(event, name_guess)
if catch_cmd.can_catch?
if catch_cmd.name_correct?
poke_spawn = catch_cmd.catch!
"Congratulations, #{event.user.mention}! You have successfully caught a level #{poke_spawn.level} **#{poke_spawn.pokemon.name}**!"
else
'That is not the right Pokemon!'
end
end
end
end
bot.command(:pokemon) do |event, given_page_num|
one_indexed_page_num = given_page_num&.to_i || 1
actual_page_num = one_indexed_page_num - 1
list_result = Pokecord::Commands::ListPokemons.new(
event.user.id.to_s,
actual_page_num
).call
if list_result.success?
list_payload = list_result.value!
embed = Pokecord::EmbedTemplates::SpawnList.new(event.user.name, list_payload).to_embed
event.channel.send_embed('', embed)
nil
else
list_result.failure
end
end
bot.command(:addfav) do |event, catch_number|
if catch_number.nil? || catch_number.to_i.zero?
I18n.t('alter_fav.add_argument_error')
else
alter_result = Pokecord::Commands::AlterFav.new(
event.user.id.to_s,
catch_number,
true
).call
alter_result.success? ? alter_result.value! : alter_result.failure
end
end
bot.command(:removefav) do |event, catch_number|
if catch_number.nil? || catch_number.to_i.zero?
I18n.t('alter_fav.remove_argument_error')
else
alter_result = Pokecord::Commands::AlterFav.new(
event.user.id.to_s,
catch_number,
false
).call
alter_result.success? ? alter_result.value! : alter_result.failure
end
end
bot.command(:fav) do |event, given_page_num|
one_indexed_page_num = given_page_num&.to_i || 1
actual_page_num = one_indexed_page_num - 1
list_result = Pokecord::Commands::ListPokemons.new(
event.user.id.to_s,
actual_page_num,
true
).call
if list_result.success?
list_payload = list_result.value!
embed = Pokecord::EmbedTemplates::SpawnList.new(event.user.name, list_payload).to_embed
event.channel.send_embed('', embed)
nil
else
list_result.failure
end
end
# TODO allow user to select on nickname or pokemon name
bot.command(:select) do |event, catch_number|
if catch_number.nil? || catch_number !~ /\A\d+\z/
'Correct usage of this command is `p!select [catch number]`'
else
select_cmd = Pokecord::Commands::Select.new(event.user.id.to_s, catch_number.to_i)
if select_cmd.valid_number?
spawned_poke = select_cmd.call
"#{event.user.mention}, you have selected your #{spawned_poke.pokemon.name}"
else
'Sorry, you do not have a Pokemon with that catch number.'
end
end
end
bot.command(:info) do |event|
info_result = Pokecord::Commands::Info.new(event.user.id.to_s).call
if info_result.success?
info_payload = info_result.value!
embed = Pokecord::EmbedTemplates::Info.new(event.user.name, info_payload).to_embed
event.channel.send_embed('', embed)
pokedex_display = info_payload.pokemon.stylized_pokedex_number
event.send_file(
File.open(
File.expand_path(
"./pokemon_info/images/#{pokedex_display}.png", File.dirname(__FILE__)
),
'r'
)
)
else
info_result.failure
end
end
bot.command(:nickname) do |event, *words|
if words.length.zero?
'Correct usage of this command is `p!nickname [one or more words]`.'
else
nickname = words.join(' ')
nickname_cmd = Pokecord::Commands::Nickname.new(event.user.id.to_s, nickname)
if nickname_cmd.no_pokemon_to_name?
"You do not have a Pokemon selected to nickname. Use `p!pokemon` to view all your Pokemon and `p!select` to choose which one you want to name."
elsif nickname_cmd.nickname_taken?
"You already have a Pokemon named **#{nickname}**! You cannot use a nickname for more than one Pokemon."
else
spawn = nickname_cmd.call
"#{event.user.mention}, you have successfully named your Pokemon \"**#{spawn.nickname}**\""
end
end
end
bot.command(:namerival) do |event, *words|
if words.length.zero?
I18n.t('name_rival.argument_error')
else
rival_name = words.join(' ')
naming_result = Pokecord::Commands::NameRival.new(event.user.id.to_s, rival_name).call
if naming_result.success?
"#{event.user.mention} #{naming_result.value!}"
else
naming_result.failure
end
end
end
bot.command(:fight) do |event, fight_code|
if fight_code.nil?
I18n.t('fight.incorrect_code')
else
fight_result = Pokecord::Commands::Fight.new(event.user.id.to_s, fight_code).call
if fight_result.success?
"#{event.user.mention}, #{fight_result.value!}"
else
fight_result.failure
end
end
end
bot.command(:trade) do |event, subcommand, arg1|
if subcommand.nil?
I18n.t('trade.subcommand_error')
else
case subcommand
when 'with'
if arg1.nil?
I18n.t('initiate_trade.argument_error')
else
result = Pokecord::Commands::InitiateTrade.new(event.user.id.to_s, arg1, event.user.name).call
if result.success?
"#{arg1}, you have been invited to trade with #{event.user.mention}. You can accept the invitation with `p!accept`."
else
result.failure
end
end
when 'add'
if arg1.nil?
I18n.t('add_to_trade.argument_error')
else
result = Pokecord::Commands::ModifyTrade.new(event.user.id.to_s, arg1, action: :add).call
if result.success?
trade = result.value!
old_message = event.channel.load_message(trade.message_discord_id)
if old_message.nil?
I18n.t('trade.discord_error')
else
old_message.edit('', Pokecord::EmbedTemplates::Trade.new(trade).to_embed)
end
nil
else
result.failure
end
end
when 'remove'
if arg1.nil?
I18n.t('remove_from_trade.argument_error')
else
result = Pokecord::Commands::ModifyTrade.new(event.user.id.to_s, arg1, action: :remove).call
if result.success?
trade = result.value!
old_message = event.channel.load_message(trade.message_discord_id)
if old_message.nil?
I18n.t('trade.discord_error')
else
old_message.edit('', Pokecord::EmbedTemplates::Trade.new(trade).to_embed)
end
nil
else
result.failure
end
end
else
I18n.t('trade.subcommand_error')
end
end
end
bot.command(:accept) do |event|
result = Pokecord::Commands::AcceptTrade.new(event.user.id.to_s, event.user.name).call
if result.success?
trade = result.value!
embed_message = event.channel.send_embed('', Pokecord::EmbedTemplates::Trade.new(trade).to_embed)
Callbacks::UpdateTrade.new(trade).call(message_discord_id: embed_message.id)
nil # don't send any message after the embed
else
result.failure
end
end
bot.command(:confirm) do |event|
confirm_result = Pokecord::Commands::ConfirmTrade.new(event.user.id.to_s).call
if confirm_result.success?
trade = confirm_result.value!
if trade.user_1_confirm && trade.user_2_confirm
execute_result = Pokecord::Commands::ExecuteTrade.new(trade.id).call
if execute_result.success?
trade_payload = execute_result.value!
messages = []
messages << "#{trade.user_1_name} and #{trade.user_2_name} have successfully exchanged Pokemon!"
trade_payload.evolution_payloads.each do |evo_payload|
familiar_name = evo_payload.spawned_pokemon.nickname || evo_payload.evolved_from.name
messages << "What!? #{evo_payload.evolved_from.name} is evolving! Your #{familiar_name} has evolved into **#{evo_payload.evolved_into.name}**!!"
end
messages.join("\n")
else
execute_result.failure
end
end
else
confirm_result.failure
end
end
bot.command(:balance) do |event|
bal_result = Pokecord::Commands::Balance.new(event.user.id.to_s).call
if bal_result.success?
"You have **#{bal_result.value!}** credits."
else
bal_result.failure
end
end
bot.command(:shop) do |event, page_num|
if page_num.nil?
embed = Pokecord::EmbedTemplates::ShopLandingPage.new.to_embed
event.channel.send_embed('', embed)
nil
elsif page_num.to_i.between?(1, 4)
embed = Pokecord::EmbedTemplates::ShopItemsPage.new(page_num.to_i).to_embed
event.channel.send_embed('', embed)
nil
else
I18n.t('shop.argument_error')
end
end
bot.command(:buy) do |event, *args|
if args.length.zero?
I18n.t('buy.argument_error')
else
amount = args.last =~ /\A\d+\z/ ? args.pop.to_i : 1
product_name = args.join(' ')
buy_result = Pokecord::Commands::Buy.new(event.user.id.to_s, product_name, amount).call
buy_result.success? ? "#{event.user.mention}, #{buy_result.value!}" : buy_result.failure
end
end
bot.command(:inventory) do |event|
result = Pokecord::Commands::ListInventory.new(event.user.id.to_s).call
if result.success?
inventory_items = result.value!
embed = Pokecord::EmbedTemplates::InventoryList.new(event.user.name, inventory_items).to_embed
event.channel.send_embed('', embed)
nil
else
result.failure
end
end
bot.command(:use) do |event, *args|
if args.length.zero?
I18n.t('use_item.argument_error')
else
product_name = args.join(' ')
use_item_result = Pokecord::Commands::Use.new(event.user.id.to_s, product_name).call
if use_item_result.success?
evo_payload = use_item_result.value!
familiar_name = evo_payload.spawned_pokemon.nickname || evo_payload.evolved_from.name
"#{event.user.mention}! What!? #{evo_payload.evolved_from.name} is evolving! Your #{familiar_name} has evolved into **#{evo_payload.evolved_into.name}**!!"
else
use_item_result.failure
end
end
end
bot.command(:hold) do |event, *args|
if args.length.zero?
I18n.t('hold.argument_error')
else
product_name = args.join(' ')
hold_result = Pokecord::Commands::Hold.new(
event.user.id.to_s,
product_name
).call
hold_result.success? ?
"#{event.user.mention}, #{hold_result.value!}" :
hold_result.failure
end
end
bot.command(:take) do |event|
take_result = Pokecord::Commands::Hold.new(event.user.id.to_s).call
take_result.success? ?
"#{event.user.mention}, #{take_result.value!}" :
take_result.failure
end
%w{
order
hint
pokedex
release
mega
moves
learn
replace
duel
bal
market
p
cancel
daily
silence
redeem
invite
server
patreon
appeal
}.each do |cmd|
bot.command cmd.to_sym do |event|
"The #{cmd} command has not been implemented yet. Check back soon for new features and updates!"
end
end
# D&D commands
bot.command :role do |event|
role_result = Dnd::Commands::AssignPartyRole.new(event.user.id.to_s).call
if role_result.success?
payload = role_result.value!
event.user.pm(I18n.t('dnd.assign_party_role', primary: payload.primary.name, secondary: payload.secondary.name))
"#{event.user.mention}, you have been sent a direct message with your randomized party role!"
else
role_result.failure
end
end
#########################
#
### Admin commands
#
#########################
bot.command(:wild, permission_level: 2) do |event|
wild_pokemon = Pokecord::WildPokemon.new
wild_pokemon.spawn!
event.send_file(File.open(wild_pokemon.pic_file, 'r'), caption: "A wild Pokemon appeared! You can try to catch it with `p!catch` (not implemented)")
end
bot.command(:admin, permission_level: 2) do |event, subcommand|
case subcommand
when 'reset_balances'
result = Pokecord::Commands::Admin::ResetBalances.new.call
result.success? ? result.value! : result.failure
else
I18n.t('admin.argument_error')
end
end
bot.run