Advertisement
Guest User

Discord Dungeons Bot

a guest
May 29th, 2018
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.03 KB | None | 0 0
  1. ############## package installation guide ###############
  2. # Install python 3.6 (3.5 to 3.7 will work)
  3. # Open Command Prompt as Administrator and run the 3 commands below:
  4. # python -m pip install -U --upgrade pip
  5. # python -m pip install -U discord.py[voice]
  6. # python -m pip install -U numpy
  7.  
  8. import asyncio, os, time, re
  9.  
  10. import discord
  11. from numpy import random
  12.  
  13.  
  14. username = "" # the default username/email to use (not required)
  15. password = "" # the default password to use (not required)
  16.  
  17. serverName = "My Discord Dungeons"  # server to bot in
  18. channelName = "rpg"                 # channel to bot in
  19. drpgBotName = "DiscordRPG".lower()  # name of the bot to parse text from
  20.  
  21. safeMode = 1    # uses humanizers and randomess to avoid suspicion
  22. passiveMode = 0 # does not attack in order to not lose hp
  23.  
  24. commandTimer = 1 # wait 1 second per command
  25. advTimer = 14.34
  26. catchTimer = 30.34
  27. forageTimer = mineTimer = chopTimer = fishTimer = 300.34
  28. searchTimer = 600.34
  29.  
  30. shortRestDuration = 50 # pause for 50 seconds
  31. shortRestFrequency = 600 # every 10 minutes
  32. longRestDuration = 1260 # 21 min rest
  33. longRestFrequency = 5800 # every 80 minutes
  34. restSchedule = ((22,24),(0,6)) # times when script is resting (not botting)
  35. slowMultiplier = 4 # multiply all timer and varience by this number during slowmode
  36. slowSchedule = ((9,11),(1,5)) # times when slowmode is active, ((start, stop),(start, stop))
  37.  
  38. currHealth = 100
  39. totalHealth = 100
  40. healthLock = 0
  41. healPerun = 0.34  # health perun to use potions at
  42.  
  43. healthPattern = None
  44. channel = None
  45. utf8name = None
  46. client = discord.Client()
  47.  
  48. startTime = time.time()
  49.  
  50. class Queue(object):
  51.     def __init__(self, maxSize):
  52.         self.maxSize = maxSize
  53.         self.items = []
  54.  
  55.     def __iter__(self):
  56.         return self.items.__iter__()
  57.  
  58.     def __repr__(self):
  59.         return self.items.__repr__()
  60.  
  61.     def __str__(self):
  62.         return self.items.__str__()
  63.  
  64.     def __getitem__(self,key):
  65.         return self.items[key]
  66.  
  67.     def queue(self, item):
  68.         if len(self.items) == 10:
  69.             self.items = self.items[1:self.maxSize]
  70.         self.items.append(item)
  71.  
  72. guiUpdateTimer = 1
  73. textGui = """Botting as {} on server {} in channel {}
  74. Time since start: {}
  75. Time spent
  76.    normal botting: {}
  77.    slow botting: {}
  78.    resting: {}
  79. Rest schedule: {}
  80. Slow mode schedule: {} <- not yet implemented
  81. Safe mode: {}
  82. Passive mode: {}
  83. Bot current status: {}
  84.  
  85. Health: {}/{} (%{:0.2f})
  86. Heal when health is below: %{:0.2f}
  87. Potions used: {}
  88. Commands sent - total: {}
  89.    adv:  {: 8d} catch: {: 8d} forage: {: 8d} mine:  {: 8d}
  90.    chop: {: 8d} fish:  {: 8d} search: {: 8d}
  91.    
  92. Timers
  93.    nextCommand: {: 12.2f}
  94.    shortRest: {: 12.2f} shortRestDuration: {: 12.2f}
  95.    longRest:  {: 12.2f} longRestDuration:  {: 12.2f}
  96.    adv:  {: 7.2f} forage: {: 7.2f}   mine: {: 7.2f}
  97.    fish: {: 7.2f} chop:   {: 7.2f} search: {: 7.2f}
  98.  
  99. Last 10 logs:
  100. """
  101. logs = Queue(8)
  102.  
  103.  
  104. # clears the terminal
  105. def clear_screen():
  106.     if os.name == "nt":         # windows/dos
  107.         os.system("cls")
  108.     elif os.name == "posix":    # linux/unix
  109.         os.system("clear")
  110.     else:
  111.         print("\n"*128)
  112.  
  113. # generate a random number in a bell cuve (normal distribution)
  114. # if lowest is None, there will be no floor
  115. # if highest is None, there will be no ceiling
  116. # target(float)the target value to generate around
  117. # return(float)the randomly generate number
  118. def safe_random(target):
  119.     if safeMode:
  120.         normalRand = random.normal(target*1.15, target*0.3)
  121.         normalRand = max(target*0.9, normalRand)
  122.         normalRand = min(target*1.4, normalRand)
  123.         return normalRand
  124.     return target
  125.  
  126. # get the hour of day as local time
  127. # return(float)the hour of day as a fraction
  128. def get_hour():
  129.     return float(time.strftime('%H')) + float(time.strftime('%M'))/60.
  130.  
  131. def log(text):
  132.     date = format_time(time.time() - startTime)
  133.     logs.queue(date + " - " + text)
  134.        
  135. def format_time(seconds):
  136.     days = int(seconds // 86400)
  137.     seconds %= 86400
  138.     hours = int(seconds // 3600)
  139.     seconds %= 3600
  140.     minutes = int(seconds // 60)
  141.     seconds %= 60
  142.     return "{:02d}:{:02d}:{:02d}:{:05.2f}".format(days,hours,minutes,seconds)
  143.  
  144. # on client start
  145. @client.event
  146. async def on_ready():
  147.     global channel, utf8name, currHealth, totalHealth, healthLock, healthPattern, startTime
  148.     print("Log in successful")
  149.     utf8name = client.user.name.encode("utf8")  # encode utf8 because unicode crashes everything
  150.     healthPattern = re.compile(r"" + client.user.name + r" has ([0-9]{1,999})\/([0-9]{1,999}) HP left")
  151.  
  152.     # set the server and channels
  153.     server = None
  154.     for _server in client.servers:
  155.         if _server.name.lower() == serverName.lower():
  156.             server = _server
  157.             break
  158.     for _channel in server.channels:
  159.         if _channel.name.lower() == channelName.lower() and _channel.type != "voice":
  160.             channel = _channel
  161.             break
  162.  
  163.     state = "botting"
  164.     potionsUsed = 0
  165.     timeSpentBotting = 0
  166.     timeSpentSlow = 0
  167.     timeSpentResting = 0
  168.    
  169.     startTime = time.time()
  170.     stateStartTime = time.time()    # keep track how long in each state    
  171.    
  172.     healPerunRand = safe_random(healPerun)
  173.     shortRestDurationRand = safe_random(shortRestDuration)
  174.     shortRestFrequencyRand = safe_random(shortRestFrequency)
  175.     longRestDurationRand = safe_random(longRestDuration)
  176.     longRestFrequencyRand = safe_random(longRestFrequency)
  177.  
  178.     commandTime = 0
  179.     advTime = 0
  180.     catchTime = 0
  181.     forageTime = 0
  182.     mineTime = 0
  183.     chopTime = 0
  184.     fishTime = 0
  185.     searchTime = 0
  186.     shortRestTime = 0
  187.     longRestTime = 0
  188.  
  189.     commandCount = [0,0,0,0,0,0,0] # counter for the commands sent
  190.  
  191.     guiTime = 0
  192.     prevTime = time.time()
  193.  
  194.     log("script started")
  195.    
  196.     while 1:
  197.         await asyncio.sleep(0.05)  
  198.         missingHealth = totalHealth - currHealth
  199.        
  200.         currHour = get_hour()
  201.         currTime = time.time()
  202.         timePassed = currTime - prevTime
  203.         if state == "botting": timeSpentBotting += timePassed
  204.         elif state == "slow botting": timeSpentSlow += timePassed
  205.         else: timeSpentResting += timePassed
  206.         prevTime = currTime
  207.  
  208.         if currTime > guiTime:
  209.             output = textGui.format(client.user.name, server, channel,
  210.                                     format_time(currTime - startTime),
  211.                                     format_time(timeSpentBotting),
  212.                                     format_time(timeSpentSlow),
  213.                                     format_time(timeSpentResting),
  214.                                     restSchedule, slowSchedule,
  215.                                     bool(safeMode), bool(passiveMode), state,
  216.                                     currHealth, totalHealth,
  217.                                     currHealth/totalHealth*100,
  218.                                     healPerunRand*100, potionsUsed,
  219.                                     sum(commandCount),
  220.                                     commandCount[0],commandCount[1],
  221.                                     commandCount[2],commandCount[3],
  222.                                     commandCount[4],commandCount[5],
  223.                                     commandCount[6],
  224.                                     commandTime - currTime,
  225.                                     max(shortRestTime-shortRestDurationRand-currTime, 0),
  226.                                     min(shortRestDurationRand, shortRestTime-currTime),
  227.                                     max(longRestTime-longRestDurationRand-currTime, 0),
  228.                                     min(longRestDurationRand, longRestTime-currTime),
  229.                                     advTime - currTime,
  230.                                     forageTime - currTime,
  231.                                     mineTime - currTime,
  232.                                     chopTime - currTime,
  233.                                     fishTime - currTime,
  234.                                     searchTime - currTime)
  235.             for record in logs:
  236.                 output += "\t"+record+"\n"
  237.             clear_screen()
  238.             print(output)
  239.             guiTime = currTime + guiUpdateTimer
  240.  
  241.         if safeMode:
  242.             shouldSkip = 0
  243.             for span in restSchedule:
  244.                 if currHour >= span[0] and currHour <= span[1]:
  245.                     shouldSkip = 1
  246.                     break
  247.             if currTime > shortRestTime:
  248.                 shortRestDurationRand = safe_random(shortRestDuration)
  249.                 shortRestFrequencyRand = safe_random(shortRestFrequency)
  250.                 shortRestTime = currTime + shortRestFrequencyRand + shortRestDurationRand
  251.             elif shortRestTime - currTime < shortRestDurationRand:
  252.                 shouldSkip = 1
  253.             if currTime > longRestTime:
  254.                 longRestDurationRand = safe_random(longRestDuration)
  255.                 longRestFrequencyRand = safe_random(longRestFrequency)
  256.                 longRestTime = currTime + longRestFrequencyRand + longRestDurationRand
  257.             elif longRestTime - currTime < longRestDurationRand:
  258.                 shouldSkip = 1
  259.  
  260.             if shouldSkip:
  261.                 if state == "botting":   # if was botting but now resting
  262.                     log("resting initiated")
  263.                 state = "resting"
  264.                 continue
  265.         if state == "resting":  # if the bot was resting but is not botting
  266.             log("botting initiated")
  267.         state = "botting"
  268.  
  269.         if currTime > commandTime:
  270.             commandTime = currTime + safeMode*safe_random(commandTimer)
  271.  
  272.             if healthLock == 0 and currHealth/totalHealth <= healPerunRand:
  273.                 healthLock = 1
  274.                 healPerunRand = safe_random(healPerun)
  275.                 if totalHealth > 50: potions = missingHealth // 50
  276.                 else: potions = 1
  277.                 try:
  278.                     await client.send_message(channel, "#!use health potion "+str(potions))
  279.                     potionsUsed += potions
  280.                 except:
  281.                     log("No response from server")
  282.                     healthLock = 0
  283.             elif currTime > advTime:
  284.                 try:
  285.                     await client.send_message(channel, "#!adv")
  286.                     commandCount[0] += 1
  287.                 except: log("No response from server")
  288.                 advTime = currTime + safe_random(advTimer)
  289.             elif currTime > catchTime:
  290.                 try:
  291.                     await client.send_message(channel, "#!catch")
  292.                     commandCount[1] += 1
  293.                 except: log("No response from server")
  294.                 catchTime = currTime + safe_random(catchTimer)
  295.             elif currTime > forageTime:
  296.                 try:
  297.                     await client.send_message(channel, "#!forage")
  298.                     commandCount[2] += 1
  299.                 except: log("No response from server")
  300.                 forageTime = currTime + safe_random(forageTimer)
  301.             elif currTime > mineTime:
  302.                 try:
  303.                     await client.send_message(channel, "#!mine")
  304.                     commandCount[3] += 1
  305.                 except: log("No response from server")
  306.                 mineTime = currTime + safe_random(mineTimer)
  307.             elif currTime > chopTime:
  308.                 try:
  309.                     await client.send_message(channel, "#!chop")
  310.                     commandCount[4] += 1
  311.                 except: log("No response from server")
  312.                 chopTime = currTime + safe_random(chopTimer)
  313.             elif currTime > fishTime:
  314.                 try:
  315.                     await client.send_message(channel, "#!fish")
  316.                     commandCount[5] += 1
  317.                 except: log("No response from server")
  318.                 fishTime = currTime + safe_random(fishTimer)
  319.             elif currTime > searchTime:
  320.                 try:
  321.                     await client.send_message(channel, "#!search")
  322.                     commandCount[6] += 1
  323.                 except: log("No response from server")
  324.                 searchTime = currTime + safe_random(searchTimer)
  325.                
  326. # on message recieved
  327. @client.event
  328. async def on_message(message):
  329.     global currHealth, totalHealth, healthLock
  330.     utf8msg = message.content.encode("utf8")
  331.     if message.author.name.lower() == drpgBotName and message.channel == channel:
  332.         if utf8msg.startswith(b"```diff\n!========[ " + utf8name):
  333.             result = healthPattern.search(message.content.replace(",",""))
  334.             if result:
  335.                 newHealth = int(result.group(1))
  336.                 totalHealth = int(result.group(2))
  337.                 if newHealth > currHealth:
  338.                     healthLock = 0
  339.                 if healthLock == 0:
  340.                     currHealth = newHealth
  341.  
  342. if not username: username = input("username(email): ")   # get username if not defined
  343. if not password: password = input("password: ")          # get password if not defined
  344.  
  345. clear_screen()  # clear user info from screen
  346.  
  347. print("Trying to log in, please wait")
  348. client.run(username, password, bot = False) # start client
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement