简体   繁体   中英

My discord bot gives an error when I try send a message

So I was writing a basic Discord bot for fun. And I wanted it to count how many times "lmao was said". So I wrote this in Python:

if 'lmao' in message.content.lower():
    aantlmao=0
    await message.channel.send('aantal keer lmao gezegd:', aantlmao, '(sinds de laatste bot restart)')
    aantlmao=aantlmao+1
    print('counted a LMAO')

The message translates to: amount of times said lmao: [lmaoCounter] (since last bot restart)

But when I type "lmao" in discord it spits out this error:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site->packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 51, in on_message
    await message.channel.send('aantal keer lmao >gezegd:',aantlmao,'(sinds de laatste bot restart)')
TypeError: send() takes from 1 to 2 positional arguments but 4 were given

It's happening because you passed 3 arguments (separated by commas) to send() in which you can pass only one. You should use string formatting to avoid this error:

await message.channel.send(f'aantal keer lmao gezegd: {aantlmao} (sinds de laatste bot restart)')

Edit:

Put your variable before your on_message event and add global before a variable to make it accessible in and outside your function:

aantlmao = 0

@client.event # you might be using @bot.event - it depends
async def on_message(message):
    global aantlmao

    if 'lmao' in message.content.lower():
        await message.channel.send(f'aantal keer lmao gezegd: {aantlmao} (sinds de laatste bot restart)')
        aantlmao += 1 # you can use the format you used before. This one is just a little bit better
        print('counted a LMAO')

Read this article about global, local, and nonlocal variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM