-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.py
791 lines (773 loc) · 36.9 KB
/
sw.py
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
import json
from typing import Union
import aiohttp
import discord
from discord.ext import commands
from discord.ext.commands import Bot
from cogs.utils.test import DEFAULT_CONTROLS, menu
from image import (
HUMANDESCRIPTION,
IMAGES,
PLANETDESCRIPTION,
PLANETS,
PLANETTHUMBNAIL,
SPECIESDESCRIPTION,
SPECIESTHUMBNAIL,
STARSHIPDESCRIPTIONS,
STARSHIPSIMAGES,
VEHICLEDESCRIPTION,
VEHICLEIMAGE,
)
class SW(commands.Cog):
"""Interact with the Star Wars API"""
def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession()
def cog_unload(self):
self.__unload()
def __unload(self):
self.session.detach()
@commands.bot_has_permissions(embed_links=True)
@commands.group(name="swapi", aliases=["starwars"])
async def starwars(self, ctx):
"""Group command for interacting with the Star Wars API"""
pass
@starwars.command()
async def person(self, ctx, person_id: Union[int, str]):
"""Gets the profile of a person by their ID"""
if isinstance(person_id, int):
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/people/" + str(person_id)
)
if response.status == 404:
return await ctx.send("Invalid Person ID.")
person = json.loads(await response.text())
embed = discord.Embed(
title=f"Person: {person['name']}",
description=HUMANDESCRIPTION[person["name"]],
color=0x32CD32,
)
embed.add_field(name="ID:", value=str(person_id))
for key, value in person.items():
if key in [
"name",
"homeworld",
"films",
"species",
"vehicles",
"starships",
"created",
"edited",
"url",
]:
continue
embed.add_field(
name=key.replace("_", " ").title(), value=value.title()
)
embed.set_thumbnail(url=IMAGES[person["name"]])
homeworld_num = int(person["homeworld"].split(r"/")[-2])
homeworld = await self.session.get(person["homeworld"])
homeworld = json.loads(await homeworld.text())
embed.add_field(
name="Homeworld",
value=f"Name: {homeworld['name']}; ID: {str(homeworld_num)}",
)
films = []
for film in person["films"]:
film_num = int(film.split(r"/")[-2])
response = await self.session.get(film)
film = json.loads(await response.text())
films.append(f"Title: {film['title']}; ID: {str(film_num)}")
if len(films) != 0:
embed.add_field(name="Films:", value="\n".join(films))
if person["species"]:
species_num = int(person["species"][0].split(r"/")[-2])
species = await self.session.get(person["species"][0])
species = json.loads(await species.text())
embed.add_field(
name="Species",
value=f"Name: {species['name']}; ID: {str(species_num)}",
)
else:
embed.add_field(name="Species", value="Name: Unknown")
vehicles = []
for vehicle in person["vehicles"]:
vehicle_num = int(vehicle.split(r"/")[-2])
response = await self.session.get(vehicle)
vehicle = json.loads(await response.text())
vehicles.append(f"Name: {vehicle['name']}; ID: {str(vehicle_num)}")
if len(vehicles) != 0:
embed.add_field(name="Vehicles:", value="\n".join(vehicles))
starships = []
for starship in person["starships"]:
starship_num = int(starship.split(r"/")[-2])
response = await self.session.get(starship)
starship = json.loads(await response.text())
starships.append(
f"Name: {starship['name']}; ID: {str(starship_num)}"
)
if len(starships) != 0:
embed.add_field(name="Starships:", value="\n".join(starships))
await ctx.send(embed=embed)
else:
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/people/?search=" + str(person_id)
)
if response.status == 404:
return await ctx.send("Invalid Person ID.")
person = json.loads(await response.text())
name = person["results"][0]["name"]
embed = discord.Embed(
title=f"Person: {name}",
description=HUMANDESCRIPTION[name],
color=0x32CD32,
)
for key, value in person["results"][0].items():
if key in [
"name",
"homeworld",
"films",
"species",
"vehicles",
"starships",
"created",
"edited",
"url",
]:
continue
embed.add_field(
name=key.replace("_", " ").title(), value=value.title()
)
embed.set_thumbnail(url=IMAGES[name])
homeworld_num = int(person["results"][0]["homeworld"].split(r"/")[-2])
homeworld = await self.session.get(person["results"][0]["homeworld"])
homeworld = json.loads(await homeworld.text())
embed.add_field(
name="Homeworld",
value=f"Name: {homeworld['name']}; ID: {str(homeworld_num)}",
)
films = []
for film in person["results"][0]["films"]:
film_num = int(film.split(r"/")[-2])
response = await self.session.get(film)
film = json.loads(await response.text())
films.append(f"Title: {film['title']}; ID: {str(film_num)}")
if len(films) != 0:
embed.add_field(name="Films:", value="\n".join(films))
if person["results"][0]["species"]:
species_num = int(
person["results"][0]["species"][0].split(r"/")[-2]
)
species = await self.session.get(person["species"][0])
species = json.loads(await species.text())
embed.add_field(
name="Species",
value=f"Name: {species['name']}; ID: {str(species_num)}",
)
else:
embed.add_field(name="Species", value="Name: Unknown")
vehicles = []
for vehicle in person["results"][0]["vehicles"]:
vehicle_num = int(vehicle.split(r"/")[-2])
response = await self.session.get(vehicle)
vehicle = json.loads(await response.text())
vehicles.append(f"Name: {vehicle['name']}; ID: {str(vehicle_num)}")
if len(vehicles) != 0:
embed.add_field(name="Vehicles:", value="\n".join(vehicles))
starships = []
for starship in person["results"][0]["starships"]:
starship_num = int(starship.split(r"/")[-2])
response = await self.session.get(starship)
starship = json.loads(await response.text())
starships.append(
f"Name: {starship['name']}; ID: {str(starship_num)}"
)
if len(starships) != 0:
embed.add_field(name="Starships:", value="\n".join(starships))
await ctx.send(embed=embed)
@starwars.command()
async def planet(self, ctx, planet_id: Union[int, str]):
"""Gets the profile of a planet by their ID"""
if isinstance(planet_id, int):
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/planets/" + str(planet_id)
)
if response.status == 404:
return await ctx.send("Invalid Planet ID.")
planet = json.loads(await response.text())
embed = discord.Embed(
title=f"Planet: {planet['name']}",
description=PLANETDESCRIPTION[planet["name"]],
color=0x800080,
)
embed.add_field(name="ID:", value=str(planet_id))
for key, value in planet.items():
if key in [
"name",
"residents",
"films",
"edited",
"created",
"url",
]:
continue
embed.add_field(
name=key.replace("_", " ").title(), value=value.title()
)
embed.set_thumbnail(url=PLANETTHUMBNAIL[planet["name"]])
embed.set_image(url=PLANETS[planet["name"]])
films = []
for film in planet["films"]:
film_num = int(film.split(r"/")[-2])
response = await self.session.get(film)
film = json.loads(await response.text())
films.append(f"Title: {film['title']}; ID: {str(film_num)}")
if len(films) != 0:
embed.add_field(name="Films:", value="\n".join(films))
residents = []
for resident in planet["residents"]:
resident_num = int(resident.split(r"/")[-2])
response = await self.session.get(resident)
resident = json.loads(await response.text())
residents.append(
f"Name: {resident['name']}; ID: {str(resident_num)}"
)
if len(residents) != 0:
embed.add_field(name="Residents:", value="\n".join(residents))
await ctx.send(embed=embed)
else:
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/planets/?search=" + str(planet_id)
)
if response.status == 404:
return await ctx.send("Invalid Planet ID.")
planet = json.loads(await response.text())
name = planet["results"][0]["name"]
embed = discord.Embed(
title=f"Planet: {name}",
description=PLANETDESCRIPTION[name],
color=0x800080,
)
for key, value in planet["results"][0].items():
if key in [
"name",
"residents",
"films",
"edited",
"created",
"url",
]:
continue
embed.add_field(
name=key.replace("_", " ").title(), value=value.title()
)
embed.set_thumbnail(url=PLANETTHUMBNAIL[name])
embed.set_image(url=PLANETS[name])
films = []
for film in planet["results"][0]["films"]:
film_num = int(film.split(r"/")[-2])
response = await self.session.get(film)
film = json.loads(await response.text())
films.append(f"Title: {film['title']}; ID: {str(film_num)}")
if len(films) != 0:
embed.add_field(name="Films:", value="\n".join(films))
residents = []
for resident in planet["results"][0]["residents"]:
resident_num = int(resident.split(r"/")[-2])
response = await self.session.get(resident)
resident = json.loads(await response.text())
residents.append(
f"Name: {resident['name']}; ID: {str(resident_num)}"
)
if len(residents) != 0:
embed.add_field(name="Residents:", value="\n".join(residents))
await ctx.send(embed=embed)
@starwars.command()
async def film(self, ctx, film_id: Union[int, str]):
"""Gets the info about a film by their ID"""
if isinstance(film_id, int):
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/films/" + str(film_id)
)
if response.status == 404:
return await ctx.send("Invalid Film ID.")
film = json.loads(await response.text())
embed = discord.Embed(
title=f"Film: {film['title']}; Page 1/4", color=0x0000FF
)
embed.add_field(name="ID:", value=str(film_id))
for key, value in film.items():
if key in [
"name",
"characters",
"planets",
"starships",
"vehicles",
"species",
"created",
"edited",
"url",
"opening_crawl",
]:
continue
value = value.title() if hasattr(value, "title") else value
embed.add_field(name=key.replace("_", " ").title(), value=value)
embed2 = discord.Embed(
title=f"Film: {film['title']}; Page 2/4", color=0x0000FF
)
embed2.add_field(name="Opening Crawl", value=film["opening_crawl"])
embed3 = discord.Embed(
title=f"Film: {film['title']}; Page 3/4", color=0x0000FF
)
residents = []
for resident in film["characters"]:
resident_num = int(resident.split(r"/")[-2])
response = await self.session.get(resident)
resident = json.loads(await response.text())
residents.append(
f"Name: {resident['name']}; ID: {str(resident_num)}"
)
if len(residents) != 0:
embed3.add_field(name="Characters:", value="\n".join(residents))
planets = []
for planet in film["planets"]:
planet_num = int(planet.split(r"/")[-2])
response = await self.session.get(planet)
planet = json.loads(await response.text())
planets.append(f"Name: {planet['name']}; ID: {str(planet_num)}")
if len(planets) != 0:
embed3.add_field(name="Planets:", value="\n".join(planets))
embed4 = discord.Embed(
title=f"Film: {film['title']}; Page 4/4", color=0x0000FF
)
objects = []
for entry in film["starships"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed4.add_field(name="Starships:", value="\n".join(objects))
objects = []
for entry in film["vehicles"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed4.add_field(name="Vehicles:", value="\n".join(objects))
objects = []
for entry in film["species"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed4.add_field(name="Species:", value="\n".join(objects))
embeds = [embed, embed2, embed3, embed4]
await menu(ctx, embeds, DEFAULT_CONTROLS)
else:
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/films/?search=" + str(film_id)
)
if response.status == 404:
return await ctx.send("Invalid Film ID.")
film = json.loads(await response.text())
name = film["results"][0]["title"]
embed = discord.Embed(title=f"Film: {name}; Page 1/4", color=0x0000FF)
for key, value in film["results"][0].items():
if key in [
"name",
"characters",
"planets",
"starships",
"vehicles",
"species",
"created",
"edited",
"url",
"opening_crawl",
]:
continue
value = value.title() if hasattr(value, "title") else value
embed.add_field(name=key.replace("_", " ").title(), value=value)
embed2 = discord.Embed(title=f"Film: {name}; Page 2/4", color=0x0000FF)
embed2.add_field(
name="Opening Crawl", value=film["results"][0]["opening_crawl"]
)
embed3 = discord.Embed(title=f"Film: {name}; Page 3/4", color=0x0000FF)
residents = []
for resident in film["results"][0]["characters"]:
resident_num = int(resident.split(r"/")[-2])
response = await self.session.get(resident)
resident = json.loads(await response.text())
residents.append(
f"Name: {resident['name']}; ID: {str(resident_num)}"
)
if len(residents) != 0:
embed3.add_field(name="Characters:", value="\n".join(residents))
planets = []
for planet in film["results"][0]["planets"]:
planet_num = int(planet.split(r"/")[-2])
response = await self.session.get(planet)
planet = json.loads(await response.text())
planets.append(f"Name: {planet['name']}; ID: {str(planet_num)}")
if len(planets) != 0:
embed3.add_field(name="Planets:", value="\n".join(planets))
embed4 = discord.Embed(title=f"Film: {name}; Page 4/4", color=0x0000FF)
objects = []
for entry in film["results"][0]["starships"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed4.add_field(name="Starships:", value="\n".join(objects))
objects = []
for entry in film["results"][0]["vehicles"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed4.add_field(name="Vehicles:", value="\n".join(objects))
objects = []
for entry in film["results"][0]["species"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed4.add_field(name="Species:", value="\n".join(objects))
embeds = [embed, embed2, embed3, embed4]
await menu(ctx, embeds, DEFAULT_CONTROLS)
@starwars.command()
async def starship(self, ctx, starship_id: Union[int, str]):
"""Gets the profile of a starship by its ID"""
if isinstance(starship_id, int):
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/starships/" + str(starship_id)
)
if response.status == 404:
return await ctx.send("Invalid Starship ID.")
starship = json.loads(await response.text())
embed = discord.Embed(
title=f"Starship: {starship['name']}",
description=STARSHIPDESCRIPTIONS[starship["name"]],
color=0x000000,
)
embed.add_field(name="ID:", value=str(starship_id))
for key, value in starship.items():
if key in ["name", "films", "edited", "created", "url", "pilots"]:
continue
embed.add_field(
name=key.replace("_", " ").title(), value=value.title()
)
embed.set_image(url=STARSHIPSIMAGES[starship["name"]])
objects = []
for entry in starship["films"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['title']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="Films:", value="\n".join(objects))
objects = []
for entry in starship["pilots"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="Pilots:", value="\n".join(objects))
await ctx.send(embed=embed)
else:
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/starships/?search=" + str(starship_id)
)
if response.status == 404:
return await ctx.send("Invalid Starship ID.")
starship = json.loads(await response.text())
name = starship["results"][0]["name"]
embed = discord.Embed(
title=f"Starship: {name}",
description=STARSHIPDESCRIPTIONS[name],
color=0x000000,
)
for key, value in starship["results"][0].items():
if key in ["name", "films", "edited", "created", "url", "pilots"]:
continue
embed.add_field(
name=key.replace("_", " ").title(), value=value.title()
)
objects = []
embed.set_image(url=STARSHIPSIMAGES[name])
for entry in starship["results"][0]["films"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['title']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="Films:", value="\n".join(objects))
objects = []
for entry in starship["results"][0]["pilots"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="Pilots:", value="\n".join(objects))
await ctx.send(embed=embed)
@starwars.command()
async def vehicle(self, ctx, vehicle_id: Union[int, str]):
"""Gets the profile of a vehicle by its ID"""
if isinstance(vehicle_id, int):
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/vehicles/" + str(vehicle_id)
)
if response.status == 404:
return await ctx.send("Invalid Vehicle ID.")
vehicle = json.loads(await response.text())
embed = discord.Embed(
title=f"Vehicle: {vehicle['name']}",
description=VEHICLEDESCRIPTION[vehicle["name"]],
color=0x228B22,
)
embed.add_field(name="ID:", value=str(vehicle_id))
for key, value in vehicle.items():
if key in ["name", "films", "edited", "created", "url", "pilots"]:
continue
embed.add_field(
name=key.replace("_", " ").title(), value=value.title()
)
objects = []
embed.set_image(url=VEHICLEIMAGE[vehicle["name"]])
for entry in vehicle["films"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['title']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="Films:", value="\n".join(objects))
objects = []
for entry in vehicle["pilots"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="Pilots:", value="\n".join(objects))
await ctx.send(embed=embed)
else:
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/vehicles/?search=" + str(vehicle_id)
)
if response.status == 404:
return await ctx.send("Invalid Vehicle ID.")
vehicle = json.loads(await response.text())
name = vehicle["results"][0]["name"]
embed = discord.Embed(
title=f"Vehicle: {name}",
description=VEHICLEDESCRIPTION[name],
color=0x228B22,
)
for key, value in vehicle["results"][0].items():
if key in ["name", "films", "edited", "created", "url", "pilots"]:
continue
embed.add_field(
name=key.replace("_", " ").title(), value=value.title()
)
objects = []
embed.set_image(url=VEHICLEIMAGE[name])
for entry in vehicle["results"][0]["films"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['title']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="Films:", value="\n".join(objects))
objects = []
for entry in vehicle["results"][0]["pilots"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="Pilots:", value="\n".join(objects))
await ctx.send(embed=embed)
@starwars.command()
async def species(self, ctx, species_id: Union[int, str]):
"""Gets the profile of a species by its ID"""
if isinstance(species_id, int):
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/species/" + str(species_id)
)
if response.status == 404:
return await ctx.send("Invalid Species ID.")
species = json.loads(await response.text())
embed = discord.Embed(
title=f"Species: {species['name']}",
description=SPECIESDESCRIPTION[species["name"]],
color=0xD2B48C,
)
embed.add_field(name="ID:", value=str(species_id))
for key, value in species.items():
if key in [
"name",
"homeworld",
"films",
"people",
"edited",
"created",
"url",
]:
continue
embed.add_field(
name=key.replace("_", " ").title(), value=value.title()
)
embed.set_thumbnail(url=SPECIESTHUMBNAIL[species["name"]])
homeworld_num = int(species["homeworld"].split(r"/")[-2])
homeworld = await self.session.get(species["homeworld"])
homeworld = json.loads(await homeworld.text())
embed.add_field(
name="Homeworld",
value=f"Name: {homeworld['name']}; ID: {str(homeworld_num)}",
)
objects = []
for entry in species["films"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['title']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="Films:", value="\n".join(objects))
objects = []
for entry in species["people"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="People:", value="\n".join(objects))
await ctx.send(embed=embed)
else:
async with ctx.typing():
response = await self.session.get(
r"https://swapi.dev/api/species/?search=" + str(species_id)
)
if response.status == 404:
return await ctx.send("Invalid Species ID.")
species = json.loads(await response.text())
name = species["results"][0]["name"]
embed = discord.Embed(
title=f"Species: {name}",
description=SPECIESDESCRIPTION[name],
color=0xD2B48C,
)
embed.add_field(name="ID:", value=str(species_id))
for key, value in species["results"][0].items():
if key in [
"name",
"homeworld",
"films",
"people",
"edited",
"created",
"url",
]:
continue
embed.add_field(
name=key.replace("_", " ").title(), value=value.title()
)
embed.set_thumbnail(url=SPECIESTHUMBNAIL[name])
homeworld_num = int(species["results"][0]["homeworld"].split(r"/")[-2])
homeworld = await self.session.get(species["results"][0]["homeworld"])
homeworld = json.loads(await homeworld.text())
embed.add_field(
name="Homeworld",
value=f"Name: {homeworld['name']}; ID: {str(homeworld_num)}",
)
objects = []
for entry in species["results"][0]["films"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['title']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="Films:", value="\n".join(objects))
objects = []
for entry in species["results"][0]["people"]:
entry_num = int(entry.split(r"/")[-2])
response = await self.session.get(entry)
entry = json.loads(await response.text())
objects.append(f"Name: {entry['name']}; ID: {str(entry_num)}")
if len(objects) != 0:
embed.add_field(name="People:", value="\n".join(objects))
await ctx.send(embed=embed)
@starwars.group(name="all")
async def _all_group(self, ctx):
"""Get all people, planets, starships, vehicles, species or films of star wars"""
pass
@_all_group.command()
async def people(self, ctx):
"""Grabs all people in the star wars API.
This command does take a bit."""
async with ctx.typing():
data = []
query = "https://swapi.dev/api/people"
while True:
response = await self.session.get(query)
text = json.loads(await response.text())
data_two = text["results"]
data += data_two
if bool(text["next"]):
query = text["next"]
else:
break
persons_list = []
for person in data:
embed = discord.Embed(title=f"Person: {person['name']}", color=0x32CD32)
num = int(person["url"].split(r"/")[-2])
embed.add_field(name="ID:", value=str(num))
for key, value in person.items():
if key in [
"name",
"homeworld",
"films",
"species",
"vehicles",
"starships",
"created",
"edited",
"url",
]:
continue
embed.add_field(
name=key.replace("_", " ").title(), value=value.title()
)
homeworld_num = int(person["homeworld"].split(r"/")[-2])
embed.add_field(name="Homeworld", value=f"ID: {str(homeworld_num)}")
persons_list.append(embed)
persons_list.sort(
key=lambda x: int(
[
field
for field in x.to_dict()["fields"]
if field["name"] == "ID:"
][0]["value"]
)
)
await menu(ctx, persons_list, DEFAULT_CONTROLS)
def setup(bot):
bot.add_cog(SW(bot))