[英]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"))
這是我的代碼。 現在顯然我遇到的錯誤是測驗機器人認為該命令是問題的答案。 請告訴我如何解決這個問題。
這是錯誤的樣子:(機器人重復錯誤的答案消息,並將命令作為答案)
問題是,您正在檢查消息是否以“-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.