victorbrowser
victorbrowser

Reputation: 71

How can I use function args to define a variable

I am recoding my discord bot to make it look less spaghetti, what I want is I want to make a new function named send_message in here I want to set the text for await message.author.send(). Normally you would do await message.author.send("Hello") I want to make it so I can call my function and put the text in the call something like this send_message(text="hello!")

Here is my code

async def send_message(self, message, text):
    await message.author.send(text)

send_message(text="Hello")

This is not working and I can't seem to figure out why, the method argument is used for message.author.send and self is used for author

Upvotes: 0

Views: 25

Answers (1)

Henry Harutyunyan
Henry Harutyunyan

Reputation: 2425

This is not working since the message and text args are both posional, required arguments. You don't specify those in the method call. As far as I see the message can't be empty since you are using it in the method. For the text you can do the following.

async def send_message(text=''):
    await message.author.send(text)

send_message(text="Hello")

Upvotes: 1

Related Questions