-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
261 lines (229 loc) · 8.47 KB
/
main.py
File metadata and controls
261 lines (229 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import discord
import os
import requests
import json
import random
from replit import db
from asyncio import sleep
from keep_alive import keep_alive
# Create an instance of a client to connect to discord
client = discord.Client()
# The default URL of the jokes API to build a request from
jokes_url = "https://v2.jokeapi.dev/"
# The categories from JokeAPI docs
categories = ["Programming", "Misc", "Dark", "Pun", "Spooky", "Christmas"]
# The flags to blacklist certain results from appearing in the API response
blacklistFlags = ["nsfw", "religious", "political", "racist", "sexist", "explicit"]
# List of words that will trigger encouragemenent
sad_words = ["sad", "depressed", "depressing", "miserable", "unhappy",
"upsetting", "upset"]
# List of discord emotes for the bot to pick from
emotes = [
":bread:",
":eyes:",
":grimacing:",
":joy:",
":laughing:",
":middle_finger:" ":neutral_face:",
":middle_finger:",
":neutral_face:",
":ok_hand:",
":rofl:",
":sweat_smile:",
":thinking:",
":unamused:",
]
# Encouraging messages
starter_encouragements = [
"You've got this",
"Never give up!",
"Hang in there, it'll get better",
"Sleep on it, I'm sure you'll change your mood",
"Maybe this will help: https://www.youtube.com/watch?v=KxGRhd_iWuE"
]
# Boolean to decide if the bot is responding to trigger words
if "responding" not in db.keys():
db["responding"] = True
# Returns a quote from the API
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
json_data = json.loads(response.text)
# Get the quote and author from the API response
quote = json_data[0] ["q"] + " - " + json_data[0] ["a"]
return quote
'''
Gets the joke whether it is a single or twopart joke
param: data - the json data from the jokes API
returns: Either a one or twopart joke
'''
def getJokeType(data):
jokeType = data["type"]
# Check whether the joke is a one liner or not
if jokeType == "single":
# Parse the joke from the API response
joke = data["joke"]
return joke
else:
setup = data["setup"]
delivery = data["delivery"]
twoPartJoke = setup + "\n" + delivery
return twoPartJoke
'''
Get a random joke
Param: options - a string of options for the API call
returns: A dict of the response from the API
'''
def get_joke(options):
response = requests.get(jokes_url + "/joke/" + options)
data = json.loads(response.text)
joke = getJokeType(data)
return joke
'''
Get a random fact
Param: option - a string of options to pass to the URL,
use this to access different endpoints
returns: The text param from the json data, the fact
'''
def get_fact(option):
facts_url = "https://uselessfacts.jsph.pl/{}.json?language=en".format(option)
response = requests.get(facts_url)
data = json.loads(response.text)
fact = data["text"]
return fact
# Allow users to add custom encouraging messages
def update_encouragement(encouraging_message):
# Get all messages, add the new one and resave the database
if "encouragements" in db.keys():
encouragements = db["encouragements"]
encouragements.append(encouraging_message)
db["encouragements"] = encouragements
else: # Create a new entry in the database
db["encouragements"] = [encouraging_message]
# Delete a message
def delete_encouragement(index):
encouragements = db["encouragements"]
if len(encouragements) > index:
del encouragements[index]
db["encouragements"] = encouragements
# Register an event
@client.event
# When bot is ready
async def on_ready():
print("Logged in as {0.user}".format(client))
# Set the bot's presence info
await client.change_presence(activity = discord.Game("type !commands for help"))
@client.event
# If bot receives a message
async def on_message(message):
# Stop the bot from replying to itself
if message.author == client.user:
return
# Reply to a mention
if client.user.mentioned_in(message):
await message.channel.send("I appreciate the mention, but you don't need to do that. Just type `!commands` for help :sweat_smile:")
# The content of a user's message
# Using lower() in case a user types a command IN CAPS
msg = message.content.lower()
# List the bot's commands
if msg.startswith("!commands"):
commands = """>>> Commands: \n
`!inspire` - Get a random inspiring quote.
`!joke` - Get a random joke. **(Random jokes aren't filtered, so it could be racist, sexist etc. You have been warned).**
`!safe` - Get a safe joke.
`!pun` - Get a pun. It's probably stupid. :sweat_smile:
`!nerd` - Get a programming || coding joke. You nerd.
`!fact` - Get a random (possibly useless) fact.
`!today` - Get the fact of the day.
`!list` - List the current custom encouraging messages.
Use this command before using `!del`, so you know which
message you're about to delete.
Type `!new` followed by your message to add a new
custom encouraging message, be nice!
Type `!del` and a no. from 0 to the current no. of custom messages to delete that message in case it turns out to be wildly inappropriate.
E.g. `!del 0` deletes the first message in the list.
`!responding` - use `!responding off` to stop the bot from
responding to trigger words.
use `!responding on` to turn it back on. Responding is on
by default.
"""
await message.channel.send(commands)
# The command to trigger the bot
if msg.startswith("!inspire"):
quote = get_quote()
# Mention the user
mention = message.author.mention
await message.channel.send(">>> " + mention + " " + quote)
'''
Tell a joke
param: joke - the joke returned from a 'get X joke' functions
'''
async def tell_joke(joke):
await message.channel.send(">>> " + joke)
await sleep(5)
# Pick a random emote from the emotes list
await message.channel.send(random.choice(emotes))
# Commands for telling each type of Joke
if msg.startswith("!joke"): # Any joke, except for programming jokes with no filters
joke = get_joke("Miscellaneous,Dark,Pun,Spooky,Christmas")
await tell_joke(joke)
if msg.startswith("!safe"): # Safe jokes
joke = get_joke("Miscellaneous,Dark,Pun,Spooky,Christmas?blacklistFlags=nsfw,religious,political,racist,sexist,explicit")
await tell_joke(joke)
if msg.startswith("!pun"): # Puns
joke = get_joke("Pun")
await tell_joke(joke)
if msg.startswith("!nerd"): # Programming jokes
joke = get_joke("Programming")
await tell_joke(joke)
if msg.startswith("!fact"): # Random fact
fact = get_fact("random")
await message.channel.send(">>> " + fact)
if msg.startswith("!today"): # Fact of the day
fact = get_fact("today")
await message.channel.send(">>> " + fact)
# Check that the bot is responding and that messages exist
if db["responding"]:
options = starter_encouragements
if "encouragements" in db.keys():
options = options + db["encouragements"]
# Check for any trigger words and send a random encouraging response
if any(word in msg for word in sad_words):
await message.channel.send(random.choice(options))
# Send a PM to the user
#await message.author.send(random.choice(starter_encouragements))
# Add new message to database from a Discord command
if msg.startswith("!new"):
# Get the text after the !new command
encouraging_message = msg.split("!new ",1)[1]
update_encouragement(encouraging_message)
await message.channel.send(">>> New encouraging message added, nice!")
# Delete a message
if msg.startswith("!del"):
encouragements = []
if "encouragements" in db.keys():
# Get the index of the message to be deleted
index = int(msg.split("!del",1)[1])
delete_encouragement(index)
encouragements = db["encouragements"]
await message.channel.send("Current custom responses are: \n")
await message.channel.send(encouragements) # send updated list
# List messages
if msg.startswith("!list"):
encouragements = []
if "encouragements" in db.keys():
encouragements = db["encouragements"]
await message.channel.send(">>> Current custom responses are: \n")
await message.channel.send(">>> " + encouragements)
# Set whether the bot responds to trigger words
if msg.startswith("!responding"):
value = msg.split("!responding ",1)[1]
if value.lower() == "on":
db["responding"] = True
await message.channel.send("Responding is on.")
else:
db["responding"] = False
await message.channel.send("Responding is off. IDGAF about your sadness.")
# Run the web server
keep_alive()
# Run the bot
client.run(os.getenv("TOKEN"))