<commit_before>import logging
import os

from telegram.ext import CommandHandler, MessageHandler
from telegram.ext.updater import Updater

from roll import RollCommand
from errors import *


logging.basicConfig(level=logging.INFO,  # filename='log.txt',
                    format='%(asctime)s %(name)s %(levelname)s: %(message)s')


text = {}
for file_name in os.listdir('./text'):
    with open('./text/' + file_name) as f:
        text[file_name] = f.read().strip()


def start_cmd(bot, update):
    bot.send_message(chat_id=update.message.chat_id,
                     text=text['start'])


def about_cmd(bot, update):
    bot.send_message(chat_id=update.message.chat_id,
                     parse_mode='Markdown',
                     text=text['about'])


def help_cmd(bot, update):
    bot.send_message(chat_id=update.message.chat_id,
                     parse_mode='Markdown',
                     text=text['help'])


def roll_cmd(bot, update, args):
    msg_args = {
        'chat_id': update.message.chat_id,
        'reply_to_message_id': update.message.message_id,
        'parse_mode': 'Markdown'
    }

    try:
        if len(args) < 1:
            raise NotEnoughArgumentsException(
                'Rolling dice requires at least one argument.')

        if args[0][0].isalpha():
            # TODO: RollCommand.from_saved(args[0])
            msg_args['text'] = f'Saved roll: {args[0]}'
        else:
            msg_args['text'] = str(RollCommand.from_args(args))
    except InvalidSyntaxException:
        msg_args['text'] = f"Syntax: {text['syntax']}"
    except FoxRollBotException as e:
        msg_args['text'] = str(e)

    bot.send_message(**msg_args)


def test(bot, update):
    import json
    msg = json.dumps(update.message.to_dict(), indent=True)
    bot.send_message(chat_id=update.message.chat_id, parse_mode='Markdown', text=f'```{msg}```')


def error_callback(bot, update, error):
    logging.error(error)


if __name__ == '__main__':
    with open('token.txt') as token_file:
        token = token_file.read().strip()
    
    updater = Updater(token)
    dispatcher = updater.dispatcher

    dispatcher.add_handler(CommandHandler('start', start_cmd))
    dispatcher.add_handler(CommandHandler('about', about_cmd))
    dispatcher.add_handler(CommandHandler('help', help_cmd))
    dispatcher.add_handler(CommandHandler('roll', roll_cmd, pass_args=True))

    dispatcher.add_handler(MessageHandler(None, test))
<commit_msg>Fix issue that sometimes made it spam a little<commit_after>import logging
import os

from telegram.ext import CommandHandler, MessageHandler
from telegram.ext.updater import Updater

from roll import RollCommand
from errors import *


logging.basicConfig(level=logging.INFO,  # filename='log.txt',
                    format='%(asctime)s %(name)s %(levelname)s: %(message)s')


text = {}
for file_name in os.listdir('./text'):
    with open('./text/' + file_name) as f:
        text[file_name] = f.read().strip()


def start_cmd(bot, update):
    bot.send_message(chat_id=update.message.chat_id,
                     text=text['start'])


def about_cmd(bot, update):
    bot.send_message(chat_id=update.message.chat_id,
                     parse_mode='Markdown',
                     text=text['about'])


def help_cmd(bot, update):
    bot.send_message(chat_id=update.message.chat_id,
                     parse_mode='Markdown',
                     text=text['help'])


def roll_cmd(bot, update, args):
    msg_args = {
        'chat_id': update.message.chat_id,
        'reply_to_message_id': update.message.message_id,
        'parse_mode': 'Markdown'
    }

    try:
        if len(args) < 1:
            raise NotEnoughArgumentsException(
                'Rolling dice requires at least one argument.')

        if args[0][0].isalpha():
            # TODO: RollCommand.from_saved(args[0])
            msg_args['text'] = f'Saved roll: {args[0]}'
        else:
            msg_args['text'] = str(RollCommand.from_args(args))
    except InvalidSyntaxException:
        msg_args['text'] = f"Syntax: {text['syntax']}"
    except FoxRollBotException as e:
        msg_args['text'] = str(e)

    bot.send_message(**msg_args)


def test(bot, update):
    # Only reply if @foxscotch sent the message
    if update.message.from_user.id == 136418592:
        import json
        msg = json.dumps(update.message.to_dict(), indent=True)
        bot.send_message(chat_id=update.message.chat_id,
                         parse_mode='Markdown',
                         text=f'```{msg}```')


def error_callback(bot, update, error):
    logging.error(error)


if __name__ == '__main__':
    with open('token.txt') as token_file:
        token = token_file.read().strip()
    
    updater = Updater(token)
    dispatcher = updater.dispatcher

    dispatcher.add_handler(CommandHandler('start', start_cmd))
    dispatcher.add_handler(CommandHandler('about', about_cmd))
    dispatcher.add_handler(CommandHandler('help', help_cmd))
    dispatcher.add_handler(CommandHandler('roll', roll_cmd, pass_args=True))

    dispatcher.add_handler(MessageHandler(None, test))

{'fraction_non_alphanumeric': 0.13017982799061767, 'fraction_numerical': 0.003713838936669273, 'mean_word_length': 2.966666666666667, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6006749', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1586, 'n_words': 302}