如何为Discord.py版申请机器人添加Accept/Deny按钮并记录操作
实现Discord版主申请机器人的点击按钮与操作记录功能
核心修改说明
要添加Accept/Reject点击式按钮,需使用discord.py中的discord.ui.Button和discord.ui.View组件,同时实现按钮回调逻辑与操作记录到指定频道的功能。
修改后的完整代码
import os import discord from discord import app_commands TOKEN = 'BOT TOKEN' GUILD = 'GUILD NAME' ADMIN_CHANNEL_ID = 00000000000 # 替换为审核频道ID LOG_CHANNEL_ID = 00000000000 # 替换为操作记录频道ID intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) tree = app_commands.CommandTree(client) # 定义带按钮的View类 class ModAppView(discord.ui.View): def __init__(self, applicant: discord.User, app_details: dict): super().__init__(timeout=None) # 设置按钮永不超时 self.applicant = applicant self.app_details = app_details # Accept按钮回调 @discord.ui.button(label="Accept", style=discord.ButtonStyle.green) async def accept_button(self, interaction: discord.Interaction, button: discord.ui.Button): # 通知申请人 try: await self.applicant.send(f"你的版主申请已通过!\n姓名:{self.app_details['name']}\n申请理由:{self.app_details['reason']}\n相关经验:{self.app_details['exp']}") except discord.Forbidden: await interaction.response.send_message("无法给申请人发送私信(对方可能关闭了私信权限)", ephemeral=True) # 记录操作到日志频道 log_channel = client.get_channel(LOG_CHANNEL_ID) if log_channel: await log_channel.send( f"**版主申请通过**\n审核人:{interaction.user.mention}\n申请人:{self.applicant.mention}\n" f"申请详情:\n姓名:{self.app_details['name']}\n理由:{self.app_details['reason']}\n经验:{self.app_details['exp']}" ) # 更新原申请消息状态 await interaction.response.edit_message(content="✅ 申请已通过", view=None) # Reject按钮回调 @discord.ui.button(label="Reject", style=discord.ButtonStyle.red) async def reject_button(self, interaction: discord.Interaction, button: discord.ui.Button): # 通知申请人 try: await self.applicant.send(f"你的版主申请未通过。\n姓名:{self.app_details['name']}\n申请理由:{self.app_details['reason']}\n相关经验:{self.app_details['exp']}") except discord.Forbidden: await interaction.response.send_message("无法给申请人发送私信(对方可能关闭了私信权限)", ephemeral=True) # 记录操作到日志频道 log_channel = client.get_channel(LOG_CHANNEL_ID) if log_channel: await log_channel.send( f"**版主申请拒绝**\n审核人:{interaction.user.mention}\n申请人:{self.applicant.mention}\n" f"申请详情:\n姓名:{self.app_details['name']}\n理由:{self.app_details['reason']}\n经验:{self.app_details['exp']}" ) # 更新原申请消息状态 await interaction.response.edit_message(content="❌ 申请已拒绝", view=None) @client.event async def on_ready(): await tree.sync(guild=discord.Object(id=000000)) # 替换为你的服务器ID for guild in client.guilds: if guild.name == GUILD: break print(f'{client.user} 已连接到:\n {guild.name}') @tree.command(name='apply', description='提交版主申请', guild=discord.Object(id=000000)) # 替换为服务器ID async def commandno1(interaction: discord.Interaction, name: str, reason: str, exp: str): # 创建申请Embed emb = discord.Embed(title='版主申请', description=f'姓名:{name}\n申请理由:{reason}\n相关经验:{exp}', color=discord.Color.blue()) emb.set_author(name=f'申请人:{interaction.user.display_name}', icon_url=interaction.user.avatar.url) # 存储申请详情 app_details = { 'name': name, 'reason': reason, 'exp': exp } # 发送带按钮的申请消息到审核频道 channel = client.get_channel(ADMIN_CHANNEL_ID) if channel: await channel.send(embed=emb, view=ModAppView(interaction.user, app_details)) # 回复申请人 await interaction.response.send_message('申请已提交,请等待审核!', ephemeral=True) client.run(TOKEN)
关键修改点
- 按钮组件实现:自定义
ModAppView类继承discord.ui.View,添加绿色Accept和红色Reject按钮,每个按钮绑定独立的回调逻辑。 - 操作记录:在按钮回调中获取日志频道,发送包含审核人、申请人、申请详情的记录消息。
- 申请人通知:尝试给申请人发送私信告知结果,处理无法发送的异常情况。
- 消息更新:点击按钮后更新原申请消息内容并移除按钮,避免重复操作。
- 细节修正:修复原代码中
emb.set_author的字符串格式化错误,改用get_channel替代fetch_channel提升效率。
注意事项
- 确保机器人拥有发送消息、嵌入内容、管理消息的权限。
- 替换代码中所有占位的ID(服务器ID、审核频道ID、日志频道ID)为实际数值。
- 若需要设置按钮超时,可修改
ModAppView初始化时的timeout参数(例如timeout=86400代表24小时后失效)。
内容的提问来源于stack exchange,提问作者voss_almorad
相关产品推荐
相关产品推荐

