如何在discord.py命令中添加参数?

时间:2018-05-30 23:33:51

标签: python discord.py

我的代码是:

weather = Weather(unit=Unit.CELSIUS)
location = weather.lookup_by_location('toronto')
forecasts = location.forecast

embed = (discord.Embed(title="-=-__THE WEATHER__-=-", color=0x15dbc7))
embed.add_field(name="Clouds", value=forecasts[0].text, inline=False) 
embed.add_field(name="Date", value=forecasts[0].date, inline=False)
embed.add_field(name="High", value=forecasts[0].high, inline=False)
embed.add_field(name="Low", value=forecasts[0].low, inline=False)
await bot.say(embed=embed)

如何使用命令后面的变量替换0? 标识命令的代码是

@bot.command(pass_context=True)
async def weather(ctx)

Im使用的导入是:

import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
import random
import weather
from weather import Weather, Unit

1 个答案:

答案 0 :(得分:0)

您可以在函数定义中传递该参数,以及converter,让discord.py知道您希望该值转换为的类型。

@bot.command(pass_context=True)
async def weather(ctx, index: int):
    weather = Weather(unit=Unit.CELSIUS)
    location = weather.lookup_by_location('toronto')
    forecasts = location.forecast

    embed = (discord.Embed(title="-=-__THE WEATHER__-=-", color=0x15dbc7))
    embed.add_field(name="Clouds", value=forecasts[index].text, inline=False) 
    embed.add_field(name="Date", value=forecasts[index].date, inline=False)
    embed.add_field(name="High", value=forecasts[index].high, inline=False)
    embed.add_field(name="Low", value=forecasts[index].low, inline=False)
    await bot.say(embed=embed)

您可以使用类似!weather 3

的内容从Discord调用此内容