繁体   English   中英

为什么我的机器人出现错误? 我该如何解决?

[英]Why is an error occurring in my bot? How can I solve it?

我有一个制作测验机器人的项目(我自己给的)。 我非常喜欢口袋妖怪,所以我正在努力。 pokedex 是我创建的一个库,其中包含每个 pokemon 的名称。 我已经包括了以后的时间。 无论如何,

    import random
    import pokedex
    import discord
    import pandas as pd
    import time
    import os
    
    client=discord.Client()
      
    
    @client.event
    async def on_ready():
      print("We have logged in as {0.user}".format(client))
    
    @client.event
    async def on_message(message):
      msg=message.content
      if message.author==client.user:
        return 
    
      if msg.startswith("-q jname"):
        await message.channel.send("Unscramble these letters to create a name of a pokemon:")
        s=random.choice(pokedex.poke)["name"]
        await message.channel.send(''.join(random.sample(s,len(s))))
        p=""
        for i in s:
          if i=="(":
            p=p+i
          elif i==")":
            p=p+i
          else:
            p=p+"-"
          await message.channel.send(p)
          if(msg==s):
            await message.channel.send("Correct Answer")
          else:
            await message.channel.send("Wrong! The correct Answer is:")
            await message.channel.send(s)
    client.run(os.getenv("Token"))

这是我的代码。 现在显然我遇到的错误是测验机器人认为该命令是问题的答案。 请告诉我如何解决这个问题。

这是错误的样子:(机器人重复错误的答案消息,并将命令作为答案) 机器人重复错误的答案消息,并将命令作为答案

看看discord.py 重写 | 如何等待作者消息?

问题是,您正在检查消息是否以“-q jname”开头,然后在该命令中检查消息是否正确。 当然“-q jname”是不正确的,所以你需要等待用户发送另一条消息,然后使用该消息进行检查。

import random
import pokedex
import discord
import pandas as pd
import time
import os

client=discord.Client()
  

@client.event
async def on_ready():
  print("We have logged in as {0.user}".format(client))

@client.event
async def on_message(message):
  msg=message.content
  if message.author==client.user:
    return 

  if msg.startswith("-q jname"):
    def check(author)
        if message.author != author:
            return False
        else:
            return True
    await message.channel.send("Unscramble these letters to create a name of a pokemon:")
    s=random.choice(pokedex.poke)["name"]
    await message.channel.send(''.join(random.sample(s,len(s))))
    msg = await client.wait_for('message', check=check(message.author),    timeout=30).content
    p=""
    for i in s:
      if i=="(":
        p=p+i
      elif i==")":
        p=p+i
      else:
        p=p+"-"
      await message.channel.send(p)
      if(msg==s):
        await message.channel.send("Correct Answer")
      else:
        await message.channel.send("Wrong! The correct Answer is:")
        await message.channel.send(s)
client.run(os.getenv("Token"))

首先使用bot.wait_for构建 Oblique 的答案,您的代码在 for 循环中具有发送函数,并且每次迭代都发送一条消息。

我在下面附上了我编辑的代码。 使用 ID 也是一种很好的做法,因此如果两个频道(或用户)具有相同的名称,则不会导致任何错误

import random
import pokedex
import discord
import pandas as pd
import time
import os

client = discord.Client()

@client.event
async def on_ready():
  print("We have logged in as {0.user}".format(client))

@client.event
async def on_message(message):
  msg = message.content
  if message.author.id == client.user.id:
    return 

  if msg.startswith("-q jname"):
    def check(check_message)
      if message.author.id != check_message.author.id:
        return False
      return True

    await message.channel.send("Unscramble these letters to create a name of a pokemon:")

    pokemon = random.choice(pokedex.poke)["name"]
    await message.channel.send(''.join(random.sample(pokemon, len(pokemon))))

    try:
      msg = await client.wait_for('message', check=check, timeout=30)
    except:
      pass
      
    if msg.content.lower() == pokemon:
      await message.channel.send("Correct Answer")
    else:
      await message.channel.send("Wrong! The correct Answer is:")
      await message.channel.send(pokemon)

client.run(os.getenv("Token"))

暂无
暂无

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

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