I'm working on a Discord bot that fetches cryptocurrency prices. I've got most of the code done, but I'm running into a weird issue. When I type the command for Bitcoin info, it shows both Bitcoin and Ethereum data. Here's a simplified version of what I'm dealing with:
```python
async def handle_message(msg):
if msg.startswith('$price'):
coin = msg.split()[1].lower()
if coin in ['bitcoin', 'btc']:
# Fetch Bitcoin data
btc_info = get_crypto_data('bitcoin')
await send_crypto_embed(msg.channel, 'Bitcoin', btc_info, color=0xF7931A)
elif coin in ['ethereum', 'eth']:
# Fetch Ethereum data
eth_info = get_crypto_data('ethereum')
await send_crypto_embed(msg.channel, 'Ethereum', eth_info, color=0x3C3C3D)
def get_crypto_data(coin):
# Placeholder for API call to get price data
return {'price': '$00,000', 'change': '+0.00%', 'market_cap': '$000B'}
async def send_crypto_embed(channel, name, data, color):
embed = discord.Embed(title=name, color=color)
embed.add_field(name='Price', value=data['price'], inline=False)
embed.add_field(name='24h Change', value=data['change'], inline=False)
embed.add_field(name='Market Cap', value=data['market_cap'], inline=False)
await channel.send(embed=embed)
The bot should only show info for the coin I ask for, but it’s showing both. Any ideas on how to fix this? I’m pretty new to Discord bots, so I might be missing something obvious.
As someone who’s been tinkering with Discord bots for a while now, I can tell you that the issue you’re facing is pretty common. I had a similar problem when I was building a weather bot. The key here is to make sure you’re only executing one block of code per command.
From what I can see, your logic looks solid, but there might be an issue with how the message is being processed. Have you checked if there’s any part of your code that might be triggering both conditions? It’s worth double-checking your event listener or the part where you’re calling ‘handle_message’.
Also, adding some debug prints could help. Try printing out the ‘coin’ variable right after you extract it from the message. This way, you can see exactly what the bot is reading from the user’s input.
If that doesn’t solve it, you might want to consider using a more robust command parsing library like discord.ext.commands. It can make handling different commands much easier and less prone to these kinds of issues.
Keep at it! Bot development can be tricky, but it’s super rewarding when you get it working just right.
seems like ur code’s not waiting for the first embed to send before moving on. try adding an ‘await’ before send_crypto_embed for both bitcoin and ethereum. that should make it wait and only send the one u asked for. good luck with ur bot!
I’ve encountered a similar issue while developing a Discord bot. The problem likely lies in your message handling logic. Instead of using if-elif, try using separate if statements for each cryptocurrency. This way, the bot won’t continue checking conditions after finding a match. Also, ensure you’re properly parsing the user’s input to extract the coin name. Here’s a quick modification:
async def handle_message(msg):
if msg.startswith('$price'):
coin = msg.split()[1].lower() if len(msg.split()) > 1 else ''
if coin in ['bitcoin', 'btc']:
btc_info = get_crypto_data('bitcoin')
await send_crypto_embed(msg.channel, 'Bitcoin', btc_info, color=0xF7931A)
return
if coin in ['ethereum', 'eth']:
eth_info = get_crypto_data('ethereum')
await send_crypto_embed(msg.channel, 'Ethereum', eth_info, color=0x3C3C3D)
return
This should resolve the issue of displaying multiple cryptocurrencies simultaneously.