Python – discord.py on_message – pass positional arguments

I am attempting to include positional arguments into an on_message function using discord.py but I receive an error below

 File "/usr/lib/python3.8/site-packages/discord/client.py", line 312, in _run_event     await coro(*args, **kwargs) TypeError: on_message() missing 3 required positional arguments: 'channels', 'textdata', and 'command'  

My aim is to pass channels – a name of a channel on discord server, textdata – file path of text file, command – command that user will enter in discord channel for data to be returned.

I just wanted to ask is it possible to pass positional arguments into on_message or can message be the only one?

The code I have is

channels =[]  @client.event async def on_message(message, channels , textdata, command):          id = client.get_guild(7319460*****229982)                         if str(message.channel) in channels:         if message.content.find(command) != -1:              with open(textdata, 'r') as file:                 msg = file.read(2000).strip()                 while len(msg) > 0:                     await message.author.send(msg)                     msg = file.read(2000).strip()                      def callall():     on_message(channels="boxingmma",  textdata="/home/brendan/Desktop/Python/liveonsatscraper/testlasttime.txt", command="!boxingfixtures")  

The reason I am attempting this is because I need to be able to run the code above on multiple discord channels, to output the data from the filepaths in textdata and to provide a different command based on the data required. I could copy the code above 100 times and input the hardcoded string values for each of the arguments but I thought it would be better to pass these values into the function above to save repeating code.

Thank you in advance to anyone that can provide guidance or a solution to this.

Asked on July 16, 2020 in Python.
Add Comment
1 Answer(s)

When the bot receives a message through on_message, there is only one parameter: message. It’s a class all on it’s own, but it can be broken down into:

message.content #what was said, string message.channel #which channel it was said in, channel class message.guild #which server it was said in, guild class message.author #who sent the message, user class 

And you can go from there.

{EDIT}

If you want it to be able to parse commands as well, do like so:

@bot.event async def on_message(message):     if message.author == bot.user: return #makes it so the bot doesn't listen to itself     await bot.process_commands(message) 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.