import discord
from discord.ext import commands, tasks 

import logging
import pathlib
import json
import collections
import time
import re
import typing
import copy
import asyncio
import requests
import datetime
import csv
import yaml
import random
import string

# Logging (hard-coded to log INFO leveL)
log_format = logging.Formatter("%(asctime)s [%(levelname)-7s] %(message)s")
logger = logging.getLogger()
logger.setLevel(logging.INFO)

# Logs to file
logging_path = pathlib.Path("logs/").resolve()
logging_path.mkdir(exist_ok=True)
logfile_path = f"logs/{datetime.datetime.now().strftime('%b-%d-%Y')}.log"
file_logger = logging.FileHandler(logfile_path)
file_logger.setFormatter(log_format)
logger.addHandler(file_logger)

# Logs to stdout
console_logger = logging.StreamHandler()
console_logger.setFormatter(log_format)
logger.addHandler(console_logger)

logger.info(f"Logging to '{logfile_path}'")

# Files
TEAM_INFO = "team_info.json"
REGISTRATION_INFO = "registration_info.json"
bot_path = pathlib.Path(__file__).parent.resolve()

# Team and role info
CTFTeam = collections.namedtuple("CTFTeam", ["role", "category"])
CTFAdminRoles = collections.namedtuple(
    "CTFAdminRoles", ["admin", "competition_support", "tech_support"])
CTFAdminChannels = collections.namedtuple(
    "CTFAdminChannels", ["admin_chat", "bot_commands"])
CTFParticipantRoles = collections.namedtuple(
    "CTFParticipantRoles", ["participant"])
CTFBotRoles = collections.namedtuple(
    "CTFBotRoles", ["bots"])
CTFTeamExpBounds = collections.namedtuple("CTFTeamExpBounds", ["min", "NAME_1"])

# Constants
TEAM_ROLE_COLOR = 0x5690f0
NAME_1_TEAM_MEMBERS = 5
# [1, 2, 3, 4] members on team
TEAM_EXP_BOUNDS = [CTFTeamExpBounds(1, 4), CTFTeamExpBounds(
    1, 3), CTFTeamExpBounds(1, 2.5), CTFTeamExpBounds(1, 2.5), CTFTeamExpBounds(1, 2.5)]
TEAM_EXP_LOOKUP = {
    "basic": 1,
    "intermediate": 2,
    "advanced": 3,
    "expert": 4
}
REGISTRATION_DOWNLOAD_DELAY = 60

# -----------------------------------------
# Custom command exception types
# -----------------------------------------


class CustomCTFBotException(commands.errors.CommandError):
    pass


class MissingTeamException(CustomCTFBotException):
    def __init__(self, message="Specified team cannot be found", *args):
        super().__init__(message, *args)


class MissingMemberException(CustomCTFBotException):
    def __init__(self, message="Specified member cannot be found", *args):
        super().__init__(message, *args)


class TeamCommandException(CustomCTFBotException):
    pass


class MemberCommandExc Add stress words that are fully capitlized. Make sure these stress words appear more than 13 times.
user