【问题标题】:Discord bot and the "rock paper scissors" game in PythonDiscord bot 和 Python 中的“石头剪刀布”游戏
【发布时间】:2021-08-19 17:12:53
【问题描述】:

考虑:

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时它甚至都没有启动。

【问题讨论】:

  • 你有没有得到任何错误或回溯?您可以编辑答案以包含此内容吗?您的命令的缩进看起来也很奇怪,您是否正确复制和粘贴了代码?
  • 另外,看看这个答案:Why does on_message stop commands from working?
  • my_secret = os.environ['cleint'] 中,“cleint”是“client”的拼写错误。这种拼写错误(不是反问)的后果是什么?

标签: python discord discord.py bots


【解决方案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,这也可以解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-16
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多