1
votes

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.

2
I use a different bot script to test what I want to do than the one on my normal discord server, so the only code missing is the importing of discord/asyncio/random/etc and the client.run on the bottom.Karia Wolfhunter
Can you post the entire stack trace?Snark
Nevermind figured out how to edit, added it to original post. Sorry I didn't think to post it :(Karia Wolfhunter
Whoops, sorry for the red herring, I am fairly certain it's the 3rd line. If you drop the "(pass_context=True" you should get thigs to work.Snark
Taking that out changed it to: 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

2 Answers

3
votes

There's a few things you need to understand here:

on_message() is an event that simply passes you the entire Message object.

Secondly, it's message.content for the string of the message. Don't use a function call ().

Third, you shouldn't be creating a new async def inside of on_message. If you really just want to respond to a message that says "buy me," here is an example of responding to someone who starts their message with "buy me." Since I'm not 100% sure what exactly your end goal is, I'm simply going to give you an example you can build off of:

@client.event
async def on_message(msg):
    if msg.content.startswith("buy me"):
        responses = ['meh', 'no thanks brudda']
        choice = random.choice(responses)
        await client.send_message(msg.channel, choice)

On a side note: This seems like you're using the "async" version of discord.py. You should think about moving to the "rewrite" library which is going to change a lot of these API calls. Also, you should be using commands.ext, but considering you don't have a good grasp on Python yet, using on_message() should be good enough.

There's a lot more to say about your code in its current state, but this should be enough to push you in the right direction.

1
votes
message.content

Appears to be a string according to the docs https://github.com/Rapptz/discord.py/blob/async/discord/message.py here. This means the passing of an argument and the two parentheses are superflous and you can drop them.