从消息中获取view()对象

问题描述 投票:0回答:2

所以我的问题是我有一个消息视图,我将消息 ID 保存在数据库中以供稍后使用

到了那个时候,我想禁用视图按钮,但为此我再次需要视图对象。

基本上我需要消息对象中的视图对象

提前谢谢你

python-3.x discord.py
2个回答
0
投票

关于视图的文档不在https://discordpy.readthedocs.io/上,所以我一直在使用 pycord 文档,因为它们在很多方面看起来基本相同。 https://docs.pycord.dev/en/master/api.html?highlight=view#discord.ui.View.from_message是你想要使用的。

问题是,如果你只是保存消息 ID,你将四处搜索以找到消息所在的频道,以便获得消息对象。最简单的方法就是使用消息链接而不是消息 ID,因为这样您就知道公会、频道和消息 ID。如果您只能使用消息 ID,您可以这样搜索:Discord.py 从不同渠道获取消息

一旦找到正确的频道,您只需要获取消息,并使用 View.from_message 从中获取视图即可。

message = channel.fetch_message(messageID)
view = discord.ui.view.from_message(message)

我不确定pycord的from_message是否与discord.py不同,但这里的discord.ui.view.py确实有from_message。

    def from_message(cls, message: Message, /, *, timeout: Optional[float] = 180.0) -> View:
        """Converts a message's components into a :class:`View`.

        The :attr:`.Message.components` of a message are read-only
        and separate types from those in the ``discord.ui`` namespace.
        In order to modify and edit message components they must be
        converted into a :class:`View` first.

        Parameters
        -----------
        message: :class:`discord.Message`
            The message with components to convert into a view.
        timeout: Optional[:class:`float`]
            The timeout of the converted view.

        Returns
        --------
        :class:`View`
            The converted view. This always returns a :class:`View` and not
            one of its subclasses.
        """
        view = View(timeout=timeout)
        for component in _walk_all_components(message.components):
            view.add_item(_component_to_item(component))
        return view

https://github.com/Rapptz/discord.py/blob/master/discord/ui/view.py


© www.soinside.com 2019 - 2024. All rights reserved.