Пример #1
0
    async def _send(
        self,
        ctx: commands.Context,
        content: str = None,
        *,
        embed: discord.Embed = None,
        reference: discord.MessageReference = None,
        mention_author: bool = False,
    ) -> discord.Message:
        components = self._get_components()
        channel = ctx.channel
        data = {"components": [c.to_dict() for c in components]}
        if content:
            data["content"] = str(content)
        if embed:
            data["embed"] = embed.to_dict()
        if reference:
            data["message_reference"] = reference.to_dict()
        allowed_mentions = channel._state.allowed_mentions.to_dict()
        allowed_mentions["replied_user"] = mention_author
        data["allowed_mentions"] = allowed_mentions

        log.debug("sending data %r" % data)
        r = discord.http.Route("POST",
                               "/channels/{channel_id}/messages",
                               channel_id=channel.id)
        response = await self.bot._connection.http.request(r, json=data)
        self._buttons_closed = False
        return channel._state.create_message(channel=channel, data=response)
Пример #2
0
    async def send(self,
                   content=None,
                   embed=None,
                   dm=False,
                   no_dm_post=False,
                   strict_post=False,
                   files=None,
                   ignore_http_check=False,
                   paginate_field_limit=None,
                   send_as_slash_command=True,
                   channel_override=None,
                   allowed_mentions=AllowedMentions(everyone=False,
                                                    roles=False),
                   hidden=False,
                   ignore_errors=False,
                   reply=True,
                   reference=None,
                   mention_author=False,
                   fail_on_dm=False):
        if (dm and not IS_DOCKER) or (self.slash_command and hidden):
            dm = False

        if dm or isinstance(self.channel, DMChannel):
            send_as_slash_command = False
            reply = False
            reference = None
            mention_author = False

        if reply and not self.slash_command:
            if reference:
                reference = MessageReference(message_id=reference.id,
                                             channel_id=reference.channel.id,
                                             guild_id=reference.guild
                                             and reference.guild.id,
                                             fail_if_not_exists=False)
            else:
                reference = MessageReference(
                    message_id=self.message.id,
                    channel_id=self.message.channel.id,
                    guild_id=self.message.guild and self.message.guild.id,
                    fail_if_not_exists=False)
        else:
            reference = None

        content = str(content) if content else None

        channel = original_channel = channel_override or (
            dm and self.author) or self.channel
        webhook = None
        msg = None

        if not dm and not self.slash_command and self.webhook_only and self.guild and hasattr(
                channel, "webhooks"):
            my_permissions = self.guild.me.guild_permissions

            if my_permissions.manage_webhooks:
                profile, _ = await get_features(Object(id=self.guild.owner_id),
                                                guild=self.guild)

                if profile.features.get("premium"):
                    webhook = await cache_get(f"webhooks:{channel.id}")

                    if not webhook:
                        try:
                            for webhook in await channel.webhooks():
                                if webhook.token:
                                    await cache_set(f"webhooks:{channel.id}",
                                                    webhook)
                                    break
                            else:
                                webhook = await channel.create_webhook(
                                    name="Bloxlink Webhooks")
                                await cache_set(f"webhooks:{channel.id}",
                                                webhook)

                        except (Forbidden, NotFound):
                            self.webhook_only = False

                            try:
                                msg = await channel.send(
                                    "Customized Bot is enabled, but I couldn't "
                                    "create the webhook! Please give me the `Manage Webhooks` permission."
                                )
                            except (Forbidden, NotFound):
                                pass
                            else:
                                self.bot_responses.append(msg.id)
            else:
                self.webhook_only = False

                try:
                    msg = await channel.send(
                        "Customized Bot is enabled, but I couldn't "
                        "create the webhook! Please give me the `Manage Webhooks` permission."
                    )
                except (Forbidden, NotFound):
                    pass
                else:
                    self.bot_responses.append(msg.id)

        paginate = False
        pages = None

        if paginate_field_limit:
            pages = Paginate.get_pages(embed, embed.fields,
                                       paginate_field_limit)

            if len(pages) > 1:
                paginate = True

        if embed and not dm and not embed.color:
            embed.color = EMBED_COLOR

        if not paginate:
            try:
                msg = await self.send_to(
                    webhook or channel,
                    content,
                    files=files,
                    embed=embed,
                    allowed_mentions=allowed_mentions,
                    send_as_slash_command=send_as_slash_command,
                    hidden=hidden,
                    reference=reference,
                    mention_author=mention_author)

                if dm and not (no_dm_post
                               or isinstance(self.channel,
                                             (DMChannel, User, Member))):
                    await self.send_to(self.channel,
                                       "**Please check your DMs!**",
                                       reference=reference,
                                       mention_author=mention_author)

            except (Forbidden, NotFound):
                channel = channel_override or (
                    not strict_post and (dm and self.channel or self.author)
                    or channel)  # opposite channel
                reply = False
                reference = None
                mention_author = False

                if isinstance(channel, (User, Member)) and isinstance(
                        original_channel, TextChannel):
                    content = f"Disclaimer: you are getting this message DM'd since I don't have permission to post in {original_channel.mention}! " \
                              f"Please make sure I have these permissions: `Read Message History`, `Send Messages`, and `Embed Links`.\n{content or ''}"[:2000]
                else:
                    content = f"{original_channel.mention}, I was unable to  DM you! Here's the message here instead:\n{content or ''}"[:
                                                                                                                                        2000]

                if webhook:
                    await cache_pop(f"webhooks:{channel.id}")

                if strict_post:
                    if not ignore_errors:
                        if dm:
                            try:
                                await self.send_to(
                                    self.channel,
                                    "I was unable to DM you! Please check your privacy settings and try again.",
                                    reference=reference,
                                    mention_author=mention_author,
                                    hidden=True)
                            except (Forbidden, NotFound):
                                pass
                        else:
                            try:
                                await self.send_to(
                                    self.author,
                                    f"I was unable to post in {channel.mention}! Please make sure I have these permissions: `Read Message History`, `Send Messages`, and `Embed Links`.",
                                    reference=reference,
                                    mention_author=mention_author)
                            except (Forbidden, NotFound):
                                pass
                    return None

                try:
                    msg = await self.send_to(channel,
                                             content,
                                             files=files,
                                             embed=embed,
                                             allowed_mentions=allowed_mentions,
                                             hidden=hidden,
                                             reference=reference,
                                             mention_author=mention_author)
                except (Forbidden, NotFound):
                    if not no_dm_post:
                        if channel == self.author:
                            try:
                                await self.send_to(
                                    self.channel,
                                    "I was unable to DM you! Please check your privacy settings and try again.",
                                    hidden=True,
                                    reference=reference,
                                    mention_author=mention_author)
                            except (Forbidden, NotFound):
                                pass
                        else:
                            try:
                                await self.send_to(
                                    self.channel,
                                    "I was unable to post in the specified channel!",
                                    hidden=True,
                                    reference=reference,
                                    mention_author=mention_author)
                            except (Forbidden, NotFound):
                                pass

            except HTTPException:
                if not ignore_http_check:
                    if self.webhook_only:
                        self.webhook_only = False

                        return await self.send(
                            content=content,
                            embed=embed,
                            dm=dm,
                            hidden=hidden,
                            no_dm_post=no_dm_post,
                            strict_post=strict_post,
                            files=files,
                            send_as_slash_command=send_as_slash_command,
                            allowed_mentions=allowed_mentions,
                            ignore_errors=ignore_errors)

                    else:
                        if embed:
                            paginate = True

                        else:
                            raise HTTPException
        if paginate:
            paginator = Paginate(self.author,
                                 channel,
                                 embed,
                                 self,
                                 field_limit=paginate_field_limit,
                                 original_channel=self.channel,
                                 hidden=hidden,
                                 pages=pages,
                                 dm=dm)

            return await paginator()

        return msg
