diff --git a/Chatbot.py b/Chatbot.py index 3a386d5..3e5c27f 100644 --- a/Chatbot.py +++ b/Chatbot.py @@ -190,6 +190,35 @@ def registerSteamID(message, discordID): 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 @@ -254,7 +283,59 @@ def timeFormat(timePlayed, message, parsedLetters, letterIndex):# A recursive fu #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,0] + +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 both 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=================== @@ -271,8 +352,6 @@ async def on_ready(): members = '\n - '.join([member.name for member in guild.members]) print(f'Guild Members:\n - {members}') -polls = {} # masud added this to hold poll data. Can be switched to a database in the future - @client.event async def on_message(message): #checks for a message if message.author == client.user: #makes sure it's a user @@ -291,7 +370,6 @@ async def on_message(message): #checks for a message 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: @@ -316,76 +394,47 @@ async def on_message(message): #checks for a message # ================ 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("🆗") # devops work item 35 - - 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 - await message.channel.send(f'The community: {communityName} already has a poll active. Poll ID:{pollID}.') # message to discord - else: # and if there is not an active poll already... + result = getPollID(message, communityName, polls) + if result[0] == True: + await message.channel.send(f'The community: {communityName} already has a poll active. Poll ID:{result[1]}.') # message to discord + else: + await message.add_reaction("🆗") 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 - polls[poll.id] = communityName.upper() # save the pollID and communityName together + 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 communityName.upper() not in polls.values(): # checks if the poll does not exist already + if getPollID(message, communityName, polls)[0] == False: # checks if the poll does not exist already await message.channel.send(f'No poll found for community: {communityName}.') # messages to discord - else: # if the poll does exist ... - - pollID = list(polls.keys())[list(polls.values()).index(communityName.upper())] # get the pollID with the matching communityName + 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 - countEmbed = discord.Embed() # Discord embed object - countDesc = "" # description of countEmbed object - countEmbed.title = f"Is the community {communityName} salty or supportive?" # title of countEmbed object. DevOps work item 32 - - tickCount = 0 - crossCount = 0 - - 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." - + countDesc = countPoll(message, communityName, pollReactions) + countEmbed = makeEmbed(discord,countDesc,communityName) # add the description to the embed and message it to the channel. - countEmbed.description = countDesc # add embed description to embed - await message.channel.send(embed = countEmbed) # send the embed to the channel the message is in + await message.channel.send(embed=countEmbed) # send the embed to the channel the message is in + # ================ up to about here ================ - # ================ 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(): - messageInfo = registerSteamID(message.content, message.author.id) - await message.channel.send(messageInfo) - #Ben did above + + 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 ','')) + 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: