Exemplo n.º 1
0
async def servers(ctx: Context) -> Message:
    name = ctx.message.server.me.nick if ctx.message.server.me.nick is not None else bot.user.name
    template = Embed().set_author(name=name,
                                  icon_url=bot.user.avatar_url).to_dict()
    first = template.copy()
    first['title'] = 'List of servers'
    first[
        'description'] = f'Counting `{len(bot.servers)}` servers for this Bot instance.'
    await bot.send_message(ctx.message.channel, embed=Embed.from_data(first))
    embed = Embed.from_data(template)
    for server in bot.servers:
        value = f'- Owner: `{server.owner.name}` ({server.owner.mention} - `{server.owner.id}`)'
        value += f'\n- Created: `{server.created_at.strftime("%Y-%m-%d %H:%M:%S")}`'
        value += f'\n- Icon: `{server.icon_url}`\n'
        value += f'\n- Region: {server.region if isinstance(server.region, str) else server.region.value}'
        value += f'\n- Channels: `{len(server.channels)}`\n- Members: `{len(server.members)}`'
        if len(server.features) > 0:
            value += f'\n- Features: `{server.features}`'
            if 'INVITE_SPLASH' in server.features:
                value += f'- Splash: `{server.splash}`\n- Splash URL: `{server.splash_url}`'
        value += f'\n- Permissions:{permissions(server.me.server_permissions.value)}'
        embed.add_field(name=f'(`{server.id}`) - {server.name}', value=value)
        if len(embed.fields) == 25 or len(str(embed.to_dict())) > 5000:
            await bot.send_message(ctx.message.channel, embed=embed)
            embed = Embed.from_data(template)
    if len(embed.fields) > 0:
        await bot.send_message(ctx.message.channel, embed=embed)
        return await bot.send_message(ctx.message.channel,
                                      embed=build_embed(
                                          ctx,
                                          'Listed all servers with details.'))
Exemplo n.º 2
0
async def channels(ctx: Context, server_id: str) -> Message:
    server = bot.get_server(server_id)
    name = ctx.message.server.me.nick if ctx.message.server.me.nick is not None else bot.user.name
    template = Embed().set_author(name=name,
                                  icon_url=bot.user.avatar_url).to_dict()
    first = template.copy()
    first[
        'title'] = f'List of channels for server `{server.name}` (`{server.id}`)'
    first[
        'description'] = f'Counting `{len(server.channels)}` channels for this server.'
    await bot.send_message(ctx.message.channel, embed=Embed.from_data(first))
    embed = Embed.from_data(template)
    for channel in server.channels:
        embed.add_field(
            name=f'`{channel.name}` ({channel.mention} - `{channel.id}`',
            value=
            f'{channel.topic}\n\nPermissions:{permissions(channel.permissions_for(server.me).value)}'
        )
        if len(embed.fields) == 25 or len(str(embed.to_dict())) > 5000:
            await bot.send_message(ctx.message.channel, embed=embed)
            embed = Embed.from_data(template)
    if len(embed.fields) > 0:
        await bot.send_message(ctx.message.channel, embed=embed)
        return await bot.send_message(ctx.message.channel,
                                      embed=build_embed(
                                          ctx,
                                          'Listed all channels with details.'))
Exemplo n.º 3
0
async def invite(ctx: Context,
                 dest: Union[Channel, Server],
                 time: int = 0,
                 use: int = 0,
                 tmp: bool = False) -> Message:
    options = {
        'max_age': time,
        'max_uses': use,
        'temporary': tmp,
        'unique': True
    }
    try:
        created = await bot.create_invite(destination=dest, options=options)
    except HTTPException:
        if isinstance(dest, Server):
            has_permission = Permissions.create_instant_invite in dest.me.server_permissions
        elif isinstance(dest, Channel):
            has_permission = Permissions.create_instant_invite in dest.permissions_for(
                dest.server.me)
        else:
            return await bot.send_message(
                ctx.message.channel,
                embed=build_embed(
                    ctx,
                    f'Destination (`{dest}`) is not a valid channel or server.',
                    status=OpStatus.FAILURE))
        if has_permission:
            return await bot.send_message(
                ctx.message.channel,
                embed=build_embed(ctx,
                                  'Invite creation denied by the Discord API.',
                                  status=OpStatus.FAILURE))
        return await bot.send_message(
            ctx.message.channel,
            embed=build_embed(
                ctx,
                'Permission `Create Instant Invite` not granted on {} `{}`'.
                format('server' if isinstance(dest, Server) else 'channel',
                       dest.name),
                status=OpStatus.FAILURE))
    return await bot.send_message(
        ctx.message.channel,
        embed=build_embed(
            ctx,
            'Created invite: {} to `{}` (can be used `{}` times and expires in `{}` seconds).'
            .format(created.url, dest.name, created.max_uses,
                    created.max_age)))
Exemplo n.º 4
0
async def replies(ctx: Context, toggle: bool = not REPLIES_STATUS) -> Message:
    global REPLIES_STATUS
    REPLIES_STATUS = toggle
    return await bot.send_message(
        ctx.message.channel,
        embed=build_embed(
            ctx,
            f'{"Enabled" if toggle else "Disabled"} replies to messages.'))
Exemplo n.º 5
0
async def status(ctx: Context, game: str = None, url: str = None) -> Message:
    is_empty = game is None or game.isspace() or len(game) < 1
    is_stream = not is_empty and url is not None and len(
        url) > 0 and not url.isspace()
    await bot.change_presence(game=None if is_empty else Game(
        name=game, type=1 if is_stream else 0, url=url if is_stream else None))
    return await bot.send_message(
        ctx.message.channel,
        embed=build_embed(
            ctx, '{} status{}{}.'.format(
                'Cleaned' if is_empty else 'Changed', '' if is_empty else
                f' to: `{"Streaming" if is_stream else "Playing"} {game}`',
                f'at `{url}`' if is_stream else '')))
Exemplo n.º 6
0
async def broadcast(ctx: Context, message: str, level: str = 'log') -> Message:
    count = 0
    for server in bot.servers:
        for channel in server.channels:
            if channel.type not in [ChannelType.text, ChannelType.group]:
                continue
            if 'general' in channel.name or 'off-topic' in channel.name:
                await log(ctx, channel, message, level)
                count += 1
    return await bot.send_message(
        ctx.message.channel,
        embed=build_embed(
            ctx,
            f'Broadcasted message to `{count}` servers.\n\n{message}',
            status=OpStatus.WARNING))
Exemplo n.º 7
0
async def simulate(ctx: Context, user: User = None, count: int = 0) -> Message:
    if user is None or count < 1:
        if user is not None:
            count = 1
            await bot.send_message(
                ctx.message.channel,
                embed=build_embed(
                    ctx,
                    'Simulation mode will be enabled for one message.',
                    status=OpStatus.WARNING))
        else:
            user = SIMULATE_CONFIG['USER']
            count = SIMULATE_CONFIG['COUNT']
            SIMULATE_CONFIG['USER'] = None
            SIMULATE_CONFIG['COUNT'] = 0
            if user is None:
                return await bot.send_message(ctx.message.channel,
                                              embed=build_embed(
                                                  ctx,
                                                  'No simulation running.',
                                                  status=OpStatus.FAILURE))
            return await bot.send_message(
                ctx.message.channel,
                embed=build_embed(
                    ctx,
                    'Stopped simulating user `{}` ({}) with `{}` messages left.'
                    .format(user.name, user.mention, count)))
    SIMULATE_CONFIG['USER'] = user
    SIMULATE_CONFIG['COUNT'] = count
    return await bot.send_message(
        ctx.message.channel,
        embed=build_embed(
            ctx,
            'Simulating user `{}` ({}) for the next `{}` messages. Use `{}debug simulate` to end prematurely.'
            .format(user.name, user.mention, count, bot.command_prefix[0]),
            status=OpStatus.WARNING))
Exemplo n.º 8
0
async def nickname(ctx: Context, nick: str = None) -> Message:
    is_empty = nick is None or nick.isspace() or len(nick) < 1
    try:
        await ctx.bot.change_nickname(member=ctx.message.server.me,
                                      nickname=None if is_empty else nick[:32])
    except Forbidden:
        return await bot.send_message(
            ctx.message.channel,
            embed=build_embed(
                ctx,
                'Permission `Change Nickname` not granted on this server.',
                status=OpStatus.FAILURE))
    except HTTPException:
        return await bot.send_message(
            ctx.message.channel,
            embed=build_embed(ctx,
                              'Nickname change denied by the Discord API.',
                              status=OpStatus.FAILURE))
    return await bot.send_message(
        ctx.message.channel,
        embed=build_embed(
            ctx,
            '{} nickname{}.'.format('Cleaned' if is_empty else 'Changed',
                                    '' if is_empty else f' to `{nick[:32]}`')))
Exemplo n.º 9
0
async def log(ctx: Context,
              dest: str,
              message: str,
              level: str = 'INFO') -> Message:
    dest = bot.get_channel(dest)
    if dest is None:
        for server in bot.servers:
            if dest in server.members:
                dest = server.get_member(dest)
                break
    level = level.upper()
    if level not in LOG_LEVELS.keys():
        return await bot.send_message(
            ctx.message.channel,
            embed=build_embed(
                ctx,
                f'Log level can be one of: `{LOG_LEVELS.keys()}`',
                status=OpStatus.FAILURE))
    prefix = LOG_LEVELS[level]
    if message.isspace() or len(message) < 1:
        return await bot.send_message(ctx.message.channel,
                                      embed=build_embed(
                                          ctx,
                                          'Cannot send an empty message',
                                          status=OpStatus.FAILURE))
    try:
        await bot.send_message(dest,
                               embed=build_embed(ctx, f'{prefix} - {message}'))
    except InvalidArgument:
        return await bot.send_message(
            ctx.message.channel,
            embed=build_embed(
                ctx,
                f'Channel `{dest.name if "name" in dest else "N/A"}` ({dest}) is not a valid destination.',
                status=OpStatus.FAILURE))
    except NotFound:
        return await bot.send_message(
            ctx.message.channel,
            embed=build_embed(
                ctx,
                f'Channel `{dest.name}` (<#{dest.id}>) could not be found.',
                status=OpStatus.FAILURE))
    except Forbidden:
        return await bot.send_message(
            ctx.message.channel,
            embed=build_embed(
                ctx,
                'Permission `Send Messages` not granted on server `{}` at channel `{}` (<#{}>).'
                .format(dest.server.name, dest.name, dest.id),
                status=OpStatus.FAILURE))
    except HTTPException:
        return await bot.send_message(
            ctx.message.channel,
            embed=build_embed(
                ctx,
                'Message operation was denied by the Discord API.',
                status=OpStatus.FAILURE))
    return await bot.send_message(
        ctx.message.channel,
        embed=build_embed(
            ctx, 'Send message to server `{}` on channel `{}` (<#{}>).\n\n{}'.
            format(dest.server.name, dest.name, dest.id, message)))