Advertisement
BenjaminPlays

main.py

Feb 19th, 2021
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. import discord
  2. import os
  3. import requests
  4. import json
  5. import random
  6. from replit import db
  7. from keep_alive import keep_alive
  8.  
  9. client = discord.Client()
  10.  
  11. sad_words = ["sad", "depressed", "unhappy", "angry", "miserable", "depressing"]
  12.  
  13. starter_encouragements = [
  14.   "Cheer up!",
  15.   "Hang in there.",
  16.   "You are a great person / bot!"
  17. ]
  18.  
  19. if "responding" not in db.keys():
  20.   db["responding"] = True
  21.  
  22. def get_quote():
  23.   response = requests.get("https://zenquotes.io/api/random")
  24.   json_data = json.loads(response.text)
  25.   quote = json_data[0]['q'] + " -" + json_data[0]['a']
  26.   return(quote)
  27.  
  28. def update_encouragements(encouraging_message):
  29.   if "encouragements" in db.keys():
  30.     encouragements = db["encouragements"]
  31.     encouragements.append(encouraging_message)
  32.     db["encouragements"] = encouragements
  33.   else:
  34.     db["encouragements"] = [encouraging_message]
  35.  
  36. def delete_encouragment(index):
  37.   encouragements = db["encouragements"]
  38.   if len(encouragements) > index:
  39.     del encouragements[index]
  40.     db["encouragements"] = encouragements
  41.  
  42. @client.event
  43. async def on_ready():
  44.   print('We have logged in as {0.user}'.format(client))
  45.  
  46. @client.event
  47. async def on_message(message):
  48.   if message.author == client.user:
  49.     return
  50.  
  51.   msg = message.content
  52.  
  53.   if msg.startswith('$inspire'):
  54.     quote = get_quote()
  55.     await message.channel.send(quote)
  56.  
  57.   if db["responding"]:
  58.     options = starter_encouragements
  59.     if "encouragements" in db.keys():
  60.       options = options + db["encouragements"]
  61.  
  62.     if any(word in msg for word in sad_words):
  63.       await message.channel.send(random.choice(options))
  64.  
  65.   if msg.startswith("$new"):
  66.     encouraging_message = msg.split("$new ",1)[1]
  67.     update_encouragements(encouraging_message)
  68.     await message.channel.send("New encouraging message added.")
  69.  
  70.   if msg.startswith("$del"):
  71.     encouragements = []
  72.     if "encouragements" in db.keys():
  73.       index = int(msg.split("$del",1)[1])
  74.       delete_encouragment(index)
  75.       encouragements = db["encouragements"]
  76.     await message.channel.send(encouragements)
  77.  
  78.   if msg.startswith("$list"):
  79.     encouragements = []
  80.     if "encouragements" in db.keys():
  81.       encouragements = db["encouragements"]
  82.     await message.channel.send(encouragements)
  83.  
  84.   if msg.startswith("$responding"):
  85.     value = msg.split("$responding ",1)[1]
  86.  
  87.     if value.lower() == "true":
  88.       db["responding"] = True
  89.       await message.channel.send("Responding is on.")
  90.     else:
  91.       db["responding"] = False
  92.       await message.channel.send("Responding is off.")
  93.  
  94. keep_alive()
  95. client.run(os.getenv('TOKEN'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement