<commit_before>#!/usr/bin/env python3
# CrabBot quotes
#
# Partially inspired by LRRBot

'''
JSON format
{
    Name1: [
        "Quote1"
        "Quote2"
        ...
    ],
    Name2: [
    ...
'''

import json
from pathlib import Path
import random
# import sqlite3  # Leaving as a note of possible db alternatives

from discord.ext import commands


class Quotes:

    def __init__(self, bot, quotes_db_path):
        self.bot = bot
        self.quotes_db_path = Path(quotes_db_path)
        self.quotes = {}  # Fallback quote initialization TODO: check what we want initialized

        # Load the db into memory
        # NOTE currently CrabBot does not have a mechanism for shutting down gracefully
        #      so for now we're gonna just write out changes whenever they happen
        if self.quotes_db_path.exists():
            # TODO JSON error checking
            with self.quotes_db_path.open() as f:
                self.quotes = json.load(f)

    @commands.group(pass_context=True,
                    help=('Read or add quotes! See "help quote" for details\n'
                          '\n'
                          'If no command is given, a random quote is printed'))
    async def quote(self, ctx):
        if ctx.invoked_subcommand is not None:
            return

        # await self.bot.say("Be more quotable, then maybe we'd finish this command. :neutral_face:")

        # TODO consider using name.lower() to standardize input. Or some kind of fuzzy matching.

        # TODO do something different to get names, currently spaces are ignored
        #       Can't have quote take args, then subcommands don't work
        name = ctx.subcommand_passed  # attempt to get the rest of the message

        # TODO? maybe detect blank strings to help with user error?
        if name is None:
            name = random.choice(list(self.quotes.keys()))

        if name in self.quotes:
            selected_quote = random.choice(self.quotes[name])
            await self.bot.say("{quote} \n  —{name}".format(quote=selected_quote, name=name))
        else:
            await self.bot.say("No quotes from {name}".format(name=name))

    @quote.command(help='List all authors of recorded quotes')
    async def authors(self):
        authors = self.quotes.keys()
        await self.bot.say(";  ".join(authors))

    @quote.command(help=('Add a quote.\n'
                         'Say the name of the person being quoted, then '
                         'write the quote in quotation marks.\n'<commit_msg>Change quote command to only run if there's no subcommand<commit_after>#!/usr/bin/env python3
# CrabBot quotes
#
# Partially inspired by LRRBot

'''
JSON format
{
    Name1: [
        "Quote1"
        "Quote2"
        ...
    ],
    Name2: [
    ...
'''

import json
from pathlib import Path
import random
# import sqlite3  # Leaving as a note of possible db alternatives

from discord.ext import commands


class Quotes:

    def __init__(self, bot, quotes_db_path):
        self.bot = bot
        self.quotes_db_path = Path(quotes_db_path)
        self.quotes = {}  # Fallback quote initialization TODO: check what we want initialized

        # Load the db into memory
        # NOTE currently CrabBot does not have a mechanism for shutting down gracefully
        #      so for now we're gonna just write out changes whenever they happen
        if self.quotes_db_path.exists():
            # TODO JSON error checking
            with self.quotes_db_path.open() as f:
                self.quotes = json.load(f)

    @commands.group(pass_context=True, invoke_without_command=True,
                    help=('Read or add quotes! See "help quote" for details\n'
                          '\n'
                          'If no command is given, a random quote is printed'))
    async def quote(self, ctx):
        # TODO consider using name.lower() to standardize input. Or some kind of fuzzy matching.

        # TODO do something different to get names, currently spaces are ignored
        #       Can't have quote take args, then subcommands don't work
        name = ctx.subcommand_passed  # attempt to get the rest of the message

        # TODO? maybe detect blank strings to help with user error?
        if name is None:
            name = random.choice(list(self.quotes.keys()))

        if name in self.quotes:
            selected_quote = random.choice(self.quotes[name])
            await self.bot.say("{quote} \n  —{name}".format(quote=selected_quote, name=name))
        else:
            await self.bot.say("No quotes from {name}".format(name=name))

    @quote.command(help='List all authors of recorded quotes')
    async def authors(self):
        authors = self.quotes.keys()
        await self.bot.say(";  ".join(authors))

    @quote.command(help=('Add a quote.\n'
                         'Say the name of the person being quoted, then '
                         'write the quote in quotation marks.\n'
{'fraction_non_alphanumeric': 0.08833636547402467, 'fraction_numerical': 0.002425712553062462, 'mean_word_length': 2.1616613418530353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3789958', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1338, 'n_words': 488}