我有一个制作测验机器人的项目(由我自己提供)。我非常喜欢口袋妖怪,所以我正在制作它。 pokedex 是我制作的一个库,里面有每个口袋妖怪的名字。我已经包括了之后的时间。总之,
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"))
这是我的代码。现在显然我遇到的错误是测验机器人认为命令是问题的答案。请告诉我如何解决这个问题。
这是错误的样子:(机器人重复错误的答案味精,并将命令作为答案)
答案 0 :(得分:1)
看看discord.py rewrite | How to wait for author message?
问题是,您正在检查消息是否以“-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"))
答案 1 :(得分:1)
首先基于 Oblique 对 bot.wait_for
的回答,您的代码在 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"))