如何在discord.py按钮类中更新变量?

huangapple go评论96阅读模式
英文:

How to update a variable in a discord.py buttons class?

问题

I understand your request. Here's the translated code portion:

  1. 所以我正在尝试创建一个命令其中包含按钮每次执行时都会发送不同的问题和不同的选项这些问题/选项是从文本文件中提取的我使用一个计数器来告诉代码提取不同的问题但似乎总是提取相同的问题
  2. 这是我用于按钮的代码
  3. ```python
  4. class buttons(discord.ui.View):
  5. def __init__(self):
  6. super().__init__()
  7. with open("temp.txt", "r") as fp:
  8. for i, line in enumerate(fp):
  9. global count
  10. count = 0
  11. if i == count:
  12. answer = json.loads(line)
  13. global current_question
  14. current_question = {}
  15. current_question["Question"] = answer.get("Question")
  16. current_question["Option1"] = answer.get("Option1")
  17. global option1
  18. option1 = current_question.get("Option1")
  19. @discord.ui.button(label=option1, style=discord.ButtonStyle.blurple)
  20. async def op1(self, interaction: discord.Interaction, button: discord.ui.Button):
  21. ...

这是执行时应该提高计数器的命令的代码:

  1. @bot.command()
  2. async def test(ctx):
  3. await ctx.send(current_question.get("Question"), view=buttons())
  4. global count
  5. count += 1

我必须将按钮放在一个类中,我不知道如何在那里更新计数变量,它卡在0上,而且一直发送相同的问题和选项。
任何帮助都将不胜感激。

  1. 如果您有任何其他需要,请随时告诉我。
  2. <details>
  3. <summary>英文:</summary>
  4. So I&#39;m trying to make a command that has buttons that sends a different question with different options each time it&#39;s executed, the questions/options are pulled from a text file. I&#39;m using a counter to tell the code to pull a different question, but it seems to be pulling the same one always.
  5. This is the code I&#39;m using for the buttons:

class buttons(discord.ui.View):
def init(self):
super().init()
with open("temp.txt", "r") as fp:
for i, line in enumerate(fp):
global count
count = 0
if i == count:
answer = json.loads(line)
global current_question
current_question = {}
current_question["Question"] = answer.get("Question")
current_question["Option1"] = answer.get("Option1")
global option1
option1 = current_question.get("Option1")
@discord.ui.button(label=option1, style=discord.ButtonStyle.blurple)
async def op1(self, interaction: discord.Interaction, button: discord.ui.Button):
...

  1. And this is the code for the command that when executed, should raise the counter by one:

@bot.command()
async def test(ctx):
await ctx.send(current_question.get("Question"), view=buttons())
global count
count +=1

  1. I have to keep the buttons in a class, and I just don&#39;t know how to get the count variable to update there, It&#39;s stuck on 0 and it&#39;s sending the same questions and options.
  2. Any help would be appreciated.
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. The `@discord.ui.button` 装饰器允许您创建一个按钮,将其关联到一个 `callback` 并以实际方式将其添加到视图中,但当您对这些按钮有可变参数时,我建议您手动创建和添加这些按钮。例如:
  7. ```py
  8. class OptionButton(discord.ui.Button):
  9. def __init__(self, option: str):
  10. super().__init__(label=option, style=discord.ButtonStyle.blurple)
  11. async def callback(self, interaction: discord.Interaction):
  12. if self.label == self.view.answer:
  13. await interaction.response.send_message("正确答案!")
  14. else:
  15. await interaction.response.send_message("错误答案!")
  16. class QuestionView(discord.ui.View):
  17. def __init__(self, options: list[str], answer: str):
  18. self.answer = answer
  19. for op in options:
  20. self.add_item(OptionButton(op))
  21. super().__init__()
  22. count = 0
  23. @bot.command()
  24. async def test(ctx):
  25. global count
  26. with open("temp.txt", "r") as fp:
  27. lines = fp.read().splitlines()
  28. question = json.loads(lines[count])
  29. await ctx.send(
  30. content=question["question"],
  31. view=QuestionView(question["options"], question["answer"])
  32. )
  33. count += 1

请注意,我访问了 questionoptionsanswer 键。您的字典没有这些键,但我认为这是存储问题数据的最佳方式。

英文:

The @discord.ui.button decorator allows you to create a button, relate it to a callback and add it to a view in a practical way, but when you have variable parameters for these buttons, I suggest that you create and add the buttons manually. For example:

  1. class OptionButton(discord.ui.Button):
  2. def __init__(self, option: str):
  3. super().__init__(label=option, style=discord.ButtonStyle.blurple)
  4. async def callback(self, interaction: discord.Interaction):
  5. if self.label == self.view.answer:
  6. await interaction.response.send_message(&quot;Right answer!&quot;)
  7. else:
  8. await interaction.response.send_message(&quot;Wrong answer!&quot;)
  9. class QuestionView(discord.ui.View):
  10. def __init__(self, options: list[str], answer: str):
  11. self.answer = answer
  12. for op in options:
  13. self.add_item(OptionButton(op))
  14. super().__init__()
  15. count = 0
  16. @bot.command()
  17. async def test(ctx):
  18. global count
  19. with open(&quot;temp.txt&quot;, &quot;r&quot;) as fp:
  20. lines = fp.read().splitlines()
  21. question = json.loads(lines[count])
  22. await ctx.send(
  23. content=question[&quot;question&quot;],
  24. view=QuestionView(question[&quot;options&quot;], question[&quot;answer&quot;])
  25. )
  26. count += 1

> Note that I'm accessing the question, options and answer keys. Your dictionary doesn't have these keys, but I think this would be the best way to store question data.

huangapple
  • 本文由 发表于 2023年4月17日 20:17:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定