【发布时间】:2022-01-26 04:35:36
【问题描述】:
我现在正在处理一个代码,由于某种原因,每当我运行它时,我的错误是“在参数之前引用了局部变量 'buzzcoins'”。
如您所见,buzzcoins 是在函数之外定义的,因此它应该是全局的?如果有人可以帮助我,那将是一个巨大的帮助!
我的密码:
import os
import random
import time
from keepalive import keep_alive
from discord.ext import commands
#COINS
buzzcoins = 1000
#COMMAND PREFIX
client = commands.Bot(command_prefix = "!")
#ON_READY MESSAGE
@client.event
async def on_ready():
await client.change_presence(activity=discord.Game("#commands"))
print("Bot is now running...")
#DICE COMMAND
@client.command()
async def dice(message, amount:int):
#DICE COMMAND - CHECKS
if amount > buzzcoins:
await message.channel.send("You bet more Buzz Coins then this server has!")
elif amount > 500:
await message.channel.send("You cannot bet more than 500 Buzz Coins at a time!")
elif amount == 0:
await message.channel.send("You cannot bet 0 Buzz Coins!")
elif amount < 0:
await message.channel.send("You cannot bet less than 0 Buzz Coins!")
else:
await message.channel.send(f"Betting {amount} Buzz Coins!")
time.sleep(1)
await message.channel.send(":game_die: Rolling the dice... :game_die:")
playerdicenumber1 = random.randint(1, 6)
playerdicenumber2 = random.randint(1, 6)
time.sleep(3)
await message.channel.send(f"You roll a {playerdicenumber1} and a {playerdicenumber2}!")
botdicenumber1 = random.randint(1, 6)
botdicenumber2 = random.randint(1, 6)
time.sleep(3)
await message.channel.send(f"Your opponent rolls a {botdicenumber1} and a {botdicenumber2}!")
time.sleep(1)
#DICE COMMAND - WIN
if playerdicenumber1 + playerdicenumber2 > botdicenumber1 + botdicenumber2:
await message.channel.send(f"You won {amount*2} Buzz Coins! :smile:")
buzzcoins = buzzcoins + amount
time.sleep(1)
await message.channel.send(f"This server now has {buzzcoins} Buzz Coins!")
#DICE COMMAND - LOSE
elif playerdicenumber1 + playerdicenumber2 < botdicenumber1 + botdicenumber2:
await message.channel.send(f"You lost {amount} Buzz Coins! :cry:")
buzzcoins = buzzcoins - amount
time.sleep(1)
await message.channel.send(f"This server now has {buzzcoins} Buzz Coins!")
#DICE COMMAND - TIE
elif playerdicenumber1 + playerdicenumber2 == botdicenumber1 + botdicenumber2:
await message.channel.send(f"You tied! Your bet has been returned! :ok_hand:")
time.sleep(1)
await message.channel.send(f"This server now has {buzzcoins} Buzz Coins!")
#KEEP_ALIVE
keep_alive()
my_secret = os.environ['token']
client.run(my_secret)
【问题讨论】:
标签: python discord discord.py