from discord.ext import commands
import orjson

class Chat(commands.Cog):

    def __init__(self, bot):
        self.bot = bot

    @commands.command(
        name="chathelp",
        help="The chat is automatically generated based on prewritten responses. Responses that are not documented will be ignored"
    )
    async def chathelp(self, ctx):
        await ctx.send("The chat is automatically generated based on prewritten responses. Responses that are not documented will be ignored")

    @commands.Cog.listener()
    async def on_message(self, message):
        if not message.author.bot:
            #message has multiple different sub definitions like content author channel
            msg = message.content.lower()
            if msg.startswith("update"):
                msg = msg.replace('update ', '') #trunicate off update
                inputs = msg.split(',')
                try:
                    msg_responses = dict(ping = inputs[0], pong = inputs[1])
                    response_file = open('./cogs/chat/responses.json', 'a')
                    response_file.write(f'{orjson.dumps(msg_responses)}\n')
                except Exception as e:
                    await message.channel.send('The syntax is incorrect. Please type in the format "update [input word], [response]". \nThis will make it so that when a user types [input word], the bot will say [response].')
                    print(e)
            else:
                try:
                    msg_save = open('./cogs/chat/responses.json', 'r')
                    msg_responses = msg_save.readlines()
                    msg_responses = [orson.loads(s.replace('\n', '')) for s in msg_responses]
                    msg_save.close()
                    for i in msg_responses:
                        if i['ping'] == msg:
                            await message.channel.send(i['pong'])
                            return
                    await message.channel.send(f'Sorry, a response to *"{msg}"* has not yet been added.')
                except Exception as e:
                    await message.channel.send(e)

    
def setup(bot):
    bot.add_cog(Chat(bot))
from discord.ext import commands
import orjson

class Chat(commands.Cog):

    def __init__(self, bot):
        self.bot = bot

    @commands.command(
        name="chathelp",
        help="The chat is automatically generated based on prewritten responses. Responses that are not documented will be ignored"
    )
    async def chathelp(self, ctx):
        await ctx.send("The chat is automatically generated based on prewritten responses. Responses that are not documented will be ignored")

    @commands.Cog.listener()
    async def on_message(self, message):
        if not message.author.bot:
            #message has multiple different sub definitions like content author channel
            msg = message.content.lower()
            if msg.startswith("update"):
                msg = msg.replace('update ', '') #trunicate off update
                inputs = msg.split(',')
                try:
                    msg_responses = dict(ping = inputs[0], pong = inputs[1])
                    response_file = open('./cogs/chat/responses.json', 'a')
                    response_file.write(f'{orjson.dumps(msg_responses)}\n')
                except Exception as e:
                    await message.channel.send('The syntax is incorrect. Please type in the format "update [input word], [response]". \nThis will make it so that when a user types [input word], the bot will say [response].')
                    print(e)
            else:
                try:
                    msg_save = open('./cogs/chat/responses.json', 'r')
                    msg_responses = msg_save.readlines()
                    msg_responses = [orson.loads(s.replace('\n', '')) for s in msg_responses]
                    msg_save.close()
                    for i in msg_responses:
                        if i['ping'] == msg:
                            await message.channel.send(i['pong'])
                            return
                    await message.channel.send(f'Sorry, a response to *"{msg}"* has not yet been added.')
                except Exception as e:
                    await message.channel.send(e)

    
def setup(bot):
    bot.add_cog(Chat(bot))
#message has multiple different sub definitions like content author channel #trunicate off update