Esempio n. 1
0
 async def edit_goal(self, bot: Red, channel: discord.TextChannel,
                     message_id: int, em: discord.Embed) -> None:
     try:
         if not channel.permissions_for(channel.guild.me).embed_links:
             return
         try:
             if version_info >= VersionInfo.from_str("3.4.6"):
                 message = channel.get_partial_message(message_id)
             else:
                 message = await channel.fetch_message(message_id)
         except (discord.errors.NotFound, discord.errors.Forbidden):
             return
         guild = channel.guild
         game_day_channels = await bot.get_cog("Hockey").config.guild(
             guild).gdc()
         role = discord.utils.get(guild.roles,
                                  name=self.team_name + " GOAL")
         if game_day_channels is not None:
             # We don't want to ping people in the game day channels twice
             if channel.id in game_day_channels:
                 role = None
         if role is None or "missed" in self.event.lower():
             await message.edit(embed=em)
         else:
             await message.edit(content=role.mention, embed=em)
     except (discord.errors.NotFound, discord.errors.Forbidden):
         return
     except Exception:
         log.exception("Could not edit goal in %s", repr(channel))
Esempio n. 2
0
    async def _send_embed(self, channel: TextChannel, embed: Embed) -> None:
        """Send an embed to the specified channel

        Parameters
        ----------
        channel : TextChannel
            Channel to send to
        embed : Embed
            Embed to use
        """
        embed.set_footer(text="Last update")
        embed.set_author(
            name=UPDATE_NAME.format(FEEDS[self.service]["friendly"]),
            icon_url=FEEDS[self.service]["avatar"],
        )

        if self.channeldata.mode == "edit":
            if edit_id := self.channeldata.edit_id.get(
                    self.incidentdata.incident_id):
                try:
                    message = channel.get_partial_message(edit_id)
                    await message.edit(embed=embed, content=None)
                except Exception:  # eg message deleted
                    edit_id = None
            if not edit_id:
                sent_message: Message = await channel.send(embed=embed)
                await self.config_wrapper.update_edit_id(
                    channel.id, self.service, self.incidentdata.incident_id,
                    sent_message.id)
Esempio n. 3
0
 async def edit(self, star_channel: discord.TextChannel, content: str) -> None:
     if self.new_message is None:
         return
     try:
         if version_info >= VersionInfo.from_str("3.4.6"):
             message_edit = star_channel.get_partial_message(self.new_message)
         else:
             message_edit = await star_channel.fetch_message(self.new_message)
         await message_edit.edit(content=content)
     except (discord.errors.NotFound, discord.errors.Forbidden):
         return
Esempio n. 4
0
 async def delete(self, star_channel: discord.TextChannel) -> None:
     if self.new_message is None:
         return
     try:
         if version_info >= VersionInfo.from_str("3.4.6"):
             message_edit = star_channel.get_partial_message(self.new_message)
         else:
             message_edit = await star_channel.fetch_message(self.new_message)
         self.new_message = None
         self.new_channel = None
         await message_edit.delete()
     except (discord.errors.NotFound, discord.errors.Forbidden):
         return
Esempio n. 5
0
    async def _send_plain(self, channel: TextChannel, msg: str) -> None:
        """Send a plain message to the specified channel

        Parameters
        ----------
        channel : TextChannel
            Channel to send to
        msg : str
            Message to send
        """
        if self.channeldata.mode == "edit":
            if edit_id := self.channeldata.edit_id.get(
                    self.incidentdata.incident_id):
                try:
                    message = channel.get_partial_message(edit_id)
                    await message.edit(embed=None, content=None)
                except Exception:  # eg message deleted
                    edit_id = None
            if not edit_id:
                sent_message = await channel.send(content=msg)
                await self.config_wrapper.update_edit_id(
                    channel.id, self.service, self.incidentdata.incident_id,
                    sent_message.id)
Esempio n. 6
0
    async def from_json(cls, mod_channel: discord.TextChannel, bot: Red,
                        case_number: int, data: dict, **kwargs):
        """Get a Case object from the provided information

        Parameters
        ----------
        mod_channel: discord.TextChannel
            The mod log channel for the guild
        bot: Red
            The bot's instance. Needed to get the target user
        case_number: int
            The case's number.
        data: dict
            The JSON representation of the case to be gotten
        **kwargs
            Extra attributes for the Case instance which override values
            in the data dict. These should be complete objects and not
            IDs, where possible.

        Returns
        -------
        Case
            The case object for the requested case

        Raises
        ------
        `discord.NotFound`
            The user the case is for no longer exists
        `discord.Forbidden`
            Cannot read message history to fetch the original message.
        `discord.HTTPException`
            A generic API issue
        """
        guild = kwargs.get("guild") or mod_channel.guild

        message = kwargs.get("message")
        if message is None:
            message_id = data.get("message")
            if message_id is not None:
                message = mod_channel.get_partial_message(message_id)

        user_objects = {"user": None, "moderator": None, "amended_by": None}
        for user_key in tuple(user_objects):
            user_object = kwargs.get(user_key)
            if user_object is None:
                user_id = data.get(user_key)
                if user_id is None:
                    user_object = None
                else:
                    user_object = bot.get_user(user_id) or user_id
            user_objects[user_key] = user_object

        channel = kwargs.get("channel") or guild.get_channel(
            data["channel"]) or data["channel"]
        case_guild = kwargs.get("guild") or bot.get_guild(data["guild"])
        return cls(
            bot=bot,
            guild=case_guild,
            created_at=data["created_at"],
            action_type=data["action_type"],
            case_number=case_number,
            reason=data["reason"],
            until=data["until"],
            channel=channel,
            modified_at=data["modified_at"],
            message=message,
            last_known_username=data.get("last_known_username"),
            **user_objects,
        )