Skip to content
Permalink
Browse files
converting code to functions
  • Loading branch information
masudm6 committed Nov 22, 2020
1 parent 70498dd commit 057a18ca7197b2c619adb09851a5e1f69b5c092a
Showing 1 changed file with 66 additions and 58 deletions.
@@ -252,27 +252,57 @@ def timeFormat(timePlayed, message, parsedLetters, letterIndex):# A recursive fu

#==================Ben=HB=Function=End==================

#masud will do some functions here
def createPoll(message):
communityName = (message.content)[5:] # saves the rest of the input as communityName
#masud will do some functions and code here

await message.add_reaction("🆗") # devops work item 35
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
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...
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
# 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

def countPoll():
pass
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


@@ -290,8 +320,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
@@ -310,7 +338,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:
@@ -332,56 +359,37 @@ async def on_message(message): #checks for a message

# ================ Masud did this ================
if "salt " == (message.content.lower())[:5]: # checks if the input matches "salt "
createPoll()
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 communityName.upper() not in polls.values(): # checks if the poll does not exist already
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: # 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():

0 comments on commit 057a18c

Please sign in to comment.