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

discord.py中如何编辑其他命令发送的embed 实现建议系统accept/deny功能

Discord 建议系统功能实现方案

前置修复

原有suggest指令存在3个需要先修正的问题:

  • 命令前缀设置错误:原代码前缀为test,需改为?才能触发?suggest类指令
  • Embed字符串未添加f标识符,导致用户名提及无法正常解析
  • 建议内容参数只能捕获单个单词,需调整参数写法支持整段文本
  • 缺少建议消息的关联逻辑,无法定位待审核的建议消息

完整实现代码

import discord
from discord.ext import commands

# 全局存储:key为建议消息ID,value为建议提交用户ID(可选,用于审核后通知提交人)
suggestion_records = {}
# 注意需要在Discord开发者后台开启机器人对应的意图权限
client = commands.Bot(command_prefix='?', intents=discord.Intents.all())

@client.command()
async def suggest(ctx, *, suggestion):
    # 构造建议Embed
    embed = discord.Embed(
        title=f'Suggestion by {ctx.author.mention}',
        description=suggestion,
        color=discord.Color.blurple() # 默认建议卡片为蓝紫色
    )
    # 发送建议消息并存档
    suggestion_msg = await ctx.send(embed=embed)
    suggestion_records[suggestion_msg.id] = ctx.author.id

@client.command()
@commands.has_permissions(administrator=True) # 可选限制:仅管理员可调用审核指令
async def accept(ctx, msg_id: int, *, reason):
    # 拉取目标建议消息
    try:
        suggestion_msg = await ctx.channel.fetch_message(msg_id)
    except:
        await ctx.send("未找到对应建议消息,请检查消息ID是否正确", delete_after=5)
        return
    # 修改Embed属性
    original_embed = suggestion_msg.embeds[0]
    original_embed.color = discord.Color.green()
    original_embed.add_field(name="审核结果", value=f"accepted for reason: {reason}", inline=False)
    # 更新消息
    await suggestion_msg.edit(embed=original_embed)
    await ctx.message.delete() # 可选:删除审核指令消息,保持频道整洁

@client.command()
@commands.has_permissions(administrator=True) # 可选限制:仅管理员可调用审核指令
async def deny(ctx, msg_id: int, *, reason):
    # 拉取目标建议消息
    try:
        suggestion_msg = await ctx.channel.fetch_message(msg_id)
    except:
        await ctx.send("未找到对应建议消息,请检查消息ID是否正确", delete_after=5)
        return
    # 修改Embed属性
    original_embed = suggestion_msg.embeds[0]
    original_embed.color = discord.Color.red()
    original_embed.add_field(name="审核结果", value=f"denied for reason: {reason}", inline=False)
    # 更新消息
    await suggestion_msg.edit(embed=original_embed)
    await ctx.message.delete() # 可选:删除审核指令消息,保持频道整洁

client.run("你的机器人Token")

使用说明

  • 提交建议:任意用户发送 ?suggest 这里填写你的建议内容 即可生成建议卡片
  • 审核通过:管理员发送 ?accept 建议消息ID 同意理由 即可修改对应建议卡片样式
  • 审核拒绝:管理员发送 ?deny 建议消息ID 拒绝理由 即可修改对应建议卡片样式

注:Discord设置中开启开发者模式后,右键消息点击「复制消息ID」即可获取目标建议的消息ID

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 22:54:05