我在嵌入图像时遇到问题。
我从列表中选择一个随机字符串(这是一个猫图像的链接),然后嵌入应该发布该图像,但是,它不是,它只是看起来像这样:
如果我将链接放入我的浏览器,他们工作正常。
我尝试使用导入random
和random.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))
因此,为了使您的代码正常工作,您必须将图片的URL更改为其他任何内容 例如,我重新上传了相同的图片以进行imgur并粘贴链接,现在它可以正常工作
embed.set_image(url="https://i.imgur.com/SJgskbM.jpg")
这可能与imgur直接链接有关,但我不确定
完整的工作代码:
齿轮(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))
卖弄.朋友
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')