Skip to content

Commit 87fbe2e

Browse files
Merge pull request Mayank94043626#100 from 2devyank/2devyank
bot
2 parents 0acc452 + da4e5ab commit 87fbe2e

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

Projects/Encourage bot/Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Encourage-Bot
2+
This bot bring joyness to your life

Projects/Encourage bot/keep_alive.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from flask import Flask
2+
from threading import Thread
3+
4+
app = Flask('')
5+
6+
@app.route('/')
7+
def home():
8+
return "Hello. I am alive!"
9+
10+
def run():
11+
app.run(host='0.0.0.0',port=8080)
12+
13+
def keep_alive():
14+
t = Thread(target=run)
15+
t.start()

Projects/Encourage bot/main.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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'))

0 commit comments

Comments
 (0)