I have a game that is basically two commands, test and test2.
test makes you generate a word that you have to guess in test2, and if you miss the word 6 times you lose.
from collections import defaultdict
word = ""
guessesLeft = 6
blanks = []
guessedLetters = []
lettersFound = 0
bot = commands.Bot(command_prefix=("!"))
bot.gamex = defaultdict(bool)
@bot.command()
async def test(ctx, *, message):
await ctx.message.delete()
global word, guessesLeft, blanks, lettersFound, guessedLetters
if not bot.gamex[ctx.guild.id]:
word = message.lower()
blanks = []
guessedLetters = []
lettersFound = 0
guessesLeft = 6
bot.gamex[ctx.guild.id] = True
for i in range(0, len(word)):
blanks .append("-")
print(i)
await ctx.send(embed=discord.Embed(title="hangman: " + " ".join(blanks)))
@bot.command()
async def test2(ctx, *, guess):
global word, guessesLeft, blanks, lettersFound, guessedLetters
if bot.gamex[ctx.guild.id]:
if str.isalpha(guess) and len(guess) is 1 and str.lower(guess) not in guessedLetters:
if str.lower(guess) in word:
await ctx.send(guess + " is in the word. Good job!")
for i in range(0, len(word)):
if word[i] == str.lower(guess):
blanks[i] = str.lower(guess)
lettersFound += 1
else:
await ctx.send(guess + " is NOT in the word.")
guessesLeft -= 1
guessedLetters.append(str.lower(guess))
await ctx.send(" ".join(blanks))
await ctx.send("Guessed letters: " + " ".join(guessedLetters))
await ctx.send("Guesses left: " + str(guessesLeft))
if guessesLeft == 0:
await ctx.send("No guesses left. You lose!")
bot.gamex[ctx.guild.id] = False
if lettersFound == len(word)-1:
await ctx.send("You've won! The word was: " + word)
bot.gamex[ctx.guild.id] = False
It's a hangman game, but the game variables are mixing on every server the bot is on, if I guess a word on one server, it appears on another server, I want each server to be individual and have commands individual.
Only those in the global are mixing.
What would the command look like so that the variables don't get mixed up between the servers?