0
async def on_message(message):
    if message.content.startswith(prefix):
        msg = message.content[20:]
    else:
        return None
    
    if msg == "bitcoin" or "BITCOIN" or "btc" or "BTC":
        url = "https://coinmarketcap.com/currencies/bitcoin/"
        hdr = {'User-Agent': 'Mozilla/5.0'}
        req = Request(url, headers=hdr)
        res = urlopen(req).read()
        soup = BeautifulSoup(res, 'html.parser')
        
        btc_t1 = soup.find_all("div", class_="priceValue___11gHJ")
        btc_t1 = [each_line.get_text().strip() for each_line in btc_t1[:20]]
        btc_t1 = " ".join(btc_t1)
        time.sleep(0.1)
        btc_t2 = soup.find_all("span", class_="sc-1v2ivon-0 fiaaIx")
        btc_t2 = [each_line.get_text().strip() for each_line in btc_t2[:20]]
        btc_t2 = " ".join(btc_t2)
        time.sleep(0.1)
        btc_t3 = soup.find_all("div", class_="statsValue___2iaoZ")
        btc_t3 = [each_line.get_text().strip() for each_line in btc_t3[:1]]
        btc_t3 = " ".join(btc_t3)
        time.sleep(0.1)

        btcem = discord.Embed(title="Bitcoin", description="BTC market price       \nPowered by Coinmarketcap", color=0xF7931A)
        btcem.set_thumbnail(url="https://s2.coinmarketcap.com/static/img/coins/64x64/1.png")
        btcem.add_field(name="Market Price", value="Price: "+ str(btc_t1) +" | "+ str(btc_t2), inline=False)
        btcem.add_field(name="Market Cap", value="Price: "+ str(btc_t3), inline=False)
        # embed.set_footer(text="Market", icon_url="")
        await message.channel.send(embed=btcem)
    if msg == "ethereum" or "ETHEREUM" or "eth" or "ETH":
        url = "https://coinmarketcap.com/currencies/ethereum/"
        hdr = {'User-Agent': 'Mozilla/5.0'}
        req = Request(url, headers=hdr)
        res = urlopen(req).read()
        soup = BeautifulSoup(res, 'html.parser')

        eth_t1 = soup.find_all("div", class_="priceValue___11gHJ")
        eth_t1 = [each_line.get_text().strip() for each_line in eth_t1[:20]]
        eth_t1 = " ".join(eth_t1)
        time.sleep(0.1)
        eth_t2 = soup.find_all("span", class_="sc-1v2ivon-0 fiaaIx")
        eth_t2 = [each_line.get_text().strip() for each_line in eth_t2[:20]]
        eth_t2 = " ".join(eth_t2)
        time.sleep(0.1)
        eth_t3 = soup.find_all("div", class_="statsValue___2iaoZ")
        eth_t3 = [each_line.get_text().strip() for each_line in eth_t3[:1]]
        eth_t3 = " ".join(eth_t3)
        time.sleep(0.1)

        ethem = discord.Embed(title="Ethereum", description="ETH market price       \nPowered by Coinmarketcap", color=0x131313)
        ethem.set_thumbnail(url="https://s2.coinmarketcap.com/static/img/coins/64x64/1027.png")
        ethem.add_field(name="Market Price", value="Price: "+ str(eth_t1) +" | "+ str(eth_t2), inline=False)
        ethem.add_field(name="Market Cap", value="Price: "+ str(eth_t3), inline=False)
        # embed.set_footer(text="Market", icon_url="")
        await message.channel.send(embed=ethem)

I'm trying to make Discord Coin, a stock bot in Python. All the modules used in the code have been installed, and I want to send crawl data by embed message, but when %bitcoin (prefix = %), the Ethereum embed along with the bitcoin embed also comes out.

enter image description here

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
mingo Fla
  • 3
  • 1

2 Answers2

1

Your if is completely messed up.
msg == "bitcoin" or "BITCOIN" or "btc" or "BTC" is always true.
Your check should be.
if msg in ('bitcoin', 'BITCOIN', 'btc', 'BTC') and this also wouldn't work in your case,

since you are doing msg = msg[20:],
it should be msg = msg[1:].

Now, I directly debugged your code, this isn't the way to ask a question on SO. You should be able to debug your code and questions on SO should be based on your algorithm, technique or documentation. see debugging

Ceres
  • 2,498
  • 1
  • 10
  • 28
0

wouldn't it be easier to make a commands out of this? like that(i havent tested it but it should work and it would be prettier):

bot = commands.Bot(command_prefix="%")
@bot.command(aliases=["BITCOIN", "btc","BTC"])
async def bitcoin(self, ctx):
   # your bitcoin code here
@bot.command(aliases=["ETHEREUM", "eth", "ETH"])
async def ethereum(self, ctx):
   # your etherum code here
loloToster
  • 1,395
  • 3
  • 5
  • 19
  • `bot = commands.Bot` is `discord.ext` module? I used `discord.py` module. Would it be better if I switched to the `discord.ext` module? – mingo Fla Apr 27 '21 at 07:53
  • In my bot i have imported both of them. And I think that you should too you can use them simultaneously. (That's what i have`import discord \n from discord.ext import commands`) – loloToster Apr 27 '21 at 08:03
  • I don't think about `discord.ext`. I change my code, it's work! thanks so much. – mingo Fla Apr 27 '21 at 08:18