【问题标题】:How can I get one value from a @client.command function and implement it into another如何从 @client.command 函数中获取一个值并将其实现到另一个
【发布时间】:2022-01-20 06:42:10
【问题描述】:

我有一个字典初始化为一个数据帧,该数据帧在 discord.py 的@client.command 函数中具有值。我想在我编写的下一个函数中使用这个数据框。我不确定如何执行此操作。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    假设你有这样的命令

    @client.command()
    async def test(ctx, arg)
        await ctx.send("the variable is " +  arg)
    

    你可以使用全局变量来分享它

    
    argument = ""
    
    @client.command()
    async def test(ctx, arg)
        global argument
        argument = arg
        await ctx.send("the variable is " +  arg)
    
    @client.command()
    async def anothertest(ctx)
        global argument
        await ctx.send("the previous arg is " +  argument)
    

    【讨论】:

    • 请注意,此变量将是全局,即机器人所在的所有服务器都相同
    • 是的,在这种情况下,您可以将其设为每个服务器 {guild.id : "that variable"} 的字典。它将使它成为公会特定的:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多