【问题标题】:Twopart Embedded API Message Discord.py(branch>rewrite)Twopart 嵌入式 API 消息 Discord.py(branch>rewrite)
【发布时间】:2021-03-04 07:42:10
【问题描述】:

【问题讨论】:

  • 嗨,很抱歉,这是一个非常糟糕的问题,请更加详细和详细,详细解释目标,展示您尝试过的内容并解释您遇到的错误。

标签: api discord.py embed


【解决方案1】:

我使用了aiohttp 顺便说一句,你需要导入它。并以json 回复,然后将信息放入嵌入中。

@bot.command()
async def joke(ctx):
    url = 'https://sv443.net/jokeapi/v2/joke/Dark?blacklistFlags=nsfw,religious,political,racist,sexist&type=twopart'
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as r:
            if r.status == 200:
                info = await r.json()
                embed = discord.Embed(title=info['setup'], description=info['delivery'])
                await ctx.send(embed=embed)

【讨论】:

  • 这可能行得通,但不建议使用 requests 或 urllib,请改用 aiohttp
【解决方案2】:

您可以使用requestsurllib 模块,但它们是blocking,您应该改用aiohttp

import aiohttp

@bot.command()
async def joke_embed(ctx):
    async with aiohttp.ClientSession as cs:
        async with cs.get('https://sv443.net/jokeapi/v2/joke/Dark?blacklistFlags=nsfw,religious,political,racist,sexist&type=twopart') as res:
            resp = await res.json()


    setup, delivery = resp['setup'], resp['delivery']
    embed = discord.Embed(title=setup, description=delivery, colour=discord.Colour.greyple())

    await ctx.send(embed=embed)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-12
    • 2020-11-07
    • 2021-03-08
    • 1970-01-01
    • 2021-02-13
    • 2020-09-08
    • 2020-03-17
    • 1970-01-01
    相关资源
    最近更新 更多