Discord.py开发:如何在一行发送多张角色图片
Hey there! Great job getting your team-building bot up and running—random team recommendations with character art sounds awesome. Let's get those three images to display in a single line instead of stacking vertically.
Here are a couple of straightforward solutions depending on how you're currently serving your character images:
1. For Hosted/Online Images (Easiest Fix)
If your character images are hosted online (like a personal server or image hosting platform), you can combine all three image markdown tags into a single line and send them in one message. Discord will render them side-by-side as long as the image dimensions aren't excessively large.
Before (Possible Original Code)
# Sends each image in a separate line/message for character in recommended_team: await ctx.send(f"")
After (Fixed Code)
# Combine all image tags into one line inline_images = " ".join([f"" for character in recommended_team]) await ctx.send(content=inline_images)
2. Using Discord Embeds (More Polished Layout)
If you want to wrap the team in a clean, styled embed with a title or description, use inline fields to place each image side-by-side:
import discord # Create an embed for the team recommendation team_embed = discord.Embed(title="🔍 Your Random Team", description="Here's your 3-person squad!") # Add each character as an inline field for index, character in enumerate(recommended_team, start=1): team_embed.add_field( name=f"Character {index}", value=f"", inline=True # This forces fields to sit side-by-side ) await ctx.send(embed=team_embed)
3. For Local Character Images
If you're using local image files instead of hosted URLs, attach all files to the same message and reference them with attachment links:
# Prepare files and image references attached_files = [] image_references = [] for idx, character in enumerate(recommended_team): # Create a File object for each local image img_file = discord.File(character.local_image_path, filename=f"char_{idx}.png") attached_files.append(img_file) # Reference the attached file in markdown image_references.append(f"") # Send all files and the combined image line await ctx.send(content=" ".join(image_references), files=attached_files)
Quick Tip
If your images are extra wide, Discord might still stack them—try resizing them to a consistent, smaller width (like 200-300px) to ensure they fit neatly side-by-side.
内容的提问来源于stack exchange,提问作者Nikolay

