Py学习  »  Python

我制作了一个python机器人,可以获取股票的价格,但我如何制作它,使参数不使用空格?

chicken duck • 3 年前 • 1552 次点击  
@client.command()
async def stockprice(ctx, stock):
    await ctx.send(stock + ' price: ' + getStockPrice(stock))

我制作了一个python机器人,可以获取股票的价格,但我如何制作它,使论点不使用#空格?

例子:

!stockprice bitcoin => !stockprice.bitcoin
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133800
 
1552 次点击  
文章 [ 1 ]  |  最新文章 3 年前
furas
Reply   •   1 楼
furas    4 年前

我不知道我是否理解你的问题。


如果要删除字符串两侧的空格/制表符/回车符,则只需

stock = stock.strip() 

如果您想使用命令作为带有点的单个字符串 !stockprice.bitcoin -那你就得用 on_message(message) 将其转换为 !stockprice bitcoin 使用 process_commands() 寄到 @client.command()

差不多是这样的

@client.event
def on_message(msg):

    if msg.author.bot:
        return

    if msg.content.startswith('!stockprice.'):
       msg.content = msg.content.replace('!stockprice.', '!stockprice ')
    
    await process_commands(msg)