Exemplo n.º 1
0
async def remindme_command(ctx: discord.ext.commands.Context,
                           interval: DurationConverter, *,
                           text: Optional[str]) -> None:
    """Set a reminder with a given message."""
    if ctx.guild is None:
        raise util.discord.UserError("Only usable in a guild")

    reminders_optional = conf[ctx.author.id]
    reminders = reminders_optional.copy(
    ) if reminders_optional is not None else []
    reminder_time = int(datetime.now(timezone.utc).timestamp()) + interval
    reminder: Reminder = {
        "guild": ctx.guild.id,
        "channel": ctx.channel.id,
        "msg": ctx.message.id,
        "time": reminder_time,
        "contents": text or ""
    }
    reminders.append(reminder)
    reminders.sort(key=lambda a: a["time"])
    conf[ctx.author.id] = FrozenList(reminders)
    await conf
    expiration_updated.release()

    await ctx.send("Created reminder {}".format(
        format_reminder(reminder))[:2000],
                   allowed_mentions=discord.AllowedMentions.none())
Exemplo n.º 2
0
async def handle_reminder(user_id: int, reminder: Reminder) -> None:
    await send_reminder(user_id, reminder)

    reminders_optional = conf[user_id]
    if reminders_optional is None: return
    reminders = reminders_optional.copy()
    reminders.remove(reminder)
    conf[user_id] = FrozenList(reminders)
    await conf
Exemplo n.º 3
0
async def location_add_category(ctx: LocContext, cat: util.discord.PartialCategoryChannelConverter) -> None:
    """Add a category to a location."""
    loc = ctx.loc
    cats = conf[loc, "categories"] or FrozenList()
    if cat.id in cats:
        raise util.discord.UserError(util.discord.format("Category {!c} is already in location {!i}", cat.id, loc))

    conf[loc, "categories"] = cats + [cat.id]
    await conf

    await ctx.send(util.discord.format("Added category {!c} to location {!i}", cat.id, loc))
Exemplo n.º 4
0
async def location_add_channel(ctx: LocContext, chan: util.discord.PartialTextChannelConverter) -> None:
    """Add a channel to a location."""
    loc = ctx.loc
    chans = conf[loc, "channels"] or FrozenList()
    if chan.id in chans:
        raise util.discord.UserError(util.discord.format("Channel {!c} is already in location {!i}", chan.id, loc))

    conf[loc, "channels"] = chans + [chan.id]
    await conf

    await ctx.send(util.discord.format("Added channel {!c} to location {!i}", chan.id, loc))
Exemplo n.º 5
0
async def location_remove_category(ctx: LocContext, cat: util.discord.PartialCategoryChannelConverter) -> None:
    """Remove a category from a location."""
    loc = ctx.loc
    cats = conf[loc, "categories"] or FrozenList()
    if cat.id not in cats:
        raise util.discord.UserError(util.discord.format("Category {!c} is already not in location {!i}", cat.id, loc))

    mcats = cats.copy()
    mcats.remove(cat.id)
    conf[loc, "categories"] = mcats

    await ctx.send(util.discord.format("Added category {!c} to location {!i}", cat.id, loc))
Exemplo n.º 6
0
async def location_remove_channel(ctx: LocContext, chan: util.discord.PartialTextChannelConverter) -> None:
    """Remove a channel from a location."""
    loc = ctx.loc
    chans = conf[loc, "channels"] or FrozenList()
    if chan.id not in chans:
        raise util.discord.UserError(util.discord.format("Channel {!c} is already not in location {!i}", chan.id, loc))

    mchans = chans.copy()
    mchans.remove(chan.id)
    conf[loc, "channels"] = mchans

    await ctx.send(util.discord.format("Removed channel {!c} from location {!i}", chan.id, loc))
Exemplo n.º 7
0
async def reminder_command(ctx: discord.ext.commands.Context) -> None:
    """Display your reminders."""
    reminders = conf[ctx.author.id] or FrozenList()
    reminder_list_md = "Your reminders include:\n{}".format("\n".join(
        "**{:d}.** Reminder {}".format(i, format_reminder(reminder))
        for i, reminder in zip(count(1), reminders)))
    if len(reminder_list_md) > 2000:
        await ctx.send(file=discord.File(io.BytesIO(
            "Your reminders include:\n{}".format("\n".join(
                "{:d}. Reminder {}".format(i, format_text_reminder(reminder))
                for i, reminder in zip(count(1), reminders))).encode("utf8")),
                                         filename="reminder_list.txt"))
    else:
        await ctx.send(reminder_list_md,
                       allowed_mentions=discord.AllowedMentions.none())
