<commit_before>""" Plugin for basic commands

Commands:
    roll
    feature
    pcbot
"""

import random
from re import match

import discord
import asyncio

from pcbot import utils, Config, Annotate
import plugins


feature_reqs = Config(filename="feature_requests", data={})


@plugins.command()
def roll(client: discord.Client, message: discord.Message, num: utils.int_range(f=1)=100):
    """ Roll a number from 1-100 if no second argument or second argument is not a number.
        Alternatively rolls `num` times (minimum 1). """
    rolled = random.randint(1, num)
    yield from client.say(message, "{0.mention} rolls `{1}`.".format(message.author, rolled))


@plugins.argument("#{open}feature_id{suffix}{close}")
def get_req_id(feature_id: str):
    """ Return the id matched in an id string.
    Format should be similar to #24 """
    req_id = match("^#([0-9])+$", feature_id)

    if req_id:
        return int(req_id.group(1)) - 1

    return None


def format_req(plugin, req_id: int):
    req_list = feature_reqs.data[plugin]

    if 0 <= req_id < len(req_list):
        req = req_list[req_id]
        checked = "-"

        # Check if the request is marked<commit_msg>Add a when command using pendulum. This will convert the given time (from UTC unless specified) to UTC and show a readable format of in how long until it happens.<commit_after>""" Plugin for basic commands

Commands:
    roll
    feature
"""

import random
from re import match

import discord
import pendulum
import pytz

from pcbot import utils, Config, Annotate
import plugins


feature_reqs = Config(filename="feature_requests", data={})


@plugins.command()
def roll(client: discord.Client, message: discord.Message, num: utils.int_range(f=1)=100):
    """ Roll a number from 1-100 if no second argument or second argument is not a number.
        Alternatively rolls `num` times (minimum 1). """
    rolled = random.randint(1, num)
    yield from client.say(message, "{0.mention} rolls `{1}`.".format(message.author, rolled))


@plugins.command(aliases="timezone")
def when(client: discord.Client, message: discord.Message, *time):
    """ Convert time from specified timezone or UTC to UTC and formatted string
    of e.g. `2 hours from now`. """
    time = list(time)
    for i, s in enumerate(time):
        if s in pytz.all_timezones:
            timezone = s
            del time[i]
            break
    else:
        timezone = "UTC"

    try:
        dt = pendulum.parse(" ".join(time), tz=timezone)
    except ValueError:
        yield from client.say(message, "Time format not recognized.")
        return
    except pytz.exceptions.UnknownTimeZoneError:
        yield from client.say(message, "Unknown timezone.")
        return

    now = pendulum.utcnow()

    yield from client.say(message, "`{} UTC` is **{} {}{}**.".format(
        dt.in_tz("UTC").to_datetime_string(),
        "is in" if dt > now else "was",
        dt.diff_for_humans(absolute=True),
        " ago" if dt < now else ""
    ))


@plugins.argument("#{open}feature_id{suffix}{close}")
def get_req_id(feature_id: str):
    """ Return the id matched in an id string.
    Format should be similar to #24 """
    req_id = match("^#([0-9])+$", feature_id)

    if req_id:
        return int(req_id.group(1)) - 1

    return None


def format_req(plugin, req_id: int):
    req_list = feature_reqs.data[plugin]

    if 0 <= req_id < len(req_list):
        req = req_list[req_id]
        checked = "-"

        # Check if the request is marked
{'fraction_non_alphanumeric': 0.12168709033912796, 'fraction_numerical': 0.011114277571957822, 'mean_word_length': 3.144037780401417, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 4, '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': '2080108', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1084, 'n_words': 352}