错误,python,discord.py"/>

首页 > 解决方案 > Discord.PY -错误

问题描述

我的 Discord.PY 机器人有问题!我已下达命令,向您展示今天的 NASA 天文学图像。它使用 NASA API。该命令必须在名为“bot-commands”的通道中执行。如果这没有发生,它应该发送一条消息说你必须在那里做。但是,当我在不同的通道中运行代码(所以不是“机器人命令”)时,它会发送以下内容:

<discord.embeds.Embed object at 0x000002598E70FEE0>

这是我的代码:

    @commands.command()
    @commands.cooldown(1, 90, commands.BucketType.user)
    async def nasa(self, ctx):
        bot_channel = discord.utils.get(ctx.guild.channels, name = "bot-commands")
        if ctx.channel != bot_channel:
            await ctx.send(f"**Wrong channel!**\nOnly use your commands in {bot_channel.mention}!")
            return
        request = requests.get("https://api.nasa.gov/planetary/apod?api_key=xxx").json()
        requestCopyright = request['copyright']
        requestDate = request['date']
        requestTitle = request['title']
        requestHDUrl = request['hdurl']
        requestUrl = request['url']
        embednasa = discord.Embed(title = "**Today's NASA Astronomogy Image of the Day**", description = f"{requestTitle} ({requestDate})", color=0x09ec23, url=requestHDUrl)
        embednasa.set_author(name = f"By: {requestCopyright}", icon_url = "https://api.nasa.gov/assets/img/favicons/favicon-192.png")
        embednasa.set_image(url=requestUrl)
        embednasa.set_footer(text="Press the blue text to see the full resolution image!")
        await ctx.send(embed=embednasa)
        jsonOpen = open('./api/nasa_used.json')
        jsonLoad = json.load(jsonOpen)
        nasaUsed = int(jsonLoad['nasa']) + 1
        nasanewUsed = {"nasa": nasaUsed}
        jsonString = json.dumps(nasanewUsed)
        jsonFile = open("./api/nasa_used.json", "w")
        jsonFile.write(jsonString)
        jsonFile.close()

有人知道这里的错误是什么吗?我没有任何线索。它不会在终端中发送任何错误消息。

标签: pythondiscord.py

解决方案


尝试这个。这使用 if 和 else 语句。机器人检查通道是否是机器人通道,如果不是,它将发送错误消息。

@commands.command()
@commands.cooldown(1, 90, commands.BucketType.user)
async def nasa(self, ctx):
        bot_channel = discord.utils.get(ctx.guild.channels, name = "bot-commands")
        if ctx.channel == bot_channel:
          request = requests.get("https://api.nasa.gov/planetary/apod?api_key=xxx").json()
          requestCopyright = request['copyright']
          requestDate = request['date']
          requestTitle = request['title']
          requestHDUrl = request['hdurl']
          requestUrl = request['url']
          embednasa = discord.Embed(title = "**Today's NASA Astronomogy Image of the Day**", description = f"{requestTitle} ({requestDate})", color=0x09ec23, url=requestHDUrl)
          embednasa.set_author(name = f"By: {requestCopyright}", icon_url = "https://api.nasa.gov/assets/img/favicons/favicon-192.png")
          embednasa.set_image(url=requestUrl)
          embednasa.set_footer(text="Press the blue text to see the full resolution image!")
          await ctx.send(embed=embednasa)
          jsonOpen = open('./api/nasa_used.json')
          jsonLoad = json.load(jsonOpen)
          nasaUsed = int(jsonLoad['nasa']) + 1
          nasanewUsed = {"nasa": nasaUsed}
          jsonString = json.dumps(nasanewUsed)
          jsonFile = open("./api/nasa_used.json", "w")
          jsonFile.write(jsonString)
          jsonFile.close()
        else:
          await ctx.send(f"**Wrong channel!**\nOnly use your commands in {bot_channel.mention}!")
          return

推荐阅读