Skip to content

Summary

SummarizeMenuView

SummarizeMenuView(bot: Bot, original_interaction: Interaction, timeout: float = 60)

Bases: View

Methods:

Name Description
select_history_count
select_target_user
submit
Source code in src/cogs/summary.py
def __init__(self, bot: commands.Bot, original_interaction: Interaction, timeout: float = 60):
    super().__init__(timeout=timeout)
    self.bot = bot
    self.original_interaction = original_interaction
    self.history_count: int | None = None
    self.target_user: Member | None = None

bot

bot = bot

original_interaction

original_interaction = original_interaction

history_count

history_count: int | None = None

target_user

target_user: Member | None = None

select_history_count

select_history_count(select: Select, interaction: Interaction) -> None
Source code in src/cogs/summary.py
@nextcord.ui.select(
    placeholder="選擇要總結的訊息數量",
    options=[
        nextcord.SelectOption(label="5", value="5"),
        nextcord.SelectOption(label="10", value="10"),
        nextcord.SelectOption(label="20", value="20"),
        nextcord.SelectOption(label="50", value="50"),
    ],
)
async def select_history_count(
    self, select: nextcord.ui.Select, interaction: Interaction
) -> None:
    # 儲存選擇的訊息數量
    self.history_count = int(select.values[0])
    # 簡單回應以確認選擇(不會產生新訊息)
    await interaction.response.defer()

select_target_user

select_target_user(user_select: UserSelect, interaction: Interaction) -> None
Source code in src/cogs/summary.py
@nextcord.ui.user_select(placeholder="選擇要總結的使用者 (可選)", min_values=0, max_values=1)
async def select_target_user(
    self, user_select: nextcord.ui.UserSelect, interaction: Interaction
) -> None:
    # 如果有選擇使用者,則儲存;否則保持 None
    if user_select.values:
        self.target_user = user_select.values[0]
    else:
        self.target_user = None
    await interaction.response.defer()

submit

submit(button: Button, interaction: Interaction) -> None
Source code in src/cogs/summary.py
@nextcord.ui.button(label="提交", style=nextcord.ButtonStyle.primary)
async def submit(self, button: nextcord.ui.Button, interaction: Interaction) -> None:
    # 如果使用者未選擇訊息數量,則採用預設 20
    if self.history_count is None:
        self.history_count = 20

    # 取得負責訊息總結的 Cog
    cog: MessageFetcher = self.bot.get_cog("MessageFetcher")
    if cog is None:
        await interaction.response.send_message("找不到訊息總結的處理器。", ephemeral=True)
        self.stop()
        return

    # 回應「處理中…」(避免互動逾時)
    await interaction.response.defer(ephemeral=True)

    # 執行總結流程
    summary = await cog.do_summarize(interaction.channel, self.history_count, self.target_user)
    await interaction.followup.send(summary, ephemeral=True)
    self.stop()

MessageFetcher

MessageFetcher(bot: Bot)

Bases: Cog

Methods:

Name Description
sum

呼叫此指令後,會彈出一個選單視窗讓你選擇

do_summarize

根據頻道、訊息數量與目標使用者,抓取並整理訊息,

Source code in src/cogs/summary.py
def __init__(self, bot: commands.Bot):
    self.bot = bot

bot

bot = bot

sum

sum(interaction: Interaction) -> None

呼叫此指令後,會彈出一個選單視窗讓你選擇 - 要總結的訊息數量 - 是否僅總結某個使用者的訊息(可選)

Source code in src/cogs/summary.py
@nextcord.slash_command(
    name="sum",
    description="透過選單選擇總結訊息。",
    name_localizations={Locale.zh_TW: "總結訊息", Locale.ja: "メッセージ要約"},
    description_localizations={
        Locale.zh_TW: "使用選單來總結此頻道中的訊息。",
        Locale.ja: "メニュー選択を通じて、このチャンネルのメッセージを要約します。",
    },
    dm_permission=True,
    nsfw=False,
)
async def sum(self, interaction: Interaction) -> None:
    """呼叫此指令後,會彈出一個選單視窗讓你選擇
    - 要總結的訊息數量
    - 是否僅總結某個使用者的訊息(可選)
    """
    view = SummarizeMenuView(self.bot, interaction)
    await interaction.response.defer(ephemeral=bool(interaction.guild))
    await interaction.followup.send(
        "請選擇總結選項:", view=view, ephemeral=bool(interaction.guild)
    )

do_summarize

do_summarize(channel: TextChannel, history_count: int, target_user: Member | None) -> str

根據頻道、訊息數量與目標使用者,抓取並整理訊息, 接著呼叫 LLM 來產生總結內容。

Returns:

Name Type Description
str str

總結的結果。

Source code in src/cogs/summary.py
async def do_summarize(
    self, channel: nextcord.TextChannel, history_count: int, target_user: Member | None
) -> str:
    """根據頻道、訊息數量與目標使用者,抓取並整理訊息,
    接著呼叫 LLM 來產生總結內容。

    Returns:
        str: 總結的結果。
    """
    messages = await self._fetch_messages(channel, history_count, target_user)
    if not messages:
        if target_user:
            return f"在此頻道中找不到 {target_user.mention} 的相關訊息。"
        return "此頻道沒有可供總結的訊息。"

    chat_history_string, attachments = self._format_messages(messages)
    final_prompt = self._create_summary_prompt(history_count, chat_history_string)
    summary = await self._call_llm(final_prompt, attachments)
    return summary