I'm new to coding and I got ambitious and started writing a discord bot that's triggered via the message content, and I've managed a few simple working commands with random answers, but when talking to my friend I got the idea of using part of a message sent from the user as part of the message sent back by the bot... Well, I'm a train wreck when it comes to python obviously because I can't figure out what I'm doing wrong in the code below:
@client.event
async def on_message(message):
if "buy me" in message.content(pass_context=True):
async def quote(ctx):
split_parts = quote.split(' ') # splits the content by the space, making a list
# split_parts[0] would be "buy"
# split_parts[0] would be "me"
# etc
split_parts.pop(0)
split_parts.pop(0)
new_quote = " ".join(split_parts)
buyquotes = ["Buying you", "No, I will not buy you"] #etc
var = int(random.random() * len(buyquotes))
await client.send_message(message.channel, "{0} {1}".format(buyquotes[var], new_quote))
Everything loads up fine but when I try to trigger the bot it tells me TypeError: 'str' is not a callable object, I've looked around and found some similar questions (to which I've tried to fix my error based on the answer) but I have absolutely no idea what I did wrong (or if what I'm trying to do is even possible). Any help would be much appreciated, I'd love to know if something like this can actually work.
Ignoring exception in on_message
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/discord/client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "nyola.py", line 11, in on_message
if "buy me" in message.content(pass_context=True):
TypeError: 'str' object is not callable
Just adding this in: My objective was to try and take a message like "buy me a TV" trigger the bot with the "buy me" words, delete the words "buy me" and then add the remaining words to the end of the bot's message so instead of "buying you" it would be "buying you a tv"
Now that this is resolved:
if "buy me" in message.content:
quote = message.content
split_parts = quote.split(' ') # splits the content by the space, making a list
# split_parts[0] would be "buy"
# split_parts[0] would be "me"
# etc
split_parts.pop(0)
split_parts.pop(0)
new = " ".join(split_parts)
buyquotes = ["Buying you", "Contacting Amazon looking to buy you", "No, I will not buy you", "You can't have", "There is no need for", "I am not buying you","I can't believe you would ask me for"]
var = int(random.random() * len(buyquotes))
await client.send_message(message.channel, "{0} {1}".format(buyquotes[var], new))
Had multiple errors in the original code, it works now, it's not perfect, but it works.
File "nyola.py", line 11, in on_message if "buy me" in message.content(): TypeError: 'str' object is not callable
which I don't get :( because that's the same exact line now I've used for all my bot's things so far and it worked perfectly.till now. – Karia Wolfhunter