import discord, os, finnhub

from discord.ext import commands

from funcs import logThis, makeFiles

commandPrefix = "!"

class Credentials():
    def __init__(self):
        with open("credentials.txt","r") as f:
            data = f.read().splitlines()

        self.token = data[0][10:].replace(" ","")
        self.finnhubKey = data[1][16:].replace(" ","")
        self.wordnikKey = data[2][16:].replace(" ","")


class Gwendolyn(commands.Bot):
    def __init__(self):
        self.credentials = Credentials()
        self.finnhubClient = finnhub.Client(api_key = self.credentials.finnhubKey)
        super().__init__(command_prefix=commandPrefix, case_insensitive=True)

# Creates the required files
makeFiles()

# Creates the Bot
client = Gwendolyn()

# Logs in
@client.event
async def on_ready():
    logThis("Logged in as "+client.user.name+", "+str(client.user.id))
    game = discord.Game("Some weeb shit")
    await client.change_presence(activity=game)

# Logs when user sends a command
@client.event
async def on_command(ctx):
    logThis(f"{ctx.message.author.display_name} ran {ctx.message.content}")

# Logs if a command experiences an error
@client.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandNotFound):
        await ctx.send("That's not a command (error code 001)")
    else:
        logThis(f"Something went wrong, {error}",str(ctx.message.channel.id))
        await ctx.send("Something went wrong (error code 000)")

#Loads cogs
for filename in os.listdir("./cogs"):
    if filename.endswith(".py"):
        client.load_extension(f"cogs.{filename[:-3]}")

try:
    # Runs the whole shabang
    client.run(client.credentials.token)
except:
    logThis("Could not log in. Remember to write your bot token in the credentials.txt file")
