【发布时间】:2021-05-29 09:37:40
【问题描述】:
我一直在尝试使用我创建的机器人发送电子邮件,并且到目前为止我已经编写了代码(如下所示)。发送电子邮件的代码有效,我已经对其进行了测试,但我不确定如何在我的不和谐机器人中实现它,所以当它收到命令时(例如::电子邮件 johnnyappleseed@gmail.com “你好,你好吗? " 将有一个 johnnyappleseed@gmail 的接收者,并且电子邮件的正文将是 "黄色的你好吗?")。 email 方法的代码在底部,但我已经包含了我的机器人的整个代码。
以下是我的整个机器人的代码:
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 = 'botqiqi2515@gmail.com'
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'))
下面是我在单独的测试文件中的电子邮件部分的代码(这部分有效,我已经测试过了):
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 = 'botqiqi2515@gmail.com'
receiver = 'johnnyappleseed@gmail.com'
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()
从我的整个机器人代码底部的 async def email(ctx, arg1, arg2): 方法可以看出,我已经编写了用于接收的方法的代码2个参数:收件人电子邮件和正文段落。我不确定从这里去哪里,不和谐用户运行命令并让我的机器人检测到该命令并调用该方法。有人可以帮助我使用我的机器人并就如何完成我的目标提供一些解释吗?谢谢!
【问题讨论】:
-
您是在问如何让机器人检测命令?
-
是的,我认为这是一个更好的问题
标签: python email discord command discord.py