繁体   English   中英

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

根据 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")

基本上他们的 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM