Final Code
Final Code
#import getpass
#getpass.getpass("pass")
##
import discord
import asyncio
import requests
import json
import urllib.parse
from discord.ext import commands
import time
from discord.ext.commands import Bot
import smtplib
import sqlite3
#from chatterbot import ChatBot
#from chatterbot.trainers import ListTrainer
#import os
##########################################
client = discord.Client()
client_command = commands
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
##########################################
@client.event
async def on_message(message):
content = message.content
channel = message.channel
UserRequest = message.content.lower()
userId = message.author.id
userName = message.author.name
timestamp = message.timestamp
strtimestapm = str(message.timestamp)
#_Final_U_INFO = ("#User Name: "+userName+" #User ID: "+userId+" #User's
Message: "+content)
aa = str("#User Name: "+userName+" #User ID: "+userId+" #Content:
"+content+" #Timestamp:"+ strtimestapm)
##########################################
if UserRequest == "help" or UserRequest == "!help":
await client.send_message(channel,"For getting news ,please use '!news'
command.___________ ")
#For sending e-mail please use '!sendemail'command => command '!mymail'
=> command'!mypass' => command'!receiver' => command '!subject' => command
'!content'.MAKE SURE THAT YOU ARE SENDING FROM gmail.com ACCOUNT,OTHERWISE
YOU CANNOT SEND ANY EMAIL CURRENTLY.")#For chatting with just write
something I will respond.")
#await client.send_message(channel,userId)This and next lines of code
is for giving user ID and name on discord respectively,of course if user
want.However I have not added this option.
#await client.send_message(channel, "<@%s>" % (userId))
#print(userId)
#print("@"+userName)
##########################################
elif message.content.lower() == '!sendemail':
if message.content.lower().startswith('!mymail'):
mymail_ = message.content
if message.content.lower().startswith('!mypass'):
mymailpass_ = message.content
if message.content.lower().startswith('!receiver'):
receiver_ = message.content
if message.content.lower().startswith('!subject'):
subject_ = message.content
if message.content.lower().startswith('!content'):
Mcontent_ = message.content
def send_email(subject_, Mcontent_):
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(mymail_,mymailpass_)
message = 'Subject: {}\n\n{}'.format(subject_, Mcontent_)
server.sendmail(receiver_,receiver_ , message)
server.quit()
print("Email sent!")
#await client.send_message(channel,"Email sent!")
except:
print("Email failed to send.")
#await client.send_message(channel,"Email failed to
send.")
#THIS COMMNAD DID NOT WORKED ,ALTHOUGH I REALLY TRIED TO INCLUD THIS
FUNCTION ON MY CHATBOT ASWELL.
##########################################
elif message.content.lower().startswith('!news'):
oldUserRequest1 = message.content.lower()
UserRequest1 = oldUserRequest1.replace('!news', '')#to remove '!news'
from the keyword
content1 = message.content
encoded = urllib.parse.quote(UserRequest1)
apiAdd2 = "https://newsapi.org/v2/everything?"
keywords2 = 'q='+ encoded
apikey = "&apikey=9430b58b162f4318bf862402d40a634d"
url2 = apiAdd2 + keywords2 + apikey
try:
jsonData2 = requests.get(url2).json()
except:
print("The link is not established!")
await client.send_message(channel,"The link is not established!")
print(url2)
subject__ = userName+"'s message"
def send_email(subject_, Mcontent_):
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login('[email protected]','kGP9Z5E1ZiEJsLA')
message = 'Subject: {}\n\n{}'.format(subject__, Mcontent_)
if userName == "ChatBot-D2":#I did not want to send bot messages to
the Email address.
pass
#if message.content.lower().startswith('!sendemail'):#line 113 and
118.AttributeError: 'str' object has no attribute 'content'
#pass
else:
server.sendmail('[email protected]','[email protected]' , message)
server.quit()
send_email(subject__,aa)
def dynamic_data_entry():
UserName = userName
UserID = userId
Message = content
Time = strtimestapm
c.execute("INSERT INTO stuffToPlot (UserName, UserID, Message, Time)
VALUES (?, ?, ?, ? )",(UserName, UserID, Message, Time))
conn.commit()
create_table()
dynamic_data_entry()
##########################################
client.run('NTE1OTE0MTYzMDA0ODk5MzI4.Dtsb3g.Kyp-_mqX9W--kGP9Z5E1ZiEJsLA')
##########################################