-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsitrep.py
More file actions
110 lines (95 loc) · 4.87 KB
/
sitrep.py
File metadata and controls
110 lines (95 loc) · 4.87 KB
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
import discord, json, os
from dotenv import load_dotenv
from discord.ext import commands
from src.request import API
from src.compare import Hexagon
load_dotenv()
token = os.getenv('BOT_TOKEN')
bot = commands.Bot(command_prefix = os.getenv('PREFIX'), help_command=None, case_sensitive = True)
@bot.event
async def on_ready():
print(f'{bot.user} reporting for duty!')
@bot.command(name = "ping")
async def ping(ctx):
await ctx.send(f'{bot.user} is online!')
@bot.command(name = "report")
async def report(ctx):
await ctx.send("Command received! Gathering intel...")
with open("constants\shards.json", "r") as api_url:
data = json.load(api_url)
api = data['shards'][0]['url']
api_response = API.get_war_report(api)
if api_response is False:
raise commands.CommandError()
war_number = api_response["warNumber"]
days_at_war, enlistments, warden_casualties, colonial_casualties = API.get_total_casualties(api)
# Embed formatting
embed=discord.Embed(title=f"War {war_number} status report", description="Amount of casualties, enlistments and war duration")
embed.add_field(name="Enlistments", value=str("{:,}".format(enlistments)), inline=False)
embed.add_field(name="Colonials", value=str("{:,}".format(colonial_casualties)), inline=True)
embed.add_field(name="Wardens", value=str("{:,}".format(warden_casualties)), inline=True)
embed.add_field(name="Days at war", value=days_at_war, inline=False)
embed.set_footer(text="End of report")
await ctx.send(embed=embed)
@bot.command(name = "hex")
async def hex(ctx, hex_name):
with open("constants\shards.json", "r") as api_url:
data = json.load(api_url)
api = data['shards'][0]['url']
hex_name = Hexagon.search_hex_name(hex_name)
if hex_name:
full_hex_name = Hexagon.split_hex_name(hex_name)
await ctx.send(f'Hex identified: {full_hex_name}.\nGathering intel...')
enlistments, warden_casualties, colonial_casualties = API.get_hex_info(api, hex_name)
else:
raise commands.CommandError()
# Embed formatting
embed=discord.Embed(title=f"{full_hex_name} status report", description="Amount of casualties, enlistments and war duration")
embed.add_field(name="Enlistments", value=str("{:,}".format(enlistments)), inline=False)
embed.add_field(name="Colonials", value=str("{:,}".format(colonial_casualties)), inline=True)
embed.add_field(name="Wardens", value=str("{:,}".format(warden_casualties)), inline=True)
embed.set_footer(text="End of report")
await ctx.send(embed=embed)
@bot.command(name = "captures")
async def captures(ctx, hex_name):
await ctx.send("Command received! Gathering intel...")
with open("constants\shards.json", "r") as api_url:
data = json.load(api_url)
api = data['shards'][0]['url']
hex_name = Hexagon.search_hex_name(hex_name)
if hex_name:
full_hex_name = Hexagon.split_hex_name(hex_name)
await ctx.send(f'Hex identified: {full_hex_name}.\nGathering intel...')
else:
raise commands.CommandError()
territory = API.get_captured_structures(api, hex_name)
warden_icons, colonial_icons = Hexagon.decipher_icon_type(territory)
embed=discord.Embed(title=f"{full_hex_name} - Wardens", description=f'Amount of captured/constructed structures by the Wardens in {full_hex_name}')
for k,v in warden_icons.items():
embed.add_field(name=f'{k}', value=v, inline=True)
await ctx.send(embed=embed)
embed=discord.Embed(title=f"{full_hex_name} - Colonials", description=f'Amount of captured/constructed structures by the Colonials in {full_hex_name}')
for k,v in colonial_icons.items():
embed.add_field(name=f'{k}', value=v, inline=True)
embed.set_footer(text="End of report")
await ctx.send(embed=embed)
# Error handling
@bot.event
async def on_command_error(ctx, error):
"""Error handler"""
if isinstance(error, commands.CommandNotFound):
message = "This command does not exist."
elif isinstance(error, commands.CommandError):
message = "Failed to receive data. Try again in a few minutes."
elif isinstance(error, commands.CommandOnCooldown):
message = f"This command is on cooldown. Please try again after {round(error.retry_after, 1)} seconds."
elif isinstance(error, commands.MissingPermissions):
message = "You are missing the required permissions to run this command!"
elif isinstance(error, commands.BadArgument):
message = "Hex not identified. Try a different input or check your spelling."
elif isinstance(error, commands.UserInputError):
message = "Something about your input was wrong, please check your input and try again!"
else:
message = "Oh no! Something went wrong while running the command!"
await ctx.send(message, delete_after = 5)
bot.run(token)