Python Discord Bot 嵌入

时间:2021-04-27 05:33:01

标签: python discord discord.py

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)

我正在尝试使用 Python 制作 Discord Coin,这是一个股票机器人。代码中用到的模块都安装好了,我想通过embed消息发送爬取数据,但是当%bitcoin(前缀=%)时,以太坊embed和比特币embed也出来了。

enter image description here

2 个答案:

答案 0 :(得分:1)

你的if完全搞砸了。
msg == "bitcoin" or "BITCOIN" or "btc" or "BTC" 始终为真。
你的支票应该是。
if msg in ('bitcoin', 'BITCOIN', 'btc', 'BTC') 这也不适用于您的情况,

既然你在做msg = msg[20:]
应该是 msg = msg[1:]

现在,我直接调试了您的代码,这不是在 SO 上提问的方式。您应该能够调试您的代码,并且关于 SO 的问题应该基于您的算法、技术或文档。 见debugging

答案 1 :(得分:0)

用它来发出命令不是更容易吗? 就像那样(我还没有测试过,但它应该可以工作,而且会更漂亮):

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