discord.py 中的打字赛车手

A typeracer in discord.py

我在做什么:我正在尝试在 discord.py 中制作打字机,但首先我会尝试使用 def checkdef inner_check.

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

代码:

@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")

基本上是他们的 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 == '!type'
  • message.channel.send 现在 ctx.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")