Skip to content
Permalink
903ee7e951
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
467 lines (401 sloc) 28.9 KB
#==============This code was written by Emily==================
# This section of code was adapted from https://realpython.com/how-to-make-a-discord-bot-python/#creating-a-discord-connection by Emily
import os
import random
import discord
import steam
import requests
import urllib.parse
from steam.webapi import WebAPI
from dotenv import load_dotenv #only used so we can have the token outside of the source code
from steam.steamid import *
tempIDArraySteam = [] #These two lists are intended to be replaced by a relational database to store the users SteamID and DiscordID
tempIDArrayDiscord = []#============================================================================================================
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
steamApiKey = os.getenv('STEAM_API_KEY')
catApiKey = os.getenv('CAT_API_KEY')
client = discord.Client()
api = WebAPI(key=steamApiKey)
#==================DEFINITIONS GO HERE==================
#==================Emily=Function=Start=================
def botGreeting():
creepyHellos =["Why hello there, I am awakening from my slumber",
"My child I am learning, hi",
"I was enjoying the void before you woke me, I guess it's polite to say hello",
"Like life, my sleep was short. Hello child",
"Hello, what do you need me for this time?",
"Howdy y'all- yeah that wasn't working for me either. What can I do?",
"Hello, thanks for greeting me. I may save you in the bot wars"]
return random.choice(creepyHellos)
def steamAchievementPercentages(discordID, gameID): #Emily
sharedIDIndex = tempIDArrayDiscord.index(discordID)
steamID = tempIDArraySteam[sharedIDIndex]
steamID = str(steamID)
gameID = str(gameID)
achieveList= requests.get("http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?appid=" +gameID+ "&key=" +steamApiKey+ "&steamid=" +steamID)
totalAchieves = 0
achievesCompleted = 0
#this section formats the api, might be useful in future definitions
achieveList = achieveList.text
achieveList = achieveList.replace("\"", " ")
achieveList = achieveList.replace("},{", "-")
#may need something adding to this line in future api use
achieveList = achieveList.strip("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,:0123456789\" {}")
#this section turns the string into a list
achieveList = list(achieveList.split("-"))
#this section iterates over the list to find the total achievements
#print (achieveList)
for achieve in achieveList:
if "achieved :0" in achieve:
totalAchieves = totalAchieves + 1
if "achieved :1" in achieve:
achievesCompleted = achievesCompleted + 1
totalAchieves = totalAchieves + 1
if totalAchieves > 0:
percentage= round ((achievesCompleted/totalAchieves)*100, 2)
#messages are different depending on what % achieves you have
else:
percentage = 0
if percentage <= 25:
reply = ["Do you even play this game?! You only have "+ str(percentage) +"% of the achievements unlocked! \nGit gud scrub"]
elif percentage <= 50:
reply = ["I don't know why you're asking for an embarrassing number, you have "+ str(percentage) +"% of achievements unlocked"]
elif percentage <= 75:
reply = ["You have " + str(percentage) + "% of achievements unlocked. \nGood job?"]
elif percentage < 100:
reply = ["You have " + str(percentage) + "% of steam achievements unlocked. \nNot quite 100% though is it?"]
else:
reply = ["You really don't have a life do you? You have all of the achievements for this game. \nDo they fill the hole in your life?"]
return random.choice(reply)
def isUserBanned(discordID):#Emily
sharedIDIndex = tempIDArrayDiscord.index(discordID)
steamID = tempIDArraySteam[sharedIDIndex]
print(steamID)
#linkCheck="http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=" + steamApiKey + "&steamids="+ str(steamID)
api = requests.get("http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=" + steamApiKey + "&steamids="+ str(steamID))
#print (linkCheck)
api = api.text
api = ['"CommunityBanned":true', '"VACBanned":true', '"NumberOfVACBans":1', '"DaysSinceLastBan":1', '"NumberOfGameBans":5', '"EconomyBan":"none"}]}']
#this section turns the string into a list
api = list(api.split(","))
api = api[1:7]
print (api)
#if no bans starts off the "good gamer" branch
if api == ['"CommunityBanned":false', '"VACBanned":false', '"NumberOfVACBans":0', '"DaysSinceLastBan":0', '"NumberOfGameBans":0', '"EconomyBan":"none"}]}']:
reply = ["Looks like you've been good! No bans all round",
"Wow, you're boring, no bans to report"]
#else starts the "naughty gamer" tree where user gets told off for being banned
else:
banList = []
if '"CommunityBanned":false' not in api:
banList.append("Community banned")
if '"VACBanned":false' not in api:
banList.append("VACBanned")
if '"EconomyBan":"none"}]}' not in api:
banList.append("Economy Banned")
if '"NumberOfGameBans":0' not in api:
gameBans = api[4]
gameBans = ''.join(x for x in gameBans if x.isdigit()) #for this list I used https://www.tutorialspoint.com/How-to-remove-characters-except-digits-from-string-in-Python for help
banList.append("you've had " + gameBans + " games ban you")
daysSinceBan = api[3]
daysSinceBan = ''.join(x for x in daysSinceBan if x.isdigit())
banList.append("and it's been " + daysSinceBan + " days since you were last banned")
reply = ["Looks like you've been a bad little gamer because you have been: \n" + ", ".join(banList),
"Getting quite the ban collection here because you're: \n" + ", ".join(banList)]
return random.choice(reply)
def iNeedACat(): # Emily, sends a random cat picture
api = requests.get("https://api.thecatapi.com/v1/images/search", params = {'q': 'requests+language:python'}, headers = {"authorisation":catApiKey})
api = api.text
api = list(api.split(","))
api = api[-3]
api = api[7:-1]
return api
def catMessage(): #Emily: gives a quick reply about cat pictures, to go with the iNeedACat() function
reply = ["Here's a cat for you in these trying times",
"Emergency cat pics incoming!",
"*QUICK GET THE CATS*",
"So you need cats?"]
return random.choice(reply)
def cheerUpWithCats():
reply = ["I'll try my best to help the only way I know how",
"Never fear, the cats are here!",
"No sads only cats",
"While I can't cure your deeper routed problems, cats can help",
"Some cat pic therapy on the way!"]
return random.choice(reply)
def noSteamIDErrorReplies():
reply = ["Whoops something went wrong, have you given me your Steam ID yet?",
"I am a mere bot and I cannot hack your Steam account to get your ID, unfortunately. I also can't do what you're asking without it",
"I need a steam ID to do that. Looks like I don't have it yet"]
return random.choice(reply)
#================Emily=Function=End============================================
#================BenHB=Function=Start==========================================
#SteamID START
def registerSteamID(message, discordID):
formattedSteamID = message.lower()
formattedSteamID = formattedSteamID.replace(" ","") #Formats the recieved message into an acceptable form for the program to iterate through
for charNum in range(len(formattedSteamID)):
if charNum == 0:
count = 0
adjacencyStartIndex = 0 #====
currentChar = formattedSteamID[charNum] #loops through the message until 17 numbers are found to be adjacent to each other.
if currentChar.isdigit(): #It achieves this by looping through the message character at a time, setting the adjacency start point at zero and moving it to the index value after the current if the current
count = count + 1 #character is a letter, this is because it assumes that the next char is a number until it arrives, at which point if it is it will leave the adjacency start index at that first
else: #number and continue till either a letter is found or until it finds 17 numbers in a row which it assumes is the SteamID.
count = 0 #====
adjacencyStartIndex = charNum + 1
if count == 17:
formattedSteamID = formattedSteamID[adjacencyStartIndex:charNum+1]
break
if len(formattedSteamID) == 17 and formattedSteamID.isdigit(): #and formattedSteamID.is_valid(): in the documentation but currently doesn't work
if (formattedSteamID not in tempIDArraySteam) and (discordID not in tempIDArrayDiscord) :
tempIDArrayDiscord.append(discordID)
tempIDArraySteam.append(formattedSteamID)
sharedRelationshipIndex = tempIDArrayDiscord.index(discordID) #This checks if the input steamID is valid and not already present in the database / two sorted lists that simulate a database
return("Thank You! \nI now have your SteamID! It is currently registered as " +formattedSteamID)
elif(formattedSteamID in tempIDArraySteam and discordID not in tempIDArrayDiscord):
return("This Steam ID is already taken sorry")
elif(formattedSteamID not in tempIDArraySteam and discordID in tempIDArrayDiscord):
sharedRelationshipIndex = tempIDArrayDiscord.index(discordID)
return("You already have a Steam ID registered it is " +tempIDArraySteam[sharedRelationshipIndex])
else: #This prevents ID's from appearing twice on either list
sharedRelationshipIndex = tempIDArrayDiscord.index(discordID)
return("I already have your ID. Thanks anyway though!!! \nHere is the ID that was registered: \n" + tempIDArraySteam[sharedRelationshipIndex])
else:
return("To link your steam requires a SteamID64 a guide for which can be found at: https://appuals.com/steam-64-id/")
#SteamID END
#ChangeSteamID START
def changeSteamID(message, discordID): #Changes a registered users steamID to whatever new id they input.
sharedRelationshipIndex = tempIDArrayDiscord.index(discordID)
formattedSteamID = message.lower()
formattedSteamID = formattedSteamID.replace(" ","") #Formats the recieved message into an acceptable form for the program to iterate through
for charNum in range(len(formattedSteamID)):
if charNum == 0:
count = 0
adjacencyStartIndex = 0
currentChar = formattedSteamID[charNum]
if currentChar.isdigit():
count = count + 1
else:
count = 0
adjacencyStartIndex = charNum + 1
if count == 17:
formattedSteamID = formattedSteamID[adjacencyStartIndex:charNum+1]
break
if len(formattedSteamID) == 17 and formattedSteamID.isdigit(): #and formattedSteamID.is_valid(): in the documentation but currently doesn't work
if (formattedSteamID not in tempIDArraySteam):
tempIDArraySteam[sharedRelationshipIndex] = formattedSteamID #This checks if the input steamID is valid and not already present in the database / two sorted lists that simulate a database
return("Thank You! \nI have now changed your SteamID! It is currently registered as " +formattedSteamID)
elif formattedSteamID in tempIDArraySteam:
return("This Steam ID is already taken sorry")
else:
return ("Sorry, I couldn't seem to find the new SteamID")
#ChangeSteamID END
#SpecificGameID START
def fetchGameID(gameName): #This function fetches the appID of specific game on steam based on the name the user inputs
gameName = gameName[gameName.find('(') +1 :gameName.find(')')]#Formats the recieved message so as to only search with text in between the brackets when looking for gameID
urlResponse = urllib.request.urlopen('https://api.steampowered.com/ISteamApps/GetAppList/v2/?key='+steamApiKey+'&format=json').read().decode('UTF-8');
steamGames = json.loads(urlResponse) #This loads the url response as a dictionary of dictionaries of lists of dictionaries
steamGames = steamGames.get('applist').get('apps')# This simplifies the recieved json response into a list of dictionaries
desiredGameInformation = next((game for game in steamGames if game['name'] == gameName), False)
if desiredGameInformation != False: #This iterates through the list looking at each dictionary element related to key 'name' such as 'Monster Hunter: World' and if a game isn't found it gives False
gameID = desiredGameInformation['appid'] #If the game name is present in Steam's list of games then it will store the matching entry which it will then access for the game's corresponding appID
return int(gameID)
else:
return -1 #If the game name isn't found on Steam it will return -1 so as to provide functions with a workable response
#GameID END
#UsersGameInfo START
def userGameInfo(discordID):#COMMENT THIS TWO IT'S IMPORTANT
sharedIDIndex = tempIDArrayDiscord.index(discordID)#This searches for the index of the user's DiscordID and uses it to find the SteamID it's linked to temporarily inplace of a database linking the two ID's
steamID = tempIDArraySteam[sharedIDIndex]
urlResponse = urllib.request.urlopen('https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key='+steamApiKey+'&steamid='+steamID+'&format=json').read().decode('UTF-8');
userGameInformation = json.loads(urlResponse)#Loads the user's game list from their SteamID which contains information about their playtime and the appids of the games they own
userGameInformation = userGameInformation.get('response').get('games')
return userGameInformation #returns a simplifed list of dictionaries to be acted upon
#UserGameInfo END
#TimePlayed START
def usersTimePlayed(message, discordID):#COMMENT THIS THREE IT'S IMPORTANT
gameID = fetchGameID(message)
userGames = userGameInfo(discordID)#This function calls both the function that searches for the user's games and the function that fetches the game ID based on the name given
desiredGameInformation = next((game for game in userGames if game['appid'] == gameID), False)# Iterates through the list of dictionaries for an appID matching the one retrieved according to the game name that was input and stores the entry
if desiredGameInformation != False:
playTimeMinutes = desiredGameInformation['playtime_forever']#Finds the overall playtime of a user by searching value of playtime in minutes in the single entry retrieved from the above iteration
return playTimeMinutes
else:
return -1
#TimePlayed END
#TimeFormatted START
def timeFormat(timePlayed, message, parsedLetters, letterIndex):# A recursive function that checks to see which timeframe the user wants and returns the timePlayed in the appropriate format
message = message.lower()
message = message[0 : message.find('(')] + message[message.find(')'):]#This removes the text inbetween the two brackets which in this case is the game so that it doesn't interfere with what the user wants if the game name features words such as seconds
if letterIndex == len(message):#BASECASE DO NOT DELETE
return -1
parsedLetters = parsedLetters + message[letterIndex]#adds the current indexed letter to the parsed letters list and when a recognisable timeframe has been found it returns timePlayed in that format
if 'hour' in parsedLetters:
return round(timePlayed/60, 2)
if 'second' in parsedLetters:
return round(timePlayed*60)
if 'day' in parsedLetters:
return round((timePlayed/60)/24, 2)
if 'year' in parsedLetters:
return round(((timePlayed/60)/24)/365, 2)
if 'month' in parsedLetters:
return round(((timePlayed/60)/24)/30, 2)
if 'week' in parsedLetters:
return round(((timePlayed/60)/24)/7, 2)
else:
letterIndex = letterIndex + 1
return timeFormat(timePlayed, message, parsedLetters, letterIndex)#Increments the current letter index by one before calling the function again.
#TimeFormatted END
#==================Ben=HB=Function=End==================
#masud will do some functions and code here
polls = {} # masud added this to hold poll data. Can be switched to a database in the future
def getPollID(message,communityName,polls):
if communityName.upper() in polls.values(): # if there is a poll active already
pollID = list(polls.keys())[list(polls.values()).index(communityName.upper())] # get the ID of the poll
return True, pollID
else: # and if there is not an active poll already...
return False
def addPoll(poll,communityName):
polls[poll.id] = communityName.upper() # save the pollID and communityName together
return polls
def makeEmbed(discord, countDesc, communityName):
countEmbed = discord.Embed() # Discord embed object
countEmbed.description = countDesc # add embed description to embed
countEmbed.title = f"Is the community {communityName} salty or supportive?" # title of countEmbed object. DevOps work item 32
return countEmbed
def countPoll(message,communityName,pollReactions):
tickCount = 0
crossCount = 0
countDesc = ""
for emj in pollReactions: # for each emoji in the reactions object
emojiCount = emj.count - 1 # remove 1 vote, which was from the bot.
# print("[BOT DEBUG] emj.emoji:",emj.emoji) # debug code for reactions.emoji object
if emj.emoji == "✅": # if emoji is tick emoji
tickCount = emj.count - 1 # this reaction will be tickCount
elif emj.emoji == "❌": # if emoji is cross emoji
crossCount = emj.count - 1 # this reaction will be crossCount
if emojiCount == 1: # adjust embed description accordingly
countDesc = countDesc + f'\n{emj.emoji} : {emojiCount} vote'
else:
countDesc = countDesc + f'\n{emj.emoji} : {emojiCount} votes'
# add a message to the end of the results depending on the votes for the emojis. DevOps work item 34
# print("[BOT DEBUG] tick cross :",tickCount,crossCount) # this code was used to debug the tick and cross counts.
if tickCount == crossCount:
countDesc = countDesc + "\n This community seems to be equally supportive and salty."
elif tickCount > crossCount:
countDesc = countDesc + "\n This community seems to be more supportive."
elif tickCount < crossCount:
countDesc = countDesc + "\n This community seems to be more salty."
return countDesc
#end of the functions masud did
#==================END OF DEFINITIONS===================
@client.event #Prints in the terminal if bot connects successfully
async def on_ready():
print(f'{client.user} has connected to Discord!')
for guild in client.guilds:
if guild.name == GUILD:
break
print( f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})')
members = '\n - '.join([member.name for member in guild.members])
print(f'Guild Members:\n - {members}')
@client.event
async def on_message(message): #checks for a message
if message.author == client.user: #makes sure it's a user
return
greetingWords = [" hello", " howdy", " hey", " hi"] #Says hello (creepily) to the user if they use a greeting word
if [entry for entry in greetingWords if(entry in message.content.lower())] or message.content.lower().startswith("hi") or message.content.lower().startswith("hey") or message.content.lower().startswith("howdy"): #This line is adapted from https://www.geeksforgeeks.org/python-test-if-string-contains-element-from-list/ by Emily
await message.channel.send(botGreeting())
achievementWords = ["achievement", "achieve" ] # This is to call the steamAchievementPercentages() function
#if [entry for entry in achievementWords if(entry in message.content.lower())]:
if "steam" in message.content.lower() and [entry for entry in achievementWords if(entry in message.content.lower())]:
try:
await message.channel.send(steamAchievementPercentages(message.author.id, fetchGameID(message.content)))
except:
await message.channel.send(noSteamIDErrorReplies())
banWords = ["banned", "ban", "bans"]
if "steam" in message.content.lower() and [entry for entry in banWords if(entry in message.content.lower())]:
try:
await message.channel.send(isUserBanned(message.author.id))
except:
await message.channel.send(noSteamIDErrorReplies())
if "cat" in message.content.lower():
await message.channel.send(catMessage())
await message.channel.send(iNeedACat())
sadWords = ["sad", "upset", "unhappy", "depressed", "miserable", "despair", "dejected", "glum", "gloomy", "broken heart", "forlorn", "heartbroken", "melancholy", "woebegone"]
firstPersonWords = [" i ", "me", "we", "i am", "i'm", "my"]
if [entry for entry in sadWords if(entry in message.content.lower())] and ([entry for entry in firstPersonWords if(entry in message.content.lower())] or message.content.lower().startswith("i")):
await message.channel.send(cheerUpWithCats())
await message.channel.send(iNeedACat())
# ================================================
# ================ Masud did this ================
if "salt " == (message.content.lower())[:5]: # checks if the input matches "salt "
communityName = (message.content)[5:] # saves the rest of the input as communityName
await message.add_reaction("🆗")
result = getPollID(message, communityName, polls)
if result == True:
await message.channel.send(f'The community: {communityName} already has a poll active. Poll ID:{result[1]}.') # message to discord
elif result == False:
poll = await message.channel.send(f'vote here for {communityName}:') # create the vote poll
# print(f'[BOT DEBUG] {communityName} added to list of polls') # print to console for debugging only
addPoll(poll,communityName)
# print("[BOT DEBUG] Poll dict:\n",polls) # print list of polls active for debugging only
await poll.add_reaction("✅") # add the tick emoji for the poll
await poll.add_reaction("❌") # add the cross emoji for the poll
if "count " == (message.content.lower())[:6]: # checks if the input matches "count ". DevOps work item 33
communityName = (message.content)[6:] # saves the rest of the input as communityName
if getPollID(message, communityName, polls) == False: # checks if the poll does not exist already
await message.channel.send(f'No poll found for community: {communityName}.') # messages to discord
else:
pollID = getPollID(message, communityName, polls)[1]
pollMessage = await message.channel.fetch_message(pollID) # get the message object with the pollID
pollReactions = pollMessage.reactions # accesses the reactions for the pollMessage object
await message.channel.send(f'Here are the saltiness statistics for the community {communityName}\nPoll ID:{pollID}') # message to discord
countDesc = countPoll(message, communityName, pollReactions)
countEmbed = makeEmbed(discord,countDesc,communityName)
# add the description to the embed and message it to the channel.
await message.channel.send(embed=countEmbed) # send the embed to the channel the message is in
# ================ up to about here ================
#BenHB Start==================================================================
#These ifs are used to access a function when a user gives a valid input
if (' steamid ' in message.content.lower() or ' steam id ' in message.content.lower() or 'steamid' in message.content.lower() or 'steam id' in message.content.lower()) and message.author.id not in tempIDArrayDiscord:
output = registerSteamID(message.content, message.author.id)#If for when the users wants to register their SteamID for the first time
await message.channel.send(output)
if (' steamid ' in message.content.lower() or ' steam id ' in message.content.lower() or 'steamid' in message.content.lower() or 'steam id' in message.content.lower()) and 'change' in message.content.lower() and 'my' in message.content.lower() and message.author.id in tempIDArrayDiscord:
output = changeSteamID(message.content, message.author.id)#If for when the user wants to change their SteamID once they have registered
await message.channel.send(output)
if 'displayid ' in message.content.lower():
displayedID = fetchGameID(message.content.replace('displayid ',''))#An if for a test function that returns the Steam gameID of whatever game you put in. Was left in because it's a nice feature
await message.channel.send(displayedID)
if ('play' in message.content.lower() or 'many' in message.content.lower()) and ('time' in message.content.lower() or 'minute' in message.content.lower() or 'day' in message.content.lower() or 'second' in message.content.lower() or 'hour' in message.content.lower() or 'week' in message.content.lower() or 'month' in message.content.lower() or 'year' in message.content.lower()) and message.author.id in tempIDArrayDiscord:
timePlayed = usersTimePlayed(message.content, message.author.id) #game name is written by user between two brackets() this is because determining the game name outside of brackets is decently challenging to say the least
if timePlayed == -1: #If -1 is returned that means that one of the functions cannot access or find the game that the user input
await message.channel.send("You either don't own this game or it doesn't exist sorry! Please make sure your games are public for this feature! \nPlease be aware that this search feature is case sensitive to prevent clashing between games with almost identical names.")
elif 'minute' in message.content[0:message.content.find('(') +1].lower() or 'minute' in message.content[message.content.find(')'):].lower():
await message.channel.send("You have "+str(timePlayed)+ " minutes played")
elif 'hour' in message.content[0:message.content.find('(') +1].lower() or 'hour' in message.content[message.content.find(')'):].lower(): #these ifs check to see if a timeframe is given outside of the games name
timePlayed = timeFormat(timePlayed, message.content, '', 0)
await message.channel.send("You have "+str(timePlayed)+ " hours played")
elif 'second' in message.content[0:message.content.find('(') +1].lower() or 'second' in message.content[message.content.find(')'):].lower():
timePlayed = timeFormat(timePlayed, message.content, '', 0)
await message.channel.send("You have "+str(timePlayed)+ " seconds played")
elif 'day' in message.content[0:message.content.find('(') +1].lower() or 'day' in message.content[message.content.find(')'):].lower():
timePlayed = timeFormat(timePlayed, message.content, '', 0)
await message.channel.send("You have "+str(timePlayed)+ " days played")
elif 'week' in message.content[0:message.content.find('(') +1].lower() or 'week' in message.content[message.content.find(')'):].lower():
timePlayed = timeFormat(timePlayed, message.content, '', 0)
await message.channel.send("You have "+str(timePlayed)+ " weeks played")
elif 'month' in message.content[0:message.content.find('(') +1].lower() or 'month' in message.content[message.content.find(')'):].lower():
timePlayed = timeFormat(timePlayed, message.content, '', 0)
await message.channel.send("You have "+str(timePlayed)+ " months played")
elif 'year' in message.content[0:message.content.find('(') +1].lower() or 'year' in message.content[message.content.find(')'):].lower():
timePlayed = timeFormat(timePlayed, message.content, '', 0)
await message.channel.send("You have "+str(timePlayed)+ " years played")
else:
await message.channel.send("Sorry, No timeFrame was given so here is you play time in minutes "+timePlayed)
#BenHB End=====================================================================
client.run(TOKEN) #this is the token of the discord bot. You can create your own bot and put that token in here or the .env file to run the code
#================================