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

discord.py机器人读取私信转发报错求助:AttributeError与TypeError问题

Fixing Your Discord Bot's DM Forwarding Issues

Hey there! Let's break down what's going wrong with your code and get that DM forwarding working properly—you're already close, just a few small tweaks needed.

Let's go through your errors first:

  1. AttributeError with message.server:
    Discord.py renamed the server attribute to guild starting in v1.0+, so using message.server will throw an error. Your switch to message.guild was the right call here.

  2. Bot has no attribute send_message:
    The send_message() method was removed in newer Discord.py versions. Instead, you should call send() directly on the channel object you want to send to.

  3. TypeError with on_message(ctx, message):
    The on_message event callback only takes a single parameter: message. The ctx (context) parameter is used for command functions (like @bot.command()), not for event handlers. Adding it here causes Python to expect an extra argument that never gets passed.

Here's the corrected working code:

@bot.event
async def on_message(message):
    # Replace with your actual channel ID (must be an integer, not a string!)
    target_channel_id = 123456789012345678
    channel = bot.get_channel(target_channel_id)
    
    # Check if the message is a DM and not from the bot itself
    if message.guild is None and message.author != bot.user:
        # Forward the DM content to your target channel (includes sender info for clarity)
        await channel.send(f"Received DM from {message.author.name} ({message.author.id}):\n{message.content}")
        # Optional: Print the DM directly to your console too
        print(f"[DM] {message.author.name}: {message.content}")
    
    # Don't forget this line—otherwise your existing emoji/moderation commands won't work!
    await bot.process_commands(message)

Key notes about the fixed code:

  • Channel ID must be an integer: Remove the quotes around your channel ID—get_channel() expects a numeric ID, not a string. You can enable Discord's Developer Mode to copy channel IDs easily.
  • Clear DM detection: message.guild is None correctly identifies direct messages, since DMs don't belong to any guild/server.
  • Proper message sending: Using channel.send() is the current standard way to send messages in Discord.py. I added the sender's name and ID to the forwarded message to help you track who sent the DM—handy for moderation use cases.
  • Preserve existing commands: Always call await bot.process_commands(message) at the end of your on_message event, otherwise your bot's existing commands will stop functioning.

If you ever run into a case where channel returns None, double-check that your bot has access to the target channel and that you're using the correct ID.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 00:54:06