Help needed with Discord bot error
I’m having trouble with my Discord bot. When I use the command !lek [drug name]
in chat, the terminal shows an error: invalid syntax (, line 1). I’m not sure how to fix it.
Here’s a simplified version of my code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.command()
async def lek(ctx, *, drug_name):
# Code to fetch drug info
info = get_drug_info(drug_name)
embed = discord.Embed(title=f'Drug Info: {drug_name}')
# Add fields to embed
await ctx.send(embed=embed)
bot.run('TOKEN')
The embed with the drug information can get way too long and in turn hits Discord’s 6000 character limit. How can I split a large embed into multiple messages so that each part is within the limit? Any help would be appreciated!
yo, ive had this problem b4. try using a library like textwrap to split ur long text into smaller chunks. something like:
import textwrap
chunks = textwrap.wrap(info, width=4000)
for chunk in chunks:
embed = discord.Embed(description=chunk)
await ctx.send(embed=embed)
this should work for most cases. good luck with ur bot!
Having dealt with Discord bot development, I can suggest an alternative approach to handle the character limit issue. Instead of using embeds, you could send the information as plain text messages. This allows for easier splitting and can potentially accommodate more content.
Here’s a basic implementation:
async def lek(ctx, *, drug_name):
info = get_drug_info(drug_name)
chunks = [info[i:i+1900] for i in range(0, len(info), 1900)]
await ctx.send(f'Drug Info: {drug_name}')
for chunk in chunks:
await ctx.send(chunk)
This method splits the content into 1900-character chunks (leaving room for Discord’s message limit) and sends each as a separate message. It’s more straightforward and avoids embed-specific restrictions.
Remember to implement error handling and consider rate limiting to prevent potential abuse.
I’ve encountered similar issues with Discord bots hitting character limits. One approach that worked well for me was to split the content into multiple embeds or messages.
Here’s a rough idea of how you could modify your code:
async def lek(ctx, *, drug_name):
info = get_drug_info(drug_name)
chunks = [info[i:i+4000] for i in range(0, len(info), 4000)]
for i, chunk in enumerate(chunks):
embed = discord.Embed(title=f'Drug Info: {drug_name} (Part {i+1})')
embed.description = chunk
await ctx.send(embed=embed)
This breaks the info into 4000-character chunks (leaving room for titles and fields) and sends each as a separate embed. You might need to adjust the chunking logic depending on your exact data structure.
Another tip: consider adding a cooldown to prevent spam and API rate limiting. Hope this helps!