python - 如何从命令 discord.py 更改变量
问题描述
这是我在 Stacked Overflow 上的第一篇文章,我找到了几乎所有我遇到的编码问题的答案。但是我没有找到解决这个问题的任何帮助:我制作了一个机器人,我想使用不和谐的命令来更改一个变量,如下所示: !test 1 然后将更改一个要测试的变量,这是我的代码:
@client.command()
async def test(ctx, variable):
print(variable)
await ctx.send(variable)
任何帮助将不胜感激:D
解决方案
您需要创建一个全局变量或机器人属性并对其进行编辑
# Global Var
var = None
@client.command()
async def test(ctx, variable):
global var
print("Before", variable)
var = variable
await ctx.send(var)
# Bot Attribute
@client.event
async def on_ready():
print("Ready")
client.var = None
@client.command()
async def test(ctx, variable):
print("Before", client.var)
client.var = variable
await ctx.send(client.var)
推荐阅读
- java - Android Studio 应用仅显示白色背景和标题
- angular - 来自 Angular 外部的变更检测和事件
- python - 无法使用 PIL 库创建屏幕录制的视频输出文件
- mysql - 使用单个插入查询将相同的项目插入三个表中
- tsql - RowNumber 在每次执行后返回不同的结果
- reactjs - 如何将变量传递给 GraphQL 过滤器查询?
- google-cloud-platform - 无法将实体导入数据存储模拟器
- c# - 如何在我的 iis8 c# web-api 应用程序上找到所有网络打印机?
- python - Pandas 滚动聚合函数列表。值错误:没有结果
- python - 在 Python 的 Kivy 中使用 Config 设置 maxfps 不会限制 fps