亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何以動態方式使用與“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 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號