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

如何让Discord modmail bot关闭modmail并拦截发送垃圾私信的用户

实现modmail用户拉黑拦截/功能关闭方案


1. 基础功能实现(内存级黑名单,重启bot后配置会重置)

首先在代码全局位置定义黑名单存储变量:
blocked_users = set()

2. 新增管理员专属拉黑/解黑指令

# 拉黑指令,使用方法:!block [用户ID]
@client.command()
@commands.has_permissions(administrator=True)
async def block(ctx, user_id: int):
    blocked_users.add(user_id)
    await ctx.send(f"已拉黑ID为{user_id}的用户,该用户后续私信不会再转发到modmail频道")

# 解黑指令,使用方法:!unblock [用户ID]
@client.command()
@commands.has_permissions(administrator=True)
async def unblock(ctx, user_id: int):
    if user_id in blocked_users:
        blocked_users.remove(user_id)
        await ctx.send(f"已解黑ID为{user_id}的用户")
    else:
        await ctx.send("该用户不在黑名单中")

3. 修改现有on_message事件逻辑

加入黑名单校验,调整后完整代码如下:

@client.event
async def on_message(message):
    channel = client.get_channel(909853337510371449)
    if message.author.id == client.user.id:
        return
    # 黑名单校验,命中则直接终止逻辑不转发
    if message.author.id in blocked_users:
        return
    if not message.guild:
        # 转发时附带用户ID,方便直接复制调用拉黑指令
        await channel.send(f"[{message.author.mention} (ID:{message.author.id})] {message.content}")
    await client.process_commands(message)

彻底关闭modmail功能方案

直接注释掉if not message.guild:下属的转发代码块即可,无需删除代码,后续需要恢复时取消注释就能正常使用。

若需要重启bot后黑名单配置依然生效,可以把blocked_users的数据写入本地json文件、数据库做持久化存储,启动时读取即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 13:36:03