From fd8b4e06d6ee9a21f775543ca894dd1ab75adf7d Mon Sep 17 00:00:00 2001 From: "Inderjit Singh (singhi8)" Date: Fri, 27 Nov 2020 11:38:21 +0000 Subject: [PATCH] Update Chatbot.py --- Chatbot.py | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/Chatbot.py b/Chatbot.py index 1657219..857aede 100644 --- a/Chatbot.py +++ b/Chatbot.py @@ -337,6 +337,114 @@ def countPoll(message,communityName,pollReactions): return countDesc #end of the functions masud did +#==================inderjit-singh functions start ===================# + +client = commands.Bot(command_prefix='!') +memberDBDict = {} +memberCreated = [] + +async def getMembers(): + while True: + #print("Getting Members") + for member in client.get_all_members(): #fetching the data in the dictionnarie into many dictionnaries + memberName = member.name + roleFound = False + if member.activity is None: + memberDict = {"member": member, "notif30": 1, "notif60": 1, "notif90": 1, "notifOnce": 0, + "lastnotif": datetime.utcnow(), 'lastAct': None, "actStart": datetime.utcnow()} + elif int(member.activity.type) == 0: + memberDict = {"member": member, "notif30": 1, "notif60": 1, "notif90": 1, "notifOnce": 0, + "lastnotif": datetime.utcnow(), 'lastAct': member.activity.name, "actStart": member.activity.created_at} + else: + memberDict = {"member": member, "notif30": 1, "notif60": 1, "notif90": 1, "notifOnce": 0, + "lastnotif": datetime.utcnow(), 'lastAct': member.activity.name, + "actStart": datetime.utcnow()} # in this sections we organise the members based on their activity using if elif and else statements + for role in member.roles: + if role.name == "GameTime Users": + roleFound = True + if role.name == "GT 60 Min": + memberDict["notif30"] = 0 + if role.name == "GT 90 Min": + memberDict["notif30"] = 0 + memberDict["notif60"] = 0 + if roleFound and memberName not in memberCreated: + print(f"Member Added/Overwritten: {memberName}") + memberDBDict[memberName] = memberDict + memberCreated.append(memberName) + elif roleFound is False and memberName in memberCreated: + print(f"Member Removed: {memberName}") + memberDBDict.pop(memberName) + memberCreated.clear(memberName) + await sleep(5) +#getting members variables using a for loop and organising them in a variable using if , else elif statements + +async def sendDM(member, gamingtime): + timeParse = (datetime.min + gamingtime).time() #calculating the gaming time + if timeParse.hour == 0: + message = f"Consider taking a break. You have been playing for {timeParse.minute} minutes!" + elif timeParse.hour == 1 and timeParse.minute == 0: + message = f"Consider taking a break. You have been playing for 1 hour! work on yourself!!!" + elif timeParse.hour == 1 and timeParse.minute != 0: + message = f"Consider taking a break. You have been playing for 1 hour and {timeParse.minute} minutes!you woul've made a fortune if you spent this time working on yourself" + elif timeParse.hour > 1 and timeParse.minute == 0: + message = f"Consider taking a break. You have been playing for {timeParse.hour} hours! the whole world is moving and your not" + elif timeParse.hour > 1 and timeParse.minute != 0: + message = f". You have been playing for {timeParse.hour} hours , poor people became rich , and rich people became poor and you still PLAYING! " + message += f" and {timeParse.minute} minutes!" + await member.send(content=message, delete_after=10) + print(f"Message sent to {member}: {message}") #notifying users about the time spent gaming +#sending the dms based on the time spent gaming using if statements + +async def equivalentTimespent(): + while True: + #print("Sending Notifications if any.") + for memberName in memberDBDict: + #print(memberDBDict[memberName]["member"]) + if memberDBDict[memberName]["member"].activity is not None: # creating arguments to trigger the dms + if memberDBDict[memberName]["member"].activity.name == memberDBDict[memberName]["lastAct"]: + if int(memberDBDict[memberName]["member"].activity.type) == 0: + timeGaming = datetime.utcnow() - memberDBDict[memberName]["actStart"] # creating time limits to send the dms + timeLimit30 = timedelta(minutes=30) + timeLimit60 = timedelta(minutes=60) + timeLimit90 = timedelta(minutes=90) + timeLimit120 = timedelta(minutes=120) + timeSinceNotif = datetime.utcnow() - memberDBDict[memberName]["lastnotif"] # equations to calculate the time spent gaming since the last notifications + notifLimit = timedelta(minutes=30) + #print(f"{memberDBDict[memberName]['member'].name}: {memberDBDict[memberName]['member'].activity.name}: {str(timeGaming)}") + if memberDBDict[memberName]["notifOnce"] == 0: + #print("This was triggered!") + timeSinceNotif = notifLimit + memberDBDict[memberName]["notifOnce"] = 1 + if timeGaming >= timeLimit120: # sending dm when gaming time is 2 h + if timeSinceNotif >= notifLimit: + memberDBDict[memberName]["lastnotif"] = datetime.utcnow() + await sendDM(memberDBDict[memberName]["member"], timeGaming) + elif timeGaming >= timeLimit90 and memberDBDict[memberName]["notif90"] == 1: #sending dms when the gaming time is 90 min + if timeSinceNotif >= notifLimit: + memberDBDict[memberName]["lastnotif"] = datetime.utcnow() + await sendDM(memberDBDict[memberName]["member"], timeGaming) + elif timeGaming >= timeLimit60 and memberDBDict[memberName]["notif60"] == 1: #sending dms when the gaming time is 60 min + if timeSinceNotif >= notifLimit: + memberDBDict[memberName]["lastnotif"] = datetime.utcnow() + await sendDM(memberDBDict[memberName]["member"], timeGaming) + elif timeGaming >= timeLimit30 and memberDBDict[memberName]["notif30"] == 1: #sending dms when the gaming time is 30 min + #print(timeSinceNotif) + if timeSinceNotif >= notifLimit: + memberDBDict[memberName]["lastnotif"] = datetime.utcnow() #sending dms when the time since the last notifications is more than the notifications limit + await sendDM(memberDBDict[memberName]["member"], timeGaming) + else: + #print("Changing Activity") + memberDBDict[memberName]["lastAct"] = memberDBDict[memberName]["member"].activity.name + memberDBDict[memberName]["actStart"] = memberDBDict[memberName]["member"].activity.created_at + else: + #print("Changing to None") + memberDBDict[memberName]["lastAct"] = memberDBDict[memberName]["member"].activity + memberDBDict[memberName]["actStart"] = datetime.utcnow() + await sleep(10) +#organising the notifications sent based on how much time and how many notificatons we sent to a certain user using variables and if , else statements + + + #==================END OF DEFINITIONS=================== @@ -466,7 +574,21 @@ async def on_message(message): #checks for a message await message.channel.send("Sorry, No timeFrame was given so here is you play time in minutes "+timePlayed) #BenHB End===================================================================== +#==================inderjit-singh start==================# +@client.event +async def on_raw_reaction_add(payload): + # Set GameTime User Role + member = payload.member + #print(f"{member.name} reacted") + channel = discord.utils.get(client.get_all_channels(), ) + + +@client.event +async def on_ready(): + print("GameTime is online!") + client.loop.create_task(getMembers()) + client.loop.create_task(deployNotifs()) 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 #================================