Python 中的 Discord bot 和“石头剪刀布”游戏

时间:2021-06-01 13:40:26

标签: python discord discord.py bots

考虑:

import os
import discord
import random
from discord.ext import commands

client = discord.Client()
bot = commands.Bot(command_prefix='!')
my_secret = os.environ['cleint']

Player = ''
ComPoint = 0
PlayerPoint = 0
turn = 0
gameOver = True
legalMoves = ['rock', 'paper', 'scissors']

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

@bot.command
async def rcp(ctx):
  global Player
  global turn
  global count
  global PlayerPoint
  global ComPoint


if turn == 3:
    turn = 0
    count = 0
    PlayerPoint = 0
    ComPoint = 0


  Player = ''
  with open('images/rock_paper_scissors__2x.png', 'rb') as rcp:
    picture = discord.File(rcp)
    await ctx.send(file = picture)
    await picture.add_reaction(':rock:')
    await picture.add_reaction(':roll_of_paper:')
    await picture.add_reaction(':scissors:')

comRandom = random.choice(legalMoves)
for reaction in picture.reactions:
  ''' Rock '''
  if reaction.emoji == ':rock:' and comRandom == ':scissors:':
    turn += 1
    PlayerPoint += 1
    ctx.send("PLAYER: ", PlayerPoint, "COMPUTER: ", ComPoint)
  elif reaction.emoji == ':rock:' and comRandom == ':paper:':
    turn += 1
    ComPoint += 1
    ctx.send("PLAYER: ", PlayerPoint, "COMPUTER: ", ComPoint)
  elif reaction.emoji == ':rock:' and comRandom == ':rock:':
    turn += 1
    ctx.send("PLAYER: ", PlayerPoint, "COMPUTER: ", ComPoint)

  ''' Paper '''
  if reaction.emoji == ':paper:' and comRandom == ':scissors:':
    turn += 1
    ComPoint += 1
    ctx.send("PLAYER: ", PlayerPoint, "COMPUTER: ", ComPoint)
  elif reaction.emoji == ':paper:' and comRandom == ':paper:':
    turn += 1
    ctx.send("PLAYER: ", PlayerPoint, "COMPUTER: ", ComPoint)
  elif reaction.emoji == ':paper:' and comRandom == ':rock:':
    turn += 1
    PlayerPoint += 1
    ctx.send("PLAYER: ", PlayerPoint, "COMPUTER: ", ComPoint)

  ''' Scissors '''
  if reaction.emoji == ':scissors:' and comRandom == ':scissors:':
    turn += 1
    ctx.send("PLAYER: ", PlayerPoint, "COMPUTER: ", ComPoint)
  elif reaction.emoji == ':scissors:' and comRandom == ':paper:':
    turn += 1
    PlayerPoint += 1
    ctx.send("PLAYER: ", PlayerPoint, "COMPUTER: ", ComPoint)
  elif reaction.emoji == ':scissors:' and comRandom == ':rock:':
    turn += 1
    ComPoint += 1
    ctx.send("PLAYER: ", PlayerPoint, "COMPUTER: ", ComPoint)


client.run(my_secret)

我有什么问题?我认为问题出在我在 Discord 中发送图像的位置。正如我打开图像时显示的那样,如果我将其带出块。它给了我另一个错误。

我会尝试像image.reaction那样做,但我认为这是错误的。

我不会认为问题出在游戏逻辑上,因为当我使用命令 !rcp 时它甚至没有启动。

1 个答案:

答案 0 :(得分:-1)

我建议简单尝试

await ctx.send(file=discord.File('images/rock_paper_scissors__2x.png')

代替

with open('images/rock_paper_scissors__2x.png', 'rb') as rcp:

因为打开会导致问题 (Discord.py Bot sending file to Discord Channel)

你也可能有一些污染问题,因为你的命令函数被称为 rcp,你的文件被称为 rcp,这也可以解决。