Reputation: 13
So there seems to be a problem here, when using: if message.content.startswith((prefix + smalltalk0[0]) or (prefix + smalltalk0[1]) or (prefix + smalltalk0[2])):
, and the if statement only seems to be working with the first condition:(prefix + smalltalk0[0])
, (where I write "sab hi", and it responds. But it does not respond to "sab hello" or "sab hey" for some sort of reason.)
Can someone please help me here, I'm guessing my error is something to do with using the multiple or statements, but correct anything else that may be wrong, many thanks!
Here is all my code (and yes I do know the help command is not quite finished and working lol, I just am troubled by the small talk area at the minute, which is located towards the end of the code!):
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)
Upvotes: 0
Views: 121
Reputation: 318578
First of all, this is not how or
works. You'd actually need to repeat the startswith()
call and separate the different calls with 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)
But you can indeed simplify it using any
:
if any(message.content.startswith(prefix + x) for x in smalltalk0):
await message.channel.send(smalltalk)
or making use of the fact that startswith()
accepts a tuple to check for:
if message.content.startswith(tuple(prefix + x for x in smalltalk0)):
await message.channel.send(smalltalk)
However, all checks in this function use the prefix. So why not do something like this at the very beginning?
if message.author == client.user:
return
if not message.content.startswith(prefix):
return
content = message.content[len(prefix):]
Like this you get content
containing just the message after the prefix, and can then simply check e.g. for this:
if message.content.startswith(tuple(smalltalk0)):
...
Upvotes: 1