我一直在尝试使用我创建的机器人发送电子邮件,到目前为止我已经编写了代码(如下所示)。发送电子邮件的代码有效,我已经对其进行了测试,但我不确定如何将其实施到我的不和谐机器人中,因此当它收到命令时(例如::email 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 个参数:收件人电子邮件和正文段落。我不确定从哪里开始,不和谐用户运行命令并让我的机器人检测该命令并调用该方法。有人可以帮助我使用我的机器人并提供一些关于如何完成我的目标的解释吗?谢谢!
答案 0 :(得分:2)
如果你的代码已经可以运行,那么你真的只需要创建命令:
from discord.ext import commands
bot = commands.Bot(command_prefix=":")
@bot.command()
async def email(ctx, email, message):
# use your email code
请注意,不在引号内的任何空格都将分隔参数,因此正确使用此命令的唯一方法是按照您在问题中的定义:
:email johnnyappleseed@gmail.com "hello how are you?"
有关创建命令的更多详细信息,请参见 here。