【问题标题】:AIOHTTP meme command discord.pyAIOHTTP meme 命令 discord.py
【发布时间】:2019-07-30 11:37:52
【问题描述】:

我正在制作一个不和谐的机器人,我正在尝试制作一个随机的 meme 命令,这是我的错误和代码:

@client.command(pass_context=True)
async def meme(ctx):
    embed = discord.Embed(title="meme", description="test")

    async with aiohttp.ClientSession() as cs:
        async with cs.get('https://www.reddit.com/r/dankmemes/new.json?sort=hot') as r:
            res = await r.json()
            embed.set_image(res['data']['children'] [random.randint(0, 25)]['data']['url'])
            await client.say(embed=embed)
Ignoring exception in command meme
Traceback (most recent call last):
  File "C:\Users\atill\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "C:\Users\atill\Downloads\Epic Bot\server.py", line 165, in meme
    embed.set_image(res['data']['children'] [random.randint(0, 25)]['data']['url'])
TypeError: set_image() takes 1 positional argument but 2 were given

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\atill\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "C:\Users\atill\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\atill\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: set_image() takes 1 positional argument but 2 were given

I'm not sure how to make it so it is in an embed aswell

【问题讨论】:

    标签: python discord discord.py aiohttp


    【解决方案1】:

    我对您的个人建议是使用https://meme-api.herokuapp.com/gimme/dankmemes 来拉动模因。如果您有兴趣添加标题,请使用此命令为您的嵌入添加标题:

    title = res['data']['children'][random.randint(0,25]["data"]["title"]I 
    

    【讨论】:

    • 我想你的意思是“title = res['data']['children'][random.randint(0,25)]["data"]["title"]"
    【解决方案2】:
    @client.command(pass_context=True)
    async def meme(ctx):
        embed = discord.Embed(title="", description="")
    
        async with aiohttp.ClientSession() as cs:
            async with cs.get('https://www.reddit.com/r/dankmemes/new.json?sort=hot') as r:
                res = await r.json()
                embed.set_image(url=res['data']['children'] [random.randint(0, 25)]['data']['url'])
                await ctx.send(embed=embed)
    

    【讨论】:

    • 这如何工作?该命令有效,但我不明白。你能告诉我它是做什么的吗?
    【解决方案3】:

    如错误中所述,函数 set_image(url) 仅接受 1 个参数,而您给了它 2 个参数。对于嵌入消息,请查看 this 线程。

    【讨论】:

    • 这里要注意的重要一点是set_image 采用单个位置 参数,即调用该方法的Embed 实例。 url 应作为关键字参数传递:embed.set_image(url=...).
    猜你喜欢
    • 2020-11-24
    • 2021-08-28
    • 2021-07-05
    • 1970-01-01
    • 2021-07-27
    • 2021-07-10
    • 1970-01-01
    • 2021-08-17
    • 2022-01-12
    相关资源
    最近更新 更多