如何使用discord.py v1.4.1发出天气命令
如果您想使用discord.py发出 weather
命令并为您的机器人添加了一个很酷的功能,我就覆盖了您,我在下面回答了如何创建discord.py中的天气
命令.
In case you would like to make a weather
command using discord.py and have a cool addition to your bot, I got you covered, I have answered below on how to create a weather
command in discord.py.
我们将制作一个像这样工作的命令-
We will be making a command which will work like this -
首先,我们将使用 openweahtermap API,该API需要一个API密钥,您可以获取只需通过简单地登录其网站即可免费.
Starting off, we are going to be using openweahtermap API, which requires an API key, you can get one for free by simple logging in to their website.
获得API密钥后,一切都很好.
Once you have got the API key, you are all good to go.
第二步是开始编码,除了discord.py之外,我们还将导入1个模块,即 requests
.我们可以简单地导入它-
The second step will be to start coding, we will import 1 module apart from discord.py which is requests
. We can simply import it -
import requests
导入后,我们可以定义以下内容,以便于使用.
After importing we can define the following things so that it's easier to use them.
api_key = "your_api_key"
base_url = "http://api.openweathermap.org/data/2.5/weather?"
下一步将是创建一个以 city
作为参数的命令.
The next step will be to create a command which takes city
as an argument.
@client.command()
async def weather(ctx, *, city: str):
然后,我们可以使用 requests
获取网站的响应,然后使用 json
读取网站的响应.我们还定义了使用命令的通道.
Afterwards, we can get the response of the website using requests
and then read the response by using json
. WE also define the channel in which the command is being used.
city_name = city
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
response = requests.get(complete_url)
x = response.json()
channel = ctx.message.channel
现在,我们使用简单的 if
语句检查 city_name
是否为有效城市.我们还将 async与channel.typing()
一起使用,这表明该机器人一直在键入内容,直到从网站获取内容为止.
Now, we check if the city_name
is a valid city by using a simple if
statement. We also use async with channel.typing()
which shows that the bot is typing till the time that it fetches the contents from the website.
if x["cod"] != "404":
async with channel.typing():
现在,我们获得了有关天气的信息.
Now we get the info about the weather.
y = x["main"]
current_temperature = y["temp"]
current_temperature_celsiuis = str(round(current_temperature - 273.15))
current_pressure = y["pressure"]
current_humidity = y["humidity"]
z = x["weather"]
weather_description = z[0]["description"]
现在,一旦我们获得了信息,便将信息放入 discord.Embed
中,就像这样-
Now, once we have the info, we put the info inside a discord.Embed
like so -
weather_description = z[0]["description"]
embed = discord.Embed(title=f"Weather in {city_name}",
color=ctx.guild.me.top_role.color,
timestamp=ctx.message.created_at,)
embed.add_field(name="Descripition", value=f"**{weather_description}**", inline=False)
embed.add_field(name="Temperature(C)", value=f"**{current_temperature_celsiuis}°C**", inline=False)
embed.add_field(name="Humidity(%)", value=f"**{current_humidity}%**", inline=False)
embed.add_field(name="Atmospheric Pressure(hPa)", value=f"**{current_pressure}hPa**", inline=False)
embed.set_thumbnail(url="https://i.ibb.co/CMrsxdX/weather.png")
embed.set_footer(text=f"Requested by {ctx.author.name}")
构造嵌入后,我们将其发送.
After constructing the embed, we send it.
await channel.send(embed=embed)
else:
await channel.send("City not found.")
我们还使用了 else
语句,如果API无法获取所提及城市的天气,则该语句发送未找到城市的信息.
We also use an else
statement which sends that the city is not found if the API is unable to fetch the weather of the city mentioned.
并且您已经成功地执行了 weather
命令!
And with that you have successfully made a weather
command!
如果您确实遇到任何错误或有任何疑问,请确保在下面对其进行注释.我会尽力提供帮助.
If you do run into any errors or have any doubts, make sure to comment them below. I will try to help as much as I can.
谢谢!