Exemplo n.º 8
0
async def priv_add_role(ctx: PrivContext,
                        role: util.discord.PartialRoleConverter) -> None:
    """Add a role to a priv."""
    priv = ctx.priv
    roles = conf[priv, "roles"] or FrozenList()
    if role.id in roles:
        raise util.discord.UserError(
            util.discord.format("Role {!M} is already in priv {!i}", role.id,
                                priv))

    conf[priv, "roles"] = roles + [role.id]
    await conf

    await ctx.send(util.discord.format("Added role {!M} to priv {!i}", role.id,
                                       priv),
                   allowed_mentions=discord.AllowedMentions.none())
Exemplo n.º 9
0
async def priv_add_user(ctx: PrivContext,
                        user: util.discord.PartialUserConverter) -> None:
    """Add a user to a priv."""
    priv = ctx.priv
    users = conf[priv, "users"] or FrozenList()
    if user.id in users:
        raise util.discord.UserError(
            util.discord.format("User {!m} is already in priv {!i}", user.id,
                                priv))

    conf[priv, "users"] = users + [user.id]
    await conf

    await ctx.send(util.discord.format("Added user {!m} to priv {!i}", user.id,
                                       priv),
                   allowed_mentions=discord.AllowedMentions.none())
Exemplo n.º 10
0
async def reminder_remove(ctx: discord.ext.commands.Context,
                          index: int) -> None:
    """Delete a reminder."""
    reminders_optional = conf[ctx.author.id]
    reminders = reminders_optional.copy(
    ) if reminders_optional is not None else []
    if index < 1 or index > len(reminders):
        raise util.discord.UserError(
            "Reminder {:d} does not exist".format(index))
    reminder = reminders[index - 1]
    del reminders[index - 1]
    conf[ctx.author.id] = FrozenList(reminders)
    await conf
    expiration_updated.release()
    await ctx.send("Removed reminder {}".format(
        format_reminder(reminder))[:2000],
                   allowed_mentions=discord.AllowedMentions.none())
Exemplo n.º 11
0
async def priv_remove_role(ctx: PrivContext,
                           role: util.discord.PartialRoleConverter) -> None:
    """Remove a role from a priv."""
    priv = ctx.priv
    roles = conf[priv, "roles"] or FrozenList()
    if role.id not in roles:
        raise util.discord.UserError(
            util.discord.format("Role {!M} is already not in priv {!i}",
                                role.id, priv))

    mroles = roles.copy()
    mroles.remove(role.id)
    conf[priv, "roles"] = mroles

    await ctx.send(util.discord.format("Removed role {!M} from priv {!i}",
                                       role.id, priv),
                   allowed_mentions=discord.AllowedMentions.none())
Exemplo n.º 12
0
async def priv_remove_user(ctx: PrivContext,
                           user: util.discord.PartialUserConverter) -> None:
    """Remove a user from a priv."""
    priv = ctx.priv
    users = conf[priv, "users"] or FrozenList()
    if user.id not in users:
        raise util.discord.UserError(
            util.discord.format("User {!m} is already not in priv {!i}",
                                user.id, priv))

    musers = users.copy()
    musers.remove(user.id)
    conf[priv, "users"] = musers

    await ctx.send(util.discord.format("Removed user {!m} from priv {!i}",
                                       user.id, priv),
                   allowed_mentions=discord.AllowedMentions.none())
Exemplo n.º 13
0
async def init() -> None:
    global conf
    conf = cast(RemindersConf, await util.db.kv.load(__name__))

    for user_id, in conf:
        obj = conf[int(user_id)]
        assert obj is not None
        conf[int(user_id)] = FrozenList({
            "guild": int(rem["guild"]),
            "channel": int(rem["channel"]),
            "msg": int(rem["msg"]),
            "time": int(rem["time"]),
            "contents": rem["contents"]
        } for rem in obj)
    await conf

    expiry_task: asyncio.Task[None] = util.asyncio.run_async(expire_reminders)
    plugins.finalizer(expiry_task.cancel)