TypeError: __call__() missing 1 required positional argument: 'context'

Question:

I want to create a program to send an email with a discord bot but when i laucnh my program i have this error. My code to send an email works fine in a separate file but when i put in in my python discord bot program there are a problem
my programm :

`

import smtplib
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from email.mime.multipart import MIMEMultipart
import os

import discord
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv(dotenv_path="config")

default_intents = discord.Intents.default()
default_intents.members = True 
default_intents.message_content = True
default_intents.messages = True
bot = commands.Bot(command_prefix=":", intents=default_intents)

mail = ""
varBody = ""

@bot.event 
async def on_ready():
    print("Bot Ready !")
    await bot.change_presence(activity = discord.Activity(type = discord.ActivityType.listening, name = "Cool Music !"))

@bot.event
async def on_message(message):
    if message.content.lower() == "login":
        await message.channel.send("mail :")
        mail = await bot.wait_for("message")
        await message.channel.send("Message : ")
        varBody = await bot.wait_for("message")
        await message.channel.send("finishing...")
        await email()

@bot.command()
async def email(, email, message):
  msg = MIMEMultipart()
  msg['Subject'] = 'Hi! I am Qiqi'
  msg['From'] = 'MAIL_ADDRESS'
  msg['To'] = mail
  password = "MAIL_PASSWORD"
  body = varBody
  msg.attach(MIMEText(body, 'html'))

  server = smtplib.SMTP('smtp.gmail.com', 587)
  server.starttls()
  server.login(msg['From'], password)
  server.sendmail(msg['From'], msg['To'], msg.as_string())
  server.quit()



bot.run("TOKEN")


#terminal error

Traceback (most recent call last):
  File "C:UserslussaAppDataLocalProgramsPythonPython39libsite-packagesdiscordclient.py", line 409, in _run_event
    await coro(*args, **kwargs)
  File "c:UserslussaDocumentsBotdiscordPythonmain.py", line 36, in on_message
    await email()
TypeError: __call__() missing 1 required positional argument: 'context'

`

i have added "ctx" in the def email

but again the same error

Asked By: Rapha

||

Answers:

I invite you to read the docs. All bot.command() need to have one mandatory parameter: context. It gives you all the info about the command used, but you can just ignore it if you don’t need it.

Also you don’t need to check the command message with on_message(), the command does it automatically.

Your code should work like this:

@bot.command(name="login")
async def email(context):
  await context.send("mail :")
  mail = await bot.wait_for("message")
  await context.send("Message : ")
  varBody = await bot.wait_for("message")
  await context.send("finishing...")

  msg = MIMEMultipart()
  msg['Subject'] = 'Hi! I am Qiqi'
  msg['From'] = 'MAIL_ADDRESS'
  msg['To'] = mail.content
  password = "MAIL_PASSWORD"
  body = varBody.content
  msg.attach(MIMEText(body, 'html'))

  server = smtplib.SMTP('smtp.gmail.com', 587)
  server.starttls()
  server.login(msg['From'], password)
  server.sendmail(msg['From'], msg['To'], msg.as_string())
  server.quit()
Answered By: Clement Genninasca

On your line 39 just remove the message parameter of the function and only pass ctx. It looks like you are not using any other parameters. Here’s the updated line email() function:

@bot.command()
async def email(ctx, mail):
  msg = MIMEMultipart()
  msg['Subject'] = 'Hi! I am Qiqi'
  msg['From'] = 'MAIL_ADDRESS'
  msg['To'] = mail
  password = "MAIL_PASSWORD"
  body = varBody
  msg.attach(MIMEText(body, 'html'))

  server = smtplib.SMTP('smtp.gmail.com', 587)
  server.starttls()
  server.login(msg['From'], password)
  server.sendmail(msg['From'], msg['To'], msg.as_string())
  server.quit()
Answered By: The Myth