Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ChatBotGroup/FinalBot.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
559 lines (491 sloc)
22.6 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from GetLocation import * | |
from discord.ext import commands | |
from imdbrating import movieRating | |
from riotSummonerRank import * | |
from riotMatches import * | |
import urllib.request | |
import urllib.parse | |
import re | |
import asyncio | |
from datetime import datetime | |
from Jordan3 import * | |
client = commands.Bot(command_prefix = ".") | |
TOKEN = 'NTA4NjM1MTMyMzIwODA4OTg1.DsJXIw.8ZQR25yaiOi1CEYuikv2SPWElmA' | |
####################################################################################################################### | |
class Flag: | |
###Tiago Flags | |
Greet = 0 | |
Farewell = 0 | |
Taboo = 0 | |
Apologie = 0 | |
location = 0 | |
places = 0 | |
placesadvice = 0 | |
radius = 0 | |
searchterm = 0 | |
type = 0 | |
placesoutput = 0 | |
###Gonçalo and Jordan's Flags | |
TimeQ = 0 | |
Weather = 0 | |
WeatherA = 0 | |
Random = 0 | |
Twitter = 0 | |
Search = 0 | |
###Kirill Flags | |
rating = 0 | |
searchRating = 0 | |
###Flags for League | |
class LoL: | |
primary = 0 | |
secondary = 0 | |
summonerRank = 0 | |
matchList = 0 | |
matchDetails = 0 | |
summonerNameMatchList = 0 | |
summonerNameCurrentRank = 0 | |
regionCurrentRank = 0 | |
regionMatchList = 0 | |
class String: | |
###Strings used for searching places | |
radius = "" | |
type = "" | |
searchterm = "" | |
# Strings used for League of Legends | |
summonerName = "" | |
region = "" | |
class Lists: | |
###Gonçalo's and Jordan's Lists#################################################################################### | |
weatherList = ["cold", "hot", "weather", "typhoon", "forecast", "wind", "temperature"] | |
yes = ["yes"] | |
no = ["no"] | |
listTime = ["time", "day", "month", "year", "minute", "hour", "date"] | |
day = ["day"] | |
time = ["time"] | |
both = ["both"] | |
neither = ["neither"] | |
Twitter = ["twitter", "topic", "trending"] | |
###Tiago's Lists#################################################################################################### | |
Greetings = ['Hello', 'Hi!', "Top o' the morning t'ya", 'Well met'] | |
Farewell = ['So long', 'Goodbye', 'Goodbye', 'See you later', 'Farewell'] | |
Taboo_words = ['flummery', 'arse', 'bullocks', 'doodle', 'tarnation', 'gee golly', 'garyloo', 'pillion'] | |
Apologie = ['Sorry', 'sorry', 'forgive'] | |
Location = ['Location', 'location'] | |
Places = ['Places', 'near'] | |
PlacesList2 = ['restaurant', 'cafe', 'night_club', 'park', 'pharmacy', 'bus_station', 'museum', 'movie_theater', | |
'gym', 'spa', 'hospital'] | |
PlacesList1 = ['Restaurants', 'restaurants', 'restaurant', 'Restaurants', | |
'Cafe', 'Cafes', 'cafe', 'cafes', | |
'Night Clubs', 'Night Club', 'NightClubs', 'NightClub', 'night clubs', 'night club', 'nightclubs', | |
'nightclub', | |
'Parks', 'Park', 'parks', 'park', | |
'Pharmacy', 'Pharmacies', 'pharmacy', 'pharmacies', | |
'Bus Stations', 'BusStations', 'bus stations', 'busstations', 'Bus Station', 'BusStation', | |
'bus station', 'busstation', | |
'Museums', 'Museum', 'museums', 'museum', | |
'Movie Theaters', 'MovieTheaters', 'movie theaters', 'movietheaters', 'Movie Theater', | |
'Movie Theater', 'movie theater', 'movietheater', | |
'Gyms', 'Gym', 'gyms', 'gym', | |
'Spas', 'Spa', 'spas', 'spa', | |
'Hospitals', 'Hospital', 'hospitals', 'hospital'] | |
RestaurantList = ['Restaurants', 'restaurants', 'restaurant', 'Restaurants'] | |
CafeList = ['Cafe', 'Cafes', 'cafe', 'cafes'] | |
NightClubList = ['Night Clubs', 'Night Club', 'NightClubs', 'NightClub', 'night clubs', 'night club', 'nightclubs', | |
'nightclub'] | |
ParkList = ['Parks', 'Park', 'parks', 'park'] | |
PharmacyList = ['Pharmacy', 'Pharmacies', 'pharmacy', 'pharmacies'] | |
BusStationList = ['Bus Stations', 'BusStations', 'bus stations', 'busstations', 'Bus Station', 'BusStation', | |
'bus station', 'busstation'] | |
MuseumList = ['Museums', 'Museum', 'museums', 'museum'] | |
MovieTheaterList = ['Movie Theaters', 'MovieTheaters', 'movie theaters', 'movietheaters', 'Movie Theater', | |
'Movie Theater', 'movie theater', 'movietheater'] | |
GymList = ['Gyms', 'Gym', 'gyms', 'gym'] | |
SpaList = ['Spas', 'Spa', 'spas', 'spa'] | |
HospitalList = ['Hospitals', 'Hospital', 'hospitals', 'hospital'] | |
Advice = ['Recommendations', 'recommendation', 'recommendations'] | |
###Kirill's Lists################################################################################################### | |
# Keywords for movies | |
rating = ['rating', 'movie rating', 'rating movie', 'movie rated'] | |
# Keywords for League of Legends | |
leagueOfLegends = ['league of legends', 'lol stats', 'lol game'] | |
regionList1 = ['na', 'br', 'eun', 'euw', 'jp', 'la', 'oc', 'tr'] | |
regionList2 = ['eune', 'lan', 'oce'] | |
regionList3 = ['kr', 'ru'] | |
###End of classes and Lists############################################################################################ | |
def timeFunction(text): | |
now = datetime.now() | |
currentYear = now.year | |
currentMonth = now.month | |
currentDay = now.day | |
currentHour = now.hour | |
currentMin = now.minute | |
if currentMonth == 1: | |
currentMonth = "January" | |
elif currentMonth == 2: | |
currentMonth = "February" | |
elif currentMonth == 3: | |
currentMonth = "March" | |
elif currentMonth == 4: | |
currentMonth = "April" | |
elif currentMonth == 5: | |
currentMonth = "May" | |
elif currentMonth == 6: | |
currentMonth = "June" | |
elif currentMonth == 7: | |
currentMonth = "July" | |
elif currentMonth == 8: | |
currentMonth = "August" | |
elif currentMonth == 9: | |
currentMonth = "September" | |
elif currentMonth == 10: | |
currentMonth = "October" | |
elif currentMonth == 11: | |
currentMonth = "November" | |
elif currentMonth == 12: | |
currentMonth = "December" | |
if currentHour <= 12: | |
hour = " pm " | |
else: | |
currentHour = currentHour - 12 | |
hour = " am " | |
if currentMin == 1: | |
min = " minute " | |
else: | |
min = " minutes " | |
try: | |
if Flag.TimeQ == 1 and Lists.day[0] in text: | |
Flag.TimeQ = 0 | |
return "Today is " + currentMonth + " " + str(currentDay) + ", " + str(currentYear) + "." | |
elif Flag.TimeQ == 1 and Lists.time[0] in text: | |
Flag.TimeQ = 0 | |
return "It's " + str(currentHour) + hour + "and " + str(currentMin) + min + "." | |
elif Flag.TimeQ == 1 and Lists.both[0] in text: | |
Flag.TimeQ = 0 | |
return "Today is " + currentMonth + " " + str(currentDay) + ", " + str(currentYear) + " and it's " + \ | |
str(currentHour) + hour + "and " + str(currentMin) + min + "." | |
elif Flag.TimeQ == 1 and Lists.neither[0] in text: | |
Flag.TimeQ = 0 | |
return "What do you want to talk about then?" | |
else: | |
return "I can't understand you." | |
except: | |
return "Something went wrong." | |
def weatherForecast(city): | |
try: | |
url = 'https://www.weather-forecast.com/locations/' + city + '/forecasts/latest' | |
headers = {} | |
headers['User-Agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " \ | |
"Chrome/125.190.23.18 Safari/537.36" | |
# the 5 next lines were based on https://docs.python.org/ | |
req = urllib.request.Request(url, headers=headers) | |
resp = urllib.request.urlopen(req) | |
respData = resp.read() | |
temperature = re.findall(r'class="temp">\S+<', str(respData)) | |
weather_text = re.findall(r'"b-metar-table__weather-text">\D+<', str(respData)) | |
n = 0 | |
while n < 4: | |
t = temperature[n] | |
wt = weather_text[n] | |
if "Dry" in wt: | |
weather = "Dry" | |
elif "clowdy" or "Clowdy" in wt: | |
weather = "Clowdy" | |
elif "snow" in wt: | |
weather = "snowy" | |
elif "rain" or "Rain" in wt: | |
weather = "Rainy" | |
elif "Fog" or "fog" in wt: | |
weather = "Foggy" | |
elif "No report." in wt: | |
weather = "Something..." | |
if n == 0: | |
sentence0 = "The temperature and weather in different spots of " + city + " today will be: " | |
sentence1 = t[13:len(t) - 21] + "ºC, and " + weather | |
if n == 1: | |
sentence2 = t[13:len(t) - 21] + "ºC, and " + weather | |
elif n == 2: | |
sentence3 = t[13:len(t) - 21] + "ºC, and " + weather | |
elif n == 3: | |
sentence4 = t[13:len(t) - 21] + "ºC, and " + weather | |
return sentence0 + "\n" + sentence1 + "\n" + sentence2 + "\n" + sentence3 + "\n" + sentence4 | |
n = n + 1 | |
except Exception as error: | |
return "Sorry, but im afraid I don't know that city." | |
def weatherAnswer(text): | |
try: | |
if any(word in text for word in Lists.weatherList): | |
Flag.Weather = 1 | |
return "Do you want to talk about the weather?" | |
elif Flag.Weather == 1 and Flag.WeatherA == 1: | |
Flag.Weather = 0 | |
Flag.WeatherA = 0 | |
return weatherForecast(text) | |
elif any(word in text for word in Lists.yes) and Flag.Weather == 1: | |
Flag.WeatherA = 1 | |
return "You want the weather forecast of which city?" | |
elif any(word in text for word in Lists.no) and Flag.Weather == 1: | |
Flag.Weather = 0 | |
return "Ok then." | |
else: | |
return "I didn't understand." | |
except: | |
return "Something went wrong." | |
###Bit of code from tutorial video: https://www.youtube.com/watch?v=hFVby_Vet7E&t=5s | |
@client.command(pass_context=True) | |
async def join(ctx): | |
"""Function that makes the bot join the voice channel""" | |
#author = ctx.message.author | |
#channel = author.voice_channel | |
channel = ctx.message.author.voice.voice_channel | |
await client.join_voice_channel(channel) | |
print("Bot joined the voice channel!") | |
@client.command(pass_context=True) | |
async def leave(ctx): | |
"""Function that makes the bot leave the voice channel""" | |
server = ctx.message.server | |
voice_client = client.voice_client_in(server) | |
await voice_client.disconnect() | |
print("Bot left the voice channel") | |
###End of code from tutorial video: https://www.youtube.com/watch?v=hFVby_Vet7E&t=5s | |
###Bit of code from tutorial video: https://www.youtube.com/watch?v=MbhXIddT2YY | |
@client.command(pass_context=True) | |
async def play(ctx, url): | |
"""Function to play a Youtube video""" | |
#get server from message | |
server = ctx.message.server | |
#access the voice client of said server | |
voice_client = client.voice_client_in(server) | |
#create youtube player of the url passed in the function | |
player = await voice_client.create_ytdl_player(url) | |
#saving player as the current server specific player | |
players[server.id] = player | |
#starts player | |
player.start() | |
###End of code from tutorial video: https://www.youtube.com/watch?v=MbhXIddT2YY# | |
@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): | |
messages = message.content | |
channel = message.channel | |
keyWords = messages.lower() | |
if message.author == client.user: | |
return | |
else: | |
try: | |
# Setting up the flags | |
###Greetings setup################################### | |
if any(word in messages for word in Lists.Greetings): | |
Flag.Greet = 1 | |
###Farewell setup#################################### | |
if any(word in messages for word in Lists.Farewell): | |
Flag.Farewell = 1 | |
###Taboo setup######################################### | |
if any(word in messages for word in Lists.Taboo_words): | |
Flag.Taboo = 1 | |
###Apologies setup################################## | |
if any(word in messages for word in Lists.Apologie): | |
Flag.Apologie = 1 | |
###Location setup######################################################## | |
if any(word in messages for word in Lists.Location) and 'my' in messages: | |
Flag.location = 1 | |
###Places setup########################################################## | |
if any(word in messages for word in Lists.Places): | |
Flag.places = 1 | |
###Places help part###################################################### | |
if any(word in messages for word in Lists.Places) and 'what' in messages: | |
Flag.placeshelp = 1 | |
###Places advice######################################################### | |
if any(word in messages for word in Lists.Advice): | |
Flag.placesadvice = 1 | |
###Movie rating part##################################################### | |
if any(word in keyWords for word in Lists.rating): | |
Flag.rating = 1 | |
###League stats part##################################################### | |
if any(word in keyWords for word in Lists.leagueOfLegends): | |
Flag.LoL.primary = 1 | |
if any(word in messages for word in Lists.weatherList): | |
Flag.Weather = 1 | |
if any(word in messages for word in Lists.listTime) and Flag.TimeQ == 0: | |
Flag.TimeQ = 1 | |
if any(word in messages for word in Lists.Twitter) and Flag.Twitter == 0: | |
Flag.Twitter = 1 | |
if Flag.Weather == 1: | |
Flag.Weather = 0 | |
output = "Do you want to talk about the weather?" | |
elif Flag.TimeQ == 1: | |
Flag.TimeQ = 0 | |
output = "Do you want to know what day it is, what time it is, both or neither?" | |
elif Flag.Twitter == 1: | |
Flag.Twitter = 0 | |
output = trending_topics(0) | |
if "1" in messages: | |
Flag.Search = 1 | |
output = trending_topics(1) + "\n" + search(0) | |
if "2" in messages: | |
Flag.Search = 1 | |
output = trending_topics(2) + "\n" + search(0) | |
if "3" in messages: | |
Flag.Search = 1 | |
output = trending_topics(3) + "\n" + search(0) | |
if "4" in messages: | |
Flag.Search = 1 | |
output = trending_topics(4) + "\n" + search(0) | |
if "5" in messages: | |
Flag.Search = 1 | |
output = trending_topics(5) + "\n" + search(0) | |
# Making the flags work | |
###Greetings Part######################### | |
elif Flag.Greet == 1: | |
output = random.choice(Lists.Greetings) | |
Flag.Greet = 0 | |
###Farewell Part########################## | |
elif Flag.Farewell == 1: | |
output = random.choice(Lists.Farewell) | |
###Curse word Part############################# | |
elif Flag.Taboo == 1: | |
output = "Please don't use that language!" | |
Flag.Taboo = 0 | |
###Apologies Part##################### | |
elif Flag.Apologie == 1: | |
output = "Alright, I forgive you." | |
Flag.Apologie = 0 | |
###Location Start################## | |
elif Flag.location == 1: | |
Flag.Location = 0 | |
output = GetLocation() | |
###Places advice###################################### | |
elif Flag.placesadvice == 1: | |
Flag.placesadvice = 0 | |
output = "Well, I would recommend only going for {0} above 3,5 rating.".format(Flag.searchterm) | |
###Places Radius confirmation############# | |
elif Flag.radius == 1: | |
Flag.radius = 0 | |
String.radius = messages | |
output = "{0}: \n".format(Flag.searchterm) + SearchPlaces(String.radius, String.type) | |
# output = 'Give me a sec...' | |
###Search Term Start############################ | |
elif Flag.searchterm == 1: | |
Flag.searchterm = 0 | |
Flag.radius = 1 | |
String.searchterm = messages | |
if any(word in messages for word in Lists.PlacesList1): | |
if any(word in messages for word in Lists.RestaurantList): | |
Lists.type = Lists.PlacesList2[0] | |
if any(word in messages for word in Lists.CafeList): | |
Lists.type = Lists.PlacesList2[1] | |
if any(word in messages for word in Lists.NightClubList): | |
Lists.type = Lists.PlacesList2[2] | |
if any(word in messages for word in Lists.ParkList): | |
Lists.type = Lists.PlacesList2[3] | |
if any(word in messages for word in Lists.PharmacyList): | |
Lists.type = Lists.PlacesList2[4] | |
if any(word in messages for word in Lists.BusStationList): | |
Lists.type = Lists.PlacesList2[5] | |
if any(word in messages for word in Lists.MuseumList): | |
Lists.type = Lists.PlacesList2[6] | |
if any(word in messages for word in Lists.MovieTheaterList): | |
Lists.type = Lists.PlacesList2[7] | |
if any(word in messages for word in Lists.GymList): | |
Lists.type = Lists.PlacesList2[8] | |
if any(word in messages for word in Lists.SpaList): | |
Lists.type = Lists.PlacesList2[9] | |
if any(word in messages for word in Lists.HospitalList): | |
Lists.type = Lists.PlacesList2[10] | |
output = 'Under what radius?' | |
else: | |
output = 'Sorry, that search term goes beyond my abilities!' | |
Flag.radius = 0 | |
###Places Start##################### | |
elif Lists.flagplaces == 1: | |
Lists.flagplaces = 0 | |
Lists.flagsearchterm = 1 | |
output = 'Sure there are, currently, you can search for these \n' \ | |
'places: \n' \ | |
'Restaurants, Cafes, Night Clubs, Parks, Pharmacies, Bus Stations, \n' \ | |
'Museums, Movie Theaters, Gyms, Spas and Hospitals.\n' \ | |
'Which one would you like to search?' | |
elif Flag.rating == 1: | |
Flag.rating = 0 | |
Flag.searchRating = 1 | |
output = "What movie to be more precise?" | |
elif Flag.searchRating == 1: | |
Flag.searchRating = 0 | |
output = movieRating(messages) | |
elif Flag.LoL.primary == 1: | |
Flag.LoL.primary = 0 | |
Flag.LoL.secondary = 1 | |
output = "What exactly you want to know?\n1 - Summoners current rank.\n2 - Get match list and check match details." | |
elif Flag.LoL.secondary == 1: | |
Flag.LoL.secondary = 0 | |
if messages == "1": | |
Flag.LoL.summonerRank = 1 | |
output = "What summoners name?" | |
elif messages == "2": | |
Flag.LoL.matchList = 1 | |
output = "What summoners name?" | |
else: | |
output = "Try again." | |
elif Flag.LoL.summonerRank == 1: | |
Flag.LoL.summonerRank = 0 | |
Flag.LoL.regionCurrentRank = 1 | |
String.summonerName = message | |
output = "In what region?" | |
elif Flag.LoL.regionCurrentRank == 1: | |
Flag.LoL.regionCurrentRank = 0 | |
String.region = messages.lower() | |
# The if statements bellow allows user write the region without the need to add extra letters/numbers. | |
# For example, instead of EUW1 user can just input EUW. | |
if any(word in String.region for word in | |
Lists.regionList1 or Lists.regionList2 or Lists.regionList3 or "las" or "LAS"): | |
if any(word in String.region for word in Lists.regionList1): | |
String.region = String.region + "1" | |
if any(word in String.region for word in Lists.regionList2): | |
String.region = String.region[:-1] + "1" | |
if any(word in String.region for word in Lists.regionList3): | |
String.region = String.region | |
if String.region == "las" or String.region == "LAS": | |
String.region = "la2" | |
output = summonerRank(String.region, String.summonerName) | |
else: | |
output = "There is no such region." | |
elif Flag.LoL.matchList == 1: | |
Flag.LoL.matchList = 0 | |
Flag.LoL.regionMatchList = 1 | |
String.summonerName = messages | |
output = "In what region?" | |
elif Flag.LoL.regionMatchList == 1: | |
Flag.LoL.regionMatchList = 0 | |
Flag.LoL.matchDetails = 1 | |
String.region = messages.lower() | |
# The if statements bellow allows user write the region without the need to add extra letters/numbers. | |
# For example, instead of EUW1 user can just input EUW. | |
if any(word in String.region for word in | |
Lists.regionList1 or Lists.regionList2 or Lists.regionList3 or "las" or "LAS"): | |
if any(word in String.region for word in Lists.regionList1): | |
String.region = String.region + "1" | |
if any(word in String.region for word in Lists.regionList2): | |
String.region = String.region[:-1] + "1" | |
if any(word in String.region for word in Lists.regionList3): | |
String.region = String.region | |
if String.region == "las" or String.region == "LAS": | |
String.region = "la2" | |
output = "From which game do you want to see the details?\n" + matchList(String.region, String.summonerName) | |
elif Flag.LoL.matchDetails == 1: | |
Flag.LoL.matchDetails = 0 | |
positionInList = messages | |
output = matchDetails(String.region, String.summonerName, int(positionInList)) | |
else: | |
output = "Sorry, I didn't understand that last part!" | |
except: | |
output = "An error has ocurred!" | |
if messages[0] == '.': | |
await client.process_commands(message) | |
else: | |
await client.send_message(channel, output) | |
client.run(TOKEN) |