【问题标题】:Discord py anime character databaseDiscord py动漫人物数据库
【发布时间】:2021-09-10 00:33:45
【问题描述】:

如何使用我的不和谐 python 机器人制作 Api 动漫角色数据库功能?我是新添加不和谐 python 机器人。此动漫角色数据库的示例 api 链接:

https://www.animecharactersdatabase.com/api_series_characters.php?character_q=naruto

【问题讨论】:

  • 请查看我的回答here 可能会有所帮助

标签: discord.py


【解决方案1】:

我认为这应该可行,试试这个!为了便于理解,我把一些东西注释掉了

# Requrired modules
import discord
from discord.ext import commands
import requests
import random
import platform

# Creating the bot with the bot prefix as `?`
client = commands.Bot(command_prefix="?")

@client.event
async def on_ready():
        print(f'Discord.py API version: {discord.__version__}')
        print(f'Python version: {platform.python_version()}')
        print(f'Logged in as {client.user.name}')
        print('Bot is ready!')

# The commandsstarts here
@client.command()
async def anime(ctx, *, query="naruto"): # 'anime' is the name of the command
    # This command can be used as `?anime character_name_here`
    # The default value will be 'naruto' for the query
    try:
        reqcont = requests.get(f"https://www.animecharactersdatabase.com/api_series_characters.php?character_q={query}")
        if reqcont.content==-1 or reqcont.content=='-1': # i found out that the website returns: -1 if there are no results, so here, we implement it
            await ctx.send("[-] Unable to find results! - No such results exists!")

        else:
            # If the website doesnt return: -1 , this will happen
            try:
                reqcont = reqcont.json()
            except Exception as e:

                # Please enable this line only while you are developing and not when deplying
                await ctx.send(reqcont.content)

                await ctx.send(f"[-] Unable to turn the data to json format! {e}")
                return # the function will end if an error happens in creating a json out of the request

            # selecting a random item for the output
            rand_val = len(reqcont["search_results"])-1
            get_index = random.randint(0, rand_val)
            curent_info = reqcont["search_results"][get_index]

            # Creting the embed and sending it
            embed=discord.Embed(title="Anime Info", description=":smiley: Anime Character Info result for {query}", color=0x00f549)
            embed.set_author(name="YourBot", icon_url="https://cdn.discordapp.com/attachments/877796755234783273/879295069834850324/Avatar.png")
            embed.set_thumbnail(url=f"{curent_info['anime_image']}")
            embed.set_image(url=f"{curent_info['character_image']}")
            embed.add_field(name="Anime Name", value=f"{curent_info['anime_name']}", inline=False)
            embed.add_field(name="Name", value=f"{curent_info['name']}", inline=False)
            embed.add_field(name="Gender", value=f"{curent_info['gender']}", inline=False)
            embed.add_field(name="Description", value=f"{curent_info['desc']}", inline=False)
            embed.set_footer(text=f"Requested by {ctx.author.mention}")
            await ctx.send(embed=embed)

    except Exception as e:
        await ctx.send(f"[-] An error has occured: {e}")

client.run("NeverShareYourToken")

【讨论】:

  • Requests 模块不推荐用于异步环境。请改用 aiohttp。
猜你喜欢
  • 2011-12-31
  • 1970-01-01
  • 2018-08-19
  • 2021-03-24
  • 1970-01-01
  • 2022-01-21
  • 2021-02-21
  • 1970-01-01
  • 2021-04-23
相关资源
最近更新 更多