不和谐的打字机

2024-09-29 22:25:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做什么:我试图在discord.py中制作一个typeracer,但首先我试图用def checkdef inner_check来绕圈子

问题:如下图所示,当我输入正确的句子时,它仍然告诉我我错了。据我检查,没有错误。我使用了来自this link here,的代码,这有助于作者的输入,以防有帮助

It's not working as I hoped it would

代码

@client.command()
async def tr(ctx):
    starttime = time.time()
    C = "Just a nice little test"
    await ctx.send(f"Type: {C}")
    a = 1
    def check(author):
        def inner_check(message):
            return message.author == author and message.content == C
        return inner_check
    while a == 1:
        msg = await client.wait_for('message', check=check(ctx.author))
        if msg == True:
            a = a - 1
        else:
            await ctx.send("wrong")
    fintime = time.time()
    total = fintime - starttime
    await ctx.send(round(total,2),"seconds")

Tags: 代码clientsendmessagereturntimedefcheck
2条回答

基本上,他们的Github示例只是做了一些调整

#! /usr/bin/env python3

import discord
import random
import asyncio

token = 'bot_token'

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged in as: ',  self.user.name,  self.user.id)
        print('                         ')

    async def on_message(self,  message):
        ##  no need for bot to reply to itself
        if message.author.id == self.user.id:
            return

        if message.content == '!type':  ##  begin typeracer game with "!type" command

            answer = 'Just a nice little test'
            timer  = 5.0
            await message.channel.send(f'You have {timer} seconds to type:  {answer}')

            def is_correct(msg):
                return msg.author == message.author

            try:
                guess = await self.wait_for('message',  check=is_correct,  timeout=timer)
            except asyncio.TimeoutError:
                return await message.channel.send('Sorry, you took too long.')

            if guess.content == answer:
                await message.channel.send('Right on!')
            else:
                await message.channel.send('Oops.')

client = MyClient()
client.run(token)

基于Doyousketch2的回答,我已经用@client.command编写了另一种方法,因为它需要更多的调整。我还包括了所用的时间(四舍五入到最接近的整秒)

发生了什么变化

  • 使用了@client.command而不是“if message.content==”!类型'
  • message.channel.send现在{}在这里
  • message.author更改为ctx.author,因为message将给出一个错误name 'message' is not defined
@client.command()
async def type(ctx):
    starttime = time.time()
    answer = 'Just a nice little test'
    timer = 17.0
    await ctx.send(f"You have {timer} seconds to type: {answer}")

    def is_correct(msg):
        return msg.author==ctx.author

    try:
        guess = await client.wait_for('message', check=is_correct, timeout=timer)
    except asyncio.TimeoutError:
        return await ctx.send("You took too long :(")

    if guess.content == answer:
        await ctx.send("You got it!")
        fintime = time.time()
        total = fintime - starttime
        await ctx.send(f"{round(total)} seconds")

    else:
        await ctx.send("Nope, that wasn't really right")
        fintime = time.time()
        total = fintime - starttime
        await ctx.send(f"{round(total)} seconds")

相关问题 更多 >

    热门问题