Пример #3
0
    async def send(self,
                   content=None,
                   embed=None,
                   dm=False,
                   no_dm_post=False,
                   strict_post=False,
                   files=None,
                   ignore_http_check=False,
                   paginate_field_limit=None,
                   send_as_slash_command=True,
                   channel_override=None,
                   allowed_mentions=AllowedMentions(everyone=False,
                                                    roles=False),
                   hidden=False,
                   ignore_errors=False,
                   reply=True,
                   reference=None,
                   mention_author=False,
                   fail_on_dm=False,
                   view=None):
        if (dm and not IS_DOCKER) or (self.interaction and hidden):
            dm = False

        if dm or isinstance(self.channel, DMChannel):
            send_as_slash_command = False
            reply = False
            reference = None
            mention_author = False

        if channel_override:
            send_as_slash_command = False

        if reply and not self.interaction:
            if reference:
                reference = MessageReference(message_id=reference.id,
                                             channel_id=reference.channel.id,
                                             guild_id=reference.guild
                                             and reference.guild.id,
                                             fail_if_not_exists=False)
            else:
                reference = MessageReference(
                    message_id=self.message.id,
                    channel_id=self.message.channel.id,
                    guild_id=self.message.guild and self.message.guild.id,
                    fail_if_not_exists=False)
        else:
            reference = None

        content = str(content) if content else None

        channel = original_channel = channel_override or (
            dm and self.author) or self.channel
        msg = None

        paginate = False
        pages = None

        if paginate_field_limit:
            pages = Paginate.get_pages(embed, embed.fields,
                                       paginate_field_limit)

            if len(pages) > 1:
                paginate = True

        if embed and not dm and not embed.color:
            embed.color = EMBED_COLOR

        if not paginate:
            try:
                msg = await self.send_to(
                    channel,
                    content,
                    files=files,
                    embed=embed,
                    allowed_mentions=allowed_mentions,
                    send_as_slash_command=send_as_slash_command,
                    hidden=hidden,
                    reference=reference,
                    mention_author=mention_author,
                    view=view)

                if dm and not (no_dm_post
                               or isinstance(self.channel,
                                             (DMChannel, User, Member))):
                    await self.send_to(self.channel,
                                       "**Please check your DMs!**",
                                       reference=reference,
                                       mention_author=mention_author)

            except (Forbidden, NotFound) as e:
                print(e)
                channel = channel_override or (
                    not strict_post and (dm and self.channel or self.author)
                    or channel)  # opposite channel
                reply = False
                reference = None
                mention_author = False

                if isinstance(channel, (User, Member)) and isinstance(
                        original_channel, TextChannel):
                    content = f"Disclaimer: you are getting this message DM'd since I don't have permission to post in {original_channel.mention}! " \
                              f"Please make sure I have these permissions: `Read Message History`, `Send Messages`, and `Embed Links`.\n{content or ''}"[:2000]
                else:
                    content = f"{original_channel.mention}, I was unable to DM you! Here's the message here instead:\n{content or ''}"[:
                                                                                                                                       2000]

                if strict_post:
                    if not ignore_errors:
                        if dm:
                            try:
                                await self.send_to(
                                    self.channel,
                                    "I was unable to DM you! Please check your privacy settings and try again.",
                                    reference=reference,
                                    mention_author=mention_author,
                                    hidden=True)
                            except (Forbidden, NotFound):
                                pass
                        else:
                            try:
                                await self.send_to(
                                    self.author,
                                    f"I was unable to post in {channel.mention}! Please make sure I have these permissions: `Read Message History`, `Send Messages`, and `Embed Links`.",
                                    reference=reference,
                                    mention_author=mention_author)
                            except (Forbidden, NotFound):
                                pass
                    return None

                try:
                    msg = await self.send_to(channel,
                                             content,
                                             files=files,
                                             embed=embed,
                                             allowed_mentions=allowed_mentions,
                                             hidden=hidden,
                                             reference=reference,
                                             mention_author=mention_author,
                                             view=view)
                except (Forbidden, NotFound):
                    if not no_dm_post:
                        if channel == self.author:
                            try:
                                await self.send_to(
                                    self.channel,
                                    "I was unable to DM you! Please check your privacy settings and try again.",
                                    hidden=True,
                                    reference=reference,
                                    mention_author=mention_author)
                            except (Forbidden, NotFound):
                                pass
                        else:
                            try:
                                await self.send_to(
                                    self.channel,
                                    "I was unable to post in the specified channel!",
                                    hidden=True,
                                    reference=reference,
                                    mention_author=mention_author)
                            except (Forbidden, NotFound):
                                pass

            except HTTPException:
                if not ignore_http_check:
                    if embed:
                        paginate = True

                    else:
                        raise HTTPException
        if paginate:
            paginator = Paginate(self.author,
                                 channel,
                                 embed,
                                 self,
                                 field_limit=paginate_field_limit,
                                 original_channel=self.channel,
                                 hidden=hidden,
                                 pages=pages,
                                 dm=dm)

            return await paginator()

        return msg