在Discord.py中的不同函数/命令之间使用变量

2024-04-26 11:16:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个变量,每次调用命令/函数时,它都会添加到该变量中

import discord
import asyncio
from discord.ext import commands
botToken = 'TOKEN'
bot = commands.Bot(command_prefix = '#')
@bot.event
async def on_ready():
    print('Bot is online and ready.')
    varible1 = 1
async def changeVariable(ctx, *, orderItem):
    varible1 = variable1 + 1
    print(variable1)

它告诉我变量在赋值之前没有赋值/引用。有没有一种方法可以使它像一个全局函数?如果需要,我可以尝试提供更多细节


Tags: 函数import命令asyncioasyncdefbotcommands
1条回答
网友
1楼 · 发布于 2024-04-26 11:16:19

您可以在导入下面的文件顶部创建一个全局变量。然后,只要在输入函数时增加变量,如下所示

import discord
import asyncio
from discord.ext import commands

function_call_count = 0 

botToken = 'TOKEN'
bot = commands.Bot(command_prefix = '#')
@bot.event

async def on_ready():
    function_call_count += 1
    print('Bot is online and ready.')
    varible1 = 1

async def changeVariable(ctx, *, orderItem):
    function_call_count += 1
    varible1 = variable1 + 1
    print(variable1)

相关问题 更多 >