Ad
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
Ad
Answer
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.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad