-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfootball_bot.py
166 lines (124 loc) · 6.56 KB
/
football_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
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
import os
import requests
import discord
from discord.ext import commands
from discord.utils import find
from datetime import date, timedelta, datetime
from table2ascii import table2ascii as t2a, PresetStyle
CURRENT_DATE = date.today()
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
top_5_ligues_array = [{"premier_league": 2021}, {"ligue_one": 2015}, {"bundesliga": 2002},
{"serie_a": 2019}, {"primeira_liga": 2017}]
endpoint_headers = {
"X-Auth-Token": os.environ.get("API_KEY")
}
endpoint_params = {
"lmit": 1,
"dateFrom": str(CURRENT_DATE),
"dateTo": str(CURRENT_DATE + timedelta(days=30))
}
# Handles errors
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
embed = discord.Embed(title="Oops!",
description="The command you inputed has missing arguments. " \
"Please check `$instructions` for more information",
colour=discord.Colour(0x4a90e2))
await ctx.send(embed=embed)
elif isinstance(error, commands.CommandNotFound):
embed = discord.Embed(title="Sorry!",
description="This command does not exist. " \
"Please check `$instructions` for more information",
colour=discord.Colour(0x4a90e2))
await ctx.send(embed=embed)
# Send a greetings message and how to start using it
@bot.event
async def on_guild_join(guild):
general = discord.utils.get(guild.text_channels, name="general")
if general and general.permissions_for(guild.me).send_messages:
embed = discord.Embed(title="Hello!",
description="To start using my features, use `$instructions`",
colour=discord.Colour(0x4a90e2))
await general.send(embed=embed)
else:
available_text_channels = [channel for channel in guild.text_channels
if channel.permissions_for(guild.me).send_messages]
if available_text_channels:
first_available_channel = available_text_channels[0]
embed = discord.Embed(title="Hello!",
description="To start using my features, use `$instructions`",
colour=discord.Colour(0x4a90e2))
await first_available_channel.send(embed=embed)
# Minimal how to use the bot message
@bot.command(name="instructions")
async def help(ctx):
embed = discord.Embed(title="Instructions",
description="With this bot you can get the next match of your favourite team!." \
"\nAll the commands are in lower case and snake case",
colour=discord.Colour(0x4a90e2))
embed.add_field(name="$list_teams",
value="This command expects one of the top 5 leagues as parameters of the command," \
"and as output gives you a list of all the teams of that league with their respective ids." \
"\n**Example**: ```$list_teams premier_league```")
embed.add_field(name="$next_match",
value="With the previously gotten id you can get the next match of that team." \
"\n**Example**: ```$next_match 61```")
await ctx.send(embed=embed)
# Get top 5 leagues teams with id, league name and league id
@bot.command(name="list_teams")
async def list_teams(ctx, inputed_league):
body_array = []
for league in top_5_ligues_array:
if list(league.keys())[0] == inputed_league:
response = requests.get(f"https://api.football-data.org/v4/competitions/{list(league.values())[0]}/teams",
headers=endpoint_headers)
teams = response.json()["teams"]
for team in teams:
body_array.append([team["name"], team["id"]])
table = t2a(
header=["Team", "Team Id"],
body=body_array,
style=PresetStyle.thin_compact
)
await ctx.send(f"```\n{table}\n```")
# Get Team next match
@bot.command(name="next_match")
async def list_teams(ctx, team_id):
response = requests.get(f"https://api.football-data.org/v4/teams/{team_id}/matches/",
headers=endpoint_headers, params=endpoint_params)
output = response.json()
home_team_logo = output["matches"][0]["homeTeam"]["crest"]
home_team_name = output["matches"][0]["homeTeam"]["name"]
away_team_logo = output["matches"][0]["awayTeam"]["crest"]
away_team_name = output["matches"][0]["awayTeam"]["name"]
timestamp = output["matches"][0]["utcDate"]
datetime_obj = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ")
formatted_date = datetime_obj.strftime("%d/%m")
datetime_now = datetime.utcnow()
remaining_time_until_match = (datetime_obj - datetime_now)
if "day" in str(remaining_time_until_match):
days_str, time_str = str(remaining_time_until_match).split(",")
remaining_days = days_str.split()[0]
remaining_hours = time_str.split(":")[0]
description_text = f"Remaining time until the match, {remaining_days} days and {remaining_hours} hours"
elif timedelta(hours=1) > remaining_time_until_match:
description_text = f"Less than 1 hour remaining for the match"
else:
remaining_hours = str(remaining_time_until_match).split(":")[0]
description_text = f"Remaining time until the match, {remaining_hours} hours"
r = requests.get(f"https://api.football-data.org/v4/teams/{team_id}", headers=endpoint_headers)
team_name = r.json()["name"]
embed = discord.Embed(title=f"{team_name} next match is on the day {formatted_date}",
description=description_text,
colour=discord.Colour(0x4a90e2), url="https://manumafe98.github.io/cv/")
embed.add_field(name="Home Team", value=home_team_name, inline=True)
embed.add_field(name="Away Team", value=away_team_name, inline=True)
embed1 = discord.Embed(url="https://manumafe98.github.io/cv/")
embed2 = discord.Embed(url="https://manumafe98.github.io/cv/")
embed1.set_image(url=home_team_logo)
embed2.set_image(url=away_team_logo)
await ctx.send(embeds=[embed, embed1, embed2])
bot.run(os.environ.get("BOT_TOKEN"))