我试图用我的不和谐机器人重新创建简单的内存游戏。这是此命令的代码:
@client.command(aliases = ['em']) # Emoji_Memory, EM
async def emoji_memory(ctx):
guild = client.get_guild(id)
board = []
for h in range(36):
board.append('⬛')
emojiListB = os.listdir('emojis')
emojiList = []
for f in emojiListB:
if str(f).endswith('.png'):
emojiList.append(f)
pairs = []
for n in range(18):
pairs.append(emojiList[random.randint(0, len(emojiList) - 1)])
sequence = {}
dList = []
for d in range(1, 37):
dList.append(d)
for p in range(len(pairs)):
temp1 = random.choice(dList)
dList.remove(temp1)
temp2 = random.choice(dList)
dList.remove(temp2)
sequence[str(pairs[p])] = [temp1, temp2]
tileList = []
for i in sequence:
for l in range(len(sequence[i])):
if sequence[i][l] <= 6:
sequence[i][l] = 'A' + str(sequence[i][l])
tileList.append(sequence[i][l])
elif sequence[i][l] <= 12:
sequence[i][l] = 'B' + str(sequence[i][l] - (6))
tileList.append(sequence[i][l])
elif sequence[i][l] <= 18:
sequence[i][l] = 'C' + str(sequence[i][l] - (12))
tileList.append(sequence[i][l])
elif sequence[i][l] <= 24:
sequence[i][l] = 'D' + str(sequence[i][l] - (18))
tileList.append(sequence[i][l])
elif sequence[i][l] <= 30:
sequence[i][l] = 'E' + str(sequence[i][l] - (24))
tileList.append(sequence[i][l])
elif sequence[i][l] <= 36:
sequence[i][l] = 'F' + str(sequence[i][l] - (30))
tileList.append(sequence[i][l])
moves = 0
matches = 0
allMatchesFound = False
while allMatchesFound != True:
count = 0
boardText = ''
for t in board:
boardText += t + ' '
count += 1
if count == 6:
boardText += '\n:regional_indicator_b: '
elif count == 12:
boardText += '\n:regional_indicator_c: '
elif count == 18:
boardText += '\n:regional_indicator_d: '
elif count == 24:
boardText += '\n:regional_indicator_e: '
elif count == 30:
boardText += '\n:regional_indicator_f: '
boardMessage = await ctx.send(embed = discord.Embed(title = '__Emoji Match__', description = '⬛ :one: :two: :three: :four: :five: :six:\n:regional_indicator_a: ' + boardText, color = 0xf6dbe9))
def check(author):
def inner_check(message):
return message.author == author and message.content.startswith('d.flip ')
return inner_check
try:
msg = await client.wait_for('message', timeout = 120.0, check = check(ctx.message.author))
except asyncio.TimeoutError:
await ctx.send('Time up, game ended, no score recorded.')
return
else:
tileString = str(msg.content)[str(msg.content).find(' ') + 1:]
tiles = tileString.split()
try:
tiles[0] = tiles[0].upper()
except:
pass
try:
tiles[1] = tiles[1].upper()
except:
pass
if len(tiles) != 2:
await ctx.send('Invalid number of tiles, try again.')
elif any([not(tiles[0] in tileList), not(tiles[1] in tileList)]):
await ctx.send('Invalid tile number, try again.')
elif tiles[0] == tiles[1]:
await ctx.send('You cannot flip the same tile twice, try again.')
else:
for i in sequence:
if tiles[0] in sequence[i]:
img = open('emojis/' + i, 'rb').read()
if tiles[1] in sequence[i]:
img2 = open('emojis/' + i, 'rb').read()
flipped = await guild.create_custom_emoji(name = 'tile' + tiles[0], image = img)
flipped2 = await guild.create_custom_emoji(name = 'tile' + tiles[1], image = img2)
await boardMessage.delete()
count = 0
moves += 1
boardText = ''
for tile in range(len(tiles)):
if 'A' in tiles[tile]:
tiles[tile] = int(tiles[tile][1:])
elif 'B' in tiles[tile]:
tiles[tile] = 6 + int(tiles[tile][1:])
elif 'C' in tiles[tile]:
tiles[tile] = 12 + int(tiles[tile][1:])
elif 'D' in tiles[tile]:
tiles[tile] = 18 + int(tiles[tile][1:])
elif 'E' in tiles[tile]:
tiles[tile] = 24 + int(tiles[tile][1:])
elif 'F' in tiles[tile]:
tiles[tile] = 30 + int(tiles[tile][1:])
for t in board:
if count + 1 == tiles[0]:
boardText += str(flipped) + ' '
elif count + 1 == tiles[1]:
boardText += str(flipped2) + ' '
else:
boardText += t + ' '
count += 1
if count == 6:
boardText += '\n:regional_indicator_b: '
elif count == 12:
boardText += '\n:regional_indicator_c: '
elif count == 18:
boardText += '\n:regional_indicator_d: '
elif count == 24:
boardText += '\n:regional_indicator_e: '
elif count == 30:
boardText += '\n:regional_indicator_f: '
boardMessage = await ctx.send(embed = discord.Embed(title = '__Emoji Match__', description = '⬛ :one: :two: :three: :four: :five: :six:\n:regional_indicator_a: ' + boardText, color = 0xf6dbe9))
if str(img) == str(img2):
await ctx.send('Match!')
matches += 1
board[tiles[0] - 1] = '⬜'
board[tiles[1] - 1] = '⬜'
if matches == 18:
await ctx.send('All matches found!')
allMatchesFound = True
await flipped.delete()
await flipped2.delete()
await asyncio.sleep(3)
await boardMessage.delete()
这似乎在随机的时间内效果很好。最终,发送d.flip (tile1) (tile2)
只会停止工作。在停止之前,似乎没有特定的工作时间。停止工作后,该命令将停止工作一段时间。即使重启机器人也无济于事。没有错误消息。