-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
316 lines (277 loc) Β· 10.6 KB
/
functions.py
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import discord
import os
import json
import requests
import random
import praw
import datetime
import urllib3
from urllib.request import urlopen
from dotenv import load_dotenv
load_dotenv()
# Secrets
API_NINJA_KEY = os.getenv("API_NINJA_KEY") # Used for Fun facts, dad jokes, and jokes
REDDIT_CLIENT_ID = os.getenv("REDDIT_CLIENT_ID") # Used for Python Reddit API Wrapper
REDDIT_CLIENT_SECRET = os.getenv(
"REDDIT_CLIENT_SECRET"
) # Used for Python Reddit API Wrapper
REDDIT_USER_AGENT = os.getenv("REDDIT_USER_AGENT") # Used for Python Reddit API Wrapper
BOT_TOKEN = os.getenv("BOT_TOKEN") # Discord Bot Token
DOPPLE_ID = os.getenv("DOPPLE_ID") # Dopple ID
DOPPLE_CHAT_ID = os.getenv("DOPPLE_CHAT_ID") # Dopple chat ID
DOPPLE_USERNAME = os.getenv("DOPPLE_USERNAME") # Dopple registered username
DOPPLE_COOKIE = os.getenv("DOPPLE_COOKIE") # Dopple cookie
# Customisable values:
LOG_FILE = (
"exhaust.txt" # Name of the log file. Keep blank to disable logging to a file
)
embed_colour = [8, 234, 142] # R,G,B
error_embed_colour = [250, 0, 0] # R, G, B
memesubs = [ # Subreddits to get a meme from
"memes",
"dankmemes",
"196",
"surrealmemes",
]
# Initialise PRAW with account information, used for accessing reddit API.
reddit = praw.Reddit(
client_id=REDDIT_CLIENT_ID,
client_secret=REDDIT_CLIENT_SECRET,
user_agent=REDDIT_USER_AGENT,
check_for_async=False,
)
# Disable warnings for unverified HTTP requests
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# <-------ALL THE FUNCTIONS USED BY THE BOT------->
# Function to return an error embed with desired content
def error(title, description):
erroremb = discord.Embed(
title=title,
description=description,
colour=discord.Color.from_rgb(
error_embed_colour[0], error_embed_colour[1], error_embed_colour[2]
),
)
return erroremb
# Function to log stuff, optionally to a text file
def log(text):
logtext = f"[ {str(datetime.datetime.now())} ] {text}"
print(logtext)
if LOG_FILE:
with open(
LOG_FILE, "a", encoding="utf-8"
) as text_file: # Exhaust.txt is the name of the log file
text_file.write(logtext)
def imageGen(prompt: str) -> bytes:
log(f"[IMAGE GEN]: Prompt: {prompt}\n")
payload = f'-----011000010111000001101001\r\nContent-Disposition: form-data; name="prompt"\r\n\r\n{prompt}\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name="output_format"\r\n\r\nbytes\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name="user_is_subscribed"\r\n\r\ntrue\r\n-----011000010111000001101001--\r\n'
headers = headers = {
"accept": "application/json, text/plain, */*",
"content-type": "multipart/form-data; boundary=---011000010111000001101001",
"origin": "https://magicstudio.com",
"referer": "https://magicstudio.com/",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
}
image = requests.request(
"POST",
"https://ai-api.magicstudio.com/api/ai-art-generator",
data=payload,
headers=headers,
)
if image.status_code != 200:
log(f"[AI IMAGE]: ERROR {image.status_code} \n")
log(f"[AI IMAGE]: ERROR {image.text} \n")
return ""
return image.content
# Define a function to return ai output on a given input
def gpt(prmpt: str):
print(f"[PROMPT]: {prmpt}")
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={os.getenv('GEMINI_API_KEY')}"
payload = {
"contents": [
{
"role": "user",
"parts": [
{
"text": f'{os.getenv("GEMINI_PROMPT")} In case you need this info, today is {datetime.datetime.now().strftime("%Y-%m-%d")}.'
}
],
},
{"role": "user", "parts": [{"text": prmpt}]},
]
}
response = requests.post(url, json=payload)
try:
response_text = (
response.json()
.get("candidates")[0]
.get("content")
.get("parts")[0]
.get("text")
)
if response_text == "":
response_text = "Something went wrong, try again later"
raise Exception(response_text)
log(f"[AI]: {response_text}")
return response_text
except Exception as e:
log(f"Error while getting AI response: {response.text}")
log(e)
return e
# Function to make an embed with the best posts of a given subreddit
def meme(posts_lim):
subreddit = random.choice(memesubs)
# subreddit - What subreddit
# posts_lim - How many top posts to choose from
# Get some top posts from a subreddit to choose from
submissions = list(reddit.subreddit(subreddit).hot(limit=posts_lim))
# Get a random post from the chosen top posts and reroll if post is nsfw
post = submissions[random.randrange(-1, posts_lim)]
# Read the post JSON
json_url = "https://www.reddit.com" + post.permalink + ".json"
json_data = urlopen(json_url).read().decode("utf-8")
post_json = json.loads(json_data)
# Get image or gif embed, if any.
try:
# Parse the json for the image link
img_link = post_json[0]["data"]["children"][0]["data"]["url_overridden_by_dest"]
img_extension = img_link[
-5:
] # Get last 5 characters of the link which contains the file's extension
except KeyError:
log("[REDDIT POST]: No media found, retrying...")
return meme(subreddit, posts_lim)
if post.over_18:
log("[REDDIT POST]: NSFW Post, skipping")
return meme(subreddit, posts_lim)
else:
if any(
extension in img_extension for extension in ("png", "jpg", "jpeg", "gif")
):
# Create embed with post title + subreddit name
log("[REDDIT POST]: Title: " + post.title + " on r/" + subreddit)
embed = discord.Embed(
title=post.title,
description="on r/" + subreddit,
color=discord.Color.from_rgb(
embed_colour[0], embed_colour[1], embed_colour[2]
),
)
embed.set_image(url=img_link)
else:
log("[REDDIT POST]: Media not in a suitable format, retrying...")
return meme(subreddit, posts_lim)
# Return the embed
return embed
# Function to return an embed with cat pics
def meow():
catresponse = requests.get("https://api.thecatapi.com/v1/images/search")
catdata = catresponse.json()
catimg = url = catdata[0]["url"]
catembed = discord.Embed(
title="Meow π",
colour=discord.Color.from_rgb(
embed_colour[0], embed_colour[1], embed_colour[2]
),
)
catembed.set_image(url=catimg)
log("[CAT IMAGE]: URL: " + url + "\n")
return catembed
# Function to return an embed with dog pics
def woof():
dogresponse = requests.get("https://random.dog/woof.json")
dogdata = dogresponse.json()
dogimg = dogdata["url"]
if any(extension in dogimg[-5:] for extension in ("png", "jpg", "jpeg", "gif")):
dogembed = discord.Embed(
title="Woof π",
colour=discord.Color.from_rgb(
embed_colour[0], embed_colour[1], embed_colour[2]
),
)
dogembed.set_image(url=dogimg)
log("[DOG IMAGE]: URL: " + dogimg + "\n")
else:
log(
"[DOG IMAGE]: Returned media URL not in suitable format. Skipping... URL: "
+ dogimg
+ "\n"
)
return woof()
return dogembed
# Function to return an embed with a joke
def joke():
api_url = "https://api.api-ninjas.com/v1/jokes"
jokeresponse = requests.get(api_url, headers={"X-Api-Key": API_NINJA_KEY})
if jokeresponse.status_code == requests.codes.ok:
jokedata = jokeresponse.json()
joke = jokedata[0]["joke"]
jokeembed = discord.Embed(
title="Joke π€£",
description=joke,
colour=discord.Color.from_rgb(
embed_colour[0], embed_colour[1], embed_colour[2]
),
)
log("[JOKE]: " + joke + "\n")
return jokeembed
else:
jokeembed = discord.Embed(
title="No joke π",
description="Error getting joke, try again later",
colour=discord.Color.from_rgb(
error_embed_colour[0], error_embed_colour[1], error_embed_colour[2]
),
)
log(f"[JOKE]: ERROR: {jokeresponse.status_code} {jokeresponse.text}\n")
return jokeembed
# Function to return an embed with a dad joke
def dadjoke():
api_url = "https://api.api-ninjas.com/v1/dadjokes"
dadresponse = requests.get(api_url, headers={"X-Api-Key": API_NINJA_KEY})
if dadresponse.status_code == requests.codes.ok:
daddata = dadresponse.json()
dadjoke = daddata[0]["joke"]
dadembed = discord.Embed(
title="Dad joke π",
description=dadjoke,
colour=discord.Color.from_rgb(
embed_colour[0], embed_colour[1], embed_colour[2]
),
)
log("[DAD JOKE]: " + dadjoke + "\n")
else:
dadembed = discord.Embed(
title="No dad joke π",
description="Error getting dad joke, try again later",
colour=discord.Color.from_rgb(
error_embed_colour[0], error_embed_colour[1], error_embed_colour[2]
),
)
log(f"[DAD JOKE]: ERROR: {dadresponse.status_code} {dadresponse.text}\n")
return dadembed
# Function to return an embed with a fact
def fact():
api_url = "https://api.api-ninjas.com/v1/facts"
factresponse = requests.get(api_url, headers={"X-Api-Key": API_NINJA_KEY})
if factresponse.status_code == requests.codes.ok:
factdata = factresponse.json()
fact = factdata[0]["fact"]
factembed = discord.Embed(
title="Fun fact! π€―",
description=fact,
colour=discord.Color.from_rgb(
embed_colour[0], embed_colour[1], embed_colour[2]
),
)
log("[FACT: " + fact)
else:
factembed = discord.Embed(
title="Not so fun fact",
description="Error getting fun fact, try again later",
colour=discord.Color.from_rgb(
error_embed_colour[0], error_embed_colour[1], error_embed_colour[2]
),
)
log(f"[FACT]: ERROR: {factresponse.status_code} {factresponse.text}\n")
return factembed