因此,在使用if message.content.startswith((prefix + smalltalk0[0]) or (prefix + smalltalk0[1]) or (prefix + smalltalk0[2])):
时,这里似乎有问题,而if语句似乎只在第一个条件下工作:(prefix + smalltalk0[0])
,(我在其中写“ sab hi”,它会做出响应。但是由于某种原因,它不会响应“ sab hello”或“ sab hey”。)
有人可以在这里帮助我吗,我猜我的错误与使用倍数或语句有关,但请更正其他可能出错的地方,非常感谢!
这是我所有的代码(是的,我确实知道help命令还没有完全完成并且可以运行,大声笑,我只是被位于代码末尾的一小段讨论区所困扰!):
import discord
import random
import asyncio
TOKEN = '*insert token here'
client = discord.Client()
prefix = ("sab ")
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith(prefix + "help"):
_help = "{0.author.mention}...Check your PMs".format(message)
await message.channel.send("Prefix: 'sab '(yes, including the space after)\nGENERAL COMMANDS\n------------\n'help': Sends some stuff on how to use the bot.\n'tell a joke': Tells a rubbish joke...".format(author))
await message.channel.send(_help)
if message.content.startswith(prefix + "tell a joke"):
joke = ["What did the grape do when he got stepped on?",
"Why couldnt the bicycle stand up by itself?",
"How did the frog die?",
"What do you call a can opener that doesn't work?",
"Why does he want his job to be cleaning mirrors?",
"What does a clock do when it's hungry?",
"Why do sea-gulls fly over the sea?"]
answer = ["He let out a little wine!",
"Because it's two-tired!",
"He Kermit suicide!",
"A can't opener!",
"Because it's something he can really see himself doing!",
"It goes back four seconds!",
"Because if they flew over the bay they would be bagels!"]
y = [0, 1, 2, 3, 4, 5, 6]
x = random.choice(y)
jokenum = joke[x]
answernum = answer[x]
msg = jokenum.format(message)
msg2 = answernum.format(message)
await asyncio.sleep(1)
await message.channel.send(msg)
await asyncio.sleep(4)
await message.channel.send(msg2)
colours = ["blue", "orange", "yellow", "red", "green" ]
p = [0, 1, 2, 3, 4]
z = random.choice(p)
colournum = colours[z]
colourcorrect = str(colournum)
if message.content.startswith(prefix + "play eye spy"):
await message.channel.send("Eye spy with my little eyes, a certain colour!(Guess it!)")
if colourcorrect in message.content:
await message.channel.send("Correct, " + colourcorrect + "!")
smalltalk0 = ["hi", "hello", "hey"]
q = [0, 1, 2]
s = random.choice(q)
smalltalk = smalltalk0[s]
if message.content.startswith((prefix + smalltalk0[0]) or (prefix + smalltalk0[1]) or (prefix + smalltalk0[2])):
await message.channel.send(smalltalk)
@client.event
async def on_ready():
print('Sabios ready')
client.run(TOKEN)
答案 0 :(得分:1)
首先,这不是or
的工作方式。实际上,您实际上需要重复startswith()
调用,并用or
分隔不同的调用:
if (
message.content.startswith(prefix + smalltalk0[0])
or message.content.startswith(prefix + smalltalk0[1])
or message.content.startswith(prefix + smalltalk0[2])
):
await message.channel.send(smalltalk)
但是您确实可以使用any
来简化它:
if any(message.content.startswith(prefix + x) for x in smalltalk0):
await message.channel.send(smalltalk)
或利用startswith()
接受元组进行检查的事实:
if message.content.startswith(tuple(prefix + x for x in smalltalk0)):
await message.channel.send(smalltalk)
但是,此功能中的 all 检查都使用前缀。那么,为什么一开始就不做这样的事情?
if message.author == client.user:
return
if not message.content.startswith(prefix):
return
content = message.content[len(prefix):]
像这样,您会得到content
,其中仅包含前缀后的消息,然后可以简单地检查例如为此:
if message.content.startswith(tuple(smalltalk0)):
...