跳到内容

Discord

DiscordReader #

基类:BasePydanticReader

Discord 读取器。

从频道读取对话。

参数

名称 类型 描述 默认值
discord_token Optional[str]

Discord token。如果未提供,则假定已设置环境变量 DISCORD_TOKEN

源代码位于 llama-index-integrations/readers/llama-index-readers-discord/llama_index/readers/discord/base.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
class DiscordReader(BasePydanticReader):
    """
    Discord reader.

    Reads conversations from channels.

    Args:
        discord_token (Optional[str]): Discord token. If not provided, we
            assume the environment variable `DISCORD_TOKEN` is set.

    """

    is_remote: bool = True
    discord_token: str

    def __init__(self, discord_token: Optional[str] = None) -> None:
        """Initialize with parameters."""
        try:
            import discord  # noqa: F401
        except ImportError:
            raise ImportError(
                "`discord.py` package not found, please run `pip install discord.py`"
            )
        if discord_token is None:
            discord_token = os.environ["DISCORD_TOKEN"]
            if discord_token is None:
                raise ValueError(
                    "Must specify `discord_token` or set environment "
                    "variable `DISCORD_TOKEN`."
                )

        super().__init__(discord_token=discord_token)

    @classmethod
    def class_name(cls) -> str:
        """Get the name identifier of the class."""
        return "DiscordReader"

    def _read_channel(
        self, channel_id: int, limit: Optional[int] = None, oldest_first: bool = True
    ) -> List[Document]:
        """Read channel."""
        return asyncio.get_event_loop().run_until_complete(
            read_channel(
                self.discord_token, channel_id, limit=limit, oldest_first=oldest_first
            )
        )

    def load_data(
        self,
        channel_ids: List[int],
        limit: Optional[int] = None,
        oldest_first: bool = True,
    ) -> List[Document]:
        """
        Load data from the input directory.

        Args:
            channel_ids (List[int]): List of channel ids to read.
            limit (Optional[int]): Maximum number of messages to read.
            oldest_first (bool): Whether to read oldest messages first.
                Defaults to `True`.

        Returns:
            List[Document]: List of documents.

        """
        results: List[Document] = []
        for channel_id in channel_ids:
            if not isinstance(channel_id, int):
                raise ValueError(
                    f"Channel id {channel_id} must be an integer, "
                    f"not {type(channel_id)}."
                )
            channel_documents = self._read_channel(
                channel_id, limit=limit, oldest_first=oldest_first
            )
            results += channel_documents
        return results

class_name classmethod #

class_name() -> str

获取类的名称标识符。

源代码位于 llama-index-integrations/readers/llama-index-readers-discord/llama_index/readers/discord/base.py
123
124
125
126
@classmethod
def class_name(cls) -> str:
    """Get the name identifier of the class."""
    return "DiscordReader"

load_data #

load_data(channel_ids: List[int], limit: Optional[int] = None, oldest_first: bool = True) -> List[Document]

从输入目录加载数据。

参数

名称 类型 描述 默认值
channel_ids List[int]

要读取的频道 ID 列表。

必需
limit Optional[int]

要读取的最大消息数。

oldest_first bool

是否优先读取最旧的消息。默认为 True

True

返回值

类型 描述
List[Document]

List[Document]:文档列表。

源代码位于 llama-index-integrations/readers/llama-index-readers-discord/llama_index/readers/discord/base.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
def load_data(
    self,
    channel_ids: List[int],
    limit: Optional[int] = None,
    oldest_first: bool = True,
) -> List[Document]:
    """
    Load data from the input directory.

    Args:
        channel_ids (List[int]): List of channel ids to read.
        limit (Optional[int]): Maximum number of messages to read.
        oldest_first (bool): Whether to read oldest messages first.
            Defaults to `True`.

    Returns:
        List[Document]: List of documents.

    """
    results: List[Document] = []
    for channel_id in channel_ids:
        if not isinstance(channel_id, int):
            raise ValueError(
                f"Channel id {channel_id} must be an integer, "
                f"not {type(channel_id)}."
            )
        channel_documents = self._read_channel(
            channel_id, limit=limit, oldest_first=oldest_first
        )
        results += channel_documents
    return results