-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
105 lines (86 loc) · 4.06 KB
/
bot.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
import discord
from discord import app_commands
from discord.ext import commands
import requests
import datetime
bot = commands.Bot(command_prefix="!", intents = discord.Intents.all(),status=discord.Status.dnd, activity=discord.Activity(type=discord.ActivityType.listening, name="orders"))
@bot.event
async def on_ready():
print('Bot is online.')
try:
synced = await bot.tree.sync()
print(f"Synced {len(synced)} command(s)")
except Exception as e:
print(e)
@bot.tree.command(name="planetinfo", description="This commands shows all active planets we are fighting for and their status and description.")
async def PlanetInfo(interaction: discord.Interaction):
response = requests.get("https://helldiverstrainingmanual.com/api/v1/war/campaign")
if response.status_code == 200:
data = response.json()
embeds = []
for planet in data:
color = get_color(int(planet['percentage']))
embed = discord.Embed(
title=planet["name"],
color=color
)
embed.add_field(name="Faction", value=planet["faction"], inline=False)
embed.add_field(name="Players", value=planet["players"], inline=False)
embed.add_field(name="Health", value=f"{planet['health']}/{planet['maxHealth']}", inline=False)
embed.add_field(name="Percentage", value=f"{planet['percentage']}%", inline=False)
# Check if biome information is available
if planet["biome"] is not None:
embed.add_field(name="Biome", value=planet["biome"]["description"], inline=False)
else:
embed.add_field(name="Biome", value="No biome information available", inline=False)
embeds.append(embed)
if embeds:
total_planets = len(data)
total_embed = discord.Embed(
title="Total Active Planets being fought:",
color=discord.Color.green()
)
total_embed.add_field(name="Total Planets", value=total_planets, inline=False)
await interaction.response.send_message(embed=embeds[0]) # Sending the first embed
for embed in embeds[1:]: # Sending subsequent embeds in separate messages
await interaction.followup.send(embed=embed)
await interaction.followup.send(embed=total_embed) # Sending total planets info
else:
await interaction.response.send_message("No data available.")
else:
await interaction.response.send_message("Failed to fetch data from the API")
def get_color(percentage):
if percentage > 55:
return discord.Color.brand_green()
elif 45 <= percentage <= 55:
return discord.Color.default()
else:
return discord.Color.brand_red()
@bot.tree.command(name="news", description="This commands shows 10 latest news from the ingame news feed from the HD2 API.")
async def News(interaction: discord.Interaction):
response = requests.get("https://helldiverstrainingmanual.com/api/v1/war/news")
if response.status_code == 200:
data = response.json()
embeds = []
for news_item in data:
embed = discord.Embed(
title=f"News ID: {news_item['id']}",
description=news_item['message'],
color=discord.Color.green()
)
embeds.append(embed)
if embeds:
await interaction.response.send_message(embeds=embeds)
else:
await interaction.response.send_message("No news available.")
else:
await interaction.response.send_message("Failed to fetch news from the API")
def convert_timestamp(timestamp):
try:
# Convert the timestamp to datetime object
return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
except Exception as e:
# Print any errors that occur during conversion
print(f"Error converting timestamp: {e}")
return "Invalid Timestamp"
bot.run("token")