You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何解决discord.py机器人私信转发至指定频道失效问题?

Troubleshooting Discord DM Forwarding Bot Issue

Let's break down why your DM forwarding feature might not be working, and walk through fixes to get it up and running:

1. Missing Message Content Intent (Most Common Culprit)

Discord.py v1.5+ requires explicit intent permissions to read message content. If you haven't enabled this, your bot won't receive any message data at all.

Fix:

  • Go to your Discord Developer Portal, select your bot, navigate to the Bot tab, and enable the Message Content Intent under "Privileged Gateway Intents".
  • Update your bot initialization code to include the intent:
import discord

intents = discord.Intents.default()
intents.message_content = True  # Enable message content access
client = discord.Client(intents=intents)

2. Invalid Channel ID or Missing Permissions

Your target channel might not exist, or your bot doesn't have permission to send messages/embeds there.

Checks:

  • Verify the channel ID 714242239215304745 is correct (right-click the channel → Copy ID, make sure Developer Mode is enabled in Discord settings).
  • Ensure your bot's role in the server has the following permissions for the target channel:
    • Send Messages
    • Embed Links
  • Test if the bot can send a basic message to the channel first:
# Add this temporarily to debug
@client.event
async def on_ready():
    channel = client.get_channel(714242239215304745)
    if channel:
        await channel.send("Bot is online and can reach this channel!")
    else:
        print("Target channel not found!")

3. Conflicting on_message Events

If you have multiple @client.event decorated on_message functions in your code, only the last one will run. Discord.py doesn't automatically handle multiple listeners for the same event unless you use client.add_listener().

Fix:

Combine all your on_message logic into a single function, or register additional listeners like this:

async def dm_forwarding(message):
    # Your DM forwarding code here

client.add_listener(dm_forwarding, "on_message")

4. Debugging to Confirm DM Reception

Add a debug print at the start of your on_message function to verify if the bot is even receiving the DM:

@client.event
async def on_message(message):
    # Debug line to log all incoming messages
    print(f"Received message from {message.author} (ID: {message.author.id}) | Guild: {message.guild} | Content: '{message.content}'")
    
    # Rest of your code...

If you don't see DM entries in your console, check if users can send DMs to your bot (users might have blocked it, or your bot's privacy settings restrict DMs from non-server members).

Optimized Working Code

Here's your code updated with all the fixes and safety checks:

import discord

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_message(message):
    print(f"Received message: Author={message.author} (ID={message.author.id}), Guild={message.guild}, Content='{message.content}'")
    
    if message.guild is None and message.author != client.user:
        target_channel_id = 714242239215304745
        channel = client.get_channel(target_channel_id)
        
        if not channel:
            print(f"Error: Target channel {target_channel_id} not found!")
            await client.process_commands(message)
            return
        
        embed = discord.Embed(
            title='Support requested!',
            description=message.content,
            color=discord.Color.from_rgb(159, 255, 255)
        )
        embed.set_footer(text=f"Requested by {message.author} | ID-{message.author.id}")
        
        try:
            await channel.send(embed=embed)
            print(f"Forwarded support request from {message.author} (ID={message.author.id})")
        except discord.Forbidden:
            print(f"Error: Bot doesn't have permission to send messages to channel {target_channel_id}")
        except Exception as e:
            print(f"Unexpected error: {str(e)}")
    
    await client.process_commands(message)

client.run("YOUR_BOT_TOKEN")

Start with checking the Message Content Intent first—it's the most likely reason your code isn't triggering. If that doesn't fix it, use the debug prints to trace where the flow breaks.

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

火山引擎 最新活动