【问题标题】:Python Discord Bot EmbedPython Discord 机器人嵌入
【发布时间】:2021-07-20 11:10:39
【问题描述】:
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也一起出来了。

【问题讨论】:

  • 尝试使用if elif 而不是if 两次
  • 我试过了。 link
  • 为什么用msg = message.content[20:]去掉内容中的前20个字符?
  • 我改了,结果没变。 link

标签: python discord discord.py


【解决方案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

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

    • bot = commands.Botdiscord.ext 模块吗?我使用了discord.py 模块。如果我切换到discord.ext 模块会更好吗?
    • 在我的机器人中,我已经导入了它们。而且我认为您也应该可以同时使用它们。 (这就是我所拥有的import discord \n from discord.ext import commands
    • 我不考虑discord.ext。我改变了我的代码,它的工作!非常感谢。
    猜你喜欢
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2021-07-17
    • 2019-01-12
    • 2019-08-10
    • 2020-04-07
    相关资源
    最近更新 更多