为了账号安全,请及时绑定邮箱和手机立即绑定

如何以动态方式使用与“wait_for”一起使用的 Discord.py“check”参数/函数?

如何以动态方式使用与“wait_for”一起使用的 Discord.py“check”参数/函数?

浮云间 2023-10-25 11:04:02
我收到以下行的错误:username = login_form.cleaned_data('username'),因为在某个地方它似乎是一个字典,但我无法理解为什么。谁能告诉我,问题是什么?views.pydef index(request):    return render(request, 'web/base.html')def register_view(request):    register_form = UserCreationForm()    if request.method == "POST":        register_form = UserCreationForm(request.POST)        if register_form.is_valid():            register_form.save()            username = register_form.cleaned_data("username")            password = register_form.cleaned_data("password1")            user = authenticate(request, username=username, password=password)            if user:                login(request, user)    return render(request, 'web/register.html', {'register_form': register_form})def login_view(request):    login_form = AuthenticationForm()    if request.method == 'POST':        login_form = AuthenticationForm(data=request.POST)        if login_form.is_valid():            username = login_form.cleaned_data('username')            password = login_form.cleaned_data('password')            user = authenticate(request, username=username, password=password)            if user:                login(request, user)    return render(request, 'web/login.html', {'login_form': login_form})def logout_view(request):    logout(request)    return redirect(reverse('web:index'))
查看完整描述

1 回答

?
茅侃侃

TA贡献1842条经验 获得超21个赞

我能够通过将我的问题+答案存储在 JSON 文件中来解决我自己的问题,对于“检查”功能,我创建了一个全局变量“triv”,它在调用之前使用 json 文件中的答案在测验功能中进行更新wait_for 和 check 函数。它运作得很好。

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)


查看完整回答
反对 回复 2023-10-25
  • 1 回答
  • 0 关注
  • 71 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信