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

Discord confession匿名表白系统如何实现无命令触发及管理日志功能

实现方案

1. 免命令直接提交表白功能

实现逻辑:放弃原有的命令触发逻辑,改用on_message事件监听用户消息,仅对指定的表白提交频道的用户消息做响应,自动识别为表白内容处理,同时过滤机器人自身消息避免循环触发。

2. 管理日志频道功能

实现逻辑:

  • 新增全局字典存储已发送的匿名表白的关联信息:键为表白频道发出的消息ID,值为发布者用户名、ID、表白内容等信息
  • 新增on_message_delete事件监听,当被删除的消息ID存在于上述字典中时,自动将关联信息推送到指定的管理日志频道

修改后完整代码

import discord
from discord.ext import commands

class Confess(commands.Cog):
    def __init__(self, client: discord.ext.commands.Bot):
        self.client = client
        # 存储已发表白的关联信息:key=表白频道消息ID,value=字典存用户信息和内容
        self.confess_records = {}
        # 频道ID配置,可自行修改
        self.SUBMIT_CHANNEL_ID = 806649868314869760 # 用户提交表白的频道
        self.PUBLISH_CHANNEL_ID = 806649874379964487 # 公开发布表白的频道
        self.LOG_CHANNEL_ID = 替换成你自己的管理日志频道ID # 管理日志频道
        
    # 监听所有消息,实现免命令提交
    @commands.Cog.listener()
    async def on_message(self, message: discord.Message):
        # 过滤机器人自己的消息、非提交频道的消息
        if message.author.bot or message.channel.id != self.SUBMIT_CHANNEL_ID:
            return
        # 过滤空消息(比如只有附件没有文字的情况,可根据需求调整)
        if not message.content.strip():
            await message.delete()
            tip_embed = discord.Embed(title="提示", description="表白内容不能为空哦")
            await message.channel.send(embed=tip_embed, delete_after=5)
            return
        
        # 原有提交逻辑复用
        await message.delete()
        success_embed = discord.Embed(title="提交成功", description=f"你的表白已经发送到<#{self.PUBLISH_CHANNEL_ID}>频道啦!")
        success_embed.set_footer(text="匿名表白")
        await message.channel.send(embed=success_embed, delete_after=10)
        
        # 发布匿名表白
        publish_channel = self.client.get_channel(self.PUBLISH_CHANNEL_ID)
        confess_embed = discord.Embed(title="匿名表白", description=message.content)
        confess_embed.set_footer(text="所有表白均为匿名发布")
        sent_msg = await publish_channel.send(embed=confess_embed)
        
        # 记录到存储字典,方便后续删除时查日志
        self.confess_records[sent_msg.id] = {
            "author_name": message.author.name,
            "author_id": message.author.id,
            "content": message.content,
            "send_time": message.created_at.strftime("%Y-%m-%d %H:%M:%S")
        }

    # 监听消息删除事件,实现日志记录
    @commands.Cog.listener()
    async def on_message_delete(self, message: discord.Message):
        # 只处理表白频道被删除的、有记录的表白消息
        if message.channel.id != self.PUBLISH_CHANNEL_ID or message.id not in self.confess_records:
            return
        # 取对应的记录
        record = self.confess_records.pop(message.id)
        # 发送到日志频道
        log_channel = self.client.get_channel(self.LOG_CHANNEL_ID)
        log_embed = discord.Embed(title="表白被删除", color=discord.Color.red())
        log_embed.add_field(name="发布者用户名", value=record["author_name"], inline=False)
        log_embed.add_field(name="发布者ID", value=record["author_id"], inline=False)
        log_embed.add_field(name="发布时间", value=record["send_time"], inline=False)
        log_embed.add_field(name="表白内容", value=record["content"], inline=False)
        await log_channel.send(embed=log_embed)

def setup(client):
    client.add_cog(Confess(client))

注意事项

  • 代码里的LOG_CHANNEL_ID需要替换为你自己创建的管理日志频道的ID
  • 如果需要持久化存储表白记录(避免机器人重启后记录丢失),可以把confess_records的内容写入本地JSON文件或者数据库
  • 如果想要支持带附件的表白,可以额外获取message.attachments的内容添加到表白 embed 里

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 12:36:04