如何以动态方式使用与 'wait_for' 一起使用的 Discord.py 'check' parameter/function?我想将参数传递给 'check' 函数
How can I use the Discord.py 'check' parameter/function used with 'wait_for' in a dynamic way? I want to pass params to the 'check' function
我正在为我的 discord 机器人制作一个 trivia/quiz 功能,并想为 wait_for 制作一个 'check' 函数,我可以将参数传递给它来过滤什么 by/against,因为目前我是 'hard coding' 测验问题和功能。虽然我想要一个 JSON 文件,其中包含我可以随机 select 提出的所有琐事问题,并将答案传递给检查功能,而不是我目前所拥有的:
编辑:我已经弄清楚了,并在下面留下了我的解决方案作为答案。
`
import os
import discord
import json
import random
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
def obama_check(message):
obama = ['Barack Obama', 'barack obama']
content = message.content.lower()
return any(t in content for t in obama)
async def quiz(ctx, cache_msg, bot):
for reaction in cache_msg.reactions:
async for user in reaction.users():
users = []
users.append(user)
await new_msg.edit(content=":confetti_ball: Game Starting!! :confetti_ball:")
await asyncio.sleep(1)
await new_msg.edit(content="Who is the 44th president of the United States?")
obama = ['Barack Obama', 'barack obama']
msg = await bot.wait_for('message', check=obama_check)
if msg:
await msg.add_reaction('✅')
await ctx.send(str(msg.author.name) + "wins this one")
await asyncio.sleep(2)
await new_msg.edit(content="What is the name of Han Solo’s ship?")
msg = await bot.wait_for('message', check=hansolo_check)
if msg:
await msg.add_reaction('✅')
await ctx.send(str(msg.author.name) + "wins this one")
await asyncio.sleep(2)
`
我想让它如何运作的一些伪代码:
`
await new_msg.edit(content=rndm_question.question)
msg = await bot.wait_for('message', check=check(ctx, message, rndm_question.answer_list))
if msg:
await msg.add_reaction('✅')
await ctx.send(str(msg.author.name) + "wins this one")
await asyncio.sleep(2)
`
上面的函数 'quiz' 在我的主文件中是这样调用的:
`
@bot.command()
async def test(ctx):
msg = await ctx.send('TEST')
await msg.add_reaction('✅')
await asyncio.sleep(5)
users = []
cache_msg = discord.utils.get(bot.cached_messages, id=msg.id)
await quiz(ctx, cache_msg, bot)
`
我能够通过将我的问题+答案存储在 JSON 文件中来解决我自己的问题,并且对于 'check' 函数,我制作了一个全局变量 'triv' 并得到更新在调用 wait_for 和检查功能之前,在测验功能中使用 json 文件中的答案。效果很好。
triv = []
async def play_trivia(new_msg, ctx, bot):
with open("trivia_questions.json", 'r') as f:
trivia = json.load(f)
data = trivia['items']
random.shuffle(data)
for item in data:
flag = 0
for key in item:
q = item['q']
a = item['a']
if flag < 1:
flag += 1
await new_msg.edit(content=q)
global triv
triv = a
msg = await bot.wait_for('message', check=check)
if msg:
await msg.add_reaction('✅')
await ctx.send(str(msg.author.name) + "wins this one")
await asyncio.sleep(2)
else:
flag += 1
await ctx.send(q)
triv = a
msg = await bot.wait_for('message', check=check)
if msg:
await msg.add_reaction('✅')
await ctx.send(str(msg.author.name) + "wins this one!!")
await asyncio.sleep(2)
def check(message):
content = message.content.lower()
return any(t in content for t in triv)
我正在为我的 discord 机器人制作一个 trivia/quiz 功能,并想为 wait_for 制作一个 'check' 函数,我可以将参数传递给它来过滤什么 by/against,因为目前我是 'hard coding' 测验问题和功能。虽然我想要一个 JSON 文件,其中包含我可以随机 select 提出的所有琐事问题,并将答案传递给检查功能,而不是我目前所拥有的:
编辑:我已经弄清楚了,并在下面留下了我的解决方案作为答案。
`
import os
import discord
import json
import random
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
def obama_check(message):
obama = ['Barack Obama', 'barack obama']
content = message.content.lower()
return any(t in content for t in obama)
async def quiz(ctx, cache_msg, bot):
for reaction in cache_msg.reactions:
async for user in reaction.users():
users = []
users.append(user)
await new_msg.edit(content=":confetti_ball: Game Starting!! :confetti_ball:")
await asyncio.sleep(1)
await new_msg.edit(content="Who is the 44th president of the United States?")
obama = ['Barack Obama', 'barack obama']
msg = await bot.wait_for('message', check=obama_check)
if msg:
await msg.add_reaction('✅')
await ctx.send(str(msg.author.name) + "wins this one")
await asyncio.sleep(2)
await new_msg.edit(content="What is the name of Han Solo’s ship?")
msg = await bot.wait_for('message', check=hansolo_check)
if msg:
await msg.add_reaction('✅')
await ctx.send(str(msg.author.name) + "wins this one")
await asyncio.sleep(2)
`
我想让它如何运作的一些伪代码:
`
await new_msg.edit(content=rndm_question.question)
msg = await bot.wait_for('message', check=check(ctx, message, rndm_question.answer_list))
if msg:
await msg.add_reaction('✅')
await ctx.send(str(msg.author.name) + "wins this one")
await asyncio.sleep(2)
`
上面的函数 'quiz' 在我的主文件中是这样调用的:
`
@bot.command()
async def test(ctx):
msg = await ctx.send('TEST')
await msg.add_reaction('✅')
await asyncio.sleep(5)
users = []
cache_msg = discord.utils.get(bot.cached_messages, id=msg.id)
await quiz(ctx, cache_msg, bot)
`
我能够通过将我的问题+答案存储在 JSON 文件中来解决我自己的问题,并且对于 'check' 函数,我制作了一个全局变量 'triv' 并得到更新在调用 wait_for 和检查功能之前,在测验功能中使用 json 文件中的答案。效果很好。
triv = []
async def play_trivia(new_msg, ctx, bot):
with open("trivia_questions.json", 'r') as f:
trivia = json.load(f)
data = trivia['items']
random.shuffle(data)
for item in data:
flag = 0
for key in item:
q = item['q']
a = item['a']
if flag < 1:
flag += 1
await new_msg.edit(content=q)
global triv
triv = a
msg = await bot.wait_for('message', check=check)
if msg:
await msg.add_reaction('✅')
await ctx.send(str(msg.author.name) + "wins this one")
await asyncio.sleep(2)
else:
flag += 1
await ctx.send(q)
triv = a
msg = await bot.wait_for('message', check=check)
if msg:
await msg.add_reaction('✅')
await ctx.send(str(msg.author.name) + "wins this one!!")
await asyncio.sleep(2)
def check(message):
content = message.content.lower()
return any(t in content for t in triv)