Exemple #1
0
    async def end(self, bot):
        guld = bot.get_guild(self.serv_id)  # get server info
        auth = dh.get_user(guld, self.auth_id)  # get author info
        chan = dh.get_channel(guld, self.chan_id)  # get channel info
        role = dh.get_role(guld, self.role_id)  # get role info

        # remove the role
        await auth.remove_roles(role)

        # create a message, and report that role has been removed
        msg = f"{auth.mention}: role {role.name} has been removed"
        await chan.send(msg)
Exemple #2
0
  async def end(self, bot, index=-1):
    '''
    Ends the timeout associated with this object
    '''
    # get data objects
    serv   = bot.get_guild(self.server_id)
    chan   = dh.get_channel(serv, self.channel_id)
    member = dh.get_user(serv, self.user_id)
    roles  = [dh.get_role(dh, role_id) for role_id in self.roles]

    # restore perms and notify user
    await member.edit(roles=roles)
    await chan.send('{}: your time out is up, permissions restored'.format(
        member.mention
    ))
Exemple #3
0
  async def end(self, bot):
    logger.info('sending reminder for <@%s> to: <#%s> ',
      self.user_id,
      self.channel_id,
    )

    chan = bot.get_channel(self.channel_id)

    if not chan:
      chan = self.channel_id
      logger.error(f'could not send message \"{self.message}\" to <#{chan}>')
      return

    guild = chan and chan.guild
    user  = chan and dh.get_user(guild, self.user_id)

    if self.command:
      member = {
        'id':            int(user.id),
        'username':      user.name,
        'avatar':        user.avatar,
        'discriminator': user.discriminator
      }
      msg = Message(
        content=self.message,
        id=0,
        channel=chan,
        author=member,
        attachments=[],
        reactions=[],
        type=0,
        channel_id=chan.id,
      )
      await bot.process_commands(msg)
    else:
      await chan.send(self.get_message(user))
    if self.times:
      next_rem = Reminder(
        self.channel_id,
        self.user_id,
        self.message,
        0,
        self.times,
        self.command,
        self.reminder_id
      )
      if next_rem.time_left > 600:
        await self.heap.push(next_rem)
Exemple #4
0
  async def begin(self, bot, timeout_role, timeout_channel):
    '''
    starts the timout:
      - user gets send to timeout
      - timeout gets stored in heap
      - timeout permissons for other channels get set
    '''
    # set end_time to proper time upon start of timeout
    self.end_time += time.time()
    serv   = bot.get_guild(self.server_id)
    chan   = dh.get_channel(serv, self.channel_id)
    member = dh.get_user(serv, self.user_id)

    # if matching timout obj exists... ignore this one
    for index,timeout_obj in enumerate(self.heap):
      if timeout_obj == self:
        self.heap.pop(index)
        await bot.say('user is in timout, their timout will be extend')
        break

    # if timeout_role does not exist, create it
    if not timeout_role:
      p = discord.Permissions.none()
      to_role = await server.create_role(
                name='timeout', mentionable=False,
                hoist=True,     permissions=p,
                colour=discord.Colour.dark_red()
      )
      if not to_role: # if it could not be created, stop
        await chan.send('no `timeout` role found/unable to create it')
        return

    # permission objects:
    #   po1 - dissallow read/send - for all channels (except next two)
    #   po2 - allow read/send     - for timeout channel only(for talk with mods)
    #   po2 - allow access        - for bot in timeout room
    po1 = discord.PermissionOverwrite(read_messages        = False,
                                      read_message_history = False,
                                      send_messages        = False
    )
    po2 = discord.PermissionOverwrite(read_messages        = True,
                                      read_message_history = False,
                                      send_messages        = True
    )
    po3 = discord.PermissionOverwrite(read_messages        = True,
                                      read_message_history = True,
                                      send_messages        = True
    )

    # dissable access to all channels for timeout_role
    #   this is done each time in case a new channel gets created/modified/etc.
    for chan in serv.channels:
      await chan.set_permissions(to_role, overwrite=po1)

    # create channel if needed
    # set permissions:
    #   p1 - normal users(not in timeout) - cannot read
    #   p2 - timeout_role(not in timeout) - can read/send
    if not to_chan:
      overwrites = {
        server.default_role: po1,
        to_role:             po2,
      }
      to_chan = await server.create_text_channel(
            'timeout_room', overwrites=overwrites
      )
    await to_chan.set_permissions(bot.user, overwrite=po3)

    # format message
    message = '{}: you are now under a {} second timeout'.format(
                member.mention,
                time
    )
    # remove current roles, and apply timout role, then send message
    await member.edit(roles=to_role)
    await asyncio.sleep(1)  # wait so that the user does not get a notification
    await chan.send(message)# in a chan they can't access

    # send message to the channel where the timeout request was made
    if to_chan and to_chan != chan:
      try:
        await to_chan.send(message)
      except:
        pass
Exemple #5
0
    async def _prune(self, ctx, num_to_delete: int, *message):
        """
    deletes specified number of messages from channel
    if message is specified, message will be echoed by bot after prune

    USAGE: .prune <num> [user] [message...]

    NOTE: if first word after number is a user,
          only user's messages will be pruned
    """
        # tmp channel/server pointer
        chan = ctx.message.channel
        serv = ctx.message.guild

        #if num_to_delete > 100:                       # api only allows up to 100
        #  await ctx.send('Sorry, only up to 100') # TODO - copy thing done in
        #  return                                      #        self._paste
        if num_to_delete < 1:  # delete nothing?
            await ctx.send('umm... no')  #  answer: no
            return

        # if the first word in the message matches a user,
        #   remove that word from the message, store the user
        try:
            user = dh.get_user(serv or self.bot, message[0])
            if user:
                message = message[1:]
        except:
            logger.debug('did not match a user')
            user = None

        check = lambda m: True
        if user:  # if a user was matched, delete messages for that user only
            logger.debug(f'pruning for user {user.name}')
            check = lambda m: str(m.author.id) == str(user.id)

        message = ' '.join(message)  #make the message a string

        logs = []
        async for m in chan.history(limit=num_to_delete, reverse=True):
            if check(m):
                logs.append(m)

        deleted = len(logs)
        old = False
        while len(logs) > 0:  # while there are messages to delete
            if len(logs) > 1:  #   if more than one left to delete and not old,
                if not old:  #     attempt batch delete [2-100] messages
                    try:
                        await chan.delete_messages(logs[:100])
                    except:  #   if problem when batch deleting
                        old = True  #     then the messages must be old
                if old:  # if old, traverse and delete individually
                    for entry in logs[:100]:
                        try:
                            await entry.delete()
                        except:
                            logger.exception(
                                '<{0.author.name}> {0.content}'.format(entry))
                logs = logs[100:]
            else:  # if only one message, delete individually
                await logs[0].delete()
                logs.remove(logs[0])

        #report that prume was complete, how many were prunned, and the message
        await ctx.send(
            ok('Deleted {} message{} {}'.format(
                deleted, '' if deleted == 1 else 's',
                f'({message})' if message else '')))