嵌入图片问题

时间:2018-08-21 09:58:53

标签: python python-3.x discord discord.py

我在嵌入图像时遇到问题。

我正在从列表中选择一个随机字符串(这是指向猫图像的链接),然后应该将嵌入内容发布到该图像上,但是实际上并不是这样,

enter image description here

如果我将链接放到浏览器中,它们就可以正常工作。
我尝试使用导入randomrandom.choice,但这都不起作用。

import discord
from discord.ext import commands
from datetime import datetime
import random

class catc:
    def __init__(self, bot):
        self.bot = bot
    @commands.command(pass_context=True)
    async def cat(self, ctx):
        f=open("cat.txt","r")
        v=f.read()
        f.close()
        fi = v.split(',')
        pe=random.randint(0,41)
        re=fi[pe]
        print(re)
        embed = discord.Embed(title="title", color=0x309bf3)
        embed.set_image(url="https://i.imgur.com/xJifyGMb.jpg")
        embed.set_footer(text="Nami Bot")
        await self.bot.say(embed=embed)

def setup(bot):
    bot.add_cog(catc(bot))

1 个答案:

答案 0 :(得分:0)

因此,为了使代码正常工作,您必须将图片的URL更改为其他任何内容
例如,我已经将同一张图片重新上传到imgur并粘贴了链接,现在它可以正常工作了

embed.set_image(url="https://i.imgur.com/SJgskbM.jpg")

这可能与imgur直接链接有关,但我不确定

enter image description here

完整的工作代码:

  

齿轮(cat_pics.py)

import discord
from discord.ext import commands
import random

class catc:
  def __init__(self, bot):
    self.bot = bot
  @commands.command(pass_context=True)
  async def cat(self, ctx):
    with open('something.txt') as f:
      mylist = list(f)
    a = random.choice(mylist)
    print(a)
    embed = discord.Embed(title="title", color=0x309bf3)
    embed.set_image(url="https://i.imgur.com/SJgskbM.jpg")
    embed.set_footer(text="Nami Bot")
    await self.bot.say(embed=embed)

def setup(bot):
  bot.add_cog(catc(bot))
  

main.py

from discord.ext.commands import Bot

startup_extensions = ["cat_pics"]
client = Bot(command_prefix='!')

@client.command()
async def load(extension_name : str):
  """Loads an extension."""
  try:
    client.load_extension(extension_name)
  except (AttributeError, ImportError) as e:
    await client.say("```py\n{}: {}\n```".format(type(e).__name__, str(e)))
    return
  await client.say("{} loaded.".format(extension_name))


if __name__ == "__main__":
  for extension in startup_extensions:
    try:
      client.load_extension(extension)
    except Exception as e:
      exc = '{}: {}'.format(type(e).__name__, e)
      print('Failed to load extension {}\n{}'.format(extension, exc))

  client.run('TOKEN')