I am new to both Python and Stack Exchange. When using this code, I receive the error, "Using variable 'status' before assignment [30,96]." It says that the variable status is unassigned in the await command in the loop, even though I assigned it above. If anyone could solve this, I would appreciate it. If any other info is needed, just ask and I can reply with it. Thanks.
import requests
from discord.ext import commands
from discord.ext import tasks
from itertools import cycle
client = commands.Bot(command_prefix = '.')
status = cycle(['Starting.', 'Starting..'])
serverOnePlayerCount = 0
serverTwoPlayerCount = 0
serverOneOnline = 'Offline'
serverTwoOnline = 'Offline'
@client.event
async def on_ready():
change_status.start()
print('Bot is ready.')
@client.command()
async def ping(ctx):
await ctx.send(f'Pong! {round(client.latency * 1000)}ms')
@tasks.loop(seconds=5)
async def change_status():
await client.change_presence(status=discord.Status.online, activity=discord.Game(name=next(status), type=3))
r1 = requests.get('https://api.minehut.com/server/5f0de3303c826f0051e583b1/')
json_data_1 = r1.json()
serverOneOnline = str(json_data_1["server"]["online"])
serverOnePlayerCount = str(json_data_1["server"]["playerCount"])
serverOneStatusMessage = 'Minecraft ----- Server 1 is {} ({}/10 players)'.format(serverOneOnline, serverOnePlayerCount)
r2 = requests.get('https://api.minehut.com/server/5f8242b2d74dc1006100293b/')
json_data_2 = r2.json()
serverTwoOnline = str(json_data_2["server"]["online"])
serverTwoPlayerCount = str(json_data_2["server"]["playerCount"])
serverTwoStatusMessage = 'Minecraft ----- Server 2 is {} ({}/10 players)'.format(serverTwoOnline, serverTwoPlayerCount)
status = cycle([serverOneStatusMessage, serverTwoStatusMessage])
client.run('token')
question from:
https://stackoverflow.com/questions/65946377/python-code-for-discord-bot-using-discord-py-getting-using-variable-before-assi 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…