0
votes

My Issue is, I cant get my bot to display the Member Author, Date and Message. Here is the error:

Ignoring exception in on_message Traceback (most recent call last): File "C:\Program Files (x86)\Python38-32\lib\site-packages\discord\client.py", line 270, in _run_event await coro(*args, **kwargs) TypeError: on_message() missing 1 required positional argument: 'ctx'

My Code:

@commands.Cog.listener()
    async def on_message(self, message, ctx):
        #await client.send_message(message.channel, discord.Message.author)
        messagestr = ""
        messagestr = ('{0.content}'.format(message))
        file_open()
        print(device_time()+" "+ctx.message.author+" "+"Message: "+messagestr)
        out1 = ""
        out1 = messagestr
        f1 = open('logs.txt','a')

        out1 = out1.replace('\n', '')
        out1 = (device_time()+" "+out1+'\r')
        f1.write(device_time()+" "+"Message: "+messagestr)
        f1.close()
        f1.close()
2

2 Answers

0
votes

on_message does not take ctx so remove that from the function and instead of ctx.message.author use message.author

0
votes

First of all on_message discord.py event doesn't takes ctx, it only takes message, this is what directly causes the error.

Then you are using +'s python concatenation methods which is not bad but python's f-strings are way better to format strings as f-strings directly convert the variable type to string (so no need to use str() function)

Also, think about using PEP-8 naming convention which makes your codes looks nicer and more easy to read. For example, you use messagestr = "", for python's interpreter, this is ok and won't throw a error but for humans this is really ugly to use, in python you should use snake_case so messagestr would become message_str = None

Here's what would i have done :

@commands.Cog.listener()
    async def on_message(self, message):
        #await client.send_message(message.channel, str(message.author))
        message_str = message.content
        file_open()
        print(f'{device_time()} {message.author} Message: {messagestr}')
        out = message_str.replace('\n', '')
        file = open('logs.txt','a')

        out = f'{device_time()} {out} \r' #Note : message.created_at would be a useful function here to replace device_time()
        file.write(f'{device_time()} Message: {message_str}')
        file.close()