Пример #1
0
    async def remindme(self, ctx: Context, *, reminder: str = None):
        if reminder is None:
            return await embed_maker.command_error(ctx)

        remind_time, reminder = format_time.parse(reminder, return_string=True)

        if not reminder:
            return await embed_maker.error(
                ctx, "You cannot have an empty reminder")

        expires = round(time.time()) + remind_time
        self.bot.timers.create(
            expires=expires,
            guild_id=ctx.guild.id,
            event="reminder",
            extras={
                "reminder": reminder,
                "member_id": ctx.author.id
            },
        )

        timestamp = datetime.datetime.utcfromtimestamp(expires)
        strftimestamp = timestamp.strftime('%H:%M:%S %Y-%m-%d (GMT)')

        return await embed_maker.message(
            ctx,
            description=
            f"Alright, in {format_time.seconds(remind_time, accuracy=10)} [{strftimestamp}] I will remind you: `{reminder}`",
            send=True,
        )
Пример #2
0
    async def anon_poll(self,
                        ctx: Context,
                        *,
                        args: Union[ParseArgs, dict] = None):
        if not args:
            return await embed_maker.command_error(ctx)

        question = args["question"]
        options = args["option"]

        # return error if required variables are not given
        if not question:
            return await embed_maker.error(ctx, "Missing question arg")

        if not options:
            return await embed_maker.error(ctx, "Missing option args")

        # get all optional variables
        poll_time = args["time"]
        if not poll_time:
            poll_time = 300  # 5 minutes

        update_interval = args["update_interval"]
        pick_count = args["pick_count"] if args["pick_count"] else "1"
        restrict_role_identifier = args["role"]

        emote_options = await self.parse_poll_options(ctx, options)
        if type(emote_options) == discord.Message:
            return

        # validate all the variables
        err = ""

        restrict_role = ""
        if restrict_role_identifier:
            restrict_role = discord.utils.find(
                lambda r: r.name.lower() == restrict_role_identifier.lower() or
                str(r.id) == restrict_role_identifier,
                ctx.guild.roles,
            )
            if restrict_role is None:
                err = "Invalid role"

        if pick_count and not pick_count.isdigit():
            err = "pick count arg is not a number"

        if poll_time is None:
            err = "Invalid time arg"

        if update_interval and format_time.parse(update_interval) is None:
            err = "Invalid update interval time"
        else:
            update_interval = format_time.parse(update_interval)
            if update_interval and update_interval < 30:
                err = "Update interval can't be smaller than 30 seconds"

        if err:
            return await embed_maker.error(ctx, err)

        description = f'**"{question}"**\n'
        description += "\n".join(f"\n{emote} | **{option}**"
                                 for emote, option in emote_options.items())

        description += f"\n\nReact with 🇻 to vote!\n"

        if restrict_role:
            description += f"Role needed to vote: <@&{restrict_role.id}>\n"

        poll_msg = await embed_maker.message(
            ctx,
            description=description,
            author={"name": "Anonymous Poll"},
            footer={"text": "Started at"},
            send=True,
        )

        await poll_msg.add_reaction("🇻")

        expires = 0 if update_interval else round(
            time.time()) + round(poll_time)

        # start timer
        # we shall also use the timer to keep track of votes and who voted
        self.bot.timers.create(
            guild_id=ctx.guild.id,
            expires=expires,
            event="anon_poll",
            extras={
                "message_id": poll_msg.id,
                "channel_id": poll_msg.channel.id,
                "question": question,
                "options": emote_options,
                "pick_count": pick_count,
                "voted": {},
                "results": dict.fromkeys(emote_options.keys(), 0),
                "update_interval": update_interval,
                "true_expire":
                0 if not update_interval else round(time.time()) + poll_time,
                "restrict_role":
                None if not restrict_role else restrict_role.id,
            },
        )
Пример #3
0
    async def mute(self,
                   ctx: Context,
                   *,
                   member_and_duration_and_reason: str = None):
        if member_and_duration_and_reason is None:
            return await embed_maker.command_error(ctx)

        guild_settings = db.get_guild_settings(ctx.guild.id)
        if guild_settings['mute_role_id'] is None:
            return await embed_maker.message(
                ctx,
                description=
                f'The mute role has not been set yet.\nYou can set it with `{ctx.prefix}setmuterole [role]`',
                colour='orange',
                send=True)

        member, duration_and_reason = await get_member_from_string(
            ctx, member_and_duration_and_reason)
        duration, reason = format_time.parse(duration_and_reason,
                                             return_string=True)

        if member is None:
            return await embed_maker.error(ctx, 'Unable to find member.')

        if duration is None:
            return await embed_maker.error(
                ctx, 'You need to give a duration for the mute')

        if reason is None:
            return await embed_maker.error(
                ctx, 'You need to give a reason for the mute.')

        if member.bot:
            return await embed_maker.error(
                ctx, 'You can\'t mute a bot. Bots are good lads')

        if member.id == ctx.author.id:
            return await embed_maker.error(ctx, 'You can\'t mute yourself.')

        if ctx.author.roles[-1].position <= member.roles[-1].position:
            return await embed_maker.error(
                ctx,
                'You can\'t mute a member who has a role that is equal or higher than yours.'
            )

        mute_timer = db.timers.find_one({
            'guild_id': ctx.guild.id,
            'event': 'automatic_unmute',
            'extras': {
                'member_id': member.id
            }
        })
        if mute_timer:
            time_left = mute_timer['expires'] - round(time.time())
            return await embed_maker.message(
                ctx,
                description=
                f'<@{member.id}> is already muted.\nTheir mute will expire in: **{format_time.seconds(time_left)}**',
                colour='orange',
                send=True)

        # send user message that they have been muted
        text = f"**{ctx.guild.name}** - You have been muted.:mute:\n**Duration**: {format_time.seconds(duration)}\n**Reason**: {reason}"
        try:
            await member.send(text)
        except:
            return await embed_maker.error(
                ctx, 'Failed to send mute message to user.')

        # confirm that user has been warned
        thirty_days_ago = time.time() - (30 * 24 * 60)
        user_warns = self.bot.moderation.cases.get_cases(ctx.guild.id,
                                                         member_id=member.id,
                                                         type='warn',
                                                         after=thirty_days_ago)
        await embed_maker.message(
            ctx,
            description=
            f'{member.mention} has been muted for **{format_time.seconds(duration)}**\n'
            f'**Reason:** {reason}\n'
            f'They now have **{len(user_warns) + 1}** mute{"s" * (len(user_warns) != 0)} in the last 30 days.',
            colour='green',
            send=True)

        # add case to the database
        self.bot.moderation.cases.add_case(ctx.guild.id,
                                           'mute',
                                           reason,
                                           member,
                                           ctx.author,
                                           extra={'duration': duration})

        # add mute role to user
        mute_role = await get_guild_role(ctx.guild,
                                         str(guild_settings['mute_role_id']))
        if mute_role is None:
            return await embed_maker.error(ctx,
                                           'Current mute role is not valid.')
        else:
            await member.add_roles(mute_role)

        # start automatic unmute timer
        self.bot.timers.create(guild_id=ctx.guild.id,
                               expires=round(time.time()) + duration,
                               event='automatic_unmute',
                               extras={'member_id': member.id})