Python 为什么使用discord.py时会出现名称未定义错误?
我正在尝试使用discord.py创建一个带有discord bot的tictactoe功能,但是每次我在discord中运行命令时,我都会收到消息 “命令引发异常:NameError:名称'gameOver'不正确。” 定义“ 几天来,我一直在试图找出我做错了什么,但运气不好,我试图在cog内部使用它,然而,当我将类中完全相同的代码复制并粘贴到我的主文件中时,它工作正常,没有问题。有人有什么建议吗Python 为什么使用discord.py时会出现名称未定义错误?,python,discord.py,Python,Discord.py,我正在尝试使用discord.py创建一个带有discord bot的tictactoe功能,但是每次我在discord中运行命令时,我都会收到消息 “命令引发异常:NameError:名称'gameOver'不正确。” 定义“ 几天来,我一直在试图找出我做错了什么,但运气不好,我试图在cog内部使用它,然而,当我将类中完全相同的代码复制并粘贴到我的主文件中时,它工作正常,没有问题。有人有什么建议吗 import discord from discord.ext import commands
import discord
from discord.ext import commands
import random
# dont forget to pass in self again
class ttt(commands.Cog):
def __init__(self, client):
self.client = client
self.ttt_games = {}
player1 = ""
player2 = ""
turn = ""
gameOver = True
board = []
winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
# game setup part
@commands.command()
async def tictactoe(self, ctx, p1: discord.Member, p2: discord.Member):
global count
global player1
global player2
global turn
global gameOver
if gameOver:
global board
board = [":white_large_square:", ":white_large_square:", ":white_large_square:",
":white_large_square:", ":white_large_square:", ":white_large_square:",
":white_large_square:", ":white_large_square:", ":white_large_square:"]
turn = ""
gameOver = False
count = 0
player1 = p1
player2 = p2
# print the board
line = ""
for x in range(len(board)):
if x == 2 or x == 5 or x == 8:
line += " " + board[x]
await ctx.send(line)
line = ""
else:
line += " " + board[x]
# determine who goes first
num = random.randint(1, 2)
if num == 1:
turn = player1
await ctx.send("It is <@" + str(player1.id) + ">'s turn.")
elif num == 2:
turn = player2
await ctx.send("It is <@" + str(player2.id) + ">'s turn.")
else:
await ctx.send("A game is already in progress! Finish it before starting a new one.")
@commands.command()
async def place(self, ctx, pos: int):
global turn
global player1
global player2
global board
global count
global gameOver
if not gameOver:
mark = ""
if turn == ctx.author:
if turn == player1:
mark = ":regional_indicator_x:"
elif turn == player2:
mark = ":o2:"
if 0 < pos < 10 and board[pos - 1] == ":white_large_square:" :
board[pos - 1] = mark
count += 1
# print the board
line = ""
for x in range(len(board)):
if x == 2 or x == 5 or x == 8:
line += " " + board[x]
await ctx.send(line)
line = ""
else:
line += " " + board[x]
checkWinner(winningConditions, mark)
print(count)
if gameOver == True:
await ctx.send(mark + " wins!")
elif count >= 9:
gameOver = True
await ctx.send("It's a tie!")
# switch turns
if turn == player1:
turn = player2
elif turn == player2:
turn = player1
else:
await ctx.send("Be sure to choose an integer between 1 and 9 (inclusive) and an unmarked tile.")
else:
await ctx.send("It is not your turn.")
else:
await ctx.send("Please start a new game using the !tictactoe command.")
def checkWinner(winningConditions, mark):
global gameOver
for condition in winningConditions:
if board[condition[0]] == mark and board[condition[1]] == mark and board[condition[2]] == mark:
gameOver = True
@tictactoe.error
async def tictactoe_error(self, ctx, error):
print(error)
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Please mention 2 players for this command.")
elif isinstance(error, commands.BadArgument):
await ctx.send("Please make sure to mention/ping players (ie. <@688534433879556134>).")
@place.error
async def place_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Please enter a position you would like to mark.")
elif isinstance(error, commands.BadArgument):
await ctx.send("Please make sure to enter an integer.")
def setup(client) :
client.add_cog(ttt(client))
导入不一致
从discord.ext导入命令
随机输入
#别忘了再次自我介绍
ttt类(commands.Cog):
定义初始化(自我,客户机):
self.client=client
self.ttt_games={}
player1=“”
player2=“”
turn=“”
gameOver=True
董事会=[]
获胜条件=[
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
#游戏设置部分
@commands.command()
异步定义tictactoe(self、ctx、p1:discord.Member、p2:discord.Member):
全局计数
全球玩家1
全球玩家2
全球转向
全球游戏结束
如果游戏结束:
全球董事会
board=[“:白色大方形:”,“:白色大方形:”,“:白色大方形:”,
“:白色大方形:”,“:白色大方形:”,“:白色大方形:”,
“:白色大方形:”,“:白色大方形:”,“:白色大方形:”]
turn=“”
gameOver=False
计数=0
player1=p1
player2=p2
#打印电路板
line=“”
对于范围内的x(透镜(板)):
如果x==2或x==5或x==8:
线路+=“”+线路板[x]
等待ctx发送(线路)
line=“”
其他:
线路+=“”+线路板[x]
#决定谁先走
num=random.randint(1,2)
如果num==1:
旋转=播放者1
等待ctx.send(“该轮到你了”)
elif num==2:
回合=玩家2
等待ctx.send(“该轮到你了”)
其他:
等待ctx.send(“一个游戏已经在进行中!在开始一个新游戏之前完成它。”)
@commands.command()
异步def位置(self、ctx、pos:int):
全球转向
全球玩家1
全球玩家2
全球董事会
全局计数
全球游戏结束
如果没有结束:
mark=“”
如果turn==ctx.author:
如果回合==玩家1:
mark=“:区域_指示器_x:”
elif turn==播放机2:
mark=“:o2:”
如果0=9:
gameOver=True
等待ctx。发送(“这是一个平局!”)
#开关转动
如果回合==玩家1:
回合=玩家2
elif turn==播放机2:
旋转=播放者1
其他:
等待ctx.send(“确保选择一个介于1和9之间的整数(包括1和9)以及一个未标记的磁贴。”)
其他:
等待ctx发送(“不轮到你了”)
其他:
等待ctx.send(“请使用!tictactoe命令开始一个新游戏。”)
def checkWinner(赢取条件、标记):
全球游戏结束
对于Winning Conditions中的条件:
如果线路板[条件[0]==标记和线路板[条件[1]]==标记和线路板[条件[2]]==标记:
gameOver=True
@蒂克塔克托错误
异步定义接触错误(self、ctx、error):
打印(错误)
如果isinstance(错误,commands.MissingRequiredArgument):
等待ctx.send(“请提及此命令的2名玩家。”)
elif isinstance(错误,commands.BadArgument):
等待ctx.send(“请确保提及/ping玩家(即)”)
@地点错误
异步定义放置错误(self、ctx、error):
如果isinstance(错误,commands.MissingRequiredArgument):
等待ctx.send(“请输入您想要标记的位置”)
elif isinstance(错误,commands.BadArgument):
等待ctx.send(“请确保输入整数”)
def设置(客户端):
客户。添加_cog(ttt(客户))
gameOver
不是一个全局变量,它是一个类变量。您正在尝试将所有类变量作为全局变量访问。如何在Barmar解决此问题?请使用self.gameOver
,self.player1
等。也不清楚为什么这些变量是类变量而不是实例属性。