0
votes

I've been trying to send an email with my bot that I've created, and I've written the code until now (shown below). The code for sending an email works, I've tested it, but I'm not sure how to implement it into my discord bot so when it receives a command (ex: :email [email protected] "hello how are you?" would have a receiver of johnnyappleseed@gmail and the body of the email would be "yellow how are you?"). The code for the email method is at the bottom, but I've included the entire code for my bot.

Below is my code for my entire bot:

import discord
import random
import smtplib, ssl 
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from email.mime.multipart import MIMEMultipart
from keepAlive import keepAlive
import os
from discord.ext import commands

client = discord.Client()

@client.event 
async def on_ready():
  print('Logged on as {0.user}'.format(client))

@client.event
async def on_message(message):

  content = message.content.lower()
  QiqiLines = ['I am Qiqi. I am a zombie. I forgot what comes next.', 'Did you ask me something? Sorry... I forgot.', 'Hold my hand please. This wind could blow me away.', 'I want to build a snowman. Will you help?', 'I like coconut milk... But, I dont know what it tastes like.' ,'All of this is because of you. Thank you very much. Can you make me a promise? From now on, please, let me protect you. Do you accept? Yes or no?']



  if message.author == client.user:
    return

  if message.content.startswith(':hello'):
    await message.channel.send('I am Qiqi. I am a zombie. I forgot what comes next.')  

  if message.content.startswith(':qiqi'):
    out = QiqiLines[random.randint(0,len(QiqiLines)-1)]
    await message.channel.send(out)


  if('ganyu') in content:
    await message.channel.send('Did someone say Ganyu? Is Ganyu here? Qiqi would like some cocomilk please!')


  if message.content.startswith(':help'):
    helpMessage = (":help - qiqi will help! \n:hello - says hi to me! Qiqi! \n:qiqi - i say something cool \nganyu - is cocogoat here?")

    await message.channel.send(helpMessage)

async def email(ctx, arg1, arg2):
  sender = '[email protected]'
  receiver = arg1
  bodySend = arg2
  msg = MIMEText(bodySend, 'html')
  msg['Subject'] = 'Hi! I am Qiqi'
  msg['From'] = sender
  msg['To'] = receiver
  s = smtplib.SMTP_SSL(host = 'smtp.gmail.com', port = 465)
  s.login(user = sender, password = os.getenv('GMAILPASS'))
  s.sendmail(sender, receiver, msg.as_string())
  s.quit()


  
keepAlive()
client.run(os.getenv('TOKEN'))

Below is my code for the email portion on a separate testing file (this part works, I've tested it):


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


sender = '[email protected]'
receiver = '[email protected]'
bodySend = "Hello"

msg = MIMEText(bodySend, 'html')
msg['Subject'] = 'Hi! I am Qiqi'
msg['From'] = sender
msg['To'] = receiver
s = smtplib.SMTP_SSL(host = 'smtp.gmail.com', port = 465)
s.login(user = sender, password = os.getenv('GMAILPASS'))
s.sendmail(sender, receiver, msg.as_string())
s.quit()

As you can see from the async def email(ctx, arg1, arg2): method at the bottom of my code for the entire bot, I've written the code for a method to receive 2 parameters: a recipient email, and a body paragraph. I'm not sure where to go from here, with the discord user running a command and having my bot detect that command and call the method. Can someone help me with my bot and provide some explanation on how to complete my goals? Thanks!

1
Are you asking about how to make a bot detect a command?Jason Rebelo Neves
yes i think that would be a better questionz.rogerye

1 Answers

2
votes

If your code already works, then you really just need to create the command:

from discord.ext import commands

bot = commands.Bot(command_prefix=":")

@bot.command()
async def email(ctx, email, message):
    # use your email code

Note that any spaces that are not within quotes will separate arguments, so the only way to use this command properly would be as you defined in your question:

:email [email protected] "hello how are you?"

More details on creating commands can be found here.