<commit_before>'KappaPride (NinjaCode2k16[tm])'
import logging
import datetime
import json

import discord.ext.commands as commands

from ext.util import isOwner


def setup(bot):
    'Adds the cog to the provided discord bot'
    bot.add_cog(Timerboard(bot))


class Timerboard:
    '''A cog defining commands for controlling the
    bot's timerboard functions'''
    def __init__(self, bot):
        self.logger = logging.getLogger(__name__)
        self.fleetjson = None
        self.bot = bot

    def loadjson(self, jsonname):
        'A function which loads a json, given the filename'
        try:
            with open(jsonname) as data_file:
                data = json.load(data_file)
            self.logger.info('Json successfully loaded.')
            return data
        except FileNotFoundError:
            return {
                "fleets": []
            }

    def savejson(self, data, jsonname):
        'A function which saves a json, given the filename'
        try:
            with open(jsonname, 'wt') as outfile:
                json.dump(data, outfile)
            self.logger.info('Json successfully saved.')
        except TypeError as error:
            self.logger.error(error)

    @commands.command()
    @commands.check(isOwner)
    async def addfleet(self, fdate: str, ftime: str, flco: str, formup: str, doct: str, ftype: str):
        '''Adds a fleet to the list of fleets in the json.
        Input fleets in the format
        "DD/MM/YYYY HH/MM FC FORMUP DOCTRINE FLEETTYPE'''
        fleetdtime = datetime.datetime.strptime((fdate + ftime), '%d/%m/%Y%H:%M')
        self.logger.info('Converted to datetime')

        self.fleetjson = self.loadjson("fleetlist.json")
        self.logger.info(self.fleetjson)
        self.fleetjson["fleets"].append({
            'fleettime': fleetdtime.isoformat(),
            'fc': flco,
            'formup': formup,
            'doctrine': doct,
            'fleettype': ftype
        })
        self.savejson(self.fleetjson, 'fleetlist.json')
        await self.bot.say(self.fleetjson["fleets"])

    @commands.command()
    @commands.check(isOwner)
    async def removefleet(self, number: str):
        'Removes a fleet from the json via number on the list of fleets'
        self.fleetjson = self.loadjson("fleetlist.json")
        try:
            number = int(number)
            if number >= 1:
                self.fleetjson['fleets'].pop(number-1)
                self.savejson(self.fleetjson, 'fleetlist.json')
                await self.bot.say(self.fleetjson["fleets"])
            else:
                await self.bot.say("You didn't enter a valid number.")
                self.logger.warning("User didn't enter a valid number.")
        except ValueError:
            self.logger.warning("User didn't enter an integer")
            await self.bot.say("Please enter an integer.")

    @commands.command()
    async def listfleets(self):
        'Lists all fleets to the chat in discord'
        self.fleetjson = self.loadjson("fleetlist.json")
        for fleet in self.fleetjson['fleets']:
            for info in self.fleetjson[fleet]:
                await self.bot.say(info, ':', self.fleetjson[info][fleet])

    @commands.command()
    async def datetime(self):
        'Prints the current datetime to the chat and logger'
        currentdate = datetime.datetime.now()
        self.logger.info(currentdate)
        await self.bot.say(currentdate)<commit_msg>Fixed addfleet command and added a range of others.<commit_after>'KappaPride (NinjaCode2k16[tm])'
import logging
import datetime
import json
from operator import itemgetter
import discord.ext.commands as commands

from ext.util import isOwner


def setup(bot):
    'Adds the cog to the provided discord bot'
    bot.add_cog(Timerboard(bot))


class Timerboard:
    '''A cog defining commands for controlling the
    bot's timerboard functions'''
    def __init__(self, bot):
        self.logger = logging.getLogger(__name__)
        self.fleetjson = None
        self.bot = bot

    def loadjson(self, jsonname):
        'A function which loads a json, given the filename'
        try:
            with open(jsonname) as data_file:
                data = json.load(data_file)
            self.logger.info('Json successfully loaded.')
            return data
        except FileNotFoundError:
            return {
                "fleets": []
            }

    def savejson(self, data, jsonname):
        'A function which saves a json, given the filename'
        try:
            data['fleets'].sort(key=itemgetter('fleettime'))
            with open(jsonname, 'wt') as outfile:
                json.dump(data, outfile)
            self.logger.info('Json successfully saved.')
        except TypeError as error:
            self.logger.error(error)

    def listfleet(self, index, announce=False):
        'Returns a string containing the fleet details given an index'
        fleets = self.loadjson("fleetlist.json")['fleets']
        response = ""
        if announce:
            response += "@everyone\n"
        fleet = fleets[index]
        response += "**Fleet {}:**\n".format(index+1)
        ftime = datetime.datetime.strptime(fleet["fleettime"], '%Y-%m-%dT%H:%M:%S')
        response += "```When: {}\n".format(ftime)
        response += "FC: {}\n".format(fleet["fc"])
        response += "Type: {}\n".format(fleet['fleettype'])
        response += "Doctrine: {}\n".format(fleet['doctrine'])
        response += "Formup: {}```\n".format(fleet['formup'])
        self.logger.info(response)
        return response

    @commands.command()
    @commands.check(isOwner)
    async def addfleet(self, fdate: str, ftime: str, flco: str, formup: str, doct: str, ftype: str):
        '''Adds a fleet to the list of fleets in the json.
        Input fleets in the format
        "DD/MM/YYYY HH/MM FC FORMUP DOCTRINE FLEETTYPE'''
        fleetdtime = datetime.datetime.strptime((fdate + ftime), '%d/%m/%Y%H:%M')
        self.logger.info('Converted to datetime')
        if fleetdtime <= datetime.datetime.now():
            await self.bot.say("Date entered is before the current date.")
            self.logger.warning("User entered an invalid date")
        else:
            self.fleetjson = self.loadjson("fleetlist.json")
            self.fleetjson["fleets"].append({
                'fleettime': fleetdtime.isoformat(),
                'fc': flco,
                'formup': formup,
                'doctrine': doct,
                'fleettype': ftype,
                'announced': False
            })
            self.savejson(self.fleetjson, 'fleetlist.json')
            await self.bot.say("Fleet successfully added!")

    @commands.command()
    @commands.check(isOwner)
    async def removefleet(self, number: str):
        'Removes a fleet from the json via number on the list of fleets'
        self.fleetjson = self.loadjson("fleetlist.json")
        try:
            number = int(number)
            if number >= 1:
                self.fleetjson['fleets'].pop(number-1)
                self.savejson(self.fleetjson, 'fleetlist.json')
                await self.bot.say("Fleet %d successfully removed." % number)
            else:
                await self.bot.say("You didn't enter a valid number.")
                self.logger.warning("User didn't enter a valid number.")
        except ValueError:
            self.logger.warning("User didn't enter an integer")
            await self.bot.say("Please enter an integer.")

    @commands.command()
    async def listfleets(self):
        'Lists all fleets to the chat in discord'
        fleets = self.loadjson("fleetlist.json")['fleets']
        n_fleets = len(fleets)
        self.logger.info(fleets)
        for idx in range(n_fleets):
            await self.bot.say(self.listfleet(idx))

    @commands.command()
    @commands.check(isOwner)
    async def announcefleets(self):
        'Announces all un-announced fleets'
        self.fleetjson = self.loadjson("fleetlist.json")
        n_fleets = len(self.fleetjson['fleets'])
        announced = False
        for idx in range(n_fleets):
            if not self.fleetjson['fleets'][idx]["announced"]:
                await self.bot.say(self.listfleet(idx, True))
                self.fleetjson['fleets'][idx]["announced"] = True
                self.savejson(self.fleetjson, 'fleetlist.json')
                announced = True
        if not announced:
            await self.bot.say("All Fleets Announced!")

    @commands.command()
    @commands.check(isOwner)
    async def resetannouncefleets(self, number: str):
        '''Resets the boolean specifying whether a fleet has been announced.
        Enter a fleet number to reset a specific fleet or "all" to reset all'''
        self.fleetjson = self.loadjson("fleetlist.json")
        n_fleets = len(self.fleetjson['fleets'])
        if number.isdecimal():
            self.fleetjson['fleets'][int(number)-1]["announced"] = False
            self.savejson(self.fleetjson, 'fleetlist.json')
            await self.bot.say("Fleet %s's announcement status reset." % number)
            self.logger.info('User reset fleet %s\'s announcement status', number)
        elif number == "all":
            for idx in range(n_fleets):
                self.fleetjson['fleets'][idx]["announced"] = False
            self.savejson(self.fleetjson, 'fleetlist.json')
            await self.bot.say("All anouncement statuses reset.")
            self.logger.info('User reset all announcement statuses')
        else:
            await self.bot.say("Please enter a valid number to reset or 'all' to reset all")
            self.logger.warning('User entered an invalid number to reset.')
{'fraction_non_alphanumeric': 0.11684025987418789, 'fraction_numerical': 0.001237496132824585, 'mean_word_length': 2.2543624161073827, 'pattern_counts': {'":': 3, '<': 4, '<?xml version=': 0, '>': 5, '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': '7083935', 'n_tokens_mistral': 2961, 'n_tokens_neox': 2802, 'n_words': 740}