-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
352 lines (305 loc) · 13.5 KB
/
main.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from typing import Final
import os, json, requests, discord, urllib.parse
from dotenv import load_dotenv
from discord import Intents, Client, Message
from responses import get_response
from discord.ext import tasks, commands
from prodiapy import Prodia
from io import BytesIO
from PIL import Image
import random
import asyncio
import aiohttp
import time
load_dotenv()
TOKEN: Final[str] = os.getenv('DISCORD_TOKEN')
# Setup
intents: Intents = discord.Intents.all()
intents.message_content = True # NOQA
intents.members = True # ignore if it gives u warnings
client = commands.Bot(command_prefix="!", intents=intents)
url_prodia = "https://api.prodia.com/v1/sd/generate"
# startup
@client.event
async def on_ready() -> None:
print(f'{client.user} is now running!')
await client.change_presence(status=discord.Status.idle)
@client.command()
async def h(ctx):
em = discord.Embed(title='HELP PAGE', color=discord.Color.yellow())
em.set_author(name='COMMANDS LIST')
em.add_field(name='!dog', value='Gives u a random dog image', inline=False)
em.add_field(name='!achiev (string)', value='Type something and it gives u a minecraft achievemnt image of that', inline=False)
em.add_field(name='!drake (string) (string)', value='Type something and it gives u a drake meme image of that', inline=False)
em.add_field(name='!supreme (string)', value='Type something and it gives u a supreme image of that', inline=False)
em.add_field(name='!ph (string) (string)', value='Type something and it gives u a pornhub image of that', inline=False)
em.add_field(name='!meme (memename) (string) (string)', value='Choose the meme, what to type and it will give u the pic', inline=False)
em.add_field(name='!joke', value='Gives u a random unfunny joke', inline=False)
em.add_field(name='!urban (string) (number)', value='Type something and it gives u the first 3 urban definitions of that, you can specify with (number) the exact result you want', inline=False)
em.add_field(name='!insult', value='Gives u a random insult', inline=False)
em.add_field(name='!ai', value='Type something and its gonna generate an image of that', inline=False)
await ctx.send(embed=em)
@client.command()
async def dog(ctx):
try:
r = requests.get("https://dog.ceo/api/breeds/image/random")
res = r.json()
if r.status_code == 200 and res['status'] == 'success':
em = discord.Embed(title='Random Dog Image', color=discord.Color.blurple())
em.set_image(url=res['message'])
await ctx.send(embed=em)
else:
await ctx.send("Failed to fetch a dog image.")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
@client.command()
async def achiev(ctx, input: str):
try:
res = requests.get(f"https://api.alexflipnote.dev/achievement?text={input}")
if res.status_code == 200:
content_type = res.headers['Content-Type']
if 'image' in content_type:
image = Image.open(BytesIO(res.content))
with BytesIO() as image_binary:
image.save(image_binary, 'PNG')
image_binary.seek(0)
await ctx.send(file=discord.File(fp=image_binary, filename='achiev_meme.png'))
else:
r = res.json()
await ctx.send(f"API returned an error: {r}")
else:
await ctx.send("Failed to fetch a drake meme")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
@client.command()
async def drake(ctx, input1: str, input2: str):
try:
res = requests.get(f"https://frenchnoodles.xyz/api/endpoints/drake/?text1={input1}&text2={input2}")
# r = res.json()
# image = Image.open(BytesIO(r.content))
if res.status_code == 200:
content_type = res.headers['Content-Type']
if 'image' in content_type:
image = Image.open(BytesIO(res.content))
with BytesIO() as image_binary:
image.save(image_binary, 'PNG')
image_binary.seek(0)
await ctx.send(file=discord.File(fp=image_binary, filename='drake_meme.png'))
else:
r = res.json()
await ctx.send(f"API returned an error: {r}")
else:
await ctx.send("Failed to fetch a drake meme")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
@client.command()
async def supreme(ctx, input: str):
try:
res = requests.get(f"https://api.alexflipnote.dev/supreme?text={input}")
# r = res.json()
# image = Image.open(BytesIO(r.content))
if res.status_code == 200:
content_type = res.headers['Content-Type']
if 'image' in content_type:
image = Image.open(BytesIO(res.content))
with BytesIO() as image_binary:
image.save(image_binary, 'PNG')
image_binary.seek(0)
await ctx.send(file=discord.File(fp=image_binary, filename='supremepic.png'))
else:
r = res.json()
await ctx.send(f"API returned an error: {r}")
else:
await ctx.send("Failed to fetch the supreme pic")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
@client.command()
async def ph(ctx, input1: str, input2:str):
try:
res = requests.get(f"https://api.alexflipnote.dev/pornhub?text={input1}&text2={input2}")
# r = res.json()
# image = Image.open(BytesIO(r.content))
if res.status_code == 200:
content_type = res.headers['Content-Type']
if 'image' in content_type:
image = Image.open(BytesIO(res.content))
with BytesIO() as image_binary:
image.save(image_binary, 'PNG')
image_binary.seek(0)
await ctx.send(file=discord.File(fp=image_binary, filename='phpic.png'))
else:
r = res.json()
await ctx.send(f"API returned an error: {r}")
else:
await ctx.send("Failed to fetch the ph pic")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
@client.command()
async def meme(ctx, input0: str, input1: str, input2:str):
try:
res = requests.get(f"https://api.memegen.link/images/{input0}/{input1}/{input2}")
# r = res.json()
# image = Image.open(BytesIO(r.content))
if res.status_code == 200:
content_type = res.headers['Content-Type']
if 'image' in content_type:
image = Image.open(BytesIO(res.content))
with BytesIO() as image_binary:
image.save(image_binary, 'PNG')
image_binary.seek(0)
await ctx.send(file=discord.File(fp=image_binary, filename='memepic.png'))
else:
r = res.json()
await ctx.send(f"API returned an error: {r}")
else:
await ctx.send("Failed to fetch the meme pic")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
@client.command()
async def insult(ctx):
try:
r = requests.get("https://evilinsult.com/generate_insult.php?lang=en&type=json")
res = r.json()
if r.status_code == 200:
em = discord.Embed(title='- Random Insult', color=discord.Color.blurple())
em.add_field(name=res['insult'], value="nigga", inline=False)
await ctx.send(embed=em)
else:
await ctx.send("Failed to fetch an insult")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
@client.command()
async def urban(ctx, *, input: str, MaxDef: int = 3):
try:
encoded_input = urllib.parse.quote(input)
r = requests.get(f"https://api.urbandictionary.com/v0/define?term={encoded_input}")
res = r.json()
if r.status_code == 200 and 'list' in res and res['list']:
definitions = res['list'] # [:MaxDef]
if not definitions:
await ctx.send(f"No definitions found for {input}")
return
definitions = definitions[:MaxDef]
for entry in definitions:
definition = entry['definition'][:1020] + '...' if len(entry['definition']) > 1024 else entry['definition']
example = entry.get('example', 'No example available')
example = example[:1020] + '...' if len(example) > 1024 else example
thumbs_up = entry.get('thumbs_up', 0)
thumbs_down = entry.get('thumbs_down', 0)
author = entry.get('author', 'Unknown')
em = discord.Embed(title=f'- Urban dictionary Definition of {input}', color=discord.Color.blurple(), url=entry['permalink'])
em.add_field(name='Definition', value=definition, inline=False)
em.add_field(name="Example", value=example, inline=False)
em.set_footer(text=f"👍 {thumbs_up} | 👎 {thumbs_down} | Author: {author}")
await ctx.send(embed=em)
else:
await ctx.send(f"No definitions found for {input}")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
@client.command()
async def ai(ctx, *, prompt):
try:
url_prodia_generate = "https://api.prodia.com/v1/sd/generate"
payload = {
"model": "v1-5-pruned-emaonly.safetensors [d7049739]",
"prompt": prompt,
"negative_prompt": "badly drawn",
"steps": 20,
"cfg_scale": 7,
"seed": -1,
"upscale": False,
"sampler": "DPM++ 2M Karras",
"width": 512,
"height": 512
}
headers = {
"accept": "application/json",
"content-type": "application/json",
"X-Prodia-Key": "XXXXXXXXXXXXXXXXXXXXXXXXX"
}
async with aiohttp.ClientSession() as session:
async with session.post(url_prodia_generate, json=payload, headers=headers) as response:
if response.status != 200:
await ctx.send(f"Error: Received status code {response.status}")
return
response_data = await response.json()
job_id = response_data.get("job")
if not job_id:
await ctx.send("Failed to start job.")
return
await ctx.send(f"Job queued, pls wait...")
await poll_job_status(ctx, job_id)
except Exception as e:
await ctx.send(f"An error occurred: {e}")
async def poll_job_status(ctx, job_id):
url_prodia_status = f"https://api.prodia.com/v1/job/{job_id}"
headers = {
"accept": "application/json",
"X-Prodia-Key": "XXXXXXXXXXXXXXXXXXXXXXXXX7"
}
while True:
await asyncio.sleep(1)
async with aiohttp.ClientSession() as session:
async with session.get(url_prodia_status, headers=headers) as response:
if response.status != 200:
await ctx.send(f"Error: Received status code {response.status}")
return
status_data = await response.json()
status = status_data.get("status")
if status == "succeeded":
image_url = status_data.get("imageUrl")
if image_url:
await send_image(ctx, image_url)
break
elif status == "failed":
await ctx.send("Job failed.")
break
async def send_image(ctx, image_url):
embed = discord.Embed(title="Generated Image")
embed.set_image(url=image_url)
await ctx.send(embed=embed)
@client.command()
async def joke(ctx):
try:
r = requests.get("https://official-joke-api.appspot.com/jokes/random")
res = r.json()
if r.status_code == 200:
em = discord.Embed(title='- Random (unfunny) joke', color=discord.Color.blurple())
em.add_field(name=res['setup'], value=" ", inline=False)
em.add_field(name=res['punchline'], value="", inline=False)
await ctx.send(embed=em)
else:
await ctx.send("Failed to fetch a joke")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("!"):
print("ao")
await client.process_commands(message)
# return
else:
username: str = str(message.author)
user_message: str = message.content
channel: str = str(message.channel)
print(f'[{channel}] {username}: "{user_message}"')
await send_message(message, user_message)
async def send_message(message: Message, user_message: str) -> None:
if not user_message:
print('(Message was empty because intents were not enabled probably)')
return
if is_private := user_message[0] == '?':
user_message = user_message[1:]
try:
response: str = get_response(user_message)
if response != '0':
await message.author.send(response) if is_private else await message.channel.send(response)
except Exception as e:
print(e)
# entry point
def main() -> None:
client.run(token=TOKEN)
if __name__ == '__main__':
main()