You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord调用send报view参数错误,如何正确使用discord.View实现交互?

报错原因排查

send() got an unexpected keyword argument 'view'报错的核心原因是当前使用的discord.py版本低于2.0,discord.View是discord.py 2.0版本才新增的交互组件容器,旧版本不支持send方法传入view参数。
可以先执行命令升级版本:

pip install -U discord.py

升级完成后执行pip show discord.py,确认Version字段≥2.0即可解决参数不识别的问题。


基础View类使用规则

  • View是所有交互组件的容器类,需要继承discord.View自定义专属组件类,按钮、下拉菜单等交互控件都要绑定到该类的实例上
  • 按钮需要通过@discord.ui.button()装饰器定义,装饰器可传入label、emoji、style等参数自定义按钮样式
  • 每个按钮对应的回调函数会在用户点击按钮时触发,回调函数的第一个参数是按钮自身,第二个是触发交互的discord.Interaction实例

实现类Jishaku的代码分页功能示例

以下是完整的可运行代码,和jsk cat效果一致,支持分页查看文件内容:

import discord
from discord.ext import commands

# 自定义分页View类
class FilePaginatorView(discord.View):
    def __init__(self, pages: list[str], author_id: int, timeout: float = 180):
        super().__init__(timeout=timeout)
        self.pages = pages
        self.author_id = author_id
        self.current_page = 0
        # 总页数只有1时直接禁用翻页按钮
        if len(self.pages) <= 1:
            self.prev_button.disabled = True
            self.next_button.disabled = True

    # 动态更新按钮禁用状态
    def update_button_states(self):
        self.prev_button.disabled = self.current_page == 0
        self.next_button.disabled = self.current_page == len(self.pages) - 1

    # 上一页按钮
    @discord.ui.button(label="上一页", style=discord.ButtonStyle.secondary, emoji="⬅️")
    async def prev_button(self, interaction: discord.Interaction, button: discord.ui.Button):
        # 限制只有触发命令的用户可以操作分页
        if interaction.user.id != self.author_id:
            return await interaction.response.send_message("你没有权限操作该分页器", ephemeral=True)
        self.current_page -= 1
        self.update_button_states()
        await interaction.response.edit_message(content=f"```\n{self.pages[self.current_page]}\n```", view=self)

    # 下一页按钮
    @discord.ui.button(label="下一页", style=discord.ButtonStyle.secondary, emoji="➡️")
    async def next_button(self, interaction: discord.Interaction, button: discord.ui.Button):
        if interaction.user.id != self.author_id:
            return await interaction.response.send_message("你没有权限操作该分页器", ephemeral=True)
        self.current_page += 1
        self.update_button_states()
        await interaction.response.edit_message(content=f"```\n{self.pages[self.current_page]}\n```", view=self)

# 自定义cat命令
@commands.command(name="cat")
async def cat_file(ctx: commands.Context, filename: str):
    # 读取目标文件
    try:
        with open(filename, "r", encoding="utf-8") as f:
            content = f.read()
    except FileNotFoundError:
        await ctx.send("指定文件不存在")
        return
    
    # 按每页1900字符拆分内容,避免超过discord单条消息长度限制
    pages = []
    current_page = ""
    for line in content.splitlines(keepends=True):
        if len(current_page) + len(line) > 1900:
            pages.append(current_page)
            current_page = line
        else:
            current_page += line
    if current_page:
        pages.append(current_page)
    
    # 发送带分页组件的消息
    await ctx.send(
        content=f"```\n{pages[0]}\n```",
        view=FilePaginatorView(pages=pages, author_id=ctx.author.id)
    )

注意事项

  • 按钮回调里必须调用interaction.response方法响应交互,否则用户端会显示「交互失败」的提示,编辑现有消息用edit_message,发仅触发用户可见的响应可以加ephemeral=True参数
  • View的timeout参数设置组件的有效时长,超时后所有按钮会自动禁用,默认是180秒,可以按需调整

内容的提问来源于stack exchange,提问作者David Fernández Alfageme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 18:03:02