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

如何用discord.py将用户建议发送至指定日志频道?

实现建议发送至日志频道的方案

要实现用户确认建议后发送到指定日志频道,需要做以下两处修改:

1. 修改View类,传递并使用建议信息

让View类接收建议的标题、描述和用户信息,在按钮回调中获取日志频道并发送消息:

class View(discord.ui.View):
    def __init__(self, title: str, description: str, user: discord.User):
        super().__init__()
        self.title = title
        self.description = description
        self.user = user

    @discord.ui.button(label='Confirm', style=discord.ButtonStyle.green)
    async def asdf(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.edit_message(content="你的建议已成功发送。", embed=None, view=None)
        
        # 获取预设的日志频道
        log_channel = interaction.client.get_channel(log_channel_id)
        if not log_channel:
            await interaction.followup.send("无法找到日志频道,请检查频道ID配置。", ephemeral=True)
            return
        
        # 构建日志频道的嵌入消息
        log_embed = discord.Embed(title=f"新建议:{self.title}", color=discord.Color.blue())
        log_embed.add_field(name="描述", value=self.description, inline=False)
        log_embed.set_author(name=self.user.name, icon_url=self.user.avatar.url)
        log_embed.set_footer(text=f"用户ID:{self.user.id}")
        
        # 发送到日志频道
        await log_channel.send(embed=log_embed)

2. 在命令中传递参数给View

调用View时把用户输入的标题、描述和用户对象传进去:

@bot.tree.command(name="suggest", description="使用此命令提交建议。")
async def hello(interaction: discord.Interaction, title: str, description: str):
    if interaction.user.id in blacklist:
        embed = discord.Embed(title=f"无法执行命令\n原因:已被拉黑", color=discord.Color.red())
        await interaction.response.send_message(embed=embed, ephemeral=True)
    else:
        embed = discord.Embed(title=f"请确认以下信息是否正确?", color=discord.Colour.random())
        embed.set_thumbnail(url=f"{interaction.user.avatar.url}")
        embed.add_field(name=f"标题", value=f"{title}", inline=False)
        embed.add_field(name="描述", value=f"{description}")
        embed.set_footer(text=f"用户ID:{interaction.user.id}")
        # 传递必要参数给View
        await interaction.response.send_message(embed=embed, view=View(title, description, interaction.user), ephemeral=True)

关键说明

  • 确保log_channel_id是你预先设置的整数类型频道ID,不要用字符串
  • 加入了日志频道不存在的错误处理,避免程序崩溃
  • 日志频道的嵌入消息单独设计,更清晰展示建议来源和内容

内容的提问来源于stack exchange,提问作者As2Bax

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 17:05:26