python - 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()
有人知道这里的错误是什么吗?我没有任何线索。它不会在终端中发送任何错误消息。
解决方案
尝试这个。这使用 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
推荐阅读
- java - 杰克逊中用于枚举的自定义 xml 反序列化器
- java - 在同一方法中同步多个块
- php - 验证失败时出现未找到错误
- javascript - Javascript字符串与变量值差异
- javascript - Aws dynamodb 错误错误 [UnknownError]:未找到 (aws-sdk)
- docker - 为什么我需要一个像 BusyBox、Alpine 和 JDK 这样的容器基础镜像?如果我的主机操作系统是 Linux 并且里面有 JDK?
- javascript - 从 JS 代码设置为 0 时,CSS flex 属性没有正确响应
- javascript - 单击按钮打开模式页面
- bokeh - 如何通过同一网络中的不同设备连接到散景服务器
- machine-learning - 如何评估机器学习黑客马拉松提交的内容?