【问题标题】:How to only run discord.py command if boolean is true如果布尔值为真,如何仅运行 discord.py 命令
【发布时间】:2021-04-12 22:05:26
【问题描述】:

所以我正在尝试为我的不和谐机器人制作一个琐事命令。我想这样做,所以当你输入答案时,你不能再不回答了。我尝试过这样的事情:

import discord
from discord.ext import commands

trivia_list = {
"Capital of Spain?": "madrid",
"Capital of India?": "delhi",
"Language with the most words?": "english",
"What is the hardest rock?": "diamond",
"Planet with the most gravity?": "jupiter",
"What year was the first iPhone released?": "2007",
"What does DC stand for (the comics)?": "detective comics",
"What country won the very first FIFA World Cup in 1930?": "uruguay",
"What is the body's largest organ?": "skin"
}

economy = {}

def random_trivia(test):
random.choice(list(trivia_list))
client = commands.Bot(command_prefix=">")


@client.command()
async def trivia(ctx, be_am):
  global ongt
  ongt = True
  global user
  global be
  user = ctx.author
  be = int(be_am)
  if int(be_am) <= economy[ctx.author]:
    await ctx.send(random_tri())

@client.command()
async def ans(ctx, answer):
  if not ongt:
    await ctx.send("Test")
  if ctx.author != user:
      return ""
  if user in economy:
    if be > economy[user]:
      await ctx.send("Sorry! You can't bet more than you already have.")
  con_ans = answer.lower()
  if trivia_list[chosen_trivia] in con_ans:
    await ctx.send("Correct!")
    if user in economy:
      economy[user] += be
    else:
      economy[user] = be
    await ctx.send(f"You now have {economy[user]} coins!")
    ongt = False
  if trivia_list[chosen_trivia] not in con_ans:
    await ctx.send(f"Incorrect! Answer: {trivia_list[random_tri]}")

除非我在输入 >ans 命令时不断收到此错误:命令引发异常:UnboundLocalError: local variable 'ongt' referenced before assignment

【问题讨论】:

  • ongtans 中的局部(非全局)变量,由于ongt = False 行。但是,您尝试在第一个 if 块中的第一个分配之前从 ongt 读取。根据您的其余代码,您可能打算通过global ongtans 中将ongt 声明为全局变量。

标签: python python-3.x discord discord.py


【解决方案1】:

您忘记在ans() 中将ongt 声明为global

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 2014-04-25
    • 2014-01-24
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多