-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (89 loc) · 3.29 KB
/
main.py
File metadata and controls
107 lines (89 loc) · 3.29 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
import os
import urllib.request
import ujson as json
import logging
from config import *
import discord
from discord.ext import commands, tasks
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
if LATEST_EXPORT.lower().startswith('http'):
req = urllib.request.Request(LATEST_EXPORT)
else:
raise ValueError from None
pass
with urllib.request.urlopen(req) as f:
ex = json.loads(f.read().decode('utf-8-sig'))
def get_season():
season = ex['gameAttributes']['season']
return season
def get_current_phase():
phase = ex['gameAttributes']['phase']
return phase
def get_teams():
teams = ex['teams']
return teams
def get_players():
players = ex['players']
return players
def get_schedule():
schedule = ex['schedule']
return schedule
def is_regseason(stat, season=get_season()):
return (stat['playoffs'] is False
and stat['season'] == season
and stat['gp'] > 0)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user.name}")
print(f"Discord.py API version: {discord.__version__}")
print("-------------------")
status_task.start()
@bot.event
async def on_command_error(context, error):
if isinstance(error, commands.CommandOnCooldown):
minutes, seconds = divmod(error.retry_after, 60)
hours, minutes = divmod(minutes, 60)
hours = hours % 24
embed = discord.Embed(
title="Hey, please slow down!",
description=f"You can use this command again in {f'{round(hours)} hours' if round(hours) > 0 else ''} {f'{round(minutes)} minutes' if round(minutes) > 0 else ''} {f'{round(seconds)} seconds' if round(seconds) > 0 else ''}.",
color=0xE02B2B
)
await context.send(embed=embed)
elif isinstance(error, commands.MissingPermissions):
embed = discord.Embed(
title="Error!",
description="You are missing the permission `" + ", ".join(
error.missing_perms) + "` to execute this command!",
color=0xE02B2B
)
await context.send(embed=embed)
elif isinstance(error, commands.MissingRequiredArgument):
embed = discord.Embed(
title="Error!",
description=str(error).capitalize(),
# We need to capitalize because the command arguments have no capital letter in the code.
color=0xE02B2B
)
await context.send(embed=embed)
raise error
@tasks.loop(minutes=1.0)
async def status_task():
status = "Working on it..."
await bot.change_presence(activity=discord.Game(status))
# Load cogs
for filename in os.listdir("./cogs"):
if filename.endswith(".py") and filename != "__init__.py":
try:
bot.load_extension(f'cogs.{filename[:-3]}')
print(f"Loaded extension 'cogs.{filename[:-3]}'")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
print(f"Failed to load extension {filename[:-3]}\n{exception}")
bot.run(BOT_TOKEN)